Question

Implement Tic-tae-toe program in multi-thread. In other words, we create two threads and create a Tic-tae-toe...

Implement Tic-tae-toe program in multi-thread.

In other words, we create two threads and create a Tic-tae-toe game between these two threads.

You can write a program to do this.

 

Tic-tae-toe When you are playing the game,

Tic-tae-toe When the game is free, restart the game,

It tells you who has won and how many times it has won between Thread1 and Thread2.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Tic Tac Toe game in multi - thread === >

1, 2 ,3 are the line numbers which means 2nd line of code is followed by 3rd line, 3rd line is followed by 4th line.

Code is properly explained as every line is explained using comments.

1 // Fig. 24.13: TicTacToeServer.java
 2 // This class maintains a game of Tic-Tac-Toe for two clients.
 3 import java.awt.BorderLayout;
 4 import java.net.ServerSocket;
 5 import java.net.Socket;
 6 import java.io.IOException;
 7 import java.util.Formatter;
 8 import java.util.Scanner;
 9 import java.util.concurrent.ExecutorService;
10 import java.util.concurrent.Executors;
11 import java.util.concurrent.locks.Lock;
12 import java.util.concurrent.locks.ReentrantLock;
13 import java.util.concurrent.locks.Condition;
14 import javax.swing.JFrame;
15 import javax.swing.JTextArea;
16 import javax.swing.SwingUtilities;
17 
18 public class TicTacToeServer extends JFrame
19 {
20 private String[] board = new String[ 9 ]; // tic-tac-toe board
21 private JTextArea outputArea; // for outputting moves
22 private Player[] players; // array of Players
23 private ServerSocket server; // server socket to connect with clients
24 private int currentPlayer; // keeps track of player with current move
25 private final static int PLAYER_X = 0; // constant for first player
26 private final static int PLAYER_O = 1; // constant for second player
27 private final static String[] MARKS = { "X", "O" }; // array of marks
28 private ExecutorService runGame; // will run players
29 private Lock gameLock; // to lock game for synchronization
30 private Condition otherPlayerConnected; // to wait for other player
31 private Condition otherPlayerTurn; // to wait for other player's turn
32 
33 // set up tic-tac-toe server and GUI that displays messages
34 public TicTacToeServer()
35 {
36 super( "Tic-Tac-Toe Server" ); // set title of window
37 
38 // create ExecutorService with a thread for each player
39 runGame = Executors.newFixedThreadPool( 2 );
40 gameLock = new ReentrantLock(); // create lock for game
41 
42 // condition variable for both players being connected
43 otherPlayerConnected = gameLock.newCondition();
44 
45 // condition variable for the other player's turn
46 otherPlayerTurn = gameLock.newCondition();
47 
48 for ( int i = 0; i < 9; i++ )
49 board[ i ] = new String( "" ); // create tic-tac-toe board
50 players = new Player[ 2 ]; // create array of players
51 currentPlayer = PLAYER_X; // set current player to first player
52 
53 try
54 {
55 server = new Server-Socket( 12345, 2 ); // set up ServerSocket
56 } // end try
57 catch ( IOException ioException )
58 {
59 ioException.printStackTrace();
60 System.exit( 1 );
61 } // end catch
62 
63 outputArea = new JTextArea(); // create JTextArea for output
64 add( outputArea, BorderLayout.CENTER );
65 outputArea.setText( "Server awaiting connections
" );
66 
67 setSize( 300, 300 ); // set size of window
68 setVisible( true ); // show window
69 } // end TicTacToeServer constructor
70 
71 // wait for two connections so game can be played
72 public void execute()
73 {
74 // wait for each client to connect
75 for ( int i = 0; i < players.length; i++ )
76 {
77 try // wait for connection, create Player, start runnable
78 {
79 players[ i ] = new Player( server.accept(), i ); 
80 runGame.execute( players[ i ] ); // execute player runnable
81 } // end try
82 catch ( IOException ioException )
83 {
84 ioException.printStackTrace();
85 System.exit( 1 );
86 } // end catch
87 } // end for
88 
89 gameLock.lock(); // lock game to signal player X's thread
90 
91 try
92 {
93 players[ PLAYER_X ].setSuspended( false ); // resume player X
94 otherPlayerConnected.signal(); // wake up player X's thread
95 } // end try
96 finally
97 {
98 gameLock.unlock(); // unlock game after signalling player X
99 } // end finally
100 } // end method execute
101 
102 // display message in outputArea
103 private void displayMessage( final String messageToDisplay )
104 {
105 // display message from event-dispatch thread of execution
106 SwingUtilities.invokeLater(
107 new Runnable()
108 {
109 public void run() // updates outputArea
110 {
111 outputArea.append( messageToDisplay ); // add message
112 } // end method run
113 } // end inner class
114 ); // end call to SwingUtilities.invokeLater
115 } // end method displayMessage
116 
117 // determine if move is valid
118 public boolean validateAndMove( int location, int player )
119 {
120 // while not current player, must wait for turn
121 while ( player != currentPlayer )
122 {
123 gameLock.lock(); // lock game to wait for other player to go
124 
125 try
126 {
127 otherPlayerTurn.await(); // wait for player's turn
128 } // end try
129 catch ( InterruptedException exception )
130 {
131 exception.printStackTrace();
132 } // end catch
133 finally
134 {
135 gameLock.unlock(); // unlock game after waiting
136 } // end finally
137 } // end while
138 
139 // if location not occupied, make move
140 if ( !isOccupied( location ) )
141 {
142 board[ location ] = MARKS[ currentPlayer ]; // set move on board
143 currentPlayer = ( currentPlayer + 1 ) % 2; // change player
144 
145 // let new current player know that move occurred
146 players[ currentPlayer ].otherPlayerMoved( location );
147 
148 gameLock.lock(); // lock game to signal other player to go
149 
150 try
151 {
152 otherPlayerTurn.signal(); // signal other player to continue
153 } // end try
154 finally
155 {
156 gameLock.unlock(); // unlock game after signaling
157 } // end finally
158 
159 return true; // notify player that move was valid
160 } // end if
161 else // move was not valid
162 return false; // notify player that move was invalid
163 } // end method validateAndMove
164 
165 // determine whether location is occupied
166 public boolean isOccupied( int location )
167 {
168 if ( board[ location ].equals( MARKS[ PLAYER_X ] ) ||
169 board [ location ].equals( MARKS[ PLAYER_O ] ) )
170 return true; // location is occupied
171 else
172 return false; // location is not occupied
173 } // end method isOccupied
174 
175 // place code in this method to determine whether game over
176 public boolean isGameOver()
177 {
178 return false; // this is left as an exercise
179 } // end method isGameOver
180 
181 // private inner class Player manages each Player as a runnable
182 private class Player implements Runnable
183 {
184 private Socket connection; // connection to client
185 private Scanner input; // input from client
186 private Formatter output; // output to client
187 private int playerNumber; // tracks which player this is
188 private String mark; // mark for this player
189 private boolean suspended = true; // whether thread is suspended
190 
191 // set up Player thread
192 public Player( Socket socket, int number )
193 {
194 playerNumber = number; // store this player's number
195 mark = MARKS[ playerNumber ]; // specify player's mark
196 connection = socket; // store socket for client
197 
198 try // obtain streams from Socket
199 {
200 input = new Scanner( connection.getInputStream() ); 
201 output = new Formatter( connection.getOutputStream() );
202 } // end try
203 catch ( IOException ioException )
204 {
205 ioException.printStackTrace();
206 System.exit( 1 );
207 } // end catch
208 } // end Player constructor
209 
210 // send message that other player moved
211 public void otherPlayerMoved( int location )
212 {
213 output.format( "Opponent moved
" ); 
214 output.format( "%d
", location ); // send location of move
215 output.flush(); // flush output 
216 } // end method otherPlayerMoved
217 
218 // control thread's execution
219 public void run()
220 {
221 // send client its mark (X or O), process messages from client
222 try
223 {
224 displayMessage( "Player " + mark + " connected
" );
225 output.format( "%s
", mark ); // send player's mark
226 output.flush(); // flush output 
227 
228 // if player X, wait for another player to arrive
229 if ( playerNumber == PLAYER_X )
230 {
231 output.format( "%s
%s", "Player X connected",
232  "Waiting for another player
" ); 
233 output.flush(); // flush output 
234 
235 gameLock.lock(); // lock game to wait for second player
236 
237 try
238 {
239 while( suspended )
240 {
241 otherPlayerConnected.await(); // wait for player O
242 } // end while
243 } // end try
244 catch ( InterruptedException exception )
245 {
246 exception.printStackTrace();
247 } // end catch
248 finally
249 {
250 gameLock.unlock(); // unlock game after second player
251 } // end finally
252 
253 // send message that other player connected
254 output.format( "Other player connected. Your move.
" );
255 output.flush(); // flush output 
256 } // end if
257 else
258 {
259 output.format( "Player O connected, please wait
" );
260 output.flush(); // flush output 
261 } // end else
262 
263 // while game not over
264 while ( !isGameOver() )
265 {
266 int location = 0; // initialize move location
267 
268 if ( input.hasNext() )
269 location = input.nextInt(); // get move location
270 
271 // check for valid move
272 if ( validateAndMove( location, playerNumber ) )
273 {
274 displayMessage( "
location: " + location );
275 output.format( "Valid move.
" ); // notify client
276 output.flush(); // flush output 
277 } // end if
278 else // move was invalid
279 {
280 output.format( "Invalid move, try again
" );
281 output.flush(); // flush output 
282 } // end else
283 } // end while
284 } // end try
285 finally
286 {
287 try
288 {
289 connection.close(); // close connection to client
290 } // end try
291 catch ( IOException ioException )
292 {
293 ioException.printStackTrace();
294 System.exit( 1 );
295 } // end catch
296 } // end finally
297 } // end method run
298 
299 // set whether or not thread is suspended
300 public void setSuspended( boolean status )
301 {
302 suspended = status; // set value of suspended
303 } // end method setSuspended
304 } // end class Player
305 } // end class TicTacToeServer.
Add a comment
Know the answer?
Add Answer to:
Implement Tic-tae-toe program in multi-thread. In other words, we create two threads and create a Tic-tae-toe...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write in Linux ( ubuntu ) !! Tic-tae-toe This is a home for multi-threaded programs....

    Please write in Linux ( ubuntu ) !! Tic-tae-toe This is a home for multi-threaded programs. In other words, we create two threads and create a Tic-tae-toe game between these two threads. You can write a program to do this. Tic-tae-toe When you are playing the game, Tic-tae-toe When the game is free, restart the game, It tells you who has won and how many times it has won between Thread1 and Thread2.

  • (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an...

    (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...

  • (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe....

    (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...

  • PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1....

    PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1. Create the data structure – Nine slots that can each contain an X, an O, or a blank. – To represent the board with a dictionary, you can assign each slot a string-value key. – String values in the key-value pair to represent what’s in each slot on the board: ■ 'X' ■ 'O' ■ ‘ ‘ 2. Create a function to print the...

  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

    18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...

  • Java project In a game of tic-tac-toe, two players take turns marking an available cell in...

    Java project In a game of tic-tac-toe, two players take turns marking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Create...

  • You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people....

    You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...

  • Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game....

    Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...

  • I need to create a Tic Tac Toe program in C++. These are the requirements Write...

    I need to create a Tic Tac Toe program in C++. These are the requirements Write a program that allows the computer to play TicTacToe against a human player or allow two human players to play one another. Implement the following conditions: The player that wins the current game goes first in the next round, and their symbol is X. The other player will be O. Keep track of the number of games played, wins, and draws for each player....

  • I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program...

    I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional String array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: a. Displays the contents of the board array. b. Allows player 1 to select a location...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT