SUBMIT REPORT User: fujioyx Name: Fujio, Yoshihiro Course: cs239 Assignment: pa2 Version: 1 Date: Mon Mar 19 08:58:40 PM 2007 Files: BridgeDriver.java BridgeHand.java Card.java Deck.java Pl ayer.java Rank.java Suit.java PDF-spec: 90/99 Honor Pledge: I have NOT received unauthorized assistance Compiling ... javac BridgeDriver.java BridgeHand.java Card.java Deck.java Player.java Rank.j ava Suit.java 2007-03-19 20:59 BridgeDriver.java Page 1 1 /** 2 * @author Yoshihiro Fujio 3 * CS239 section 2 4 * PA2 5 * V1 date 2007/03/11 6 **/ 7 /********************************************************************** 8 * BridgeDriver.java 9 * 10 * This will welcome user, introduce the Bridge game, ask 4 player's 11 * names, jump to each method, show the result of the game, tell the 12 * player with highest hand, and give the end program message. 13 ********************************************************************/ 14 import java.util.Scanner; 15 16 public class BridgeDriver 17 { 18 public static void main(String args[]) 19 { 20 final int PLAYERS = 4; // numbers of players 21 String[] player; // Array of players 22 Deck myDeck; // Deck of cards 23 BridgeHand[] hand; // Array of BridgeHands 24 Player[] play; // Array of Player 25 Scanner keyb; // keyboard scanner 26 27 keyb = new Scanner(System.in); 28 player = new String[PLAYERS]; // set array to 4 29 myDeck = new Deck(); 30 hand = new BridgeHand[PLAYERS]; // set array to 4 31 play = new Player[PLAYERS]; // set array to 4 32 33 34 System.out.println("Welcome to Bridge Game.\n"); // welcome user 35 // introducing program 36 System.out.println("This program will ask 4 player's name,\n" + 37 " play the Bridge Game, and Announce the\n" + 38 " player with the highest hand\n"); 39 40 41 42 // asking player's name 43 for (int ii = 0; ii < PLAYERS; ii++) 44 { 45 System.out.println("What is player " + (ii + 1) + "'s name: "); 46 player[ii]= keyb.nextLine(); 47 } 48 49 for (int ii = 0; ii < PLAYERS; ii++) 50 { 51 hand[ii] = new BridgeHand(); // instantiate each BridgeHand 52 play[ii] = new Player(player[ii], hand[ii]); // instantiate each Player 53 } 54 55 myDeck.shuffle(); // shuffle the deck 56 System.out.println(myDeck); // check to make sure deck is shuffled 57 58 59 // give cards to each players 60 for (int ii = 0; ii < 13; ii++) 61 { 62 // give one card to player for each time until all player get 13 cards 63 for ( int round = 0; round < PLAYERS; round++) 64 { 65 hand[round].putCardInHand(myDeck.deal()); 66 67 } 68 } 69 70 // show the result of players' hands and their total points 71 for (int ii = 0; ii < PLAYERS; ii++) 72 { 73 System.out.println(play[ii]); 74 } 75 76 77 // check if all player has same points 78 if (hand[0].evaluateHand() == hand[1].evaluateHand() && 79 hand[0].evaluateHand() == hand[2].evaluateHand() && 80 hand[0].evaluateHand() == hand[3].evaluateHand()) 2007-03-19 20:59 BridgeDriver.java Page 2 81 System.out.println("all players have the same points."); 82 83 // check if three players have the highest card or not 84 else if (hand[0].evaluateHand() == hand[1].evaluateHand() && 85 hand[0].evaluateHand() == hand[2].evaluateHand() && 86 hand[0].evaluateHand() > hand[3].evaluateHand()) 87 System.out.println(player[0] + ", " + player[1] + " and " + player[2] + " has 88 the highest hand."); 89 else if (hand[0].evaluateHand() == hand[1].evaluateHand() && 90 hand[0].evaluateHand() == hand[3].evaluateHand() && 91 hand[0].evaluateHand() > hand[2].evaluateHand()) 92 System.out.println(player[0] + ", " + player[1] + " and " + player[3] + " has 93 the highest hand."); 94 else if (hand[0].evaluateHand() == hand[2].evaluateHand() && 95 hand[0].evaluateHand() == hand[3].evaluateHand() && 96 hand[0].evaluateHand() > hand[1].evaluateHand()) 97 System.out.println(player[0] + ", " + player[2] + " and " + player[3] + " has 98 the highest hand."); 99 else if (hand[1].evaluateHand() == hand[2].evaluateHand() && 100 hand[1].evaluateHand() == hand[3].evaluateHand() && 101 hand[1].evaluateHand() > hand[0].evaluateHand()) 102 System.out.println(player[1] + ", " + player[2] + " and " + player[3] + " has 103 the highest hand."); 104 105 // check if two players have highest points or not 106 else if (hand[0].evaluateHand() == hand[1].evaluateHand() && 107 hand[0].evaluateHand() > hand[2].evaluateHand() && 108 hand[0].evaluateHand() > hand[3].evaluateHand()) 109 System.out.println(player[0] + " and " + player[1] + " has the highest hand."); 110 else if (hand[0].evaluateHand() == hand[2].evaluateHand() && 111 hand[0].evaluateHand() > hand[1].evaluateHand() && 112 hand[0].evaluateHand() > hand[3].evaluateHand()) 113 System.out.println(player[0] + " and " + player[2] + " has the highest hand."); 114 else if (hand[0].evaluateHand() == hand[3].evaluateHand() && 115 hand[0].evaluateHand() > hand[1].evaluateHand() && 116 hand[0].evaluateHand() > hand[2].evaluateHand()) 117 System.out.println(player[0] + " and " + player[3] + " has the highest hand."); 118 else if (hand[1].evaluateHand() == hand[2].evaluateHand() && 119 hand[1].evaluateHand() > hand[0].evaluateHand() && 120 hand[1].evaluateHand() > hand[3].evaluateHand()) 121 System.out.println(player[1] + " and " + player[2] + " has the highest hand."); 122 else if (hand[1].evaluateHand() == hand[3].evaluateHand() && 123 hand[1].evaluateHand() > hand[2].evaluateHand() && 124 hand[1].evaluateHand() > hand[0].evaluateHand()) 125 System.out.println(player[1] + " and " + player[3] + " has the highest hand."); 126 else if (hand[2].evaluateHand() == hand[3].evaluateHand() && 127 hand[2].evaluateHand() > hand[1].evaluateHand() && 128 hand[2].evaluateHand() > hand[0].evaluateHand()) 129 System.out.println(player[2] + " and " + player[3] + " has the highest hand."); 130 131 // check if player 1 has the highest card or not 132 else if (hand[0].evaluateHand() > hand[1].evaluateHand() && 133 hand[0].evaluateHand() > hand[2].evaluateHand() && 134 hand[0].evaluateHand() > hand[3].evaluateHand()) 135 System.out.println(player[0] + " has the highest hand."); 136 // check if player 2 has the highest card or not 137 else if (hand[1].evaluateHand() > hand[0].evaluateHand() && 138 hand[1].evaluateHand() > hand[2].evaluateHand() && 139 hand[1].evaluateHand() > hand[3].evaluateHand()) 140 System.out.println(player[1] + " has the highest hand."); 141 // check if player 3 has the highest card or not 142 else if (hand[2].evaluateHand() > hand[1].evaluateHand() && 143 hand[2].evaluateHand() > hand[0].evaluateHand() && 144 hand[2].evaluateHand() > hand[3].evaluateHand()) 145 System.out.println(player[2] + " has the highest hand."); 146 // check if player 4 has the highest card or not 147 else if (hand[3].evaluateHand() > hand[1].evaluateHand() && 148 hand[3].evaluateHand() > hand[2].evaluateHand() && 149 hand[3].evaluateHand() > hand[0].evaluateHand()) 150 System.out.println(player[3] + " has the highest hand."); 151 152 153 System.out.println(); 154 System.out.println("this program ended normally"); // end message 155 156 } // end main 157 } // end class 2007-03-19 20:59 BridgeHand.java Page 1 1 /** 2 * @author Yoshihiro Fujio 3 * CS239 section 2 4 * PA2 5 * V1 date 2007/03/11 6 **/ 7 /********************************************************************** 8 * BridgeHand.java 9 * 10 * Represents a cards hold by each player 11 ********************************************************************/ 12 13 public class BridgeHand 14 { 15 private Card[] hand; 16 private int i = 0; // array number of the current hand 17 18 /*************************************************************** 19 * Creates a cards of player that contains 13 cards. 20 ***************************************************************/ 21 public BridgeHand() 22 { 23 hand = new Card[13]; 24 } // end BridgeHand 25 26 /*************************************************************** 27 * evaluates hand 28 ***************************************************************/ 29 public int evaluateHand() 30 { 31 32 int totalPoints = 0; // total points of a hand 33 int club = 0; // number of cards with suit CLUB 34 int diamond = 0; // number of cards with suit DIAMIOND 35 int heart = 0; // number of cards with suit HEART 36 int spade = 0; // number of cards with suit SPADE 37 38 39 for (int ii = 0; ii < 13; ii++) 40 { 41 // check points of Jack, Queen, King, and Ace 42 totalPoints += hand[ii].getPoints(); 43 44 // add a count to each counter if there is one 45 if (hand[ii].suit == Suit.CLUB) 46 club++; 47 if (hand[ii].suit == Suit.DIAMOND) 48 diamond++; 49 if (hand[ii].suit ==Suit.HEART) 50 heart++; 51 if (hand[ii].suit == Suit.SPADE) 52 spade++; 53 54 } 55 56 // check points for void suit, singleton, and doubleton; 57 // for club 58 if (club == 0) 59 totalPoints += 3; 60 if (club == 1) 61 totalPoints += 2; 62 if (club == 2) 63 totalPoints += 1; 64 65 // for diamond 66 if (diamond == 0) 67 totalPoints += 3; 68 if (diamond == 1) 69 totalPoints += 2; 70 if (diamond == 2) 71 totalPoints += 1; 72 73 // for heart 74 if (heart == 0) 75 totalPoints += 3; 76 if (heart == 1) 77 totalPoints += 2; 78 if (heart == 2) 79 totalPoints += 1; 80 2007-03-19 20:59 BridgeHand.java Page 2 81 // for spade 82 if (spade == 0) 83 totalPoints += 3; 84 if (spade == 1) 85 totalPoints += 2; 86 if (spade == 2) 87 totalPoints += 1; 88 89 return totalPoints; 90 } // end evaluateHand 91 92 /*************************************************************** 93 * put a card of one player in a hand 94 ***************************************************************/ 95 public void putCardInHand(Card aCard) 96 { 97 hand[i] = aCard; 98 99 i++; 100 101 } // end putCardInHand 102 103 /********************************************************* 104 * The string of cards in hand 105 * @return The String of list of cards in hand 106 ********************************************************/ 107 public String toString () 108 { 109 String handCards = ""; 110 111 for (int ii = 0; ii < 13; ii++) 112 { 113 handCards += hand[ii] + "\n"; 114 } 115 116 return handCards; 117 } // end toString 118 } // end class 2007-03-19 20:59 Card.java Page 1 1 /** 2 * @author Yoshihiro Fujio 3 * CS239 section 2 4 * PA2 5 * V1 date 2007/03/11 6 **/ 7 /***************************************************************** 8 * Rank.java 9 * 10 * Represents the available value of cards and their points 11 ***************************************************************/ 12 public class Card 13 { 14 Rank rank; // value of cards 15 Suit suit; // symbol of cards 16 17 /********************************************************** 18 * Create a new cards with the given attributes. 19 * @param rank The values of cards 20 * @param suit The symbol of cards 21 **********************************************************/ 22 public Card (Rank rank, Suit suit) 23 { 24 this.rank = rank; 25 this.suit = suit; 26 } // end Cards 27 28 /****************************************************** 29 * Returns the points of the card 30 * @return point of cards 31 ******************************************************/ 32 public int getPoints() 33 { 34 return rank.getPoints(); 35 } // end getPoints 36 37 /********************************************************* 38 * The string of rank and suit 39 * @return The String of rank followed by suit 40 ********************************************************/ 41 public String toString () 42 { 43 String rankAndSuit; 44 45 rankAndSuit = String.format("%s of %s", rank.toString(), suit.toString()); 46 47 return rankAndSuit; 48 } // end toString 49 } // end class 2007-03-19 20:59 Deck.java Page 1 1 /** 2 * @author Yoshihiro Fujio 3 * CS239 section 2 4 * PA2 5 * V1 date 2007/03/11 6 **/ 7 /********************************************************************** 8 * Deck.java 9 * 10 * Represents a Deck of cards. 11 ********************************************************************/ 12 13 public class Deck 14 { 15 private Card[] cards; 16 private int numCards = 52; // numbers of cards left in deck 17 18 /*************************************************************** 19 * Creates a Deck of cards that contains 52 cards. 20 ***************************************************************/ 21 public Deck() 22 { 23 Card allCard; 24 int ii = 0; 25 26 cards = new Card[52]; 27 28 for (Suit suit: Suit.values()) 29 { 30 for (Rank rank: Rank.values()) 31 { 32 allCard = new Card(rank, suit); 33 cards[ii] = allCard; 34 ii++; 35 } 36 } 37 } // end Deck 38 39 /*************************************************************** 40 * shuffles a deck 41 ***************************************************************/ 42 public void shuffle() 43 { 44 int firstArray; 45 int secondArray; 46 Card Array; 47 48 for ( int ii = 0; ii < 50; ii++) // shuffle 50 times 49 { 50 firstArray = (int)(Math.random() * 52); // decide first array 51 secondArray = (int)(Math.random() * 52); // decide second array 52 53 Array = cards[firstArray]; // store value of first array to Array 54 cards[firstArray] = cards[secondArray]; // store value of second array to first 55 array 56 cards[secondArray] = Array; // store value of array to second array 57 } 58 } // end shuffle 59 60 /*************************************************************** 61 * deal cards to players 62 ***************************************************************/ 63 public Card deal() 64 { 65 66 Card top; // the card on the top of deck 67 68 top = cards[numCards - 1]; // set top equals to the array of numCards 69 numCards--; // minus the numCards, so it does not draw same one 70 71 return top; 72 } // end deal 73 74 /********************************************************* 75 * The string of cards 76 * @return The String of list of cards 77 ********************************************************/ 78 public String toString () 79 { 80 String doCards = ""; 2007-03-19 20:59 Deck.java Page 2 81 82 for (int ii = 0; ii < 52; ii++) 83 { 84 doCards += cards[ii] + "\n"; 85 } 86 87 return doCards; 88 } // end toString 89 } // end class 90 2007-03-19 20:59 Player.java Page 1 1 /** 2 * @author Yoshihiro Fujio 3 * CS239 section 2 4 * PA2 5 * V1 date 2007/03/11 6 **/ 7 /********************************************************************** 8 * Player.java 9 * 10 * Represents a cards hold by each player 11 ********************************************************************/ 12 13 public class Player 14 { 15 private String name; 16 private BridgeHand hand; 17 18 /********************************************************** 19 * Create a new player with its name and hand 20 * @param name The name of player 21 * @param hand The hand of player 22 **********************************************************/ 23 public Player (String name, BridgeHand hand) 24 { 25 this.name = name; 26 this.hand = hand; 27 } // end Cards 28 29 /****************************************************** 30 * Returns the name of Player 31 * @return name of player 32 ******************************************************/ 33 public String getName() 34 { 35 return name; 36 } // end getName 37 38 /****************************************************** 39 * Returns the cards in hand 40 * @return cards in hand 41 ******************************************************/ 42 public BridgeHand getHand() 43 { 44 return hand; 45 } // end getHand 46 47 /********************************************************* 48 * The string of name and the total 49 * @return The name of player and the total points 50 ********************************************************/ 51 public String toString () 52 { 53 String result; 54 55 result = getName() + ": \n" + getHand() + "\n" + hand.evaluateHand() + " Points\n"; 56 57 return result; 58 } // end toString 59 } // end class 2007-03-19 20:59 Rank.java Page 1 1 /** 2 * @author Yoshihiro Fujio 3 * CS239 section 2 4 * PA2 5 * V1 date 2007/03/11 6 **/ 7 /***************************************************************** 8 * Rank.java 9 * 10 * Represents the available value of cards and their points 11 ***************************************************************/ 12 public enum Rank 13 { 14 TWO (0), 15 THREE (0), 16 FOUR (0), 17 FIVE (0), 18 SIX (0), 19 SEVEN (0), 20 EIGHT (0), 21 NINE (0), 22 TEN (0), 23 JACK (1), 24 QUEEN (2), 25 KING (3), 26 ACE (4); 27 28 private int points; // points of card 29 30 /********************************************************** 31 * Create a new Rank values with the given attributes. 32 * 33 * @param points The points of card 34 **********************************************************/ 35 private Rank (int points) 36 { 37 this.points = points; 38 } // end Rank 39 40 /****************************************************** 41 * Returns the points of card 42 * @return points of card 43 ******************************************************/ 44 public int getPoints() 45 { 46 return points; 47 } // end getPoints 48 49 } // end enum 50 2007-03-19 20:59 Suit.java Page 1 1 /** 2 * @author Yoshihiro Fujio 3 * CS239 section 2 4 * PA2 5 * V1 date 2007/03/11 6 **/ 7 /***************************************************************** 8 * Suit.java 9 * 10 * Represents the symbols of cards 11 ***************************************************************/ 12 public enum Suit 13 { 14 CLUB, 15 DIAMOND, 16 HEART, 17 SPADE 18 } // end enum