game kartu menggunakan library random java

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
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. import java.util.concurrent.TimeUnit;
  4.    
  5. public class Game {
  6.     private final String[] sym = new String[]
  7.     {
  8.         "♤",
  9.         "♥",
  10.         "♢",
  11.         "♧"
  12.     };
  13.     private final String[] val = new String[]
  14.     {
  15.        "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
  16.         "J", "Q", "K"
  17.     };
  18.     private int ansl, ansr, nlft, nrgt, ans, gss;
  19.     private int score;
  20.    
  21.     public Game()
  22.     {
  23.         score = 0;
  24.         ansl = 0;
  25.         ansr = 0;
  26.         nlft = 0;
  27.         nrgt = 0;
  28.         gss = 0;
  29.         ans = 0;
  30.         System.out.println("Shuffling Cards... Please Wait...");
  31.         try
  32.         {
  33.             TimeUnit.SECONDS.sleep(5);
  34.         }
  35.         catch (InterruptedException ie)
  36.         {
  37.             System.out.println("\n");
  38.         }
  39.         System.out.println("Here's your mystery card:");
  40.         nlft = getCardNum();
  41.         nrgt = getCardNum();
  42.         String numleft = val[nlft];
  43.         String numright = val[nrgt];
  44.         String symleft = sym[getCardSym()];
  45.         String symright = sym[getCardSym()];
  46.         printCard("L", "R", " ", " ", 0, 0);
  47.         play();
  48.         System.out.println("Your cards were:");
  49.         printCard(numleft, numright, symleft, symright, nlft+1, nrgt+1);
  50.     }
  51.    
  52.     public int check()
  53.     {
  54.         if (gss == 1)
  55.         {
  56.             int a = ansl%2;
  57.             int b = (nlft + 1)%2;
  58.             if (a == 0 && b == 0)
  59.             {
  60.                 score = score + 3;
  61.                 System.out.println("Congratulation! You guessed it right.");
  62.                 System.out.println("You got 3 points.");
  63.             }
  64.             else if ( a != 0 && b !=0 )
  65.             {
  66.                 score = score + 3;
  67.                 System.out.println("Congratulation! You guessed it right.");
  68.                 System.out.println("You got 3 points.");
  69.             }
  70.             else
  71.             {
  72.                 score = score - 1;
  73.                 System.out.println("Ow! You guessed wrongly.");
  74.                 System.out.println("You got -1 points.");
  75.                 System.out.println("\nBut don't worry, You can play again.");
  76.             }
  77.         }
  78.         else if (gss == 2)
  79.         {
  80.             int a = ansr%2;
  81.             int b = (nrgt + 1)%2;
  82.             if (a == 0 && b == 0)
  83.             {
  84.                 score = score + 3;
  85.                 System.out.println("Congratulation! You guessed it right.");
  86.                 System.out.println("You got 3 points.");
  87.             }
  88.             else if ( a != 0 && b !=0 )
  89.             {
  90.                 score = score + 3;
  91.                 System.out.println("Congratulation! You guessed it right.");
  92.                 System.out.println("You got 3 points.");
  93.             }
  94.             else
  95.             {
  96.                 score = score - 1;
  97.                 System.out.println("Ow! You guessed wrongly.");
  98.                 System.out.println("You got -1 points.");
  99.                 System.out.println("\nBut don't worry, You can play again.");
  100.             }
  101.         }
  102.         else if (gss == 3)
  103.         {
  104.             int a = ansl%2;
  105.             int b = (nlft + 1)%2;
  106.             int c = ansr%2;
  107.             int d = (nrgt + 1)%2;
  108.             if (((a == 0 && b == 0) || (a != 0 && b != 0)) &&
  109.             ((c == 0 && d == 0) || (c != 0 && d != 0)))
  110.             {
  111.                 score = score + 8;
  112.                 System.out.println("Congratulation! You guessed it right.");
  113.                 System.out.println("You got 8 points.");
  114.             }
  115.             else
  116.             {
  117.                 score = score - 4;
  118.                 System.out.println("Ow! You guessed wrongly.");
  119.                 System.out.println("You got -4 points.");
  120.                 System.out.println("\nBut don't worry, You can play again.");
  121.             }
  122.         }
  123.         else if (gss == 4)
  124.         {
  125.            int a = (ansl + ansr)%2;
  126.            int b = (nlft + nrgt + 2)%2;
  127.             if (a == 0 && b == 0)
  128.             {
  129.                 score = score + 6;
  130.                 System.out.println("Congratulation! You guessed it right.");
  131.                 System.out.println("You got 6 points.");
  132.             }
  133.             else if ( a != 0 && b !=0 )
  134.             {
  135.                 score = score + 6;
  136.                 System.out.println("Congratulation! You guessed it right.");
  137.                 System.out.println("You got 6 points.");
  138.             }
  139.             else
  140.             {
  141.                 score = score - 3;
  142.                 System.out.println("Ow! You guessed wrongly.");
  143.                 System.out.println("You got -3 points.");
  144.                 System.out.println("\nBut don't worry, You can play again.");
  145.             }
  146.         }
  147.         else
  148.         {
  149.             score = score;
  150.         }
  151.         return score;
  152.     }
  153.    
  154.     public void play()
  155.     {
  156.         System.out.println("Wanna Guess?");
  157.         System.out.println("1. Left card only.");
  158.         System.out.println("2. Right card only.");
  159.         System.out.println("3. Left and right card.");
  160.         System.out.println("4. Addition of both cards.");
  161.         System.out.println("Or insert number other than 1 - 4 to cancel the game.");
  162.         Scanner sc = new Scanner(System.in);
  163.         gss = sc.nextInt();
  164.         switch(gss)
  165.         {
  166.             case 1:
  167.             System.out.println("Left Card is: ");
  168.             System.out.println("(insert 1 = Odd or 2 = Even)");
  169.             ansl = sc.nextInt();
  170.             break;
  171.             case 2:
  172.             System.out.println("Right Card is: ");
  173.             System.out.println("(insert 1 = Odd or 2 = Even)");
  174.             ansr = sc.nextInt();
  175.             break;
  176.             case 3:
  177.             System.out.println("Left Card is: ");
  178.             System.out.println("(insert 1 = Odd or 2 = Even)");
  179.             ansl = sc.nextInt();
  180.             System.out.println("Right Card is: ");
  181.             System.out.println("(insert 1 = Odd or 2 = Even)");
  182.             ansr = sc.nextInt();
  183.             break;
  184.             case 4:
  185.             System.out.println("Left + Right Card Number is: ");
  186.             System.out.println("(insert 1 = Odd or 2 = Even)");
  187.             ansl = sc.nextInt();
  188.             ans = ansr + ansl;
  189.             break;
  190.             default:
  191.             System.out.println("You have choosen to cancel the game.");
  192.             break;
  193.         }
  194.     }
  195.    
  196.     public void printCard(String numleft, String numright, String symleft, String symright, int num1, int num2)
  197.     {
  198.         if (num1 != 10 && num2 != 10)
  199.         {
  200.             System.out.println("_____________     _____________");
  201.             System.out.println("|           |     |           |");
  202.             System.out.println("|           |     |           |");
  203.             System.out.println("|           |     |           |");
  204.             System.out.println("|     "+numleft+"     |     |     "+numright+"     |");
  205.             System.out.println("|     "+symleft+"     |     |     "+symright+"     |");
  206.             System.out.println("|           |     |           |");
  207.             System.out.println("|           |     |           |");
  208.             System.out.println("|___________|     |___________|\n");
  209.         }
  210.         else if (num1 == 10 && num2 != 10)
  211.         {
  212.             System.out.println("______________     _____________");
  213.             System.out.println("|            |     |           |");
  214.             System.out.println("|            |     |           |");
  215.             System.out.println("|            |     |           |");
  216.             System.out.println("|     "+numleft+"     |     |     "+numright+"     |");
  217.             System.out.println("|      "+symleft+"     |     |     "+symright+"     |");
  218.             System.out.println("|            |     |           |");
  219.             System.out.println("|            |     |           |");
  220.             System.out.println("|____________|     |___________|\n");
  221.         }
  222.         else if (num1 != 10 && num2 == 10)
  223.         {
  224.             System.out.println("_____________     ______________");
  225.             System.out.println("|           |     |            |");
  226.             System.out.println("|           |     |            |");
  227.             System.out.println("|           |     |            |");
  228.             System.out.println("|     "+numleft+"     |     |     "+numright+"     |");
  229.             System.out.println("|     "+symleft+"     |     |      "+symright+"     |");
  230.             System.out.println("|           |     |            |");
  231.             System.out.println("|           |     |            |");
  232.             System.out.println("|___________|     |____________|\n");
  233.         }
  234.         else
  235.         {
  236.             System.out.println("______________     ______________");
  237.             System.out.println("|            |     |            |");
  238.             System.out.println("|            |     |            |");
  239.             System.out.println("|            |     |            |");
  240.             System.out.println("|     "+numleft+"     |     |     "+numright+"     |");
  241.             System.out.println("|      "+symleft+"     |     |      "+symright+"     |");
  242.             System.out.println("|            |     |            |");
  243.             System.out.println("|            |     |            |");
  244.             System.out.println("|____________|     |____________|\n");
  245.         }
  246.     }
  247.    
  248.     public int getCardNum()
  249.     {
  250.         Random rnd = new Random();
  251.         int num = rnd.nextInt(12);
  252.         return num;
  253.     }
  254.    
  255.     public int getCardSym()
  256.     {
  257.         Random rnd = new Random();
  258.         int sym = rnd.nextInt(3);
  259.         return sym;
  260.     }
  261. }

play.java
 
  1. import java.util.Scanner;
  2.    
  3. public class Play {
  4.    
  5.     public static void main(String[] args)
  6.     {
  7.         int totalScore = 0;;
  8.         System.out.println("Welcome to The Game of Odds or Evens");
  9.             System.out.println("Instruction:");
  10.             System.out.println("1. You will be given 2 random cards,");
  11.             System.out.println("   named LEFT card and RIGHT card.");
  12.             System.out.println("2. The cards are set upside down.");
  13.             System.out.println("3. You should guess whether those two");
  14.             System.out.println("   cards or their sum result are odd or");
  15.             System.out.println("   even number.");
  16.             System.out.println("4. Note that Ace is equal to 1, Jack");
  17.             System.out.println("   is equal to 11, Queen is equal to 12,");
  18.             System.out.println("   and King is equal to 13.");
  19.             System.out.println("5. Guessing score details: ");
  20.             System.out.println("   Left card only, true = 3 pts, false = -1 pts.");
  21.             System.out.println("   Right card only, true = 3 pts, false = -1 pts.");
  22.             System.out.println("   Left and right card, true = 8 pts, false = -4 pts.");
  23.             System.out.println("   Addition of both cards, true = 6 pts, false = -3 pts.\n");
  24.             System.out.println("Want to start the game?");
  25.             System.out.println("Insert y (yes) or n (no)");
  26.             int t = 0;
  27.             while( t == 0)
  28.             {
  29.                 Scanner sc = new Scanner(System.in);
  30.                 char yn = sc.next().charAt(0);
  31.                 if (yn == 'y')
  32.                 {
  33.                     t = 1;
  34.                     Game g = new Game();
  35.                     totalScore= totalScore + (g.check());
  36.                     int t2 = 0;
  37.                 while(t2 == 0)
  38.                 {
  39.                         System.out.println("\nContiune Playing?");
  40.                         System.out.println("Insert y (yes) or n (no)");
  41.                         char yn2 = sc.next().charAt(0);
  42.                         if (yn2 == 'y')
  43.                         {
  44.                             Game gg = new Game();
  45.                             totalScore= totalScore + (gg.check());
  46.                         }
  47.                         else if (yn2 == 'n')
  48.                         {
  49.                             t2 = 1;
  50.                             System.out.println("Your score accumulation: " +totalScore);
  51.                             System.out.println("Thanks for Playing");
  52.                         }
  53.                         else
  54.                         {
  55.                             System.out.println("\nPlease insert y or n only.");
  56.                         }
  57.                     }
  58.                 }
  59.                 else if (yn == 'n')
  60.                 {
  61.                     t = 1;
  62.                     System.out.println("~See You~");
  63.                 }
  64.                 else
  65.                 {
  66.                     System.out.println("\nPlease insert y or n only.");
  67.                 }  
  68.         }
  69.     }
  70. }


 


Komentar

Postingan populer dari blog ini

PBKK - Pengenalan Framework CodeIgniter

Framework Desktop Electron

ETS PBO - I-KIOS INDOMARET