SUBMIT REPORT User: wisemabd Name: Bryan D Wiseman Course: cs239 Assignment: pa2 Version: 1 Date: Mon Mar 19 03:49:53 PM 2007 Files: BridgeHand.java Card.java Deck.java Driver.java Player.j ava Rank.java Suit.java Late Penalty: 10 (submitted after 03/19/2007,11:35) PDF-spec: 90/99 Honor Pledge: I have NOT received unauthorized assistance Compiling ... javac BridgeHand.java Card.java Deck.java Driver.java Player.java Rank.java Su it.java 2007-03-19 15:49 BridgeHand.java Page 1 1 /**************************** 2 *This class creates the hands 3 *for the different players. 4 *@author - Bryan Wiseman 5 *@version - 1 6 *****************************/ 7 //Date: 3/18/07 8 //Section: 1 9 //PA: 2 10 11 public class BridgeHand 12 { 13 14 Card[] hand; //the hand of the player 15 int points; //points value of player's hand 16 17 //constructor of hand 18 public BridgeHand() 19 { 20 hand = new Card [13]; 21 points = 0; 22 } 23 /********************* 24 *Method calculates the 25 *total point value of 26 *the player's hand 27 *@return - points of hand 28 *********************/ 29 public int evaluateHand() 30 { 31 32 int heart; //number of hearts in hand 33 int spade; //number of spades in hand 34 int club; //number of clubs in hand 35 int diamond; //number of diamonds in hand 36 37 heart = 0; 38 spade = 0; 39 club = 0; 40 diamond = 0; 41 42 //loop gets points of individual cards 43 for(int num = 0; num < 13; num++) 44 { 45 points = points + hand[num].getPoints(); 46 } 47 //gets number of each suit in hand 48 for(int num = 0; num < 13; num++) 49 { 50 if(hand[num].suit == Suit.HEARTS) 51 { 52 heart++; 53 } 54 else if(hand[num].suit == Suit.SPADES) 55 { 56 spade++; 57 } 58 else if(hand[num].suit == Suit.DIAMONDS) 59 { 60 diamond++; 61 } 62 else if(hand[num].suit == Suit.CLUBS) 63 { 64 club++; 65 } 66 } 67 //points for void 68 if(spade == 0) 69 { 70 points = points + 3; 71 } 72 if(heart == 0) 73 { 74 points = points + 3; 75 } 76 if(diamond == 0) 77 { 78 points = points + 3; 79 } 80 if(club == 0) 2007-03-19 15:49 BridgeHand.java Page 2 81 { 82 points = points + 3; 83 } 84 //points for Singleton 85 if(spade == 1) 86 { 87 points = points + 2; 88 } 89 if(heart == 1) 90 { 91 points = points + 2; 92 } 93 if(diamond == 1) 94 { 95 points = points + 2; 96 } 97 if(club == 1) 98 { 99 points = points + 2; 100 } 101 //points for Doubleton 102 if(spade == 2) 103 { 104 points = points + 1; 105 } 106 if(heart == 2) 107 { 108 points = points + 1; 109 } 110 if(diamond == 2) 111 { 112 points = points + 1; 113 } 114 if(club == 2) 115 { 116 points = points + 1; 117 } 118 119 return points; 120 } 121 /******************* 122 *Method puts cards into 123 *the players hand 124 *@param - Card 125 *@param - index for array 126 ********************/ 127 public void putCardInHand(Card card, int index) 128 { 129 hand[index] = card; 130 } 131 /****************** 132 *Method puts hand together 133 *and points for each hand in 134 *String value 135 *@return - String of hand and points 136 ********************/ 137 public String toString() 138 { 139 String output; 140 141 output = ""; 142 143 for(int num = 0; num < hand.length; num++) 144 { 145 output = output + hand[num].toString() + "\n"; 146 } 147 148 output = output + "\nPoints = " + points; 149 150 return output; 151 } 152 153 } 154 2007-03-19 15:49 Card.java Page 1 1 /****************************** 2 *This method reads the cards for 3 *the player to be able to use 4 *@author - Bryan Wiseman 5 *@version - 1 6 ********************************/ 7 //Date: 3/13/07 8 //Section:1 9 //PA: 2 10 11 public class Card 12 { 13 14 15 Rank rank; //rank value of card 16 Suit suit; //suit value of card 17 18 19 /******************* 20 *This method reads the 21 *cards for the deck. 22 *@param - playing card 23 **********************/ 24 public Card(Rank rank, Suit suit) 25 { 26 this.rank = rank; 27 this.suit = suit; 28 } 29 /************** 30 *Method returns 31 *the point value 32 *of card 33 *@return - points 34 ****************/ 35 public int getPoints() 36 { 37 int output; //the points value of card 38 39 output = rank.getPoints(); 40 41 return output; 42 } 43 /*************** 44 *Method outputs String 45 *of the rank and suit of 46 *card in a String form 47 *@return - card rank in suit in String 48 ****************/ 49 public String toString() 50 { 51 String output; //the value of card in string 52 53 output = rank + " of " + suit; 54 55 return output; 56 } 57 } 2007-03-19 15:49 Deck.java Page 1 1 2 /**************************** 3 *This program makes the deck, 4 *shuffles the cards, and deals 5 *them to the four players 6 *@author - Bryan Wiseman 7 *@version - 1 8 *****************************/ 9 //Date: 3/13/07 10 //Section: 1 11 //PA:2 12 13 public class Deck 14 { 15 Card[] deck; 16 int num; 17 18 //constructor of the deck 19 public Deck() 20 { 21 deck = new Card [52]; 22 num = -1; 23 24 int index; 25 26 index = 0; 27 28 29 for(Suit x : Suit.values()) 30 for(Rank y :Rank.values()) 31 { 32 deck[index] = new Card( y, x); 33 index++; 34 } 35 36 } 37 38 39 /******************* 40 *Method shuffles the 41 *Deck into a random order 42 ********************/ 43 public void shuffle() 44 { 45 int ranHolder1; //first random position in deck 46 int ranHolder2; //second random position in deck 47 Card holder; //holds card value for the switch 48 49 ranHolder1 = 0; 50 ranHolder2 = 0; 51 //loop shuffles the deck 52 for(int num = 0; num <= 75; num++) 53 { 54 ranHolder1 = (int) (Math.random() * 51 + 1); 55 ranHolder2 = (int) (Math.random() * 51 + 1); 56 57 if(ranHolder1 == ranHolder2) 58 { 59 ranHolder2 = (int) (Math.random() * 51 + 1); 60 } 61 62 holder = deck[ranHolder1]; 63 deck[ranHolder1] = deck[ranHolder2]; 64 deck[ranHolder2] = holder; 65 } 66 } 67 /****************** 68 *Method deals out the 69 *cards in the deck to 70 *the hand 71 *@return - Card 72 ********************/ 73 public Card deal() 74 { 75 76 num += 1; 77 78 79 return deck[num]; 80 2007-03-19 15:49 Deck.java Page 2 81 82 } 83 /******************** 84 *Method puts the deck 85 *into string format 86 *@return - deck in String format 87 ***********************/ 88 public String toString() 89 { 90 String output; //output string 91 92 output = ""; 93 94 for(int num = 0; num < deck.length; num++) 95 { 96 output = output + deck[num].toString() + "\n"; 97 } 98 99 return output; 100 } 101 102 103 104 105 } 2007-03-19 15:49 Driver.java Page 1 1 import java.util.Scanner; 2 /***************** 3 *this method is the 4 *driver for the bridge 5 *game 6 *@author - Bryan Wiseman 7 *@version - 1 8 ********************/ 9 //Date: 3/13/07 10 //Section: 1 11 //PA: 2 12 13 public class Driver 14 { 15 /****************** 16 *This method welcomes 17 *user and runs the 18 *bridge game 19 ********************/ 20 public static void main(String[] args) 21 { 22 String player1; //the player 1 name 23 String player2; //the player 2 name 24 String player3; //the player 3 name 25 String player4; //the player 4 name 26 String finalScore; //the winner of the game 27 Deck deck; //the new deck 28 BridgeHand hands1; //player 1 hand 29 BridgeHand hands2; //player 2 hand 30 BridgeHand hands3; //player 3 hand 31 BridgeHand hands4; //player 4 hand 32 Player pl1; //player 1 stats 33 Player pl2; //player 2 stats 34 Player pl3; //player 3 stats 35 Player pl4; //player 4 stats 36 int score1; //player 1 score 37 int score2; //player 2 score 38 int score3; //player 3 score 39 int score4; //player 4 score 40 Scanner scan; 41 42 scan = new Scanner(System.in); 43 finalScore = ""; 44 //welcome 45 System.out.println("Welcome to the Bridge Game"); 46 //program explanation 47 System.out.print("This program plays a game of Bridge with four people.\n"); 48 //gets player 1's name 49 System.out.print("Please enter name of player 1: "); 50 player1 = scan.nextLine(); 51 System.out.println(); 52 //gets player 2's name 53 System.out.print("Please enter name of player 2: "); 54 player2 = scan.nextLine(); 55 System.out.println(); 56 //gets player 3's name 57 System.out.print("Please enter name of player 3: "); 58 player3 = scan.nextLine(); 59 System.out.println(); 60 //gets player 4's name 61 System.out.print("Please enter name of player 4: "); 62 player4 = scan.nextLine(); 63 System.out.println(); 64 //echos players name 65 System.out.println("The players are: " + player1 + ", " 66 + player2 + ", " + player3 + ", " + 67 player4 + ".\n"); 68 //gets new deck 69 deck = new Deck(); 70 //shuffles new deck 71 deck.shuffle(); 72 //activates players hands 73 hands1 = new BridgeHand(); 74 hands2 = new BridgeHand(); 75 hands3 = new BridgeHand(); 76 hands4 = new BridgeHand(); 77 //deals the cards into their hands 78 for(int num = 0; num < 13; num++) 79 { 80 hands1.putCardInHand(deck.deal(), num); 2007-03-19 15:49 Driver.java Page 2 81 hands2.putCardInHand(deck.deal(), num); 82 hands3.putCardInHand(deck.deal(), num); 83 hands4.putCardInHand(deck.deal(), num); 84 } 85 //gets the stats on each player 86 pl1 = new Player( player1, hands1); 87 pl2 = new Player( player2, hands2); 88 pl3 = new Player( player3, hands3); 89 pl4 = new Player( player4, hands4); 90 //gets the scores for each player 91 score1 = hands1.evaluateHand(); 92 score2 = hands2.evaluateHand(); 93 score3 = hands3.evaluateHand(); 94 score4 = hands4.evaluateHand(); 95 //sees who won the game 96 if(score1 > score2) 97 { 98 if(score1 > score3) 99 { 100 if(score1 > score4) 101 { 102 finalScore = "Player 1 wins"; 103 } 104 else if(score1 == score4) 105 { 106 finalScore = "Player 1 and Player 4 tie"; 107 } 108 else if(score1 < score4) 109 { 110 finalScore = "Player 4 wins"; 111 } 112 } 113 else if(score1 == score3) 114 { 115 if(score1 > score4) 116 { 117 finalScore = "Player 1 and Player 3 tie"; 118 } 119 else if(score1 == score4) 120 { 121 finalScore = "Player 1 and Player 3 and Player 4 tie"; 122 } 123 else if(score1 < score4) 124 { 125 finalScore = "Player 4 wins"; 126 } 127 } 128 else if(score1 < score3) 129 { 130 if(score3 > score4) 131 { 132 finalScore = "Player 3 win"; 133 } 134 else if(score3 == score4) 135 { 136 finalScore = "Player 3 and Player 4 tie"; 137 } 138 else if(score3 < score4) 139 { 140 finalScore = "Player 4 wins"; 141 } 142 } 143 } 144 else if(score1 == score2) 145 { 146 if(score1 > score3) 147 { 148 if(score1 > score4) 149 { 150 finalScore = "Player 1 and Player 2 tie"; 151 } 152 else if(score1 == score4) 153 { 154 finalScore = "Player 1 and Player 2 and Player 4 tie"; 155 } 156 else if(score1 < score4) 157 { 158 finalScore = "Player 4 wins"; 159 } 160 } 2007-03-19 15:49 Driver.java Page 3 161 else if(score1 == score3) 162 { 163 if(score1 > score4) 164 { 165 finalScore = "Player 1 and Player 2 and Player 3 tie"; 166 } 167 else if(score1 == score4) 168 { 169 finalScore = "Player 1 and Player 2 and Player 3 and Player 4 tie"; 170 } 171 else if(score1 < score4) 172 { 173 finalScore = "Player 4 wins"; 174 } 175 } 176 else if(score1 < score3) 177 { 178 if(score3 > score4) 179 { 180 finalScore = "Player 3 win"; 181 } 182 else if(score3 == score4) 183 { 184 finalScore = "Player 3 and Player 4 tie"; 185 } 186 else if(score3 < score4) 187 { 188 finalScore = "Player 4 wins"; 189 } 190 } 191 } 192 else if(score1 < score2) 193 { 194 if(score2 > score3) 195 { 196 if(score2 > score4) 197 { 198 finalScore = "Player 2 wins"; 199 } 200 else if(score2 == score4) 201 { 202 finalScore = "Player 2 and Player 4 tie"; 203 } 204 else if(score2 < score4) 205 { 206 finalScore = "Player 4 wins"; 207 } 208 } 209 else if(score2 == score3) 210 { 211 if(score2 > score4) 212 { 213 finalScore = "Player 2 and Player 3 tie"; 214 } 215 else if(score2 == score4) 216 { 217 finalScore = "Player 2 and Player 3 and Player 4 tie"; 218 } 219 else if(score1 < score4) 220 { 221 finalScore = "Player 4 wins"; 222 } 223 } 224 else if(score2 < score3) 225 { 226 if(score3 > score4) 227 { 228 finalScore = "Player 3 win"; 229 } 230 else if(score3 == score4) 231 { 232 finalScore = "Player 3 and Player 4 tie"; 233 } 234 else if(score3 < score4) 235 { 236 finalScore = "Player 4 wins"; 237 } 238 } 239 } 240 //prints out players stats and the winner of the game 2007-03-19 15:49 Driver.java Page 4 241 System.out.println(pl1.toString()); 242 System.out.println(); 243 System.out.println(pl2.toString()); 244 System.out.println(); 245 System.out.println(pl3.toString()); 246 System.out.println(); 247 System.out.println(pl4.toString()); 248 System.out.println(); 249 System.out.println(finalScore); 250 System.out.println("Program ended normally"); 251 } 252 } 2007-03-19 15:49 Player.java Page 1 1 /*********************** 2 *This class takes the name 3 *of the player and their 4 *hand and make it available 5 *to driver 6 *@author - Bryan Wiseman 7 *@version - 1 8 ************************/ 9 //Date: 3/18/07 10 //Section: 1 11 //PA: 2 12 13 public class Player 14 { 15 String player; 16 BridgeHand hand; 17 //Player constructor 18 public Player( String player1, BridgeHand hand1) 19 { 20 player = player1; 21 hand = hand1; 22 } 23 /********** 24 *Method returns 25 *the name of player 26 *@return - player's name 27 **********/ 28 public String getName() 29 { 30 return player; 31 } 32 /************ 33 *Method returns 34 *the hand of player 35 *@return - hand of cards 36 *************/ 37 public BridgeHand getHand() 38 { 39 return hand; 40 } 41 /***************** 42 *This method adds the 43 *players name to the BridgeHand 44 *string and outputs it 45 *@return - The output string with name 46 *******************/ 47 public String toString() 48 { 49 String output; //the output of BridgeHand and the Name 50 51 output = "Name: " + player + "\n\n"; 52 output = output + hand.toString(); 53 54 return output; 55 } 56 } 2007-03-19 15:49 Rank.java Page 1 1 /*the enumerated values of the cards of a deck and their 2 individual point value in bridge*/ 3 public enum Rank 4 { 5 TWO (0) , 6 THREE (0) , 7 FOUR (0) , 8 FIVE (0) , 9 SIX (0) , 10 SEVEN (0) , 11 EIGHT (0) , 12 NINE (0) , 13 TEN (0) , 14 JACK (1) , 15 QUEEN (2) , 16 KING (3) , 17 ACE (4) ; 18 19 private final int points; //the points value 20 //rank constructor 21 Rank(int points) 22 { 23 this.points = points; 24 } 25 26 /*********** 27 *This returns 28 *point value of 29 *rank. 30 *@return - points 31 **************/ 32 public int getPoints() 33 { 34 return points; 35 } 36 } 2007-03-19 15:49 Suit.java Page 1 1 //the enumerated values for the suits in a deck 2 public enum Suit 3 { 4 CLUBS, 5 DIAMONDS, 6 HEARTS, 7 SPADES, 8 }