Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please show the detailed answer using Java, thanks! We write a simple version of the tic-tac-toe game which uses a board consisting of 3x3 squares.

Please show the detailed answer using Java, thanks!

image text in transcribed

We write a simple version of the tic-tac-toe game which uses a board consisting of 3x3 squares. There are two players and one side puts an "X while the other side puts an O" in one of the nine squares altemately. The game ands when all the squares are filled or one side wins by getting 3 "X" (or "O in a straight lne, including the diagonal lines. (i) Create a class TicTacToe with a string array board of size 9 which is initialized to "1", "2", "9". Copy the content of the class as the answers to this part. i) Write a method printBoard of TicTacToe which prints the content of the array to the console in the following format: 1 2 3 4 5 6 7 89 Copy the content of the method as the answers to this part () Write a driver class TestTicTacToe with a ma in () method which creates a TicTacToe object and print the board to the console. Copy the content of the class as the answers to this part (iv) Write a method playerMove) of the class TicTacToe which gets an integer from the console after displaying the prompt "Player move: It updates the corresponding square in the board by replacing the digit string by an "X". For example, if the player enters 3, the output is: Player move: 3 1 2 X 4 5 6 7 8 9 You can assume the input is a valid integer but you needs to check its value (1 to 9 inclusively) and the square should not be occupied. If the input is outside the allowed range or the square is occupied, display a suitable error message and ask the user to input again. To obtain an integer from the console, you can use the following statements in suitable places: import java.util.Scanner; // import Scanner class Scanner input new Scanner (System.in); int move = input. next int(); // declare scanner object "input" // get next integer by "nextInt O" Copy the content of the method and the compilation output as the answers to this part (v) Write a method computer RandomMove of the class TicTacToe to choose an empty square (containing an integer) randomly and put an "O" in it. The move of the computer is displayed. If the computer likes to place an "O" in square 9 by random selection, the output becomes: Computer move:9 1 2 X 4 5 6 7 8 0 Copy the content of the method as the answer to this part. (vi) Write a method play) of the class TicTacToe to play the game until all the squares are filled Since the computer uses a less intelligent method to play, it is allowed to move first. The checking of winning is not required here and it will be done later. The board should be displayed before the game starts and after each move. Copy the content of the method as the answer to this part. (vii) This part is more difficult. Write a method won of the class TicTacToe to check if there are 3 "X" or "O" in a straight line and it retums true if so. Otherwise it retuns false. In the method play(), won is called to check ifone side has won and it ends the game if it is true. Also, add a statement in TestTicTacToe so that the game can be played. Copy the content of the new method and changes as the answer to this part We write a simple version of the tic-tac-toe game which uses a board consisting of 3x3 squares. There are two players and one side puts an "X while the other side puts an O" in one of the nine squares altemately. The game ands when all the squares are filled or one side wins by getting 3 "X" (or "O in a straight lne, including the diagonal lines. (i) Create a class TicTacToe with a string array board of size 9 which is initialized to "1", "2", "9". Copy the content of the class as the answers to this part. i) Write a method printBoard of TicTacToe which prints the content of the array to the console in the following format: 1 2 3 4 5 6 7 89 Copy the content of the method as the answers to this part () Write a driver class TestTicTacToe with a ma in () method which creates a TicTacToe object and print the board to the console. Copy the content of the class as the answers to this part (iv) Write a method playerMove) of the class TicTacToe which gets an integer from the console after displaying the prompt "Player move: It updates the corresponding square in the board by replacing the digit string by an "X". For example, if the player enters 3, the output is: Player move: 3 1 2 X 4 5 6 7 8 9 You can assume the input is a valid integer but you needs to check its value (1 to 9 inclusively) and the square should not be occupied. If the input is outside the allowed range or the square is occupied, display a suitable error message and ask the user to input again. To obtain an integer from the console, you can use the following statements in suitable places: import java.util.Scanner; // import Scanner class Scanner input new Scanner (System.in); int move = input. next int(); // declare scanner object "input" // get next integer by "nextInt O" Copy the content of the method and the compilation output as the answers to this part (v) Write a method computer RandomMove of the class TicTacToe to choose an empty square (containing an integer) randomly and put an "O" in it. The move of the computer is displayed. If the computer likes to place an "O" in square 9 by random selection, the output becomes: Computer move:9 1 2 X 4 5 6 7 8 0 Copy the content of the method as the answer to this part. (vi) Write a method play) of the class TicTacToe to play the game until all the squares are filled Since the computer uses a less intelligent method to play, it is allowed to move first. The checking of winning is not required here and it will be done later. The board should be displayed before the game starts and after each move. Copy the content of the method as the answer to this part. (vii) This part is more difficult. Write a method won of the class TicTacToe to check if there are 3 "X" or "O" in a straight line and it retums true if so. Otherwise it retuns false. In the method play(), won is called to check ifone side has won and it ends the game if it is true. Also, add a statement in TestTicTacToe so that the game can be played. Copy the content of the new method and changes as the answer to this part

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spomenik Monument Database

Authors: Donald Niebyl, FUEL, Damon Murray, Stephen Sorrell

1st Edition

0995745536, 978-0995745537

Students also viewed these Databases questions

Question

=+1. What do you recommend to the governor?

Answered: 1 week ago