Create a simple tic-tac-toe game in java. Create a double array (2 dimensional a
ID: 3539546 • Letter: C
Question
Create a simple tic-tac-toe game in java. Create a double array (2 dimensional array) of type char to store the state of the tic-tac-toe board. Use JOptionPane to ask the user to pick a square. The input should be a single digit, 1 for the top left corner , 2 for top center square, 3 for top right square, etc. Use a loop to keep asking for the next move until the board is filled. Do not worry about checking who won. If the user inputs a square that has already been picked, the user should be prompted for a different square. After each move, print out the current state of the board to the console. the user will only be asked the moves for X. Moves for O will be generated by the program using the random method in the Math class. ( This method returns a number of type double between 0 and 1. In order to get a random number between 1 and 9, multiply the double returned by the random function by 9 and then call the ceil function from the Math class on this result.)
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
const int ROWS = 3;
const int COLS = 3;
char board[ROWS][COLS] = {' '};
cout << "| " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << "| ";
cout << "|____|____|____| ";
cout << "| " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << "| ";
cout << "|____|____|____| ";
cout << "| " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << "| ";
cout << "|____|____|____| ";
return 0;
}
Thanks,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.