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

Hello, I need help with writing a basic tic tac toe game in C++ under several co

ID: 3827993 • Letter: H

Question

Hello, I need help with writing a basic tic tac toe game in C++ under several conditions.

1. You cannot use arrays or vectors for this project.

2. Ask the user to flip a coin to determine who (either the user or the computer) should play first.

3. Ask the user (or the first player) to choose their own marker, between ‘X’ and ‘O’.

4. Create separate .h and .cpp files for each function.

5. Create a function to draw the tic tac toe board

6. Create a function to check the input is within the valid range

7. Create a function to check the designated space is available

8. Create a function to check if we have a winner (in which case the program should end)

9. Create a main function that will call all these functions (and more if necessary) to play tic tac toe

10.Make sure to display the position that the computer chooses after each of the computer’s turns, as shown in the example on the previous page.

I have included some of the example screenshots! hope it helps.

And thank you very much!

CAUsersWshcieeWDocumentsAVisual Studio 2015 ProjectsWProjectivDebugNProjecti.exe Welcome to the game of Tic Tac Toe Below you will indicate your position (1 9) as shown in the table Tic Tac Toe User (x) Computer (0) 1 2 3 4 I 5 I 6 7 8 9 Enter your position:

Explanation / Answer

This is the basic TicTacToe program in c++. Hope it meets all your requirements and the it will help you understand how to make the code on yourself.

#include <iostream>

#include <conio.h>

#include <windows.h>

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

void gameBoard();

int checkWinner();

int scoreA=0;

int scoreB=0;

int z;

bool overallwin(int, int, int);

int main() // main function

{

int i;

int j;

int choice;

for(j=48; j < 58; j++)

{

square[j-48]=j;

}

bool shutoff = false; // if the game will continue or not

std::cout<<"do you want best out of 3 or 5?:";

std::cin>>z;

int mark;

int player;

while(shutoff == false)

{

for(j=48; j < 58; j++)

{

square[j-48]=j;

}

player=1,i,choice;

system ("cls");

}

if(z==3) // if player's choice was best out of 3

{

do

{ gameBoard();

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

std::cout<<"Player"<<player<<", make your choice: ";

std::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

{

std::cout<<"Dude, invalid move!!!";

player--;

getch();

}

player++;

i=checkWinner();

}

while(i==0);

{

gameBoard();

}

if(i==1)

{

std::cout<<"Winner is, Player:1";

scoreA++;

getch();

system("cls");

for(j=48; j < 58; j++)

{

square[j-48]=j;

}

gameBoard();

}

else if (i==2)

{

std::cout<<"Winner is, Player:2";

scoreB++;

getch();

system("cls");

for(j=48; j < 58; j++)

{

square[j-48]=j;

}

gameBoard();

}

else

{

std::cout<<"It's a draw!";

getch();

system("cls");

for(j=48; j < 58; j++)

{

square[j-48]=j;

}

gameBoard();

}

shutoff = overallwin(scoreA, scoreB, z);

}

else if(z==5)

{

do

{ gameBoard();

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

std::cout<<"Player"<<player<<", make your choice: ";

std::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

{

std::cout<<"Dude, invalid move!!!";

player--;

getch();

}

player++;

i=checkWinner();

}

while(i==0);

{

gameBoard();

}

if(i==1)

{

std::cout<<"Winner is, Player:1";

scoreA++;

getch();

system("cls");

for(j=48; j < 58; j++)

{

square[j-48]=j;

}

gameBoard();

}

else if (i==2)

{

std::cout<<"Winner is, Player:2";

scoreB++;

getch();

system("cls");

for(j=48; j < 58; j++)

{

square[j-48]=j;

}

gameBoard();

}

else

{

std::cout<<"It's a draw!";

getch();

system("cls");

for(j=48; j < 58; j++)

{

square[j-48]=j;

}

gameBoard();

}

shutoff = overallwin(scoreA, scoreB, z);

}

// if player decides to enter a different number

//There will be a bug if you enter a letter

else if(z!=3,5)

{

std::cout<<"Enter a 3 or a 5"<<std::endl;

std::cout<<"Press any key to go back";

getch();

system("cls");

main();

}

}

return 0;

}

bool overallwin(int scoreA, int scoreB, int z)

if(z==3)

{

if (scoreB==2)

{

std::cout<<"overall winner is player2";

getch();

system ("cls");

std::cout<<"Thanks For Playing!!!!";

getch();

return true;

}

else if (scoreA==2)

{

std::cout<<"overall winner is player1";

getch();

system ("cls");

std::cout<<"Thanks For Playing!!!!";

getch();

return true;

}

return false;

}

if(z==5)

{

if (scoreB==3)

{

std::cout<<"overall winner is player2";

getch();

system("cls");

std::cout<<"Thanks for Playing!!!!";

getch();

return true;

}

else if (scoreA==3)

{

std::cout<<"overall winner is player1";

getch();

system("cls");

std::cout<<"Thanks for Playing!!!!";

getch();

return true;

}

return false;

}

}

int checkWinner()

{

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

{

return 1;

}

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

{

return 1;

}

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

{

return 1;

}

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

{

return 1;

}

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

{

return 1;

}

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

{

return 1;

}

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

{

return 1;

}

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

{

return 1;

}

else if(square[1] == square[2] && square[2] == square[3] && square[1]=='O')

{

return 2;

}

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

{

return 2;

}

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

{

return 2;

}

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

{

return 2;

}

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

{

return 2;

}

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

{

return 2;

}

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

{

return 2;

}

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

{

return 2;

}

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

}

else

{

return 0;

}

}

//This function draws the gameboard

void gameBoard()

{ system("cls");

std::cout<<"Tic Tac Toe, Best out of "<< z <<std::endl;

std::cout<<"Programmed by Shaka Kanenobu"<<std::endl;

std::cout<<"Player 1-X: Player 2-O"<<std::endl;

std::cout<<std::endl;

std::cout<<"score:"<<scoreA<<" : "<<scoreB<<std::endl;

std::cout<<" | | "<<std::endl;

std::cout<<" "<<square[1]<<" "<<square[2]<<" "<<square[3]<<" "<<std::endl;

std::cout<<"____|____|____"<<std::endl;

std::cout<<" | | "<<std::endl;

std::cout<<" "<<square[4]<<" "<<square[5]<<" "<<square[6]<<" "<<std::endl;

std::cout<<"____|____|____"<<std::endl;

std::cout<<" | | "<<std::endl;

std::cout<<" "<<square[7]<<" "<<square[8]<<" "<<square[9]<<" "<<std::endl;

std::cout<<" | | "<<std::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