Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Random;
- import java.util.Scanner;
- public class ThreeGames1 {
- static int game = 0;
- static int exit = 0;
- static boolean gameContinues = true;
- public static void main(String[] args) throws IOException {
- String Username = new String("Gerry"+"Evelynn"+"Joan");
- String Password = new String("guess"+"jack"+"sticks");
- BufferedReader br = new BufferedReader(
- new InputStreamReader(System.in));
- BufferedReader exitGame = new BufferedReader(
- new InputStreamReader(System.in));
- System.out.println("*** Please login ***\n");
- System.out.println("Username: ");
- Username = br.readLine();
- System.out.println(Username + " ");
- System.out.println("Password: ");
- Password = br.readLine();
- System.out.println(Password + " ");
- // Integer for player input
- int playerChoice = 0;
- ThreeGames game = new ThreeGames();
- Random randomNumber = new Random();
- System.out.println("Welcome to the System, please choose a game\n");
- playerChoice = (int) randomNumber.nextInt(3);
- playerChoice++; // this adds an addition to the players choice
- boolean bad_user_choice = true;
- while (bad_user_choice) // This ensures the user only inputs 1, 2 or 3 any other number will be invalid
- {
- System.out.println("Guess the Secret Number = 1, Pick up Sticks = 2, Black Jack = 3 Please enter a number: ");
- playerChoice = Integer.parseInt(br.readLine());
- if ((playerChoice == 1) || (playerChoice == 2) || (playerChoice == 3))
- bad_user_choice = false;
- }
- if (playerChoice == 1) {
- int amount_of_guesses = 0;
- int secretNumber;
- int guess =0;
- boolean are_you_ready = true;
- secretNumber = (int)randomNumber.nextInt(18);
- secretNumber++;
- System.out.println("Welcome! Try to guess the secret number");
- System.out.println();
- System.out.print("Enter a number between 1-19: ");
- while (are_you_ready)
- {
- guess=Integer.parseInt(br.readLine());
- if (guess == secretNumber)
- { System.out.println("Your guess is correct. Congratulations!");
- amount_of_guesses++;
- System.out.println("Total amount of guesses: " + amount_of_guesses );
- System.out.println(" Press 0 to Exit Game, or any other number to continue");
- exit = Integer.parseInt(exitGame.readLine());
- if (exit == 0) gameContinues = false;
- }
- if (guess < secretNumber)
- { System.out.println("Your guess is smaller than the secret number.");
- System.out.println("guess again");
- amount_of_guesses++;
- }
- if (guess > secretNumber)
- { System.out.println("Your guess is greater than the secret number.");
- System.out.println("guess again");
- amount_of_guesses++;
- }
- if (guess > 19)
- {System.out.println("please pick a number between 1 and 19.");
- }
- }
- }
- if (playerChoice == 2) {
- int numSticks = 21;
- System.out.println("Would you like to go first? (Y/n)");
- Scanner input = new Scanner(System.in);
- String goFirst = input.nextLine();
- Scanner take = new Scanner(System.in);
- int numToTake = 0;
- while (numSticks >0) {
- if (goFirst.equals("y") || goFirst.equals("Y")) {
- System.out.println("There are " + numSticks + " sticks");
- System.out.println("How many sticks to take (1 or 2)");
- numToTake = take.nextInt();
- if (numToTake >2) {
- numToTake = 2;
- }
- else if (numToTake <1) {
- numToTake = 1;
- }
- numSticks = numSticks - numToTake;
- if (numSticks <= 0) {
- System.out.println("You Lose!");
- }
- else {
- if ((numSticks - 2) % 3 == 0 || numSticks - 2 == 0) {
- numToTake = 1;
- }
- else {
- numToTake = 2;
- }
- System.out.println("Computer takes " + numToTake + " sticks");
- numSticks = numSticks - numToTake;
- if (numSticks <= 0) {
- System.out.println("You Win!");
- }
- else {
- System.out.println("There are " + numSticks + " sticks");
- System.out.println("How many Sticks to take (1 or 2)");
- numToTake = take.nextInt();
- if(numToTake >2) {
- numToTake = 2;
- }
- else if (numToTake < 1) {
- numToTake = 1;
- }
- numSticks = numSticks - numToTake;
- if (numSticks <= 0) {
- System.out.println("You Lose!");
- }
- }
- }
- }
- }
- }
- if (playerChoice == 3) {
- Scanner keyboard = new Scanner(System.in);
- //52 Cards, Aces = 11, Picture cards = 10, Ace's cannot be reduced to 1.
- 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};
- //Shuffle. Once per game.
- shuffleArray(newCard);
- //Start BlackJack.
- System.out.println("Welcome to BlackJack!");
- System.out.println();
- System.out.println("You get a " + newCard[0] + " and a " + newCard[1] + ".");
- int playerTotal = newCard[0] + newCard[1];
- System.out.println("Your total is " + playerTotal + ".");
- System.out.println();
- //Player can get blackjack/bust in the 1st deal. - awaiting betting system (enhanced bets for blackjack in first round)
- if (playerTotal == 21){
- System.out.println("Blackjack, you win.");
- System.out.println(" Press 0 to Exit Game, or any other number to continue");
- exit = Integer.parseInt(exitGame.readLine());
- if (exit == 0) gameContinues = false;
- }
- if (playerTotal > 21){
- System.out.println("Bust, You lose.");
- System.out.println(" Press 0 to Exit Game, or any other number to continue");
- exit = Integer.parseInt(exitGame.readLine());
- if (exit == 100) gameContinues = false;
- }
- // Dealer cards
- System.out.println("The dealer has a " + newCard[2] + " showing, and a hidden card.");
- int dealerTotal = newCard[2] + newCard[3];
- if (dealerTotal > 21){ //Dealer bust check.
- System.out.println();
- System.out.println("Dealers total is " + dealerTotal + ".");
- System.out.println("Dealer is bust, you win!");
- System.out.println(" Press 0 to Exit Game, or any other number to continue");
- exit = Integer.parseInt(exitGame.readLine());
- if (exit == 100) gameContinues = false;
- }
- if (dealerTotal == 21){ //Dealer blackjack check.
- System.out.println();
- System.out.println("Dealer reveals his second card: " + newCard[3] + ".");
- System.out.println("Dealers total is " + dealerTotal + ".");
- System.out.println();
- System.out.println("Dealer has BlackJack, you lose.");
- System.out.println(" Press 0 to Exit Game, or any other number to continue");
- exit = Integer.parseInt(exitGame.readLine());
- if (exit == 100) gameContinues = false;
- }
- System.out.println("His total is hidden.");
- System.out.println();
- // Hit or Stay for player.
- System.out.print("Would you like to \"hit\" or \"stay\"? ");
- String hitStay = keyboard.next();
- System.out.println();
- //cc = card count
- int cc = 4;
- if (hitStay.equalsIgnoreCase("hit")){
- // While loop to ensure different cards & multiple "hits".
- while (playerTotal < 21 && hitStay.equalsIgnoreCase("hit")){
- if (hitStay.equalsIgnoreCase("hit")){
- System.out.println("You drew a " + newCard[cc] + ".");
- playerTotal = playerTotal + newCard[cc];
- System.out.println("Your total is " + playerTotal + ".");
- System.out.println();
- cc++; //Adds 1 to ensure next card is different.
- // Bust & Blackjack check.
- if (playerTotal > 21){
- System.out.println("You are bust, You lose.");
- System.out.println(" Press 0 to Exit Game, or any other number to continue");
- exit = Integer.parseInt(exitGame.readLine());
- if (exit == 100) gameContinues = false;
- }
- if (playerTotal == 21){
- System.out.println("Blackjack, you win.");
- System.out.println(" Press 0 to Exit Game, or any other number to continue");
- exit = Integer.parseInt(exitGame.readLine());
- if (exit == 100) gameContinues = false;
- }
- System.out.print("Would you like to \"hit\" or \"stay\"? ");
- hitStay = keyboard.next();
- System.out.println();
- }
- }
- }
- // Dealers turn, only if Round 1 didn't end in bust/blackjack.
- keyboard.close();
- System.out.println("Ok dealers turn.");
- System.out.println("His hidden card was a " + newCard[3] + "."); // reveal hidden from round one.
- while (dealerTotal < 16){ // Dealer will stay on 16+ and hit if below.
- System.out.println();
- System.out.println("Dealer chooses to hit.");
- System.out.println("He draws a " + newCard[cc] + ".");
- cc++;
- dealerTotal = dealerTotal + newCard[cc];
- System.out.println();
- System.out.println("His total is " + dealerTotal);
- // bust check - no need for blackjack check due to final win sequence
- if (dealerTotal > 21){
- System.out.println();
- System.out.println("Dealer is bust, YOU WIN!");
- System.out.println(" Press 0 to Exit Game, or any other number to continue");
- exit = Integer.parseInt(exitGame.readLine());
- if (exit == 0) gameContinues = false;
- }
- // stay condition.
- if (dealerTotal < 21 && dealerTotal > 16){
- System.out.println();
- System.out.println("Dealer Stays.");
- }
- }
- // final win sequence.
- System.out.println();
- System.out.println("Dealer total is " + dealerTotal);
- System.out.println("Your total is " + playerTotal);
- System.out.println();
- if (dealerTotal > playerTotal){
- System.out.println("Dealer wins.");
- }
- if (dealerTotal == playerTotal){
- System.out.println("You both draw.");
- }
- if (dealerTotal < playerTotal){
- System.out.println("You win.");
- }
- }
- }
- static void shuffleArray(int[] deckCards){
- Random rnd = new Random();
- for (int i = deckCards.length - 1; i > 0; i--)
- {
- int index = rnd.nextInt(i + 1);
- // Swap
- int a = deckCards[index];
- deckCards[index] = deckCards[i];
- deckCards[i] = a;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement