Advertisement
Guest User

Untitled

a guest
Apr 4th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.95 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class ThreeGames1 {
  6.    
  7.     static int game = 0;
  8.     static int exit = 0;
  9.     static boolean gameContinues = true;
  10.  
  11.        
  12. public static void main(String[] args) throws IOException {
  13.    
  14.     String Username = new String("Gerry"+"Evelynn"+"Joan");
  15.     String Password = new String("guess"+"jack"+"sticks");
  16.  
  17.     BufferedReader br = new BufferedReader(
  18.     new InputStreamReader(System.in));
  19.     BufferedReader exitGame = new BufferedReader(
  20.             new InputStreamReader(System.in));
  21.  
  22.     System.out.println("*** Please login ***\n");
  23.  
  24.     System.out.println("Username: ");
  25.     Username = br.readLine();
  26.     System.out.println(Username + " ");
  27.  
  28.     System.out.println("Password: ");
  29.     Password = br.readLine();
  30.     System.out.println(Password + " ");
  31.    
  32.  
  33.  // Integer for player input
  34.     int playerChoice = 0;
  35.    
  36.     ThreeGames game = new ThreeGames();
  37.     Random randomNumber = new Random();
  38.    
  39.     System.out.println("Welcome to the System, please choose a game\n");
  40.    
  41.         playerChoice = (int) randomNumber.nextInt(3);
  42.         playerChoice++;  // this adds an addition to the players choice
  43.        
  44.         boolean bad_user_choice = true;
  45.        
  46.         while (bad_user_choice)   // This ensures the user only inputs 1, 2 or 3 any other number will be invalid
  47.         {
  48.         System.out.println("Guess the Secret Number = 1, Pick up Sticks = 2, Black Jack = 3 Please enter a number: ");
  49.         playerChoice = Integer.parseInt(br.readLine());
  50.         if ((playerChoice == 1) || (playerChoice == 2) || (playerChoice == 3))
  51.             bad_user_choice = false;   
  52.         }
  53.        
  54.         if (playerChoice == 1) {
  55.            
  56.             int amount_of_guesses = 0;
  57.             int secretNumber;
  58.             int guess =0;
  59.            
  60.             boolean are_you_ready = true;
  61.            
  62.             secretNumber = (int)randomNumber.nextInt(18);
  63.             secretNumber++;  
  64.  
  65.         System.out.println("Welcome! Try to guess the secret number");
  66.         System.out.println();
  67.         System.out.print("Enter a number between 1-19: ");
  68.         while (are_you_ready)
  69.         {
  70.  
  71.         guess=Integer.parseInt(br.readLine());
  72.  
  73.         if (guess == secretNumber)
  74.         {   System.out.println("Your guess is correct. Congratulations!");
  75.         amount_of_guesses++;
  76.         System.out.println("Total amount of guesses: " + amount_of_guesses  );
  77.         System.out.println(" Press 0 to Exit Game, or any other number to continue");
  78.         exit = Integer.parseInt(exitGame.readLine());
  79.         if (exit == 0) gameContinues  = false;
  80.         }
  81.         if (guess < secretNumber)
  82.         {   System.out.println("Your guess is smaller than the secret number.");
  83.                 System.out.println("guess again");
  84.         amount_of_guesses++;
  85.         }  
  86.         if (guess > secretNumber)
  87.         { System.out.println("Your guess is greater than the secret number.");
  88.                 System.out.println("guess again");
  89.         amount_of_guesses++;
  90.         }
  91.         if (guess > 19)
  92.         {System.out.println("please pick a number between 1 and 19.");
  93.        
  94.         }  
  95.         }
  96.         }
  97.        
  98.         if (playerChoice == 2) {
  99.            
  100.             int numSticks = 21;
  101.             System.out.println("Would you like to go first? (Y/n)");
  102.             Scanner input = new Scanner(System.in);
  103.             String goFirst = input.nextLine();
  104.             Scanner take = new Scanner(System.in);
  105.             int numToTake = 0;
  106.            
  107.             while (numSticks >0) {
  108.                
  109.                 if (goFirst.equals("y") || goFirst.equals("Y")) {
  110.                     System.out.println("There are " + numSticks + " sticks");
  111.                     System.out.println("How many sticks to take (1 or 2)");
  112.                     numToTake = take.nextInt();
  113.                    
  114.                     if (numToTake >2) {
  115.                         numToTake = 2;
  116.                     }
  117.                     else if (numToTake <1) {
  118.                         numToTake = 1;
  119.                     }
  120.                     numSticks = numSticks - numToTake;
  121.                    
  122.                     if (numSticks <= 0) {
  123.                         System.out.println("You Lose!");
  124.                     }
  125.                     else {
  126.                        
  127.                         if ((numSticks - 2) % 3 == 0 || numSticks - 2 == 0) {
  128.                             numToTake = 1;
  129.                         }
  130.                         else {
  131.                             numToTake = 2;
  132.                         }
  133.                         System.out.println("Computer takes " + numToTake + " sticks");
  134.                         numSticks = numSticks - numToTake;
  135.                        
  136.                         if (numSticks <= 0) {
  137.                             System.out.println("You Win!");
  138.                         }
  139.                         else {
  140.                             System.out.println("There are " + numSticks + " sticks");
  141.                             System.out.println("How many Sticks to take (1 or 2)");
  142.                             numToTake = take.nextInt();
  143.                            
  144.                             if(numToTake >2) {
  145.                                 numToTake = 2;
  146.                             }
  147.                             else if (numToTake < 1) {
  148.                                 numToTake = 1;
  149.                             }
  150.                             numSticks = numSticks - numToTake;
  151.                            
  152.                             if (numSticks <= 0) {
  153.                                 System.out.println("You Lose!");
  154.                             }
  155.                         }
  156.                     }
  157.                 }
  158.             }
  159.         }
  160.        
  161. if (playerChoice == 3) {
  162.  
  163.     Scanner keyboard = new Scanner(System.in);
  164.  
  165. //52 Cards, Aces = 11, Picture cards = 10, Ace's cannot be reduced to 1.
  166. int[] newCard = {2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11};
  167.  
  168. //Shuffle. Once per game.
  169. shuffleArray(newCard);
  170.  
  171. //Start BlackJack.
  172. System.out.println("Welcome to BlackJack!");
  173. System.out.println();
  174. System.out.println("You get a " + newCard[0] + " and a " + newCard[1] + ".");
  175. int playerTotal = newCard[0] + newCard[1];
  176. System.out.println("Your total is " + playerTotal + ".");
  177. System.out.println();
  178.  
  179. //Player can get blackjack/bust in the 1st deal. - awaiting betting system (enhanced bets for blackjack in first round)
  180. if (playerTotal == 21){
  181.     System.out.println("Blackjack, you win.");
  182.     System.out.println(" Press 0 to Exit Game, or any other number to continue");
  183.     exit = Integer.parseInt(exitGame.readLine());
  184.     if (exit == 0) gameContinues  = false;
  185. }
  186. if (playerTotal > 21){
  187.     System.out.println("Bust, You lose.");
  188.     System.out.println(" Press 0 to Exit Game, or any other number to continue");
  189.     exit = Integer.parseInt(exitGame.readLine());
  190.     if (exit == 100) gameContinues  = false;
  191. }
  192. // Dealer cards
  193. System.out.println("The dealer has a " + newCard[2] + " showing, and a hidden card.");
  194. int dealerTotal = newCard[2] + newCard[3];
  195. if (dealerTotal > 21){     //Dealer bust check.
  196.     System.out.println();
  197.     System.out.println("Dealers total is " + dealerTotal + ".");
  198.     System.out.println("Dealer is bust, you win!");
  199.     System.out.println(" Press 0 to Exit Game, or any other number to continue");
  200.     exit = Integer.parseInt(exitGame.readLine());
  201.     if (exit == 100) gameContinues  = false;
  202. }
  203. if (dealerTotal == 21){    //Dealer blackjack check.
  204.     System.out.println();
  205.     System.out.println("Dealer reveals his second card: " + newCard[3] + ".");
  206.     System.out.println("Dealers total is " + dealerTotal + ".");
  207.     System.out.println();
  208.     System.out.println("Dealer has BlackJack, you lose.");
  209.     System.out.println(" Press 0 to Exit Game, or any other number to continue");
  210.     exit = Integer.parseInt(exitGame.readLine());
  211.     if (exit == 100) gameContinues  = false;
  212. }
  213. System.out.println("His total is hidden.");
  214. System.out.println();
  215.  
  216.  
  217. // Hit or Stay for player.
  218. System.out.print("Would you like to \"hit\" or \"stay\"? ");
  219. String hitStay = keyboard.next();
  220. System.out.println();
  221.  
  222. //cc = card count
  223. int cc = 4;
  224. if (hitStay.equalsIgnoreCase("hit")){
  225.     // While loop to ensure different cards & multiple "hits".
  226.     while (playerTotal < 21 && hitStay.equalsIgnoreCase("hit")){
  227.         if (hitStay.equalsIgnoreCase("hit")){
  228.             System.out.println("You drew a " + newCard[cc] + ".");
  229.             playerTotal = playerTotal + newCard[cc];
  230.             System.out.println("Your total is " + playerTotal + ".");
  231.             System.out.println();
  232.             cc++;        //Adds 1 to ensure next card is different.
  233.             // Bust & Blackjack check.
  234.             if (playerTotal > 21){
  235.                 System.out.println("You are bust, You lose.");
  236.                 System.out.println(" Press 0 to Exit Game, or any other number to continue");
  237.                 exit = Integer.parseInt(exitGame.readLine());
  238.                 if (exit == 100) gameContinues  = false;
  239.             }
  240.             if (playerTotal == 21){
  241.                 System.out.println("Blackjack, you win.");
  242.                 System.out.println(" Press 0 to Exit Game, or any other number to continue");
  243.                 exit = Integer.parseInt(exitGame.readLine());
  244.                 if (exit == 100) gameContinues  = false;
  245.             }
  246.             System.out.print("Would you like to \"hit\" or \"stay\"? ");
  247.             hitStay = keyboard.next();
  248.             System.out.println();
  249.         }
  250.     }        
  251. }
  252.  
  253. // Dealers turn, only if Round 1 didn't end in bust/blackjack.
  254. keyboard.close();
  255. System.out.println("Ok dealers turn.");
  256. System.out.println("His hidden card was a " + newCard[3] + "."); // reveal hidden from round one.
  257.  
  258. while (dealerTotal < 16){ // Dealer will stay on 16+ and hit if below.
  259.     System.out.println();
  260.     System.out.println("Dealer chooses to hit.");
  261.     System.out.println("He draws a " + newCard[cc] + ".");
  262.     cc++;
  263.     dealerTotal = dealerTotal + newCard[cc];
  264.     System.out.println();
  265.     System.out.println("His total is " + dealerTotal);
  266.     // bust check - no need for blackjack check due to final win sequence
  267.     if (dealerTotal > 21){
  268.         System.out.println();
  269.         System.out.println("Dealer is bust, YOU WIN!");
  270.         System.out.println(" Press 0 to Exit Game, or any other number to continue");
  271.         exit = Integer.parseInt(exitGame.readLine());
  272.         if (exit == 0) gameContinues  = false;
  273.     }
  274.     // stay condition.
  275.     if (dealerTotal < 21 && dealerTotal > 16){
  276.         System.out.println();
  277.         System.out.println("Dealer Stays.");
  278.     }
  279. }
  280.  
  281. // final win sequence.
  282. System.out.println();
  283. System.out.println("Dealer total is " + dealerTotal);
  284. System.out.println("Your total is " + playerTotal);
  285. System.out.println();
  286.  
  287. if (dealerTotal > playerTotal){
  288.     System.out.println("Dealer wins.");
  289. }
  290. if (dealerTotal == playerTotal){
  291.     System.out.println("You both draw.");
  292. }
  293. if (dealerTotal < playerTotal){
  294.     System.out.println("You win.");
  295. }
  296. }
  297. }
  298.  
  299. static void shuffleArray(int[] deckCards){
  300.  
  301. Random rnd = new Random();
  302. for (int i = deckCards.length - 1; i > 0; i--)
  303. {
  304.     int index = rnd.nextInt(i + 1);
  305.     // Swap
  306.     int a = deckCards[index];
  307.     deckCards[index] = deckCards[i];
  308.     deckCards[i] = a;
  309. }
  310. }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement