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

www.ee.nm. Calculus 2n... (21) Related Write a tic-tac-toe program. Your program

ID: 3732581 • Letter: W

Question

www.ee.nm. Calculus 2n... (21) Related Write a tic-tac-toe program. Your program should allow two players to play against each other or one player plays against the computer. At the beginning of each game you should provide the user with a selection between the two options or to quit the game. The user should enter the row and the column where he/she wants to play in the following format x,y, where x and y are the coordinates. You should check if: 1. an invalid coordinate (either outside the board or a coordinate already played) 2. an invalid format After each move you should display the current status of the board and either ask the next player to make a move or declare the winner or a tie. The board may look something like lolxl After a winner or a tie is declared, provide the statistics of how many wins, loses or ties each player got: player 1 vs player 2 /computer, then the game starts over ind the user is presented with the menu again. " If the user chooses to play against the computer use a random number generator to choose location to make the next move. You should check and make sure that that position is vald Report Write a report describing your entire process. Your report should include: Introduction: describe in your own words what the projeet is about . Program discusion: describe how you will go about structuring your program Results present the results of your program with test data and discuss who it worked and some analysis of how efficient it is . Conclusion: provide some remarks regarding the project and what you learned Extra Credit You can earn up to 10% extra credit if you add intelligence to the program as you the user plays against the computer. For example, the computer realizes if the human is about to win and tries to block his winning move.

Explanation / Answer

Please find the working code for the Tic Tac Toe Game.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

TicTacToe.cpp

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/******************************************************************************

Online C++ Compiler.

Code, Compile, Run and Debug C++ program online.

Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

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

int checkwin();

void board();

int main()

{

int player = 1,i,choice;

int selection;

bool cont = true;

  

char mark;

cout<<" Welcome To Tic Tac Toe ";

cout<<" Select the menu item ";

cout<<" 1. Player vs Player ";

cout<<" 2. Player vs Computer ";

cout<<" 3. Quit ";

cin>>selection;

if(selection==1){

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

}

else if(selection==2){

cout<<"Sorry..! this selection is currently unavailable";

}

else if(selection==3){

cont = false;

i=10;

// continue;

}

if(cont = false){

  

do

{

board();

// do{

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

cout << "Player " << player << ", enter enter the coordinate between 1 to 9: ";

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 co ordinate / the coordinate is already occupied ";

player--;

cin.ignore();

cin.get();

}

i=checkwin();

player++;

// }while(cont);

}while(i==-1);

}

if(i!=10){

board();  

}

if(i==1)

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

else if(i==10){

cout<<"Exiting the Game..";

}

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;

  

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

1. This project is about the game developement which is an interactive game between 2 palyers or a player and a computer.

The logic behind the program is very easy. Here I have used do while loop to iterate throught the board and another method to make the board.

After the game , based on the calculations, i am deciding which player won.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////