|
In lesson 5, the "Player" class was constructed. The building blocks are now all in place to allow us to build the "Game" class, a class that provides the major functionality for a game of Color Belot. The code for this class is located in the file "Game.java". Before looking at the class methods, let's examine the class data fields preceding the closing right brace of the class. The "belotCards" object will hold information about the deck of Belot Cards. The "players" array contains information about each of the two players. The "dealer" variable indicates which player has dealt. The "trump" variable indicates the trump suit. The "flippedCard" is the card that has been turned over after the cards are dealt. The "whoseTurn" variable indicates which player should lead the next card. And the "caller" variable indicates which player named trump. Following the opening left brace of the class is the constructor for the class, "Game". The constructor creates a new "Cards" object, a new "Players" object and then instantiates two "Player" objects. The dealer is arbitrarily set to be the first player (player 0). Locate the "dealFiveCards" method. This procedure sets "whoseTurn" (the player who plays the first card) to the dealer, "dealer" to the other player (this is in preparation for the next deal), and "trump" to unknown. The "belotCards" are then assigned values (initially, nines and jacks are worth 0 and 2 points respectively) and shuffled. The "for" loop is used to give each player five cards and then sort the cards. Should the game ever be modified to the three player version of Color Belot, this general "for" loop will make the code easier to modify. Finally, the "flippedCard" is determined. Locate the "callTrump" method. This procedure has player zero call "EVERYTHING" trump and then reassigns the values of jacks and nines accordingly. Recall that in the Color Belot application, no input is being accepted from the user. Thus a game of Color Belot is being simulated. In the next phase of this tutorial (the user interface), the user will be able to make decisions for herself. Locate the "finishDealing" method. This procedure finishes dealing after trump has been named. The "caller" of trump is given the next three cards from the deck, including the card that is currently flipped over. The other player is given three cards as well. Each player's hand is then resorted and finally, a new card is flipped over. Locate the "scoreBonuses" method. This rather lengthy method figures out any special scoring bonuses (belots, four of a kind, runs) that a player will receive. The purpose of the first "for" loop is to score belots for each player. If "EVERYTHING" is trump, there are potentially four belots (recall that a belot is a king and a queen in the same trump suit). If a suit is trump, there is potentially only one belot. If "NOTHING" is trump, there are no belots. Belots are not mutually exclusive, so each player can potentially score belot points. Once the belots are calculated, the purpose of the next "while" loop is to see if either player has all four jacks, nines, aces, tens, kings, or queens. Only one of a four a kind can be counted, so if both players have four of a kind, the player with the higher four of a kind scores the bonus and the other player receives nothing. Take a look at the logic of the "while" loop. The outer loop goes through the cards in descending order (jack, nine, ace, ten, king, queen) until either one of the players is found to have four of a kind or the cards are exhausted. In the inner "for" loop each player is checked to see if he has four of the current card. If a four of a kind is found, the bonus is scored and the "bonusesDone" variable is set to true because neither player is allowed to score any more bonuses. If neither player has a four of a kind (four of a kind is quite rare), the final segment of code looks to see if either player has a run. The "for" loop collects the run information about each player and compares it against the "bestRun" seen so far. Whoever has the better run (if someone even has a run in the first place) gets to score it. The final "if" determines if one of the players has a run. If the player does, a message is printed and the player's score is updated. Locate the "playCards" method. This is the key procedure for playing a single game of Color Belot. The procedure uses three local variables; "cards" keeps track of the two "Card" objects being played on the current trick, "otherPlayer" indicates the player who plays second on the current trick, and "wins" keeps track of how many tricks each player has won. The second "for" loop simulates a game of Color Belot. Each iteration through the loop represents one of the eight tricks. The logic of a trick works as follows. First, the player who plays second is calculated, then the player who plays first plays the first card in his hand (this is an arbitrary choice for the application) and the second player follows. The inner "for" loop then discards the played cards and adds the points of the trick to the winner's hand. The last block of code takes into account two more bonuses. First, the winner of the last trick receives a 10 point bonus. Second, if either player takes all of the tricks, they receive a 100 point bonus, as well as any points that the other player scored through bonuses. Locate the "calculateScores" method. This procedure translates the raw points that each player scored during the game into game points. If the defender outscored the caller, the defender receives all of the player's raw game points and the caller receives no raw game points. If the defender and the caller receive the same number of raw points, then the caller receives no raw game points. Once the raw game points are updated to reflect the caller possibly being set, the final "for" loop of the procedure converts the raw game points into game points for each player. Locate the "printStatus" method. This procedure prints out information relevant to the current game. If the "printPlayers" variable is true, the flipped card and trump is printed as well as all of the cards in each player's hand. Regardless of the value of "printPlayers", the player's current raw game score and total score is printed. Locate the "otherPlayer" method. This function is passed a "player" and returns an integer representing the other player. Locate the "printResult" method. This procedure prints out the player's name and the card that he or she just played. Locate the "pause" method. The purpose of this procedure is to require the user to enter any key for the simulation to continue. Without this procedure, the output from the simulation would pass by faster than the user could digest it. Do not worry about the details of this procedure for the time being. In lesson 7, all of the classes covered in lessons 2 through 6 will be tied together by a driver. Once that is done, the Color Belot application will be complete! |