Question
CO SCI : Programming In Java (Game: play a tic-tac-toe game) In a a game of tic tac toe, two players take turns marking an
CO SCI : Programming In Java
(Game: play a tic-tac-toe game) In a a game of tic tac toe, two players take turns marking an available cell in a 3X3 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 a program for playing Tic Tac Toe. The program prompts two players to enter X token and O token alternately. When ever a token is entered, the program redisplays the board on the console and determines the status of the game (win, draw, or continue).
Sample run:
------------- |? |? |? | ------------- |? |? |? | ------------- |? |? |? | ------------- Enter a row for player X: 1 Enter a column for player X: 1
------------- |? |? |? | ------------- |? | X |? | ------------- |? |? |? | ------------- Enter a row for player O: 1 Enter a column for player O: 2
------------- |? |? |? | ------------- |? | X | O | ------------- |? |? |? | ------------- Enter a row for player X: 0 Enter a column for player X: 0
------------- | X |? |? | ------------- |? | X | O | ------------- |? |? |? | ------------- Enter a row for player O: 1 Enter a column for player O: 0
------------- | X |? |? | ------------- | O | X | O | ------------- |? |? |? | ------------- Enter a row for player X: 2 Enter a column for player X: 2
------------- | X |? |? | ------------- | O | X | O | ------------- |? |? | X | ------------- X player won
Make use of functions as appropriate, not have monolithic code in main function. The input needed for the program is row,column indices for each player.
Example 1 -
So, for example this set of input would allow X to win along the main diagonal.
0 0 1 0 1 1 2 0 2 2
Alternatively, the input could be provided in one line:
0 0 1 0 1 1 2 0 2 2
The first pair is for Xs move second pair Os move and so on. If you are not having enough input values you will have an error.
Example 2 -
Another possibilty is O to win in last column. The input values could be:
1 0 0 2 2 0 0 1 2 1 0 0
Example 3 -
Give a set of input for a draw game.
Run you program for the three set of inputs: X win, O win and a draw. Include them in output sample.
Q1. Instead of computer being a referee, what changes would you need to be make if the computer was a player
Q2. What is the simplest strategy that the computer could use on its turn?
Q3. What is the optimum strategy that the computer could use on its turn?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started