|
Having constructed the "Cards" and "RunInfo" classes in lesson 4, it is now time to build the "Player" class. The purpose of the "Player" class is to keep track of relevant information associated with a single Color Belot player. The code for this class is located in a file named "Player.java". Preceding the closing right brace of the class are the data fields associated with this class. The "numberCards" field keeps track of how many cards are currently in the player's hand. The "playerCards" field is an array that contains the "Card" objects that the player holds. The "name" field is a string that contains the player's name. The "rawGamePoints" field keeps track of how many points the player has scored in the current Color Belot game. The "convertedGamePoints" field keeps track of the number of game points that the player has scored in all of the previous Color Belot games. Remember that the goal of Color Belot is to reach 500 game points first. Would you like to review the rules of Color Belot? With the data fields in hand, let's now take a look at the methods contained in the "Player" class, starting with the constructor following the opening left brace of the class. The constructor is fairly straightforward. It is called when a Color Belot game first begins. Initially, the player has 0 cards, can have up to 8 cards, has a name, has 0 points in the current game, and has 0 game points. Locate the "dealCards" method. The purpose of this method is to give the player a specified number of cards, "howMany", from the deck of cards, "belotCards". For example, at the start of a game, each player must be dealt an initial five cards. Locate the "print" method. This method prints out the cards that the player currently holds. Notice that the "print" method calls a "print" method in the "Card" class, after accessing a "Card" in the "playerCards" array. Locate the "discard" method. This method is called with the "Card" that the player wants to play. The method removes this "Card" from the "playerCards" array by shifting all of the "Card" objects following the "Card" to be discarded down one position. This technique keeps the "playerCards" sorted. The code for this procedure is somewhat subtle. First of all, a while loop is used to find the position of the "Card" to be removed. Then, all of the "Card" objects following the "Card" to be discarded are shifted one position. Look carefully at the logic of this method and make sure that you understand it before proceeding. Locate the "sortCards" method. This method has two parameters, "trump" and "sortField". The "playerCards" can be sorted (using Bubble sort, ugh!) in one of two manners. If the value of "sortField" is "USE_TRUMP", then trump will be used when sorting. If the value of "sortField" is "DONT_USE_TRUMP" then trump will not be used during the sort. Trump can influence where in a suit a jack or a nine belongs. Within the nested "for" loops, the first conditional is used to decide which comparison method to call. Once that is done, it is business as usual for the rest of the sort routine. Locate the "findPlay" method. In the Color Belot application, no intelligence at all is used to figure out what card the non-leader will play. The non-leader simply plays the first card of the led suit that is encountered in the "playerCards". If there is no card that matches the suit, the first card in the hand (position 0 in a Java array) is played. Locate the "findNumberBelots" method. This function counts and returns the number of belots (a belot is a king and a queen of trump) that are contained in a player's hand. Unless "EVERYTHING" is trump, there will be at most 1 belot. If "EVERYTHING" is trump, a hand could potentially hold 4 belots! The method works by scanning through the player's sorted cards sequentially. If a king of trump is found, the next card is examined to see if it is the queen of trump. Remember that the hand has been sorted so these two cards, if they exist, will appear consecutively. Do you understand the code? Locate the "hasAllFour" method. This function returns true if the player has all four of the "cardName". The method works by scanning through the "playerCards" and counting the number of cards that match "cardName". If the number is four, the method returns a true. Recall that in Color Belot, a player receives bonus points for having all four aces, tens, kings, queens, jacks, or nines. Locate the "findRunInfo" method. This function returns a "RunInfo" object. The "RunInfo" object contains the number of cards in the player's longest run as well as the top card in that run. Recall that in Color Belot, cards in a run must be in the same suit and must be in consecutive order, using ordinary card order (e.g. ace, king, queen, jack, ten, nine, eight). The code for this method is again, somewhat subtle. First, the "playerCards" are sorted into regular order. Then, the "playerCards" are scanned sequentially and the current run is tracked. After each card is examined, the "currentRun" is compared against the "longestRun" and if it is better, the "longestRun" is updated accordingly. To be better, the "currentRun" must contain more cards, contain the same number of cards and have a higher top card, or contain the same number of cards with the same top card and be in trump. Once the "longestRun" is found, a "RunInfo" object is made with the appropriate information, the "playerCards" are sorted back into Belot order, and the "RunInfo" object is returned. Locate the several one line methods following the "findRunInfo" method. Many of these methods are accessor (prefixed with "get") and modifier (prefixed with "set") methods. Accessor and modifier methods are important methods from an object-oriented programming standpoint. Look through these methods to see what types of functionality is being provided. Then look at the "Player" class as a whole to see the big picture. Congratulations, you are done with today's lesson! |