Question: Write a program that allows two people to play a game of tic-tac-toe, with the computer acting as a referee. Here is what your program

 Write a program that allows two people to play a gameof tic-tac-toe, with the computer acting as a referee. Here is whatyour program might look like in action: Welcome! Tic-Tac-Toe is a twoplayer game. Enter player one's name: Foo Enter player two's name: BarPlayers take turns marking a square. Only squares not already marked can

Write a program that allows two people to play a game of tic-tac-toe, with the

computer acting as a referee. Here is what your program might look like in action:

Welcome! Tic-Tac-Toe is a two player game. Enter player one's name: Foo Enter player two's name: Bar

Players take turns marking a square. Only squares not already marked can be picked. Once a player has

marked three squares in a row, he or she wins! If all squares are marked and no three squares are the same, a tied game is declared. Have Fun!

Game board: || || ||

It is Foo's turn. Pick a row between 1 and 3: 1 Pick a column between 1 and 3: 1

Game board: |X| || ||

It is Bar's turn. Pick a row between 1 and 3: 2 Pick a column between 1 and 3: 2

Game board: |X| |O| ||

It is Foo's turn. Pick a row between 1 and 3: 2 Pick a column between 1 and 3: 2 ILLEGAL CHOICE! TRY AGAIN... Pick a row between 1 and 3: 2 Pick a column between 1 and 3: 1 Game board: |X| |XO| ||

It is Bar's turn. Pick a row between 1 and 3: 3 Pick a column between 1 and 3: 1

Game board: |X|

|XO| |O|

It is Foo's turn. Pick a row between 1 and 3: 3 Pick a column between 1 and 3: 3

Game board: |X| |XO| |O X|

It is Bar's turn. Pick a row between 1 and 3: 1 Pick a column between 1 and 3: 3

Game board: |X O| |XO| |O X|

Game Over - Bar WINS!!!

In solving this problem, create a TicTacToeBoard class that contains a two- dimensional array representing the current state of a tic-tac-toe board. Initially, every element of this array could contain a space character, to indicate it is empty. You should also define a toString method in this class to make it possible for a client program to print the board as shown in the above output.

Your main program will alternately let each of the two players make a move, but not allow any move that is illegal either because a particular square is already occupied, or because a value less than 1 or greater than 3 has been input for the row or column position. After each turn is made the computer should check if any row, column or diagonal contains three X marks or three O marks; if so, a winner should be declared and the game should end at that point.

You may also add the following optional method to TicToeBoard.java if you like: // returns true if the board is a tie (optional) public boolean isTie() {...}

Write a program that allows two people to play a game of "tic-tac-toe", with the computer acting as a "referee." Here is what your program might look like in action: Welcome! Tic-Tac-Toe is a two player game. Enter player one's name: Foo Enter player two's name: Bar Players take turns marking a square. Only squares not already marked can be picked. Once a player has marked three squares in a row, he or she wins! If all squares are marked and no three squares are the same, a tied game is declared. Have Fun! It is Foo's turn. Pick a row between 1 and 3: 1 Pick a column between 1 and 3:1 It is Bar's turn. Pick a row between 1 and 3: 2 Pick a column between 1 and 3: 2 It is Foo's turn. Pick a row between 1 and 3: 2 Pick a column between 1 and 3: 2 ILLEGAL CHOICE! TRY AGAIN... Pick a row between 1 and 3: 2 Pick a column between 1 and 3: 1 Game board: xxO It is Bar's turn. Pick a row between 1 and 3: 3 Pick a column between 1 and 3: 1 Game board: x It is Foo's turn. Pick a row between 1 and 3: 3 Pick a column between 1 and 3:3 Game board: It is Bar's turn. Pick a row between 1 and 3: 1 Pick a column between 1 and 3:3 Game board: Game Over - Bar WINS!!! In solving this problem, create a TicTacToeBoard class that contains a twodimensional array representing the current state of a tic-tac-toe board. Initially, every element of this array could contain a space character, to indicate it is empty. You should also define a toString method in this class to make it possible for a client program to print the board as shown in the above output. Your main program will alternately let each of the two players make a move, but not allow any move that is illegal - either because a particular square is already occupied, or because a value less than 1 or greater than 3 has been input for the row or column position. After each turn is made the computer should check if any row, column or diagonal contains three ' X ' marks or three ' O ' marks; if so, a winner should be declared and the game should end at that point. You may also add the following optional method to TicToeBoard.java if you like: // returns true if the board is a tie (optional) public boolean isTie() {}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!