SUBMIT REPORT User: hermanjl Name: Herman, Jonathan Course: cs239 Assignment: pa2 Version: 2 Date: Mon Mar 19 03:16:41 PM 2007 Files: BridgeHand.java BridgeDriver.java Card.java Deck.java Pl ayer.java 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 BridgeDriver.java BridgeHand.java Card.java Deck.java Player.java Rank.j ava Suit.java 2007-03-19 15:16 BridgeDriver.java Page 1 1 import java.io.*; 2 import java.util.*; 3 4 /********************************************************************** 5 * This class plays a game of Bridge by asking for four players, giving 6 * each 13 cards from a shuffled deck, and determining the winner. 7 * 8 * @author Jonathan Herman 9 * @version 1 10 **********************************************************************/ 11 // Elizabeth Adams 12 // Date: 3/06/07 13 // PA 2 14 // Section: 2 15 16 public class BridgeDriver 17 { 18 /** 19 * This method plays a game of Bridge by asking for four players, giving 20 * each 13 cards from a shuffled deck, and determining the winner. 21 * 22 * @param args command line arguments-unused 23 */ 24 public static void main (String args[]) 25 { 26 String name1, name2, name3, name4, winner; 27 Player player1, player2, player3, player4; 28 BridgeHand hand1, hand2, hand3, hand4, winningHand; 29 30 Deck myDeck; 31 Scanner keyboard; 32 33 myDeck = new Deck(); 34 keyboard = new Scanner (System.in); 35 36 // welcomes the user and explains what the program will do 37 System.out.println ("Welcome to the Bridge program. This program " + 38 "will first ask for four players. Then, it will \n" + 39 "shuffle a deck and deal out a hand of 13 cards " + 40 "to each player. After this is done, the program \n" + 41 "will determine which player(s) won the game and " + 42 "print out the results.\n"); 43 44 // gets the first player's name 45 System.out.print ("What is the first player's name: "); 46 name1 = keyboard.nextLine(); 47 System.out.println ("The first player's name is " + name1 + "."); 48 49 // gets the second player's name 50 System.out.print ("What is the second player's name: "); 51 name2 = keyboard.nextLine(); 52 System.out.println ("The second player's name is " + name2 + "."); 53 54 // gets the third player's name 55 System.out.print ("What is the third player's name: "); 56 name3 = keyboard.nextLine(); 57 System.out.println ("The third player's name is " + name3 + "."); 58 59 // gets the fourth player's name 60 System.out.print ("What is the fourth player's name: "); 61 name4 = keyboard.nextLine(); 62 System.out.println ("The fourth player's name is " + name4 + ".\n"); 63 64 // creates the hands 65 hand1 = new BridgeHand (name1); 66 hand2 = new BridgeHand (name2); 67 hand3 = new BridgeHand (name3); 68 hand4 = new BridgeHand (name4); 69 70 // shuffles the deck 71 myDeck.shuffle(); 72 73 // deals out 13 cards to each player's hand 74 for (int x = 0; x < 13; x++) 75 { 76 hand1.putCardInHand (myDeck.deal()); 77 hand2.putCardInHand (myDeck.deal()); 78 hand3.putCardInHand (myDeck.deal()); 79 hand4.putCardInHand (myDeck.deal()); 80 } 2007-03-19 15:16 BridgeDriver.java Page 2 81 82 // gives hands to players 83 player1 = new Player (name1, hand1); 84 player2 = new Player (name2, hand2); 85 player3 = new Player (name3, hand3); 86 player4 = new Player (name4, hand4); 87 88 // prints out each player's hand and score. 89 System.out.println (player1); 90 System.out.println (player2); 91 System.out.println (player3); 92 System.out.println (player4); 93 94 // finds the high score 95 winningHand = hand1; 96 if (hand2.evaluateHand() > winningHand.evaluateHand()) 97 winningHand = hand2; 98 if (hand3.evaluateHand() > winningHand.evaluateHand()) 99 winningHand = hand3; 100 if (hand4.evaluateHand() > winningHand.evaluateHand()) 101 winningHand = hand4; 102 103 // determines the winner 104 winner = winningHand.getName(); 105 106 // checks to see if there was a tie 107 if (winningHand.equalScore (hand1)) 108 winner += " and " + hand1.getName(); 109 if (winningHand.equalScore (hand2)) 110 winner += " and " + hand2.getName(); 111 if (winningHand.equalScore (hand3)) 112 winner += " and " + hand3.getName(); 113 if (winningHand.equalScore (hand4)) 114 winner += " and " + hand4.getName(); 115 116 // prints out the results 117 System.out.println (winner + " won the game.\n"); 118 System.out.println ("This program dealt 4 hands for each player given " + 119 "by the user and found the winner(s).\nThis program " + 120 "has ended normally."); 121 } // end main method 122 123 } // end BridgeDriver class 2007-03-19 15:16 BridgeHand.java Page 1 1 import java.io.*; 2 import java.util.*; 3 4 /********************************************************************** 5 * This class represents a hand in the game of bridge. 6 * 7 * @author Jonathan Herman 8 * @version 1 9 **********************************************************************/ 10 // Elizabeth Adams 11 // Date: 3/06/07 12 // PA 2 13 // Section: 2 14 15 public class BridgeHand 16 { 17 private int nextCard; 18 private String playerName; 19 private Card[] hand; 20 21 /** 22 * Default constructor. 23 */ 24 public BridgeHand() 25 { 26 nextCard = 0; 27 hand = new Card [13]; 28 this.playerName = "no name"; 29 } 30 31 /** 32 * This constructor creates a new BridgeHand with a given playerName and 33 * gives it an array to hold 13 cards. 34 * 35 * @param playerName The name of the player the BridgeHand belongs to. 36 */ 37 public BridgeHand (String playerName) 38 { 39 nextCard = 0; 40 hand = new Card [13]; 41 this.playerName = playerName; 42 } 43 44 /** 45 * This method returns the numeric value corresponding to the BridgeHand. 46 * 47 * @return The value of the hand. 48 */ 49 public int evaluateHand() 50 { 51 int score; 52 score = 0; 53 54 // adds the point value of each card in hand to score 55 for (int x = 0; x < hand.length; x++) 56 score += hand[x].getPoints(); 57 58 return score; 59 } 60 61 /** 62 * This method puts a given card into the next available field in hand. 63 * 64 * @param aCard The card to be inserted. 65 */ 66 public void putCardInHand (Card aCard) 67 { 68 hand [nextCard] = aCard; 69 nextCard ++; 70 } 71 72 /** 73 * This method returns the playerName. 74 * 75 * @return The playerName. 76 */ 77 public String getName() 78 { 79 return playerName; 80 } 2007-03-19 15:16 BridgeHand.java Page 2 81 82 /** 83 * This method returns true if two BridgeHands have the same score but 84 * belong to different players. 85 * 86 * @param other The 2nd BridgeHand to compare. 87 * @return The result of the comparison. 88 */ 89 public boolean equalScore (BridgeHand other) 90 { 91 if ((this.evaluateHand() == other.evaluateHand()) && 92 (this.getName().compareTo (other.getName()) != 0)) 93 return true; 94 else 95 return false; 96 } 97 98 99 /** 100 * This method returns a String with the owner, contents, and score of the 101 * BridgeHand. 102 * 103 * @return The owner, contents, and score of the BridgeHand. 104 */ 105 public String toString() 106 { 107 String myString; 108 109 // adds the player's name to myString 110 myString = playerName + "'s hand: \n"; 111 112 // adds each field in hand to myString 113 for (int x = 0; x < hand.length; x++) 114 myString += hand [x] + "\n"; 115 116 // adds the hands value to myString 117 myString += "This hand is worth " + evaluateHand() + " points.\n"; 118 119 return myString; 120 } 121 122 } // end BridgeHand class 2007-03-19 15:16 Card.java Page 1 1 import java.io.*; 2 import java.util.*; 3 4 /********************************************************************** 5 * This class represents a Card with a Rank and a Suit. 6 * 7 * @author Jonathan Herman 8 * @version 1 9 **********************************************************************/ 10 // Elizabeth Adams 11 // Date: 3/06/07 12 // PA 2 13 // Section: 2 14 15 public class Card 16 { 17 private Rank rank; 18 private Suit suit; 19 20 /** 21 * This constructor creates a Card object with a given Rank and Suit. 22 * 23 * @param rank The Card's Rank. 24 * @param suit The Card's Suit. 25 */ 26 public Card (Rank rank, Suit suit) 27 { 28 this.rank = rank; 29 this.suit = suit; 30 } 31 32 /** 33 * This method returns the point value of the card. 34 * 35 * @return The point value of the card. 36 */ 37 public int getPoints() 38 { 39 return rank.getPoints(); 40 } 41 42 /** 43 * This method returns a String representation of the card. 44 * 45 * @return The String represenation of the card. 46 */ 47 public String toString() 48 { 49 return rank + " of " + suit; 50 } 51 52 } // end Card class 2007-03-19 15:16 Deck.java Page 1 1 import java.io.*; 2 import java.util.*; 3 4 /********************************************************************** 5 * This class represents a deck of 52 cards. 6 * 7 * @author Jonathan Herman 8 * @version 1 9 **********************************************************************/ 10 // Elizabeth Adams 11 // Date: 3/06/07 12 // PA 2 13 // Section: 2 14 15 public class Deck 16 { 17 private static int dealt; 18 private static Card[] cards; 19 20 /** 21 * This constructor creates a Deck with 52 cards. 22 */ 23 public Deck() 24 { 25 int counter; 26 27 counter = 0; 28 dealt = -1; 29 cards = new Card [52]; 30 31 // fills the array with Cards for each possible suit and rank 32 for (Suit s: Suit.values()) 33 for (Rank r: Rank.values()) 34 { 35 cards [counter] = new Card (r, s); 36 counter ++; 37 } 38 } 39 40 /** 41 * This method shuffles the Deck by swapping two random Cards fifty times. 42 */ 43 public void shuffle() 44 { 45 int index1, index2; 46 47 Random rand; 48 Card tempCard; 49 50 rand = new Random(); 51 52 // swaps two random cards in the deck 53 for (int x = 0; x < 50; x++) 54 { 55 // generates two random indices 56 index1 = rand.nextInt (52); 57 index2 = rand.nextInt (52); 58 59 // swaps the cards at those two indices 60 tempCard = cards [index1]; 61 cards [index1] = cards [index2]; 62 cards [index2] = tempCard; 63 } 64 } 65 66 /** 67 * This method increments the index of the next available card and returns 68 * the card at that index. 69 * 70 * @return The next available Card. 71 */ 72 public Card deal() 73 { 74 dealt ++; 75 return cards [dealt]; 76 } 77 78 /** 79 * This method returns the contents of the Deck. 80 * 2007-03-19 15:16 Deck.java Page 2 81 * @return The contents of the Deck. 82 */ 83 public String toString() 84 { 85 String deck; 86 deck = ""; 87 88 // adds each card to deck 89 for (int x = 0; x < cards.length; x++) 90 deck += cards[x] + "\n"; 91 92 return deck; 93 } 94 95 } // end Deck class 2007-03-19 15:16 Player.java Page 1 1 import java.io.*; 2 import java.util.*; 3 4 /********************************************************************** 5 * This class represents a player with a hand in the game of bridge. 6 * 7 * @author Jonathan Herman 8 * @version 1 9 **********************************************************************/ 10 // Elizabeth Adams 11 // Date: 3/06/07 12 // PA 2 13 // Section: 2 14 15 public class Player 16 { 17 private String name; 18 private BridgeHand hand; 19 20 /** 21 * This constructor creates a Player with the given name and hand. 22 * 23 * @param name The name of the player. 24 * @param hand The hand of the player. 25 */ 26 public Player (String name, BridgeHand hand) 27 { 28 this.name = name; 29 this.hand = hand; 30 } 31 32 /** 33 * This method returns the name of the player. 34 * 35 * @return The player's name. 36 */ 37 public String getName() 38 { 39 return name; 40 } 41 42 /** 43 * This method returns the BridgeHand of the player. 44 * 45 * @return The player's hand. 46 */ 47 public BridgeHand getHand() 48 { 49 return hand; 50 } 51 52 /** 53 * This method returns the player's name and hand. 54 * 55 * @return The player's name and hand. 56 */ 57 public String toString() 58 { 59 return hand.toString(); 60 } 61 62 } // end Player class 2007-03-19 15:16 Rank.java Page 1 1 import java.io.*; 2 import java.util.*; 3 4 /********************************************************************** 5 * This enum describes a card's Rank and the points it is worth. 6 * 7 * @author Jonathan Herman 8 * @version 1 9 **********************************************************************/ 10 // Elizabeth Adams 11 // Date: 3/06/07 12 // PA 2 13 // Section: 2 14 15 public enum Rank 16 { 17 TWO (0), 18 THREE (0), 19 FOUR (0), 20 FIVE (0), 21 SIX (0), 22 SEVEN (0), 23 EIGHT (0), 24 NINE (0), 25 TEN (0), 26 JACK (1), 27 QUEEN (2), 28 KING (3), 29 ACE (4); 30 31 private int points; 32 33 /** 34 * This constructor creates a Rank and assigns a given value to points. 35 * 36 * @param points The point value of the Rank. 37 */ 38 private Rank (int points) 39 { 40 this.points = points; 41 } 42 43 /** 44 * This method returns the point value of a Rank. 45 * 46 * @return The point value of a Rank. 47 */ 48 public int getPoints() 49 { 50 return points; 51 } 52 53 } // end Rank enum 2007-03-19 15:16 Suit.java Page 1 1 import java.io.*; 2 import java.util.*; 3 4 /********************************************************************** 5 * This enum describes a card's suit. 6 * 7 * @author Jonathan Herman 8 * @version 1 9 **********************************************************************/ 10 // Elizabeth Adams 11 // Date: 3/06/07 12 // PA 2 13 // Section: 2 14 15 public enum Suit 16 { 17 CLUBS, DIAMONDS, HEARTS, SPADES; 18 }