Code
// BlackJack by Andre import java.util.*; import hsa.*; public class blackjack{ static Console c; static final int maxSuits = 4; static final int spades = 0; static final int clubs = 1; static final int hearts = 2; static final int diamonds = 3; static final int maxCards = 14; static final int A = 1; static final int J = 11; static final int Q = 12; static final int K = 13; static final int maxGiven = 10; static int[] plrCards; static int[] plrSuits; static int[] compCards; static int[] compSuits; public static void main (String[] args) { Console con = new Console(); int money = 1000; int buyin = 75; int bet = 0; int win = (bet) * 3; int lose = 100; int tie = 50; int bust = 100; int numWon = 0; int numLost = 0; int intMenu; int intStartGame; String strGameName; String strUsername; strUsername = ""; strGameName = "BlackJack"; con.println(); con.println("Welcome to the BlackJack main menu"); con.println("__________.__ __ ____. __"); con.println("\\\\______ \\\\ | _____ ____ | | __ | |____ ____ | | __"); con.println(" | | _/ | \\\\__ \\\\ _/ ___\\\\| |/ / | \\\\__ \\\\ _/ ___\\\\| |/ /"); con.println(" | | \\\\ |__/ __ \\\\\\\\ \\\\___| </\\\\__| |/ __ \\\\\\\\ \\\\___| < "); con.println(" |______ /____(____ /\\\\___ >__|_ \\\\________(____ /\\\\___ >__|_ \\\\ "); con.println(" \\\\/ \\\\/ \\\\/ \\\\/ \\\\/ \\\\/ \\\\/"); con.println(); con.println(); con.println(); con.println(".--..-..-. .---..-..-..--. .---. .---. .-..-..---..---..---..-..----."); con.println("|-< > / | | || .` || \\ \\| |-< | |- \\ / | |- \\ \\ \\ \\ | || || |"); con.println("`--' `-' `-^-'`-'`-'`-'-'`-'`-'`---' `' `---'`---'`---'`-'`----'"); con.println(); con.println(" MENU "); con.println("____________________________________________________________________________"); con.println("If you would like information about the BlackJack tables and start a game: Press 1."); con.println("If you would like to view the highscores file: Press 2"); con.println("If you would like credits: Press 3"); con.println("If you would like to quit the program: Press 4"); con.println("____________________________________________________________________________"); con.println(); intMenu = con.readInt(); con.clear(); if( intMenu == 1){ con.println("Table Information:"); con.println(); con.println("Please note that the keys ( 1 and 0) will be used throughout this program."); con.println("To navigate the menus and play the BlackJack game."); con.println(); con.println("Winning a Game of BlackJack: $" + win); con.println("Losing a Game of BlackJack: $-" + lose); con.println("Tieing a Game of BlackJack: $-" + tie); con.println("Busting out of a Game of BlackJack: $-" + bust); con.println(); con.println("If you would like to start a game Press 1 Once again!"); con.println(); }else if( intMenu == 2){ con.println(); con.println("Credits: Andre Vessio, Mr Cadawas for teaching."); con.println("Really, I shouldn't be even writing this because in my previous program,"); con.println("I said I didn't have to do my CPT :D"); con.println(); try { Thread.sleep(2000); } catch(InterruptedException e) { } con.println("Press 1 to Start the BlackJack Game"); con.println("Here are the instructions: "); con.println("Table Information:"); con.println(); con.println("Please note that the keys ( 1 and 0) will be used throughout this program."); con.println("To navigate the menus and play the BlackJack game."); con.println(" ________________________________________________"); con.println(" |Winning a Game of BlackJack: You win 3 times your bet |"); con.println(" |Losing a Game of BlackJack: You loose your bet |"); con.println(" |Tieing a Game of BlackJack: You get your money back |"); con.println(" |Busting out of a Game of BlackJack: You loose your bet |"); con.println(" |------------------------------------------------"); con.println("If you would like to start a game Press 1 Once again!"); con.println(); }else if(intMenu == 3){ con.println("Exiting"); System.exit(0); con.clear(); } intStartGame = con.readInt(); if (intStartGame == 1){ con.println(); con.println("Starting Game..."); con.println(); } con.println("In order to play " +strGameName+ " you must pay a $ " + buyin + " buy in."); con.println(); con.println("Do you wish to play? (1 for YES, 2 for NO)"); int answer = con.readInt(); if( answer != 1 ) { con.clear(); } con.clear(); // Arrays for cards String[] nameSuits = new String[maxSuits]; nameSuits[spades] = "Spades"; nameSuits[clubs] = "Clubs"; nameSuits[hearts] = "Hearts"; nameSuits[diamonds] = "Diamonds"; String[] nameCards = new String[maxCards]; nameCards[A] = "A"; nameCards[J] = "J"; nameCards[Q] = "Q"; nameCards[K] = "K"; int[] valueCards = new int[maxCards]; valueCards[A] = 1; valueCards[J] = 10; valueCards[Q] = 10; valueCards[K] = 10; for( int i = 2; i <= 10; i++ ) { nameCards[i] = String.valueOf(i); valueCards[i] = i; } plrCards = new int[maxGiven]; plrSuits = new int[maxGiven]; compCards = new int[maxGiven]; compSuits = new int[maxGiven]; generateCards(); int given = 2; boolean finished = false; while( true ) { con.clear(); con.println("Username:" + strUsername); con.println("Money: $" + money); con.println("Games Won: " + numWon); con.println("Games Lost: " + numLost); con.println(); con.println("My Cards:"); int plrTotal1 = 0; int plrTotal2 = 0; boolean plrAce = false; for( int i = 0; i < given; i++ ) { con.println("Card " + (i + 1) + ": " + nameCards[plrCards[i]] + " of " + nameSuits[plrSuits[i]]); if( plrCards[i] == A ) { plrAce = true; plrTotal1 += 1; plrTotal2 += 11; } else { plrTotal1 += valueCards[plrCards[i]]; plrTotal2 += valueCards[plrCards[i]]; } } int compTotal1 = valueCards[compCards[0]]; int compTotal2 = valueCards[compCards[0]]; boolean compAce = false; if( compCards[0] == A ) { compAce = true; compTotal2 += 10; } con.println(); con.println("Dealer Cards:"); if( finished ) { int numCompCards = 2; compTotal1 = 0; compTotal2 = 0; compAce = false; int temp1 = 0; int temp2 = 0; boolean tempAce = false; for( int i = 0; i < maxGiven; i++ ) { temp1 += valueCards[compCards[i]]; temp2 += valueCards[compCards[i]]; if( compCards[i] == A ) { tempAce = true; temp2 += 10; } if( 16 <= temp1 && temp1 <= 21 || tempAce && 16 <= temp2 && temp2 <= 21 || temp1 > 21 ) { numCompCards = i + 1; break; } if( temp1 > 21 ) { temp1 -= valueCards[compCards[i]]; numCompCards = i; break; } } compTotal1 = 0; compTotal2 = 0; compAce = false; for( int i = 0; i < numCompCards; i++ ) { compTotal1 += valueCards[compCards[i]]; compTotal2 += valueCards[compCards[i]]; if( compCards[i] == A ) { compAce = true; compTotal2 += 10; } con.println("Card " + (i + 1) + ": " + nameCards[compCards[i]] + " of " + nameSuits[compSuits[i]]); } } else { con.println("Card 1: " + nameCards[compCards[0]] + " of " + nameSuits[compSuits[0]]); con.println("Card 2: - 2nd Dealer Card Hidden-"); } con.println(); if( plrAce ) { if( plrTotal1 <= 21 ) { con.println("My Value 1: " + plrTotal1); } else { con.println("My Value 1: " + plrTotal1 + " [BUSTED]"); } if( plrTotal2 <= 21 ) { con.println("My Value 2: " + plrTotal2); } else { con.println("My Value 2: " + plrTotal2 + " [BUSTED]"); } } else { if( plrTotal1 <= 21 ) { con.println("My Value: " + plrTotal1); } else { con.println("My Value: " + plrTotal1 + " [BUSTED]"); } } con.println(); if( compAce ) { con.println("Dealers' 1st Value: " + compTotal1); con.println("Dealer's 2nd Value: " + compTotal2); } else { con.println("Dealer Value: " + compTotal1); } con.println(); if( plrTotal1 > 21 ) { numLost++; money -= bust; con.println("You busted!"); con.println(); con.println("Play again? 1=Yes 0=No"); answer = con.readInt(); if( answer == 1 ) { given = 2; finished = false; generateCards(); con.clear(); continue; } else { break; } } if( !finished ) { con.println(); con.println("Do you want another card? 1=Yes 0=No"); answer = con.readInt(); if( answer == 1 ) { given++; } else { finished = true; } } else { con.println(); // Calculate computer total if( plrAce ) { if( compAce ) { if( 21 >= plrTotal1 && plrTotal1 > compTotal1 && 21 >= plrTotal1 && plrTotal1 > compTotal2 || 21 >= plrTotal2 && plrTotal2 > compTotal1 && 21 >= plrTotal2 && plrTotal2 > compTotal2 || compTotal1 > 21 ) { numWon++; con.println("You won!"); money += win; } else { numLost++; con.println("You lost!"); money -= lose; } } else { if( 21 >= plrTotal1 && plrTotal1 > compTotal1 || 21 >= plrTotal2 && plrTotal2 > compTotal1 || compTotal1 > 21 ) { numWon++; con.println("You won!"); money += win; } else { numLost++; con.println("You lost!"); money -= lose; } } } else { if( compAce ) { if( 21 >= plrTotal1 && (plrTotal1 > compTotal1 || compTotal1 > 21) && (plrTotal1 > compTotal2 || compTotal2 > 21) || compTotal1 > 21 ) { numWon++; con.println("You won!"); money += win; } else { numLost++; con.println("You lost!"); money -= lose; } } else { if( 21 >= plrTotal1 && plrTotal1 > compTotal1 || compTotal1 > 21 ) { numWon++; con.println("You won!"); money += win; } else { numLost++; con.println("You lost!"); money -= lose; } } } con.println(); con.println("Play again? 1=Yes 0=No"); answer = con.readInt(); if( answer == 1 ) { given = 2; finished = false; generateCards(); } else { break; } } con.clear(); } } public static void generateCards() { for( int i = 0; i < maxGiven; i++ ) { boolean allow; do { allow = true; plrCards[i] = getCard(); for( int j = i - 1; j >= 0; j-- ) { if( plrCards[j] == plrCards[i] && plrSuits[i] == plrSuits[j] ) { allow = false; break; } } if( allow ) { for( int j = i; j >= 0; j-- ) { if( plrCards[i] == compCards[j] && plrSuits[i] == compSuits[j] ) { allow = false; break; } } } } while( !allow ); do { allow = true; compCards[i] = getCard(); for( int j = i - 1; j >= 0; j-- ) { if( compCards[j] == compCards[i] && compSuits[i] == compSuits[j] ) { allow = false; break; } } if( allow ) { for( int j = i; j >= 0; j-- ) { if( compCards[i] == plrCards[j] && compSuits[i] == plrSuits[j] ) { allow = false; break; } } } } while( !allow ); plrSuits[i] = getSuit(); compSuits[i] = getSuit(); } } public static int getSuit() { return randomNum(0, maxSuits - 1); } public static int getCard() { return randomNum(1, maxCards - 1); } public static int randomNum(int min, int max) { return (int)(Math.random() * (max + 1 - min) + min); } }


LinkBack URL
About LinkBacks
Reply With Quote
.