|
Color Belot Server Code, Part II In today's lesson, we will examine the structure of the "Server" class and the "ServerThread" class that exist in the "Server.java" file. Server Class The "main" method of the "Server" application is invoked when the "Server.java" file is run. It establishes a "ServerSocket" on port 41962 and then waits (via the "accept") method for two clients. Once two clients have arrived, one "ServerThread" is created (see below) and a "ClientHandler" (see yesterday) is created for each client. Each "ClientHandler" is launched via the "start" method. ServerThread Class The "ServerThread" is the central logic engine for a game of Color Belot. Each "ClientHandler" calls the "ServerThread" whenever it has received information from a client. The constructor for the "ServerThread" class creates a new "Game" of Color Belot, calls the "startGame" method, and then sets "numberAgains" to 0. The "numberAgains" variable is used at the end of a complete game of Color Belot. If its value is two, a brand new game is started. The "startGame" method sets the total points of each player to zero, selects a dealer, and deals five cards. The "setClients" method gives the "ServerThread" pointers to each of the "ClientHandler"s. This is useful when the "ServerThread" has received information from one "ClientHandler" that should be sent on to the other. The "trumpIsKnown" method is called when either player has selected trump. At this point, each player is given an additional three cards via the "finishDealing" method, bonuses are scored, and play is started. Bonus points are transmitted to each player via the "RAWPOINTS" command. Finally, the leader of the first trick is determined. The "showCards" method takes as parameters a client number and the number of cards that are currently in the client's hand. If the number of cards is five, the dealer of the hand is sent to the client via the "DEALER" command. The "CARDSTODISPLAY" message tells the client how many cards are currently in his hand. The "for" loop then sends the card image numbers to the client via the "CARD" message. Following the "for" loop, the card back image is sent as both the client's last play and the opponent's last play. Then, the flipped card is sent to be displayed in the appropriate position (10). Finally, the "PAINT" message instructs the client to update its screen to display all of this new information. Do you understand the arguments to these commands?? The "makeBid" method is called each time either client makes a bid. If the bid is "Pass", the bid is merely relayed to the opponent. If the bid is "Accept", the trump suit of the flipped card is determined via an "if" construct. (Note: "Clubs", "Diamonds", "Hearts", and "Spades" are the only possible suits at this point!) The trump suit is relayed to the opponent via a "FIRSTBID" message and to the caller via a "TRUMP" message. The "trumpIsKnown" method is called, a "PLAYCARD" message is sent to the client whose turn it is to play first, and each client is sent its full hand via a "showCards" method call. The final "else" in the "makeBid" method handles the case where the client has freely named trump. In this case, any suit could have been named, as well as "Nothing" trump and "Everything" trump. Once trump is identified, the following logic is very similar to what happens when trump is "Accept"ed in the previous case. The "sendMessage" method sends a "message" to the appropriate client. It does this by placing the "message" in the "DataOutputStream" via the "writeUTF" method and then "flush"ing the "DataOutputStream". The "passOut" method is called when both players have passed twice in the game of Color Belot. A "SLEEP" message is issued to each client, causing the client to sleep for five seconds. Then a new game of Color Belot is begun: a dealer is selected, five cards are dealt, the dealer is identified, each player is delivered her five cards via the "showCards" method, a "STARTBIDDING" message is sent to the client who must bid first, and a "PAINT" message is sent to this same client. The "transmit" command sends a "message" from one "client" to the other via the "sendMessage" method. The "makePlay" method is called when a player leads a card. The card led is relayed to the opponent via the "FOLLOWCARD" and "PAINT" messages. The "finishPlay" method is called when a player plays the second card for a specific trick. The two cards played are stored in the "cardOne" and "cardTwo" variables. Then a winner is determined and the new raw scores of each player is recorded. The second card played is sent to the opponent via the "CARD" message. If this trick is the final one for the current hand of Color Belot (i.e. there are zero cards left in each player's hand), then the "finishPlay" and "calculateScores" methods are called to wrap up the current hand of Belot. The raw points, game points, and total points for each player are now recorded in local variables and broadcast to each player via "RAWPOINTS", "GAMEPOINTS", and "TOTALPOINTS" messages. Each client is also sent a "PAINT" method to update its screen and a "SLEEP" method to sleep for five seconds before proceeding. In the event that the game is not finished, a new dealer is selected via the "selectDealer" method, five cards are dealt to each player via the "dealFiveCards" method, the "dealer" is determined, the five cards are sent to each client via the "showCards" method, and a "STARTBIDDING" and "PAINT" message is sent to the client that should bid first in the new game. In the event that the trick is not the final trick of the current game (the "else" part at the bottom of the "finishPlay" method), each client is sent its current raw points via the "RAWPOINTS" and "PAINT" messages. A "PLAYCARD" message is sent to the client who won the last trick. This client gets to play the first card in the next trick. The "quit" method sends a "QUIT" message to the other client. The "again" method checks to see if both clients want to play another game of Color Belot. If they do, the raw points, game points, and total points are all set back to 0 and relayed to each client via the "RAWPOINTS", "GAMEPOINTS", and "TOTALPOINTS" messages. Five cards are dealt and transmitted to each player. The player who should bid first is given a "STARTBIDDING" message. Each player is sent a "PAINT" message to update its display. The "otherPlayer" method calculates the number of the other player (either 0 or 1). There are five instance variables used by the "ServerThread" class. The variable "belotGame" holds the information about the current game of Color Belot. The "clients" array stores points to each of the two "ClientHandler"s. The "trickLeader" indicates which client plays first in the current trick. The "cardLed" is the index of the card that was played first on the current trick. Finally, "numberAgains" counts how many of the clients (there are only two!) want to play a new game of Color Belot once the previous game is finished. Congratulations! You are now finished with this tutorial. If you have paid close attention, you should now be in a position to write your own server/client applications that feature user interfaces. Happy Javaing! |