Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey guys, I need help creating a Tic-Tac-Toe game programmed in Java. The game h

ID: 3776302 • Letter: H

Question

Hey guys,

I need help creating a Tic-Tac-Toe game programmed in Java. The game has to be a 1D (One-Dimension) array board. It has to be a human plays against the computer type of Tic-Tac-Toe. Here is the requirements for the program. Thank you in advance!

You will develop a program in which a human plays against the computer.
1. Validate user input at every opportunity.
a. Do not allow number entries less than 0
b. Do not allow number entries greater than 8
c. Do not allow non-numeric entries
2. Do not use global variables in the development of the program
3. You may use global constants in the development of the program
4. Use one-dimensional arrays to keep track of the game:
a. Computer moves
b. Human moves
5. You must use functions to pass arrays and implement other program requirements.
6. The program must be developed using functions so that the main() function consists mostly of function calls
7. Computer must be aggressive and take every opportunity to win the game, block the user from winning and if the human makes a mistake, then the computer will capitalize on it and win if possible
8. The main() function must use a loop to keep the user in the program until he/she wants to quit.
9. You may not use infinite loops: for(;;) or while(true)
10. You may not use the break statement to exit loops

Explanation / Answer

Answer:

#include <iostream>

using namespace std;

char input[10] = {'o','1','2','3','4','5','6','7','8','9'};

int checkwinner();
void displayboard();

int main()
{
int player = 1,i,option;

char enable;
do
{
displayboard();
player=(player%2)?1:2;

cout << "Player " << player << ", enter a number: ";
cin >> option;

enable=(player == 1) ? 'X' : 'O';

if (option == 1 && input[1] == '1')

input[1] = enable;
else if (option == 2 && input[2] == '2')

input[2] = enable;
else if (option == 3 && input[3] == '3')

input[3] = enable;
else if (option == 4 && input[4] == '4')

input[4] = enable;
else if (option == 5 && input[5] == '5')

input[5] = enable;
else if (option == 6 && input[6] == '6')

input[6] = enable;
else if (option == 7 && input[7] == '7')

input[7] = enable;
else if (option == 8 && input[8] == '8')

input[8] = enable;
else if (option == 9 && input[9] == '9')

input[9] = enable;
else
{
cout<<"Invalid move ";

player--;
cin.ignore();
cin.get();
}
i=checkwinner();

player++;
}while(i==-1);
displayboard();
if(i==1)

cout<<"==>Player "<<--player<<" win ";
else
cout<<"==>Game draw";

cin.ignore();
cin.get();
return 0;
}


int checkwinner()
{
if (input[1] == input[2] && input[2] == input[3])

return 1;
else if (input[4] == input[5] && input[5] == input[6])

return 1;
else if (input[7] == input[8] && input[8] == input[9])

return 1;
else if (input[1] == input[4] && input[4] == input[7])

return 1;
else if (input[2] == input[5] && input[5] == input[8])

return 1;
else if (input[3] == input[6] && input[6] == input[9])

return 1;
else if (input[1] == input[5] && input[5] == input[9])

return 1;
else if (input[3] == input[5] && input[5] == input[7])

return 1;
else if (input[1] != '1' && input[2] != '2' && input[3] != '3'
&& input[4] != '4' && input[5] != '5' && input[6] != '6'
&& input[7] != '7' && input[8] != '8' && input[9] != '9')

return 0;
else
return -1;
}

void displayboard()
{
system("cls");
cout << " Tic Tac Toe ";

cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;

cout << " | | " << endl;
cout << " " << input[1] << " | " << input[2] << " | " << input[3] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << input[4] << " | " << input[5] << " | " << input[6] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << input[7] << " | " << input[8] << " | " << input[9] << endl;

cout << " | | " << endl << endl;
}