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

Make a tic tac toe game in c++. Please make sure it runs, and please let the cod

ID: 3843641 • Letter: M

Question

 Make a tic tac toe game in c++. Please make sure it runs, and please let the code be elementary since this is an intro class, thanks. Here is the assignment.  Make a Tic-Tac-Toe game for 2 human players.  "The object of Tic Tac Toe is to get three in a row. You play on a three by three game board. The first player is known as X and the second is O. Players alternate placing Xs and Os on the game board until either oppent has three in a row or all nine squares are filled. X always goes first, and in the event that no one has three in a row, the stalemate is called a cat game." Source: http://web.cecs.pdx.edu/~bart/cs541-fall2001/homework/tictactoe-rules.html  ------------------------------------------------------------------------------ At minimum your program must be able to play one game of Tic-Tac-Toe between 2 human players.  The 'X' player should always go first, and players will alternate until either the board is filled, or a player wins. When the game reaches its conclusion, announce the winner of the game, or announce that the game is a draw.  Players must be able to win by any of the 8 possible winning conditions (3 rows, 3 columns, 2 diagonals)  Give the users clear, explicit instructions on how to make their moves.  Do not allow a player to place their symbol on a space that has already been used. If a user tries, ask for a different choice of space.  Before each move, print what the game board looks like (i.e. all X's and O's currently on the board, and all the empty spaces that have yet to be taken).  Expect that the user will give bad input, and do not allow the program to advance until good input is received. Do not allow your program to crash, end abruptly, or otherwise have unexpected results due to invalid input. 

Explanation / Answer

#include<isostream>

using namespace std;

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

int checkwin();

void board();

int main()

{

int player= 1,i,choice;

char mark;

do

{

board();

player=(player%2)?1:2;

cout <<"player" << player << " , enter a number: " ;

cin >> choice;

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

if ( choice == 1 && square[1] == '1' )

square[1] = mark;

else if (choice == 2 && square[2] == '2' )

square[2] = mark;

else if (choice == 3 && square[3] == '3' )

  square[3] = mark;

else if (choice == 4 && square[4] == '4' )

  square[4] = mark;

else if (choice == 5 && square[5] == '5' )

  square[5] = mark;

else if (choice == 6 && square[6] == '6' )

  square[6] = mark;

else if (choice == 7 && square[7] == '7' )

  square[7] = mark;

else if (choice == 8 && square[8] == '8' )

  square[8] = mark;

else if (choice == 9 && square[9] == '9' )

  square[9] = mark;

else

{

cout<<"invalid move";

player--;

cin.ignore();

cin.get();

}

i=checkwin();

player++;

}

while(i==-1);

board();

if(i==1)

cout<<"==>Player "<<---player<<"win";

else

cout<<"==Game draw";

cin.ignore();

cin.get();

return 0;

}

int checkwin()

{

if (square[1] == square[2] && square[2] == square[3])

return 1;

else if (square[4] == square[5] && square[5] == square[6] )

return 1;

else if (square[7] == square[8] && square[8] == square[9] )

return 1;

else if (square[1] == square[4] && square[4] == square[7] )

return 1;

else if (square[2] == square[5] && square[5] == square[8] )

return 1;

else if (square[3] == square[6] && square[6] == square[9] )

return 1;

else if (square[1] == square[5] && square[5] == square[9] )

return 1;

else if (square[3] == square[5] && square[5] == square[7] )

return 1;

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

return 0;

else

return -1;

}

void board()

{

system("cls");

cout<<" Tic Tac Toe ";

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

cout << endl;

cout << " | | " <<endl;

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

cout << " | | " <<endl;

cout << " | | " <<endl;

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

cout << " | | " <<endl;

cout << " | | " <<endl;

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

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

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote