CARD GAME
kelas kelas pada Card game :
Kelas MainOperator :
import java.util.*;
public class MainOperator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Checker check = new Checker();
Hint h = new Hint(check.getAngka());
System.out.println("Selamat datang di permainan tebak kartu!");
System.out.println("Silakan menebak kartu random yang telah di generate oleh komputer!");
System.out.println();
int i=0;
while (!check.getBenar())
{
System.out.println("Masukkan angka (1-10, J, Q, K, A): ");
String input = sc.next();
input = input.toUpperCase();
i++;
if (!check.checkInput(input))
{
System.out.println("Input anda salah");
continue;
}
check.checkJawaban(input);
h.getHint(input);
}
System.out.println("Selamat Jawaban anda benar! Setelah mencoba "+ i +" kali");
sc.close();
}
}
Kelas Hint :
public class Hint extends Converter {
private int angkaRD;
public Hint(int angka)
{
angkaRD = angka;
}
public void getHint(String input)
{
int num = checkInt(input);
if (num > angkaRD)
{
System.out.println("(kartu sebenarnya lebih kecil dari "+ input+")");
}
else if (num < angkaRD)
{
System.out.println("(kartu sebenarnya lebih besar dari "+ input+")");
}
}
}
Kelas Converter :
public class Converter {
protected int checkInt(String input)
{
int in = 0;
try
{
in = Integer.parseInt(input);
}
catch (NumberFormatException | NullPointerException npe)
{
if (input.equals("J")) in = 11;
else if (input.equals("Q")) in = 12;
else if (input.equals("K")) in = 13;
else if (input.equals("A")) in = 14;
}
return in;
}
}
Kelas Checker :
public class Checker extends Converter {
private boolean benar = false;
private int angka;
private int check;
public Checker() {
Randomizer rd = new Randomizer();
angka = rd.randomNext(14, 1);
}
public boolean getBenar()
{
return benar;
}
public int getAngka()
{
return angka;
}
public void checkJawaban(String input)
{
if (angka <= 10 && check == angka)
{
benar = true;
}
else if (angka > 10)
{
if (check == angka)
benar = true;
}
if (!benar) System.out.println("Jawaban anda salah, silakan menebak kembali");
}
public boolean checkInput(String input)
{
check = checkInt(input);
if(check >= 1 && check <= 10)
return true;
else if (input.equals("J") || input.equals("Q") || input.equals("K") || input.equals("A"))
return true;
return false;
}
}
Kelas Randomizer :
import java.util.*;
public class Randomizer {
protected int randomNext(int max, int min)
{
Random rd = new Random();
int angkaRand = rd.nextInt((max - min)+ 1) + min;
return angkaRand;
}
}
OutPut :
Kelas MainOperator :
import java.util.*;
public class MainOperator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Checker check = new Checker();
Hint h = new Hint(check.getAngka());
System.out.println("Selamat datang di permainan tebak kartu!");
System.out.println("Silakan menebak kartu random yang telah di generate oleh komputer!");
System.out.println();
int i=0;
while (!check.getBenar())
{
System.out.println("Masukkan angka (1-10, J, Q, K, A): ");
String input = sc.next();
input = input.toUpperCase();
i++;
if (!check.checkInput(input))
{
System.out.println("Input anda salah");
continue;
}
check.checkJawaban(input);
h.getHint(input);
}
System.out.println("Selamat Jawaban anda benar! Setelah mencoba "+ i +" kali");
sc.close();
}
}
Kelas Hint :
public class Hint extends Converter {
private int angkaRD;
public Hint(int angka)
{
angkaRD = angka;
}
public void getHint(String input)
{
int num = checkInt(input);
if (num > angkaRD)
{
System.out.println("(kartu sebenarnya lebih kecil dari "+ input+")");
}
else if (num < angkaRD)
{
System.out.println("(kartu sebenarnya lebih besar dari "+ input+")");
}
}
}
Kelas Converter :
public class Converter {
protected int checkInt(String input)
{
int in = 0;
try
{
in = Integer.parseInt(input);
}
catch (NumberFormatException | NullPointerException npe)
{
if (input.equals("J")) in = 11;
else if (input.equals("Q")) in = 12;
else if (input.equals("K")) in = 13;
else if (input.equals("A")) in = 14;
}
return in;
}
}
Kelas Checker :
public class Checker extends Converter {
private boolean benar = false;
private int angka;
private int check;
public Checker() {
Randomizer rd = new Randomizer();
angka = rd.randomNext(14, 1);
}
public boolean getBenar()
{
return benar;
}
public int getAngka()
{
return angka;
}
public void checkJawaban(String input)
{
if (angka <= 10 && check == angka)
{
benar = true;
}
else if (angka > 10)
{
if (check == angka)
benar = true;
}
if (!benar) System.out.println("Jawaban anda salah, silakan menebak kembali");
}
public boolean checkInput(String input)
{
check = checkInt(input);
if(check >= 1 && check <= 10)
return true;
else if (input.equals("J") || input.equals("Q") || input.equals("K") || input.equals("A"))
return true;
return false;
}
}
Kelas Randomizer :
import java.util.*;
public class Randomizer {
protected int randomNext(int max, int min)
{
Random rd = new Random();
int angkaRand = rd.nextInt((max - min)+ 1) + min;
return angkaRand;
}
}
OutPut :
Comments
Post a Comment