game kartu menggunakan library random java
- Dapatkan link
- X
- Aplikasi Lainnya
Game kartu menggunakan library random java
Nama : Muhammad Fawwaz Zuhdan Nauvali
NRP : 05111740000106
Kelas : PBO A
Berikut ini adalah program untuk membuat game menebak kartu sederhana menggunakan library random di java. Pemain akan diberi 2 kartu, pemain bisa menebak salah satu dari kartu atau keduanya atau jumlah dari keduanya merupakan bilangan ganjil atau genap.
game.java
play.java
Nama : Muhammad Fawwaz Zuhdan Nauvali
NRP : 05111740000106
Kelas : PBO A
Berikut ini adalah program untuk membuat game menebak kartu sederhana menggunakan library random di java. Pemain akan diberi 2 kartu, pemain bisa menebak salah satu dari kartu atau keduanya atau jumlah dari keduanya merupakan bilangan ganjil atau genap.
game.java
- import java.util.Scanner;
- import java.util.Random;
- import java.util.concurrent.TimeUnit;
- public class Game {
- private final String[] sym = new String[]
- {
- "♤",
- "♥",
- "♢",
- "♧"
- };
- private final String[] val = new String[]
- {
- "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
- "J", "Q", "K"
- };
- private int ansl, ansr, nlft, nrgt, ans, gss;
- private int score;
- public Game()
- {
- score = 0;
- ansl = 0;
- ansr = 0;
- nlft = 0;
- nrgt = 0;
- gss = 0;
- ans = 0;
- System.out.println("Shuffling Cards... Please Wait...");
- try
- {
- TimeUnit.SECONDS.sleep(5);
- }
- catch (InterruptedException ie)
- {
- System.out.println("\n");
- }
- System.out.println("Here's your mystery card:");
- nlft = getCardNum();
- nrgt = getCardNum();
- String numleft = val[nlft];
- String numright = val[nrgt];
- String symleft = sym[getCardSym()];
- String symright = sym[getCardSym()];
- printCard("L", "R", " ", " ", 0, 0);
- play();
- System.out.println("Your cards were:");
- printCard(numleft, numright, symleft, symright, nlft+1, nrgt+1);
- }
- public int check()
- {
- if (gss == 1)
- {
- int a = ansl%2;
- int b = (nlft + 1)%2;
- if (a == 0 && b == 0)
- {
- score = score + 3;
- System.out.println("Congratulation! You guessed it right.");
- System.out.println("You got 3 points.");
- }
- else if ( a != 0 && b !=0 )
- {
- score = score + 3;
- System.out.println("Congratulation! You guessed it right.");
- System.out.println("You got 3 points.");
- }
- else
- {
- score = score - 1;
- System.out.println("Ow! You guessed wrongly.");
- System.out.println("You got -1 points.");
- System.out.println("\nBut don't worry, You can play again.");
- }
- }
- else if (gss == 2)
- {
- int a = ansr%2;
- int b = (nrgt + 1)%2;
- if (a == 0 && b == 0)
- {
- score = score + 3;
- System.out.println("Congratulation! You guessed it right.");
- System.out.println("You got 3 points.");
- }
- else if ( a != 0 && b !=0 )
- {
- score = score + 3;
- System.out.println("Congratulation! You guessed it right.");
- System.out.println("You got 3 points.");
- }
- else
- {
- score = score - 1;
- System.out.println("Ow! You guessed wrongly.");
- System.out.println("You got -1 points.");
- System.out.println("\nBut don't worry, You can play again.");
- }
- }
- else if (gss == 3)
- {
- int a = ansl%2;
- int b = (nlft + 1)%2;
- int c = ansr%2;
- int d = (nrgt + 1)%2;
- if (((a == 0 && b == 0) || (a != 0 && b != 0)) &&
- ((c == 0 && d == 0) || (c != 0 && d != 0)))
- {
- score = score + 8;
- System.out.println("Congratulation! You guessed it right.");
- System.out.println("You got 8 points.");
- }
- else
- {
- score = score - 4;
- System.out.println("Ow! You guessed wrongly.");
- System.out.println("You got -4 points.");
- System.out.println("\nBut don't worry, You can play again.");
- }
- }
- else if (gss == 4)
- {
- int a = (ansl + ansr)%2;
- int b = (nlft + nrgt + 2)%2;
- if (a == 0 && b == 0)
- {
- score = score + 6;
- System.out.println("Congratulation! You guessed it right.");
- System.out.println("You got 6 points.");
- }
- else if ( a != 0 && b !=0 )
- {
- score = score + 6;
- System.out.println("Congratulation! You guessed it right.");
- System.out.println("You got 6 points.");
- }
- else
- {
- score = score - 3;
- System.out.println("Ow! You guessed wrongly.");
- System.out.println("You got -3 points.");
- System.out.println("\nBut don't worry, You can play again.");
- }
- }
- else
- {
- score = score;
- }
- return score;
- }
- public void play()
- {
- System.out.println("Wanna Guess?");
- System.out.println("1. Left card only.");
- System.out.println("2. Right card only.");
- System.out.println("3. Left and right card.");
- System.out.println("4. Addition of both cards.");
- System.out.println("Or insert number other than 1 - 4 to cancel the game.");
- Scanner sc = new Scanner(System.in);
- gss = sc.nextInt();
- switch(gss)
- {
- case 1:
- System.out.println("Left Card is: ");
- System.out.println("(insert 1 = Odd or 2 = Even)");
- ansl = sc.nextInt();
- break;
- case 2:
- System.out.println("Right Card is: ");
- System.out.println("(insert 1 = Odd or 2 = Even)");
- ansr = sc.nextInt();
- break;
- case 3:
- System.out.println("Left Card is: ");
- System.out.println("(insert 1 = Odd or 2 = Even)");
- ansl = sc.nextInt();
- System.out.println("Right Card is: ");
- System.out.println("(insert 1 = Odd or 2 = Even)");
- ansr = sc.nextInt();
- break;
- case 4:
- System.out.println("Left + Right Card Number is: ");
- System.out.println("(insert 1 = Odd or 2 = Even)");
- ansl = sc.nextInt();
- ans = ansr + ansl;
- break;
- default:
- System.out.println("You have choosen to cancel the game.");
- break;
- }
- }
- public void printCard(String numleft, String numright, String symleft, String symright, int num1, int num2)
- {
- if (num1 != 10 && num2 != 10)
- {
- System.out.println("_____________ _____________");
- System.out.println("| | | |");
- System.out.println("| | | |");
- System.out.println("| | | |");
- System.out.println("| "+numleft+" | | "+numright+" |");
- System.out.println("| "+symleft+" | | "+symright+" |");
- System.out.println("| | | |");
- System.out.println("| | | |");
- System.out.println("|___________| |___________|\n");
- }
- else if (num1 == 10 && num2 != 10)
- {
- System.out.println("______________ _____________");
- System.out.println("| | | |");
- System.out.println("| | | |");
- System.out.println("| | | |");
- System.out.println("| "+numleft+" | | "+numright+" |");
- System.out.println("| "+symleft+" | | "+symright+" |");
- System.out.println("| | | |");
- System.out.println("| | | |");
- System.out.println("|____________| |___________|\n");
- }
- else if (num1 != 10 && num2 == 10)
- {
- System.out.println("_____________ ______________");
- System.out.println("| | | |");
- System.out.println("| | | |");
- System.out.println("| | | |");
- System.out.println("| "+numleft+" | | "+numright+" |");
- System.out.println("| "+symleft+" | | "+symright+" |");
- System.out.println("| | | |");
- System.out.println("| | | |");
- System.out.println("|___________| |____________|\n");
- }
- else
- {
- System.out.println("______________ ______________");
- System.out.println("| | | |");
- System.out.println("| | | |");
- System.out.println("| | | |");
- System.out.println("| "+numleft+" | | "+numright+" |");
- System.out.println("| "+symleft+" | | "+symright+" |");
- System.out.println("| | | |");
- System.out.println("| | | |");
- System.out.println("|____________| |____________|\n");
- }
- }
- public int getCardNum()
- {
- Random rnd = new Random();
- int num = rnd.nextInt(12);
- return num;
- }
- public int getCardSym()
- {
- Random rnd = new Random();
- int sym = rnd.nextInt(3);
- return sym;
- }
- }
play.java
- import java.util.Scanner;
- public class Play {
- public static void main(String[] args)
- {
- int totalScore = 0;;
- System.out.println("Welcome to The Game of Odds or Evens");
- System.out.println("Instruction:");
- System.out.println("1. You will be given 2 random cards,");
- System.out.println(" named LEFT card and RIGHT card.");
- System.out.println("2. The cards are set upside down.");
- System.out.println("3. You should guess whether those two");
- System.out.println(" cards or their sum result are odd or");
- System.out.println(" even number.");
- System.out.println("4. Note that Ace is equal to 1, Jack");
- System.out.println(" is equal to 11, Queen is equal to 12,");
- System.out.println(" and King is equal to 13.");
- System.out.println("5. Guessing score details: ");
- System.out.println(" Left card only, true = 3 pts, false = -1 pts.");
- System.out.println(" Right card only, true = 3 pts, false = -1 pts.");
- System.out.println(" Left and right card, true = 8 pts, false = -4 pts.");
- System.out.println(" Addition of both cards, true = 6 pts, false = -3 pts.\n");
- System.out.println("Want to start the game?");
- System.out.println("Insert y (yes) or n (no)");
- int t = 0;
- while( t == 0)
- {
- Scanner sc = new Scanner(System.in);
- char yn = sc.next().charAt(0);
- if (yn == 'y')
- {
- t = 1;
- Game g = new Game();
- totalScore= totalScore + (g.check());
- int t2 = 0;
- while(t2 == 0)
- {
- System.out.println("\nContiune Playing?");
- System.out.println("Insert y (yes) or n (no)");
- char yn2 = sc.next().charAt(0);
- if (yn2 == 'y')
- {
- Game gg = new Game();
- totalScore= totalScore + (gg.check());
- }
- else if (yn2 == 'n')
- {
- t2 = 1;
- System.out.println("Your score accumulation: " +totalScore);
- System.out.println("Thanks for Playing");
- }
- else
- {
- System.out.println("\nPlease insert y or n only.");
- }
- }
- }
- else if (yn == 'n')
- {
- t = 1;
- System.out.println("~See You~");
- }
- else
- {
- System.out.println("\nPlease insert y or n only.");
- }
- }
- }
- }
- Dapatkan link
- X
- Aplikasi Lainnya
Komentar
Posting Komentar