/***************************************** * Software Engineering with BlackJack * an Example written by Mark Rich * for CS302, Spring 2000 ****************************************/ Problem: Develop a computer simulation for the game of BlackJack. Analysis: The goal of the game for a player is to get the highest number of points without exceeding 21. If their point total is greater than the dealer's point total, they win. Players are asked to place their bets. Then each player and the dealer are dealt two cards. In turn, each player is asked if they would like another card. When a player either decides to stay and ask for no more cards, or else their point total exceeds 21 (a bust), the game play passes to the next player. When all players have either busted or stuck, the Dealer deals him/herself cards until either a bust or their point total exceeds 17. If a player busted, they automatically loose their money. Players win their bets if their point total exceeds that of the dealer. If there is a tie, no money is won or lost. When the Dealer busts, all unbusted players automatically win. Design: We need the following classes: Card, Deck, Hand, Player, and Dealer class Card data (instance): suit spots value otherValue methods (instance): Constructor(suit, spots) getValue() returns value getOtherValue() returns otherValue class Deck data (instance): depthOfDeck Card(s) with an ordering methods (instance): Constructor(depthOfDeck) dealCard() returns top Card shuffle() addCard(Card) class Hand data (instance): Card(s) with an ordering value methods (instance): Constructor() addCard(Card) removeCards() getHandValue() returns value class Player data (instance): money Hand called myHand methods (instance): Constructor(money) hitMe() returns true or false getHandValue() returns Hand value getMoney() returns money getBet() returns the current bet (ask user) takeCard(Card) takeWinnings(winnings) class Dealer extends Player (through inheritence) data (class): hitLimit data (instance): Hand called myHand methods (instance): Constructor(depthOfDeck) hitMe() returns true or false getHandValue() returns Hand value takeCard(Card) class BlackJack data (instance): Player(s) called player1, player2, etc... Dealer called casinoStooge Deck called book bets called bet1, bet2, etc... methods (class): Constructor(numberOfPlayers, depthOfDeck, money) main(args) methods (instance): play() dealTwo(Player or Dealer) getBets() PseudoCode trace to describe the program flow BlackJack.main(); // Declare and Create a new BlackJack game BlackJack game; game = new BlackJack(3, 5, 1000); create a BlackJack object with 3 Players player1, player2, player3, a Deck depth of 5 called book, a Dealer called casinoStooge, and give each player $1000. // Play the game of BlackJack game.play(); while (at least one player has money leftover) book.shuffle(); // Collect the bets for the players getBets(); int bet1 = player1.getBet(); int bet2 = player2.getBet(); etc... // Deal the initial two cards to each player for (each playerX and casinoStooge) dealTwo(playerX); Card tempCard = book.dealCard(); playerX.takeCard(tempCard); myHand.addCard(tempCard); tempCard = book.dealCard(); playerX.takeCard(tempCard); myHand.addCard(tempCard); // Deal cards to the players until they stand or bust for (each playerX) boolean wantsHit = true; int handValue = playerX.getHandValue(); while (wantsHit == true and handValue < 22) wantsHit = playerX.hitMe(); find myHand value through myHand.getHandValue(); decide if player wants more cards returns true or false. if (wantsHit == true) Card tempCard = book.dealCard(); playerX.takeCard(tempCard); myHand.addCard(tempCard); handValue = playerX.getHandValue(); // Deal cards to the dealer until he/she stands or busts boolean wantsHit = true; int handValue = casinoStooge.getHandValue(); while (wantsHit == true and handValue < 22) wantsHit = casinoStooge.hitMe(); if (myHand.getHandValue() < hitLimit) return true else return false if (wantsHit == true) Card tempCard = book.dealCard(); casinoStooge.takeCard(tempCard); myHand.addCard(tempCard); handValue = casinoStooge.getHandValue(); // Determine who wins and loses for (each playerX) if (playerX.getHandValue() > 21) playerX.takeWinnings( -betX ) money = money - betX; myHand.removeCards(); else if (casinoStooge.getHandValue() > 21) playerX.takeWinnings( betX ) money = money + betX; myHand.removeCards(); else if (playerX.getHandValue() > casinoStooge.getHandValue()) playerX.takeWinnings( betX ) money = money + betX; myHand.removeCards(); else if (playerX.getHandValue() < casinoStooge.getHandValue()) playerX.takeWinnings( -betX ) money = money - betX; myHand.removeCards(); else playerX.takeWinnings( 0 ) money = money + 0; myHand.removeCards(); // Remove players that are bankrupt if (playerX.getMoney() <= 0) delete playerX from collection of players. // return to the top of the while loop under play.