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

Write a class encapsulating the concept of a tic-tac-toe game as follows: Two pl

ID: 3547334 • Letter: W

Question

Write a class encapsulating the concept of a tic-tac-toe game as follows:

Two players will be playing, player 1 and player 2.

The board is represented by an array of 9 integer elements: elements at indexes 0, 1, and 2 represent the first row; elements at indexes 3, 4, and 5 represent the second row; elements at indexes 6, 7, and 8 represent the third row.

The value 0 in the array indicates that this space is available; the value 1 indicates the space is occupied by player 1; and the value 2 indicates that this space is occupied by player 2.

In the main method of your client class, your program will simulate a tic-tac-toe game from the command line (or a JOptionPane dialog box), doing the following:

In your TicTacToe class, you will need to code the following methods:

Explanation / Answer


#include <iostream.h>

#include <conio.h>

#include<process.h>

#include<stdlib.h>

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

int checkwin();//FUNTION FOR RESULT//

void board();//FUNCTION TO DRAW THE BOARD//

void guide();//FUNCTION FOR USER GUIDE//

main()

{

int player = 1,i,choice,option;

char mark,choice1;

do

{

cout<<"1.ENTER THE GAME "<<endl<<"2.USER GUIDE"<<endl<<"3.EXIT";

cin>>option;

switch(option)

{

case 1:

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--;

getch();

}

i=checkwin();

player++;

}while(i==-1);

board();

if(i==1)

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

else


cout<<"==>Game draw";break;

case 2:guide();

break;


case 3:exit(0);break;

default:cout<<"Wrong Choice";

}

cout<<" Do you want to continue";

cin>>choice1;

}while(choice1=='y'||choice1=='Y');

getch();

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()

{

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;

}


void guide()

{



cout<<" The TIC-TAC-TOE games board is similar to that of # key or it looks something like this : _|_|_"<<endl<<" "<<

"_|_|_ "<<endl<<" "<<

" | | ";

cout<<" In this gaps turn by turn crosses and zeroes have to be filled up";

cout<<"TIC-TAC-TOE is basically a X and O game in which the player has to complete 3";

cout<<" crosses or zeroe which may be horizontal vertical or even diagonal x|x|x"<<

" -|-|-"<<

" _|_|_"<<

" | | ";

cout<<endl<<endl<<"OR x|_|_ "<<endl<<

" -|x|_ "<<

" -|-|x";


cout<<endl<<endl<<"OR x|_|_"<<endl<<

" x|_|_"<<endl<<

" x| | ";;


}

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