'Correcting a Tic Tac Toe-Draw function
I am making a multiplayer tic tac toe game with a semi-graphical interface. I have made the code and most of it works.The only part of it that doesn't work is the draw function.
I do understand that I am using TurboC++,which is a highly out of date compiler,but the Indian education system follows only TurboC++,so I have to make my project in it.(The syllabus was changed to have Python instead of C++ recently but I happened to be in the last batch of students that will not be taught Python)
The problem is in the last part of the result() function. I was unable to find what was wrong with it.I have not used graphics.h because it is not in my syllabus.
result function alone:
struct mat //To store the positions and what is present in the 9 boxes
{
int x,y;char ch;
};
struct xo //To keep track of the match
{
int actp,actx,acty;
mat pos[3][3];
char flag;
void setup() //To create the boxes/bars
{
actp=1,actx=1,acty=1;
flag=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
pos[i][j].ch=0;
}
}
void result() //To find the result
{
char flage;
for(int i=0;i<3;i++) //Column win
{
if(pos[i][0].ch==pos[i][1].ch&&pos[i][1].ch==pos[i][2].ch)
flage=pos[i][0].ch;
}
for(i=0;i<3;i++) //Row win
{
if(pos[0][i].ch==pos[1][i].ch&&pos[1][i].ch==pos[2][i].ch)
flage=pos[0][i].ch;
}
if(pos[0][0].ch==pos[1][1].ch&&pos[1][1].ch==pos[2][2].ch) //Diagonal win
flage=pos[0][0].ch;
if(pos[0][2].ch==pos[1][1].ch&&pos[1][1].ch==pos[2][0].ch) //Other diagonal win
flage=pos[0][2].ch;
if(flage=='X')flag='1';
else if(flage=='O')flag='2';
else flag='0';
int chk=1;
for(i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{if(pos[i][j].ch=='0'){chk=0;gotoxy(3,15);cout<<chk;} }//Added cout statement for debugging
}
if(flag=='0'&&chk==0)flag='D';//I understand that the condition is supposed to be chk==1,but it works only if I have this condition
}
}a;
Here is the whole code,if necessary: https://drive.google.com/open?id=19WMexp3Hw_p9hO3qiYm0HRj-GGAJeaTr
A screenshot: https://i.stack.imgur.com/wGh7a.jpg
If I use the correct condition, the program says that the match is drawn just after 1 move.
With this wrong condition, the program works to a certain extent and is able to find winners but never declares a match drawn even if it happens.
Thanks for the help!!
Solution 1:[1]
I have corrected the errors, will elaborate soon. Thanks for the help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Karthick Ashwath |
