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

TIC-TAC-TOE Open the home page Your Assignment Your goal is to write a Tic-Tac-T

ID: 3882318 • Letter: T

Question

TIC-TAC-TOE Open the home page Your Assignment Your goal is to write a Tic-Tac-Toe game. You should set up the Tic-Tac-Toe boxes and record the alternating X's and O's with mouse clicks. When all 9 boxes are full, the game ends and the user should be prompted for another game. At each move, you should prompt the user for a mouse click, indicating whether it is X's or O's turn. You may assume that X always gets the first move. The program ends when the user enters a negative response to the endgame prompt. Note that each game you will draw exactly 9 objects (5 X's and 4 O's). Your program should not repeat the procedure for drawing an X or O more than necessary. That is, you should loop your drawing command rather than simply repeating code multiple times. You should use a proper while loop structure What you DON'T have to do: The user will choose all X and O locations; you are not expected to write a Tic-Tac-Toe playing program. You may assume that the user knows how to play the game. You are not responsible for tracking poor mouse clicks by the user, such as not clicking within a box or a user clicking all 9 X's and O's in the same box. You may assume that the game ends when 9 mouse clicks are recorded (presumably filling the 9 boxes). You should output a "Game Over" statement at the end of each game, but you are not responsible for determining who won.

Explanation / Answer

main.cpp

#include <iostream>

#include<stdio.h>

#include<stdlib.h>

#include <graphics.h>

using namespace std;

void boards()

{

settextstyle(10,0,6);

int wt=textwidth("Tic Tac Toe");

outtextxy((400-wt)/2,80,"Tic Tac Toe");

line(160,220,160,460);

line(160+1,220,160+1,460);

line(160-1,220,160-1,460);

line(80,300,320,300);

line(80,300+1,320,300+1);

line(80,300-1,320,300-1);

line(80,380,320,380);

line(80,380+1,320,380+1);

line(80,380-1,320,380-1);

line(240,220,240,460);

line(240+1,220,240+1,460);

line(240-1,220,240-1,460);

settextstyle(10,0,2);

int ht=textheight("Tic Tac Toe");

wt=textwidth("Tic Tac Toe");

outtextxy(400-wt-5,600-ht,"Tic Tac Toe");

}

void brd(int baari, int baari1)

{

settextstyle(10,0,6);

int wt=0,ht=0;

if(baari==1){

ht=textheight("Q");

wt=textwidth("Q");

}

else{

ht=textheight("O");

wt=textwidth("O");

}

int u=(80-wt)/2;

int v=(80-ht)/2;

switch(baari1)

{

case 1:

if(baari==1)

outtextxy(u+80,v+220,"Q");

else

outtextxy(u+80,v+220,"O");

break;

case 2:

if(baari==1)

outtextxy(u+160,v+220,"Q");

else

outtextxy(u+160,v+220,"O");

break;

case 3:

if(baari==1)

outtextxy(u+240,v+220,"Q");

else

outtextxy(u+240,v+220,"O");

break;

case 4:

if(baari==1)

outtextxy(u+80,v+300,"Q");

else

outtextxy(u+80,v+300,"O");

break;

case 5:

if(baari==1)

outtextxy(u+160,v+300,"Q");

else

outtextxy(u+160,v+300,"O");

break;

case 6:

if(baari==1)

outtextxy(u+240,v+300,"Q");

else

outtextxy(u+240,v+300,"O");

break;

case 7:

if(baari==1)

outtextxy(u+80,v+380,"Q");

else

outtextxy(u+80,v+380,"O");

break;

case 8:

if(baari==1)

outtextxy(u+160,v+380,"Q");

else

outtextxy(u+160,v+380,"O");

break;

case 9:

if(baari==1)

outtextxy(u+240,v+380,"Q");

else

outtextxy(u+240,v+380,"O");

break;

}

}

int functional(char *arr){

if((arr[0]==arr[4] && arr[0]==arr[8] && arr[0]=='Q') || (arr[2]==arr[4] && arr[2]==arr[6] && arr[2]=='Q'))

return 1;

else if((arr[0]==arr[4] && arr[0]==arr[8] && arr[0]=='O') || (arr[2]==arr[4] && arr[2]==arr[6] && arr[2]=='O'))

return 2;

if((arr[0]==arr[1] && arr[1]==arr[2] && arr[0]=='Q') || (arr[3]==arr[4] && arr[4]==arr[5] && arr[3]=='Q') || (arr[6]==arr[7] && arr[7]==arr[8] && arr[6]=='Q'))

return 1;

else if((arr[0]==arr[1] && arr[1]==arr[2] && arr[0]=='O') || (arr[3]==arr[4] && arr[4]==arr[5] && arr[3]=='O') || (arr[6]==arr[7] && arr[7]==arr[8] && arr[6]=='O'))

return 2;

if((arr[0]==arr[3] && arr[3]==arr[6] && arr[0]=='Q') || (arr[1]==arr[4] && arr[4]==arr[7] && arr[1]=='Q') || (arr[2]==arr[5] && arr[5]==arr[8] && arr[2]=='Q'))

return 1;

if((arr[0]==arr[3] && arr[3]==arr[6] && arr[0]=='O') || (arr[1]==arr[4] && arr[4]==arr[7] && arr[1]=='O') || (arr[2]==arr[5] && arr[5]==arr[8] && arr[2]=='O'))

return 2;

return 0;

}

void cmpMove(char *arr,int baari){

int arr1[8],baari1[8];

for(int p=0;p<8;p++){

arr1[p]=0;baari1[p]=-1;

}

for(int p=0;p<9;p++){

if(arr[p]=='O'){

arr1[p/3]++;

arr1[3+p%3]++;

if(p%4==0)

arr1[6]++;

if(p==2 || p==4 || p==6)

arr1[7]++;

}

else if(arr[p]==0){

baari1[p/3]=p;

baari1[3+p%3]=p;

if(p%4==0)

baari1[6]=p;

if(p==2 || p==4 || p==6)

baari1[7]=p;

}

}

for(int p=0;p<8;p++){

if(arr1[p]==2 && baari1[p]!=-1){

brd(0,baari1[p]+1);

arr[baari1[p]]='O';

return;

}

baari1[p]=-1;arr1[p]=0;

}

for(int p=0;p<9;p++){

if(arr[p]=='Q'){

arr1[p/3]++;

arr1[3+p%3]++;

if(p%4==0)

arr1[6]++;

if(p==2 || p==4 || p==6)

arr1[7]++;

}

else if(arr[p]==0){

baari1[p/3]=p;

baari1[3+p%3]=p;

if(p%4==0)

baari1[6]=p;

if(p==2 || p==4 || p==6)

baari1[7]=p;

}

}

for(int p=0;p<8;p++){

if(arr1[p]==2 && baari1[p]!=-1){

brd(0,baari1[p]+1);

arr[baari1[p]]='O';

return;

}

baari1[p]=-1;arr1[p]=0;

}

if(arr[4]==0){

brd(0,5);

arr[4]='O';

return;

}

if(baari==1){

brd(0,1);

arr[0]='O';

return;

}

if(baari==3){

if(arr[4]=='Q'){

brd(0,3);

arr[2]='O';

return;

}

baari1[0]=baari1[1]=-1;int j=0;

for(int p=0;p<9;p++)

if(arr[p]=='Q')

baari1[j++]=p;

if((baari1[0]==3 && baari1[1]==5) || (baari1[0]==2 && baari1[1]==7)){

brd(0,1);

arr[0]='O';

return;

}

if(baari1[0]+baari1[1]==8){

brd(0,2);

arr[1]='O';

return;

}

if(baari1[0]==0 || baari1[0]==1){

if(baari1[1]==5 || baari1[1]==8){

brd(0,3);

arr[2]='O';

return;

}

else if(baari1[1]==3 || baari1[1]==6){

brd(0,1);

arr[0]='O';

return;

}

else{

brd(0,7);

arr[6]='O';

return;

}

}

else if(baari1[0]==1 || baari1[0]==2){

if(baari1[1]==3 || baari1[1]==6){

brd(0,1);

arr[0]='O';

return;

}

else{

brd(0,9);

arr[8]='O';

return;

}

}

else if(baari1[0]==3){

brd(0,7);

arr[6]='O';

return;

}

brd(0,9);

arr[8]='O';

return;

}

for(int p=0;p<9;p++){

if(arr[p]==0){

brd(0,1+p);

arr[p]='O';

return;

}

}

}

int main()

{

initwindow(400,600,"Tic Tac Toe");

boards();

int u=0;

POINT posit;

int baari=0;

char arr2[9]={0};

for(int p=0;p<9;p++)

arr2[p]=0;

settextstyle(10,0,3);

int wt=textwidth("Your Turn !!");

outtextxy((400-wt)/2,500,"Your Turn !!");

while(u==0 && baari<=9)

{

if(GetAsyncKeyState(VK_LBUTTON))

{

GetCursorPos(&posit);

long Q=mousex();

long P=mousey();

int mv=0;

if(baari%2==0){

if(Q>80 && Q<160 && P>220 && P<300 && arr2[0]==0)

{

brd(1,1);

mv=1;

arr2[0]='Q';

}

else if(Q>160 && Q<240 && P>220 && P<300 && arr2[1]==0)

{

brd(1,2);

mv=1;

arr2[1]='Q';

}

else if(Q>240 && Q<320 && P>220 && P<300 && arr2[2]==0)

{

brd(1,3);

mv=1;

arr2[2]='Q';

}

else if(Q>80 && Q<160 && P>300 && P<380 && arr2[3]==0)

{

brd(1,4);

mv=1;

arr2[3]='Q';

}

else if(Q>160 && Q<240 && P>300 && P<380 && arr2[4]==0)

{

brd(1,5);

mv=1;

arr2[4]='Q';

}

else if(Q>240 && Q<320 && P>300 && P<380 && arr2[5]==0)

{

brd(1,6);

mv=1;

arr2[5]='Q';

}

else if(Q>80 && Q<160 && P>380 && P<460 && arr2[6]==0)

{

brd(1,7);

mv=1;

arr2[6]='Q';

}

else if(Q>160 && Q<240 && P>380 && P<460 && arr2[7]==0)

{

brd(1,8);

mv=1;

arr2[7]='Q';

}

else if(Q>240 && Q<320 && P>380 && P<460 && arr2[8]==0)

{

brd(1,9);

arr2[8]='Q';

mv=1;

}

if(mv){

baari++;

u=functional(arr2);

delay(200);

if(u!=0 || baari==9)

break;

cmpMove(arr2,baari);

baari++;

u=functional(arr2);

}

}

}

}

cout<<"Thank You !!";

if(u==1){

settextstyle(10,0,3);

int wt=textwidth("You Wins !!");

outtextxy((400-wt)/2,500,"You Wins !!");

}

else if(u==2){

settextstyle(10,0,3);

int wt=textwidth("Computer Wins !!");

outtextxy((400-wt)/2,500,"Computer Wins !!");

}

else if(u==0){

settextstyle(10,0,3);

int wt=textwidth("Its arr Draw !!");

outtextxy((400-wt)/2,500,"Its arr Draw !!");

}

getch();

return 0;

}

Rate an upvote....Thankyou.

Hope this helps.....