Card Game Using Library Random
Pada kali ini, saya akan membuat sebuah program sederhana permainan kartu blackjack dengan tidak adanya suits atau apapun. Berikut adalah code programnya:
import java.util.Scanner;
import java.util.*;
public class Blackjack {
public static void main(String[] args) {
String kartulain, MainLagi = "y", ctn = null;
int KartuSelanjutnya, kartu1, kartu2, dkartu1, dkartu2, kartuTotal = 0, dTotal = 0;
Scanner keyboard = new Scanner(System.in);
Random random = new Random();
//mulai
while ("y".equals(MainLagi)) {
dkartu1 = random.nextInt(10) + 1;
dkartu2 = random.nextInt(10) + 1;
kartu1 = random.nextInt(10) + 1;
kartu2 = random.nextInt(10) + 1;
kartuTotal = kartu1 + kartu2;
dTotal = dkartu1 + dkartu2;
System.out.println("Dealer menunjukkan: " + dkartu1);
System.out.println("Kartu pertama: " + kartu1 + ", " + kartu2);
System.out.println("Total: " + kartuTotal);
System.out.println("Kartu lainnya (y/n)? : ");
kartulain = keyboard.nextLine();
while ("y".equals(kartulain)) {
KartuSelanjutnya = random.nextInt(10) + 1;
kartuTotal += KartuSelanjutnya;
System.out.println("kartu: " + KartuSelanjutnya);
System.out.println("Total: " + kartuTotal);
if (kartuTotal > 21) {
System.out.println("Kamu kalah, Dealer menang");
System.out.println("Apakah kamu ingin bermain lagi? (y/n):");
MainLagi = keyboard.nextLine();
}
if (kartuTotal < 21)
System.out.println("Kartu lain (y/n)?: ");
kartulain = keyboard.nextLine();
if ("n".equals(kartulain))
System.out.println("Dealer mempunyai: " + dTotal);
System.out.println("Kamu mempunyai: " + kartuTotal);
while ("n".equals(kartulain)) {
if (dTotal < kartuTotal && kartuTotal < 21) {
System.out.println("Yeaay kamu menang!");
System.out.println("Bermain lagi? (y/n): ");
MainLagi = keyboard.nextLine();
if (MainLagi.equalsIgnoreCase("y"))
MainLagi = "y";
if (dTotal > kartuTotal && dTotal < 21)
System.out.println("Kamu kalah!");
MainLagi = keyboard.nextLine();
}
}
}
}
}
}
Hasil compile
import java.util.Scanner;
import java.util.*;
public class Blackjack {
public static void main(String[] args) {
String kartulain, MainLagi = "y", ctn = null;
int KartuSelanjutnya, kartu1, kartu2, dkartu1, dkartu2, kartuTotal = 0, dTotal = 0;
Scanner keyboard = new Scanner(System.in);
Random random = new Random();
//mulai
while ("y".equals(MainLagi)) {
dkartu1 = random.nextInt(10) + 1;
dkartu2 = random.nextInt(10) + 1;
kartu1 = random.nextInt(10) + 1;
kartu2 = random.nextInt(10) + 1;
kartuTotal = kartu1 + kartu2;
dTotal = dkartu1 + dkartu2;
System.out.println("Dealer menunjukkan: " + dkartu1);
System.out.println("Kartu pertama: " + kartu1 + ", " + kartu2);
System.out.println("Total: " + kartuTotal);
System.out.println("Kartu lainnya (y/n)? : ");
kartulain = keyboard.nextLine();
while ("y".equals(kartulain)) {
KartuSelanjutnya = random.nextInt(10) + 1;
kartuTotal += KartuSelanjutnya;
System.out.println("kartu: " + KartuSelanjutnya);
System.out.println("Total: " + kartuTotal);
if (kartuTotal > 21) {
System.out.println("Kamu kalah, Dealer menang");
System.out.println("Apakah kamu ingin bermain lagi? (y/n):");
MainLagi = keyboard.nextLine();
}
if (kartuTotal < 21)
System.out.println("Kartu lain (y/n)?: ");
kartulain = keyboard.nextLine();
if ("n".equals(kartulain))
System.out.println("Dealer mempunyai: " + dTotal);
System.out.println("Kamu mempunyai: " + kartuTotal);
while ("n".equals(kartulain)) {
if (dTotal < kartuTotal && kartuTotal < 21) {
System.out.println("Yeaay kamu menang!");
System.out.println("Bermain lagi? (y/n): ");
MainLagi = keyboard.nextLine();
if (MainLagi.equalsIgnoreCase("y"))
MainLagi = "y";
if (dTotal > kartuTotal && dTotal < 21)
System.out.println("Kamu kalah!");
MainLagi = keyboard.nextLine();
}
}
}
}
}
}
Hasil compile
Komentar
Posting Komentar