2Sssssblackjack/Src/Blackjack Game Engine/Card.Java Blackjack/Src/Card.Java

2Sssssblackjack/Src/Blackjack Game Engine/Card.Java Blackjack/Src/Card.Java

<p> ...... @@ -0,0 +1,17 @@ 1 +<?xml version="1.0" encoding="UTF-8"?> 2 +<projectDescription> 3 + <name>BlackjackGameEngine</name> 4 + <comment></comment> 5 + <projects> 6 + </projects> 7 + <buildSpec> 8 + <buildCommand> 9 + <name>org.eclipse.jdt.core.javabuilder</name> 10 + <arguments> 11 + </arguments> 12 + </buildCommand> 13 + </buildSpec> 14 + <natures> 15 + <nature>org.eclipse.jdt.core.javanature</nature> 16 + </natures> 17 +</projectDescription></p><p>17  BlackjackGameEngine/bin/.project</p><p>View</p><p>...... @@ -0,0 +1,17 @@ 1 +<?xml version="1.0" encoding="UTF-8"?> 2 +<projectDescription> 3 + <name>BlackjackGameEngine</name> 4 + <comment></comment> 5 + <projects> 6 + </projects> 7 + <buildSpec> 8 + <buildCommand> 9 + <name>org.eclipse.jdt.core.javabuilder</name> 10 + <arguments> 11 + </arguments> 12 + </buildCommand> 13 + </buildSpec> 14 + <natures> 15 + <nature>org.eclipse.jdt.core.javanature</nature> 16 + </natures> 17 +</projectDescription></p><p>2  blackjack/src/blackjack_game_engine/Card.java → blackjack/src/Card.java View</p><p>...... @@ -1,5 +1,3 @@ 1 - 2 - 3 1 /** 4 2 * @filename Card.java 5 3 * @author Alex Euzent</p><p>0  blackjack/src/blackjack_game_engine/Dealer.java → blackjack/src/Dealer.java</p><p>View File renamed without changes</p><p>0  blackjack/src/blackjack_game_engine/GameDeck.java → blackjack/src/GameDeck.java</p><p>View File renamed without changes</p><p>32  blackjack/src/blackjack_game_engine/GameEngine.java → blackjack/src/GameEngine.java</p><p>View</p><p>...... @@ -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 </p><p>34  blackjack/src/blackjack_game_engine/GamePlayer.java → blackjack/src/GamePlayer.java</p><p>View</p><p>@@ -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<Card> 48 50 */ 49 51 public ArrayList<Card> showHand(){ 50 52 return hand.showCards(); @@ -53,7 +55,7 @@ public GamePlayer(double score) { 53 55 54 56 /** 55 57 * Returns current hand sum 56 - * @return 58 + * @return int 57 59 */ 58 60 public int currHandSum(){ 59 61 return hand.getHandSum(); @@ -62,7 +64,7 @@ public int currHandSum(){ 62 64 63 65 /** 64 66 * Accessor for player score 65 - * @return 67 + * @return double 66 68 */ 67 69 public double getScore(){ 68 70 return score; @@ -72,7 +74,7 @@ public double getScore(){ 72 74 /** 73 75 * Accessor for currently 74 76 * selected bet amount 75 - * @return 77 + * @return int 76 78 */ 77 79 public int getCurrentBet(){ 78 80 return currBet; @@ -82,7 +84,7 @@ public int getCurrentBet(){ 82 84 /** 83 85 * Returns amount that is 84 86 * currently being bet 85 - * @return 87 + * @return int 86 88 */ 87 89 public int getActiveBet(){ 88 90 return activeBet; @@ -91,11 +93,18 @@ public int getActiveBet(){ 91 93 92 94 /** 93 95 * Makes opening bet at the start 94 - * of a round 96 + * of a round. Returns false if 97 + * player lacks score to make bet 98 + * @return boolean 95 99 */ 96 - public void anteUp(){ 97 - activeBet = currBet; 98 - score -= activeBet; 100 + public boolean anteUp(){ 101 + boolean done = false; 102 + if(score - currBet >= 0){ 103 + activeBet = currBet; 104 + score -= activeBet; 105 + done = true; 106 + } 107 + return done; 99 108 100 109 } 101 110 @@ -128,12 +137,13 @@ public void decreaseBet(){ 128 137 129 138 /** 130 139 * Commits current selected bet as 131 - * an active bet 132 - * @return 140 + * an active bet. Returns false if 141 + * player lacks score to make bet 142 + * @return boolean 133 143 */ 134 144 public boolean commitBet(){ 135 145 boolean done = false; 136 - int newBet = currBet - activeBet; 146 + int newBet = currBet; 137 147 if(score - newBet >= 0){ 138 148 score -= newBet; 139 149 done = true;</p><p>1  blackjack/src/blackjack_game_engine/Hand.java → blackjack/src/Hand.java</p><p>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 }</p><p>53  blackjack/src/blackjack_game_engine/Round.java → blackjack/src/Round.java</p><p>View</p><p>...... @@ -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 </p><p>241  blackjack/src/testMain.java</p><p>View</p><p>@@ -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<Card> playerHand = new ArrayList<Card>(game.getPlayerHand()); + ArrayList<Card> dealerHand = new ArrayList<Card>(game.getDealerHand()); + + //Methods to retrieve the players bets and score are detailed below. + //playerBet is the amount currently being bet on this round + int playerBet = game.getPlayerBet(); + int selectedBet = game.getSelectedBet(); + double playerScore = game.getPlayerScore(); + + System.out.println("------"); + System.out.println("Dealer's Cards: "); + + //Be sure to hide the first card in the dealers hand + //getDealerHand will give you all the cards + System.out.println("XXXXXXXXXXXXXX"); + for(int x = 1; x < dealerHand.size(); x++){ + System.out.println(dealerHand.get(x)); + } + System.out.println("\nPlayer's Cards: "); + for(Card card : playerHand){ + System.out.println(card); + } + System.out.println("------"); + + System.out.println("\nAmmount currently bet: " + playerBet); + System.out.println("Selected Bet: " + selectedBet); + System.out.println("Player score is " + playerScore); + System.out.println("------"); + + } + + public static void winScreen(boolean playerWin){ + ArrayList<Card> playerHand = new ArrayList<Card>(game.getPlayerHand()); + ArrayList<Card> dealerHand = new ArrayList<Card>(game.getDealerHand()); + double playerScore = game.getPlayerScore(); + + System.out.println("------"); + System.out.println("GAME OVER"); + //didPlayerTie is true only if the last round + //ended in a tie. It should be called before the + //didPlayerWin method as it's value will be incorrect + //if the round was a tie. But neither should be called + //unless isRoundActive is false + if(game.didGameTie()){ + System.out.println("------"); + System.out.println("Game Tied"); + }else if(playerWin){ + System.out.println("------"); + System.out.println("Player Wins!"); + } else { + System.out.println("------"); + System.out.println("Player Loses"); + + } + System.out.println("Score = " + playerScore); + System.out.println("------"); + System.out.println("Dealer's Cards: "); + for(int x = 0; x < dealerHand.size(); x++){ + System.out.println(dealerHand.get(x)); + } + System.out.println("\nPlayer's Cards: "); + for(Card card : playerHand){ + System.out.println(card); + } + System.out.println("------"); + } + + +}</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    15 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us