2Sssssblackjack/Src/Blackjack Game Engine/Card.Java Blackjack/Src/Card.Java
Total Page:16
File Type:pdf, Size:1020Kb
...... @@ -0,0 +1,17 @@ 1 + 2 +
17 BlackjackGameEngine/bin/.project
View
...... @@ -0,0 +1,17 @@ 1 + 2 +
2 blackjack/src/blackjack_game_engine/Card.java → blackjack/src/Card.java View
...... @@ -1,5 +1,3 @@ 1 - 2 - 3 1 /** 4 2 * @filename Card.java 5 3 * @author Alex Euzent
0 blackjack/src/blackjack_game_engine/Dealer.java → blackjack/src/Dealer.java
View File renamed without changes
0 blackjack/src/blackjack_game_engine/GameDeck.java → blackjack/src/GameDeck.java
View File renamed without changes
32 blackjack/src/blackjack_game_engine/GameEngine.java → blackjack/src/GameEngine.java
View
...... @@ -1,5 +1,3 @@ 1 - 2 - 3 1 /** 4 2 * @filename GameEngine.java 5 3 * @author Alex Euzent, Justin Pounders @@ -52,6 +50,7 @@ public GameEngine() { 52 50 public GameEngine(double playScore) { 53 51 deck = new GameDeck(); 54 52 player = new GamePlayer(playScore); 53 + dealer = new Dealer(); 55 54 round = new Round(player, dealer, deck); 56 55 cardsDealt = false; 57 56 } @@ -59,11 +58,16 @@ public GameEngine(double playScore) { 59 58 60 59 /** 61 60 * Starts a round of play 62 - * and deals cards 63 - */ 64 - public void startRound(){ 65 - round.startRound(); 66 - cardsDealt = true; 61 + * and deals cards. Returns 62 + * true if round started successfully 63 + * @return boolean 64 + */ 65 + public boolean startRound(){ 66 + boolean go = round.startRound(); 67 + if(go){ 68 + cardsDealt = true; 69 + } 70 + return go; 67 71 } 68 72 69 73 @@ -109,10 +113,17 @@ public boolean didGameTie(){ 109 113 110 114 /** 111 115 * Processes a player's input once 112 - * a round is in progress 116 + * a round is in progress. The 117 + * boolean return is only for the 118 + * commit bet option and will be 119 + * false if the player lacks the score 120 + * to make a bet. All other options should 121 + * always be true. 113 122 * @param tag 123 + * @return boolean 114 124 */ 115 - public void playersTurn(int tag){ 125 + public boolean playersTurn(int tag){ 126 + boolean done = true; 116 127 if(cardsDealt){ 117 128 switch(tag){ 118 129 case 0: //quit @@ -128,7 +139,7 @@ public void playersTurn(int tag){ 128 139 break; 129 140 130 141 case 3: //commit bet 131 - round.playerBet(); 142 + done = round.playerBet(); 132 143 break; 133 144 134 145 case 4: //hit @@ -140,6 +151,7 @@ public void playersTurn(int tag){ 140 151 break; 141 152 } 142 153 } 154 + return done; 143 155 } 144 156 145 157
34 blackjack/src/blackjack_game_engine/GamePlayer.java → blackjack/src/GamePlayer.java
View
@@ -26,6 +26,7 @@ public GamePlayer() { 26 26 hand = new Hand(); 27 27 score = 100; 28 28 currBet = 5; 29 + activeBet = 0; 29 30 } 30 31 31 32 @@ -38,13 +39,14 @@ public GamePlayer(double score) { 38 39 hand = new Hand(); 39 40 this.score = score; 40 41 currBet = 5; 42 + activeBet = 0; 41 43 } 42 44 43 45 44 46 /** 45 47 * Returns a copy of cards 46 48 * currently in hand 47 - * @return 49 + * @return ArrayList
1 blackjack/src/blackjack_game_engine/Hand.java → blackjack/src/Hand.java
View @@ -28,6 +28,7 @@ public Hand() { 28 28 * @param card 29 29 */ 30 30 public void getCard(Card card){ 31 + 31 32 hand.add(card); 32 33 runHandSum(); 33 34 }
53 blackjack/src/blackjack_game_engine/Round.java → blackjack/src/Round.java
View
...... @@ -1,5 +1,3 @@ 1 - 2 - 3 1 /** 4 2 * @filename Round.java 5 3 * @author Alex Euzent @@ -12,7 +10,7 @@ 12 10 13 11 private GameDeck deck; 14 12 private boolean playerTurn, noWinner, playerWin, tie, firstTurn; 15 - private int moves, lastMoves; 13 + private int moves, lastMoves, roundCount; 16 14 private GamePlayer player; 17 15 private Dealer dealer; 18 16 @@ -31,22 +29,36 @@ public Round(GamePlayer player, Dealer dealer, GameDeck deck) { 31 29 playerTurn = true; 32 30 moves = 0; 33 31 lastMoves = 0; 32 + roundCount = 0; 34 33 } 35 34 36 35 37 36 /** 38 - * Resets object for a new round 37 + * Resets object for a new round. 38 + * Returns true if round started 39 + * successfully. False if player 40 + * lacks score to ante up 41 + * @return boolean 39 42 */ 40 - public void startRound(){ 41 - collectTrash(); 42 - firstTurn = true; 43 - playerTurn = true; 44 - tie = false; 45 - lastMoves = moves; 46 - moves = 0; 47 - noWinner = true; 48 - playerWin = false; 49 - deal(); 43 + public boolean startRound(){ 44 + boolean ante = player.anteUp(); 45 + if(ante){ 46 + if(roundCount > 0){ 47 + collectTrash(); 48 + } 49 + roundCount++; 50 + firstTurn = true; 51 + playerTurn = true; 52 + tie = false; 53 + lastMoves = moves; 54 + moves = 0; 55 + noWinner = true; 56 + playerWin = false; 57 + deal(); 58 + } else { 59 + noWinner = false; 60 + } 61 + return ante; 50 62 } 51 63 52 64 @@ -54,7 +66,6 @@ public void startRound(){ 54 66 * Deals cards and collects opening bet 55 67 */ 56 68 private void deal(){ 57 - player.anteUp(); 58 69 for(int x = 0; x < 4; x++){ 59 70 if(x/2 == 0){ 60 71 //dealers cards @@ -64,6 +75,7 @@ private void deal(){ 64 75 player.hit(deck.getCard()); 65 76 } 66 77 } 78 + 67 79 } 68 80 69 81 @@ -205,13 +217,16 @@ public void playerStay(){ 205 217 206 218 /** 207 219 * Allows a player to commit a bet 208 - * if it's their turn 220 + * if it's their turn. Returns false 221 + * if player lacks score to bet. 222 + * @return boolean 209 223 */ 210 - public void playerBet(){ 224 + public boolean playerBet(){ 225 + boolean done = false; 211 226 if(playerTurn){ 212 - player.commitBet(); 213 - 227 + done = player.commitBet(); 214 228 } 229 + return done; 215 230 } 216 231 217 232
241 blackjack/src/testMain.java
View
@@ -0,0 +1,241 @@ +//This source is meant to be a guide on how to use the +//GameEngine class to run a blackjack game. The GameEngine +//class is meant to be a board thats state can be read +//at any time and limits user input to a few carefully +//designed methods. You can compile and run this to play +//a text version of the game which is fully functional. +import java.util.*; +import java.io.*; + +public class testMain { + + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + //The game engine source is your access point to the whole game + //To use it to restore a previous game, just pass the old score + //to the constructor. + static GameEngine game = new GameEngine(); + + + public static void main(String[] args) { + boolean done = false; + while(!done){ + System.out.println("------"); + System.out.println("NEW GAME"); + System.out.println("------"); + + //Your temporary bet value is stored inside of the game + //and can be accessed using getSelectedBet. + System.out.println("\nSelected Bet is " + game.getSelectedBet()); + System.out.println("------"); + + int x = preDealMenu(); + + switch(x){ + case 0: done = true; + break; + + //Changing a bet before a game starts + //should be done with the preBet method + //The value selected will be committed + //once a round starts + case 1: game.preBet(game.UPBET); + break; + + case 2: game.preBet(game.DOWNBET); + break; + + case 3: runGame(); + break; + } + } + + } + + //This method showcases some of the changes I made about betting + //Now each round can fail when you try to start it if the player + //lacks the score to ante up + public static void runGame(){ + if(game.startRound()){ + runRound(); + } else { + System.out.println("------"); + System.out.println("Game could not be started. Player lacks score to play."); + } + + } + + public static void runRound(){ + + boolean done = false; + while(!done){ + displayGame(); + int x = inGameMenu(); + switch(x){ + case 0: done = true; + //End round is called automatically when a win occurs + //but you must call it explicitly if you want to end + //a round early + game.endRound(); + break; + + //The playersTurn method handles all + //ingame interaction with the player. + //I've written a series of constant ints + //that will make calling this function very + //easy. + case 1: game.playersTurn(game.UPBET); + break; + + case 2: game.playersTurn(game.DOWNBET); + break; + + case 3: //The whole playersTurn method is now a boolean but commiting + //a bet is the only place it can fail. This will only happen if + //the player lacks the score to make a bet. Otherwise it will always + //return true + if(!game.playersTurn(game.GOBET)){ + System.out.println("Player lacks score to make bet"); + }; + break; + + case 4: game.playersTurn(game.HIT); + break; + + case 5: game.playersTurn(game.STAY); + break; + + } + + //isRoundActive is your way to see if a game + //is in progress + if(!game.isRoundActive()){ + //didPlayerWin is true only if the player won + //the last round. But it should not be called unless + //isRoundActive is false + winScreen(game.didPlayerWin()); + game.endRound(); + done = true; + } + + } + + + + } + + + + public static int preDealMenu(){ + int i = 0; + System.out.println("\nPre-Game Menu"); + System.out.println("0 - Quit"); + System.out.println("1 - Increase Bet"); + System.out.println("2 - Decrease Bet"); + System.out.println("3 - Deal Cards"); + System.out.print("Make Selection: "); + try{ + i = Integer.parseInt(br.readLine()); + + }catch(NumberFormatException nfe){ + System.err.println("Invalid Format!"); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return i; + } + + public static int inGameMenu(){ + int i = 0; + System.out.println("\nIn-Game Menu"); + System.out.println("0 - Quit"); + System.out.println("1 - Increase Bet"); + System.out.println("2 - Decrease Bet"); + System.out.println("3 - Commit Bet"); + System.out.println("4 - Hit"); + System.out.println("5 - Stay"); + + System.out.print("Make Selection: "); + try{ + i = Integer.parseInt(br.readLine()); + + }catch(NumberFormatException nfe){ + System.err.println("Invalid Format!"); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return i; + } + + public static void displayGame(){ + //You can get a copy of the cards currently in play by using the below commands + ArrayList