SUBMIT REPORT User: spaltts Name: Spalt, Trevor Course: cs239 Assignment: pa2 Version: 1 Date: Mon Mar 19 03:48:29 PM 2007 Files: BridgeHand.java Card.java Dealer.java Deck.java Rank.jav a Suit.java player.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 Dealer.java Deck.java player.java Rank.java Su it.java 2007-03-19 15:48 BridgeHand.java Page 1 1 import java.util.Random; 2 3 /******************************************************************* 4 Name: Trevor Spalt 5 Date: March 12, 2007 6 Section: 2 7 Lab: PA2 8 /******************************************************************* 9 * This class is to make a bridge hand consisting of 13 card object. 10 * The hand is filled by the dealing of the dealer (driver) which is 11 * carried out by the putCardInHand method. This method also evaluates 12 * the points of the hand and returns the string value of the entire 13 * 13 card hand 14 */ 15 public class BridgeHand 16 { 17 private Card hand[] = new Card[13]; 18 19 // use this counter to increment the card index in which the card 20 // is being placed 21 private int counter; 22 23 // Created a variable to hold the points 24 private int pointHolder; 25 26 /******************************************************************* 27 * The constructor only initializes the counter, which incriments the 28 * put card in hand method, and initializes the value for pointHolder. 29 *******************************************************************/ 30 public BridgeHand() 31 { 32 counter = 0; 33 pointHolder = 0; 34 } 35 36 /******************************************************************* 37 * evaluateHand() finds the total point value of the hand. First it 38 * calls upon the rank class to get the point value of a given rank. 39 * Then it takes a look at the number of cards with the same suit and 40 * gives out points based on that value. 41 * 42 * @return pointHolder - This is the total value of the hand 43 *******************************************************************/ 44 public int evaluateHand() 45 { 46 // Create a variable to hold the number of each of the suits 47 int spade, heart, diamond, club; 48 spade = 0; 49 heart = 0; 50 diamond = 0; 51 club = 0; 52 53 // First calculate the points earned by the face value of the cards 54 for(int look = 0; look < 13; look++) 55 { 56 pointHolder += hand[look].getPoints(); 57 } 58 59 // This for loop adds one every time one of the suits is present 60 // which will allow me to calculate the total number of suits in 61 // the hand. 62 for(int look = 0; look < 13; look++) 63 { 64 if(hand[look].getSuit() == Suit.SPADE) 65 { 66 spade++; 67 } 68 else if(hand[look].getSuit() == Suit.HEART) 69 { 70 heart++; 71 } 72 else if(hand[look].getSuit() == Suit.CLUB) 73 { 74 club++; 75 } 76 else 77 { 78 diamond++; 79 } 80 }// end for 2007-03-19 15:48 BridgeHand.java Page 2 81 82 // Add points if any suits have a total of less then 1 2 or 3 83 if(spade == 0 || diamond == 0 || club == 0 || heart == 0) 84 pointHolder += 3; 85 else if(spade == 1 || diamond == 1 || club == 1 || heart == 1) 86 pointHolder += 2; 87 else if(spade == 2 || diamond == 2 || club == 2 || heart == 2) 88 pointHolder += 1; 89 90 return pointHolder; 91 } 92 93 /******************************************************************* 94 * This method takes a card from the dealer and adds it to the 13 95 * card hand. 96 * 97 * @param aCard - this is a card from the dealer 98 *******************************************************************/ 99 public void putCardInHand(Card aCard) 100 { 101 hand[counter] = aCard; 102 103 // Forgot to increment the array at first. 104 counter++; 105 } 106 107 /******************************************************************* 108 * toString() prints out all the cards in the hand line by line. This 109 * will make it easier for the user to see each card that they have. 110 * 111 * @return - returns all the values of the hand 112 *******************************************************************/ 113 public String toString() 114 { 115 return hand[0] + "\n" + hand[1] + "\n" + hand[2] + "\n" + hand[3] + 116 "\n" + hand[4] + "\n" + hand[5] + "\n" + hand[6] + "\n" + hand[7] + 117 "\n" + hand[8] + "\n" + hand[9] + "\n" + hand[10] + "\n" + hand[11] + 118 "\n" + hand[12]; 119 }//end toString 120 }// end Deck Class 2007-03-19 15:48 Card.java Page 1 1 /******************************************************************* 2 Name: Trevor Spalt 3 Date: March 12, 2007 4 Section: 2 5 Lab: PA2 6 /******************************************************************* 7 * This enum class simply makes a enum type for each of the four suits. 8 * This allows the deck method to use these four different types 9 * efficiently 10 */ 11 public class Card 12 { 13 private Rank rank; 14 private Suit suit; 15 16 /* 17 * The cunstructor assigns a rank and suit to each card that is 18 * called. 19 * 20 * @param r - the rank of a card 21 * @param s - the suit of a card 22 */ 23 public Card(Rank r, Suit s) 24 { 25 rank = r; 26 suit = s; 27 } 28 29 /* 30 * This method simply returns the number of points assgned to a given 31 * rank. 32 * 33 * @return rank.getPoints() - returns the point value of a rank 34 */ 35 public int getPoints() 36 { 37 return rank.getPoints(); 38 } 39 40 /* 41 * This method returns the name of the suit. 42 * 43 * @return suit - returns the suit value of the card 44 */ 45 public Suit getSuit() 46 { 47 return suit; 48 } 49 50 /* 51 * The toString method just prints out the rank and suit of the 52 * card object 53 * 54 * @return - returns the rank and suit 55 */ 56 public String toString() 57 { 58 return rank + " of " + suit; 59 }// end toString 60 }// end Class 2007-03-19 15:48 Dealer.java Page 1 1 import java.util.Scanner; 2 3 /******************************************************************* 4 Name: Trevor Spalt 5 Date: March 12, 2007 6 Section: 2 7 Lab: PA2 8 /******************************************************************* 9 * This is the main driver. It will act as the dealer and fill use 10 * the other classes to fill the players hands (deal). It will also 11 * take in the players names, and finally give the output at the end 12 * of the program. 13 /******************************************************************* 14 * 15 * I recieved limited help on this assignment by a PA. 16 */ 17 18 public class Dealer 19 { 20 public static void main (String [] args) 21 { 22 Deck myDeck; 23 myDeck = new Deck(); 24 Card myCard; 25 26 int pointHolder1, pointHolder2, pointHolder3, pointHolder4; 27 28 String playerName1, playerName2, playerName3, playerName4; 29 30 Scanner inputScanner; 31 inputScanner = new Scanner(System.in); 32 33 // This variable will hold the string of all winning players 34 String winningPlayers; 35 36 // Create four Bridge hands for the four players 37 BridgeHand player1, player2, player3, player4; 38 player1 = new BridgeHand(); 39 player2 = new BridgeHand(); 40 player3 = new BridgeHand(); 41 player4 = new BridgeHand(); 42 43 // Introduce the program and give explanation to what it does 44 System.out.println 45 ("************ Welcome to the BridgeHand Program ************"); 46 System.out.println 47 ("This program will take in four different players names. It "); 48 System.out.println 49 ("will then create four unique bridge hands, each consisting "); 50 System.out.println 51 ("of 13 cards, and evaluate those hands to find which one is "); 52 System.out.println 53 ("the best bridge hand."); 54 55 // Take in the players names and assign them to seperate player 56 // objects. 57 System.out.print("\nEnter player Name: "); 58 playerName1 = inputScanner.next(); 59 System.out.print("\nEnter player Name: "); 60 playerName2 = inputScanner.next(); 61 System.out.print("\nEnter player Name: "); 62 playerName3 = inputScanner.next(); 63 System.out.print("\nEnter player Name: "); 64 playerName4 = inputScanner.next(); 65 66 // Shuffle the deck 67 myDeck.Shuffle(); 68 69 // Place the card in each of the players hands 70 // using a loop to do so 71 for(int i = 0; i < 13; i++) 72 { 73 // Get a card to put in the player1's hand 74 myCard = myDeck.Deal(); 75 player1.putCardInHand(myCard); 76 77 // Get a card to put in the player2's hand 78 myCard = myDeck.Deal(); 79 player2.putCardInHand(myCard); 80 2007-03-19 15:48 Dealer.java Page 2 81 // Get a card to put in the player3's hand 82 myCard = myDeck.Deal(); 83 player3.putCardInHand(myCard); 84 85 // Get a card to put in the player4's hand 86 myCard = myDeck.Deal(); 87 player4.putCardInHand(myCard); 88 } 89 90 // Create 4 player object. One for each player. 91 player play1, play2, play3, play4; 92 play1 = new player(playerName1, player1); 93 play2 = new player(playerName2, player2); 94 play3 = new player(playerName3, player3); 95 play4 = new player(playerName4, player4); 96 97 // Evaluate the point value of each of the four players hands 98 pointHolder1 = player1.evaluateHand(); 99 pointHolder2 = player2.evaluateHand(); 100 pointHolder3 = player3.evaluateHand(); 101 pointHolder4 = player4.evaluateHand(); 102 103 // Output the player's name, hand, and total points 104 System.out.println("\n" + play1 + "\n" + "Total Number of points: " + pointHolder1); 105 System.out.println("\n" + play2 + "\n" + "Total Number of points: " + pointHolder2); 106 System.out.println("\n" + play3 + "\n" + "Total Number of points: " + pointHolder3); 107 System.out.println("\n" + play4 + "\n" + "Total Number of points: " + pointHolder4); 108 109 // Use an if statement to find which hand(s) is the winner 110 if(pointHolder1 > pointHolder2) 111 { 112 if(pointHolder1 > pointHolder3) 113 { 114 if(pointHolder1 > pointHolder4) 115 { 116 winningPlayers = play1.getName(); 117 }// end if 118 else if(pointHolder1 == pointHolder4) 119 { 120 //player4 wins and 1 121 winningPlayers = play1.getName() + " " + play4.getName(); 122 }// end else if 123 else 124 { 125 winningPlayers = play4.getName(); 126 }// end else 127 }// end if 128 else if(pointHolder3 > pointHolder4) 129 { 130 winningPlayers = play3.getName(); 131 }// end else if 132 else if(pointHolder3 == pointHolder4) 133 { 134 winningPlayers = play3.getName() + " " + play4.getName(); 135 }// end else if 136 else 137 { 138 winningPlayers = play4.getName(); 139 }// end else 140 }// end if 141 else if(pointHolder2 > pointHolder3) 142 { 143 if(pointHolder2 > pointHolder4) 144 { 145 winningPlayers = play2.getName(); 146 }// end if 147 else if(pointHolder2 == pointHolder4) 148 { 149 winningPlayers = play2.getName() + " " + play4.getName(); 150 }// end else if 151 else 152 { 153 winningPlayers = play4.getName(); 154 }// end else 155 }// end else if 156 else if(pointHolder3 > pointHolder4) 157 { 158 winningPlayers = play3.getName(); 159 }// end else if 160 else if(pointHolder3 == pointHolder4) 2007-03-19 15:48 Dealer.java Page 3 161 { 162 winningPlayers = play3.getName() + " " + play4.getName(); 163 }// end else if 164 else 165 { 166 winningPlayers = play4.getName(); 167 }// end else 168 169 // Output the winner or winners and then display end message. 170 System.out.println("\nThe winner(s) is: " + winningPlayers); 171 System.out.print("\nThe program has ended normally"); 172 }// end main 173 }// end class 2007-03-19 15:48 Deck.java Page 1 1 import java.util.Random; 2 3 /******************************************************************* 4 Name: Trevor Spalt 5 Date: March 12, 2007 6 Section: 2 7 Lab: PA2 8 /******************************************************************* 9 * The deck class's purpose is to make a deck of 52 different cards. 10 * It is to shuffle those cards so that they are randomly put in the 11 * array and then it is to deal out the cards one by one to the dealer. 12 * Finally, the deck method is to print out the deck if asked for by the 13 * dealer. 14 */ 15 public class Deck 16 { 17 18 private Card deckArray[] = new Card[52]; 19 20 // This variable allows me to increment the card that is givin out 21 // by one each time the deal method is called 22 private int count; 23 24 /******************************************************************* 25 * The constructor fills the deckArray with the 52 different cards in 26 * order using a for each loop that will look at all suits and ranks 27 * 28 *******************************************************************/ 29 public Deck() 30 { 31 int counter; 32 counter = 0; 33 count = 0; 34 35 for (Suit suit : Suit.values()) 36 { 37 for (Rank rank : Rank.values()) 38 { 39 deckArray[counter] = new Card(rank, suit); 40 counter++; 41 } 42 } 43 }// end constructor 44 45 /******************************************************************* 46 * This method will shuffle the cards using random indexes in order to 47 * swap the cards within the indexes. 48 *******************************************************************/ 49 public void Shuffle() 50 { 51 // use a random num generator to get random indexes to 52 // to swap for the shuffle 53 Random generator = new Random(); 54 int cardIndex1, cardIndex2; 55 Card tempHold; 56 57 // This shuffles the cards a total of 55 times to make 58 // sure the cards are well shuffled 59 for(int counter = 0; counter < 55; counter++) 60 { 61 cardIndex1 = generator.nextInt(52); 62 cardIndex2 = generator.nextInt(52); 63 64 tempHold = deckArray[cardIndex1]; 65 deckArray[cardIndex1] = deckArray[cardIndex2]; 66 deckArray[cardIndex2] = tempHold; 67 }// end for 68 }// end Shuffle()*/ 69 70 /******************************************************************* 71 * The deal method will return a single card at a time to the main 72 * driver which will simulate the dealer. 73 * 74 * @return deckArray - return a continuous card 52 times for each card 75 * in the array 76 *******************************************************************/ 77 public Card Deal() 78 { 79 count++; 80 2007-03-19 15:48 Deck.java Page 2 81 return deckArray[count - 1]; 82 }// end Deal() 83 84 /******************************************************************* 85 * The toString method will return the entire deck. 86 *******************************************************************/ 87 public String toString() 88 { 89 // returns the requesed card index by the dealer or driver. 90 return ""; 91 }// end toString() 92 93 }// end Deck Class 2007-03-19 15:48 player.java Page 1 1 /******************************************************************* 2 Name: Trevor Spalt 3 Date: March 12, 2007 4 Section: 2 5 Lab: PA2 6 /******************************************************************* 7 * This class is going to 8 */ 9 public class player 10 { 11 private String name; 12 private BridgeHand hand; 13 14 /******************************************************************* 15 * The constructor fills initializes both the player's name and the 16 * hand of the player 17 * 18 * @param name - the player's name 19 * @param hand - the 13 card hand of the player 20 *******************************************************************/ 21 public player(String name, BridgeHand hand1) 22 { 23 this.name = name; 24 hand = hand1; 25 } 26 27 /******************************************************************* 28 * This method returns the name of the player 29 * 30 * @return name - the player's name 31 *******************************************************************/ 32 public String getName() 33 { 34 return name; 35 } 36 37 /******************************************************************* 38 * This method returns the hand of the player 39 * 40 * @return hand - the player's hand 41 *******************************************************************/ 42 public BridgeHand getHand() 43 { 44 return hand; 45 } 46 47 /******************************************************************* 48 * This method returns the name of the player and the hand that they 49 * have. 50 * 51 * @return the name and hand of the player. 52 *******************************************************************/ 53 public String toString() 54 { 55 return name + "'s Bridge Hand \n" + 56 "--------------------------------\n" 57 + hand + 58 "\n--------------------------------"; 59 } 60 }// end Suit 2007-03-19 15:48 Rank.java Page 1 1 /******************************************************************* 2 Name: Trevor Spalt 3 Date: March 12, 2007 4 Section: 2 5 Lab: PA2 6 /******************************************************************* 7 * This enum class simply makes a enum type for each of the four suits. 8 * This allows the deck method to use these four different types 9 * efficiently 10 */ 11 public enum Rank 12 { 13 TWO(0), 14 THREE(0), 15 FOUR(0), 16 FIVE(0), 17 SIX(0), 18 SEVEN(0), 19 EIGHT(0), 20 NINE(0), 21 TEN(0), 22 JACK(1), 23 QUEEN(2), 24 KING(3), 25 ACE(4); 26 27 private final int points; 28 29 /******************************************************************* 30 * The constructor puts the point value of the rank into the variable 31 * assigned for it 32 * 33 * @param points - the point value of the rank 34 *******************************************************************/ 35 Rank(int points) 36 { 37 this.points = points; 38 } // end Rank 39 40 /******************************************************************* 41 * The method returns the point value of a rank. 42 * 43 * @return points - the point value of the rank 44 *******************************************************************/ 45 public int getPoints() 46 { 47 return points; 48 } // end getPoints 49 }// end Class 2007-03-19 15:48 Suit.java Page 1 1 /******************************************************************* 2 Name: Trevor Spalt 3 Date: March 12, 2007 4 Section: 2 5 Lab: PA2 6 /******************************************************************* 7 * This enum class simply makes a enum type for each of the four suits. 8 * This allows the deck method to use these four different types 9 * efficiently 10 */ 11 public enum Suit 12 { 13 SPADE, DIAMOND, HEART, CLUB 14 }// end Suit