Making of One Player Game Tick Tack Toe Game
Hello There,
This article is for those who do not have any knowledge of Machine learning
We are here to see how a single-player tick tack toe game can be made using a non-machine learning algorithm.
Many machine learning algorithm involving Reinforcement learning can do solve this problem. But as per my little experience, I can roughly say that the problem can be solved easily and efficiently using simple logic.
let us consider an array Board(board[9]) or The Playground be represented by an Array.
We define the term Priorities.
At The Initial Movement or in some cases of Conflict we are required to see The Priorities.
The Priorities of The Game are:-
1: Middle Block (board[4]) has The Highest Priority
2: Corner Blocks has the second-highest priority that is….board[0,2,6,8]
3: The other Block is having the least Priority
You can see my code at:-
See the YouTube demonstration follows:-
There is a trick in my code. If you understand my code then you have to find a case where a computer can fail(I left a case intentionally!!!!).
I would appreciate if you mail me the case left at:-
rjtjdm@gmail.com
What if you want to write it using a machine learning algorithm than follow the below link.
Happy Exploring!!
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<dos.h>
#include<stdio.h>
union REGS in,out;
int posx,posy,mstate;
int chance=1;
int pwin=-1;
int board[8];
void initgraphics()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
cout<<"Graphics error Ohhhh:";
cout<<"Press any key to halt:";
getch();
exit(1); /* return with error code */
}
}
void initMouse()
{
in.x.ax=1;
int86(51,&in,&out);
}
void initMouseClick()
{
in.x.ax=3;
int86(51,&in,&out);
mstate=out.x.bx;
posx=out.x.cx;
posy=out.x.dx;
// cout<<"Position x:"<<posx<<","<<posy<<","<<mstate<<"\r";
}
void init()
{
for(int i=0;i<9;i++)
{
board[i]=-1;
}
chance=1;
pwin=-1;
outtextxy(250,5,"Tick Tack Toe");
outtextxy(200,15,"(Press any Key To Restart)");
line(0,30,getmaxx(),30);
}
void makeBoard()
{
line(200, 50,200, 350);
line(300,50,300,350);
line(100,150,400,150);
line(100,250,400,250);
}
void updateBoard(int place)
{ if(board[place]!=-1)
{
return;
}
if(chance==1)
{
switch(place)
{
case 0:
line(115,65,185,135);
line(115,135,185,65);
break;
case 1:
line(215,65,285,135);
line(215,135,285,65);
break;
case 2:
line(315,65,385,135);
line(315,135,385,65);
break;
case 3:
line(115,165,185,235);
line(115,235,185,165);
break;
case 4:
line(215,165,285,235);
line(215,235,285,165);
break;
case 5:
line(315,165,385,235);
line(315,235,385,165);
break;
case 6:
line(115,265,185,335);
line(115,335,185,265);
break;
case 7:
line(215,265,285,335);
line(215,335,285,265);
break;
case 8:
line(315,265,385,335);
line(315,335,385,265);
break;
}
board[place]=1;
}
else
{
switch(place)
{
case 0:
circle(150,100,35);
break;
case 1:
circle(250,100,35);
break;
case 2:
circle(350,100,35);
break;
case 3:
circle(150,200,35);
break;
case 4:
circle(250,200,35);
break;
case 5:
circle(350,200,35);
break;
case 6:
circle(150,300,35);
break;
case 7:
circle(250,300,35);
break;
case 8:
circle(350,300,35);
break;
}
board[place]=0;
}
chance*=-1;
}
void playerMove()
{
if(posy>50&&posy<150)
{
if(posx>100&&posx<200&&mstate==1)
{
updateBoard(0);
}
if(posx>200&&posx<300&&mstate==1)
{
updateBoard(1);
}
if(posx>300&&posx<400&&mstate==1)
{
updateBoard(2);
}
}
else if(posy>150&&posy<250)
{
if(posx>100&&posx<200&&mstate==1)
{
updateBoard(3);
}
if(posx>200&&posx<300&&mstate==1)
{
updateBoard(4);
}
if(posx>300&&posx<400&&mstate==1)
{
updateBoard(5);
}
}
else if(posy>250&&posy<350)
{
if(posx>100&&posx<200&&mstate==1)
{
updateBoard(6);
}
if(posx>200&&posx<300&&mstate==1)
{
updateBoard(7);
}
if(posx>300&&posx<400&&mstate==1)
{
updateBoard(8);
}
}
}
int winc()
{
if(((board[1]==0&&board[2]==0)||(board[3]==0&&board[6]==0)||(board[4]==0&&board[8]==0))&&board[0]==-1)
{
updateBoard(0);
return 1;
}
if(((board[0]==0&&board[2]==0)||(board[4]==0&&board[7]==0))&&board[1]==-1)
{
updateBoard(1);
return 1;
}
if(((board[0]==0&&board[1]==0)||(board[4]==0&&board[6]==0)||(board[5]==0&&board[8]==0))&&board[2]==-1)
{
updateBoard(2);
return 1;
}
if(((board[0]==0&&board[6]==0)||(board[4]==0&&board[5]==0))&&board[3]==-1)
{
updateBoard(3);
return 1;
}
if(((board[0]==0&&board[8]==0)||(board[1]==0&&board[7]==0)||(board[2]==0&&board[6]==0)||(board[3]==0&&board[5]==0))&&board[4]==-1)
{
updateBoard(4);
return 1;
}
if(((board[3]==0&&board[4]==0)||(board[2]==0&&board[8]==0))&&board[5]==-1)
{
updateBoard(5);
return 1;
}
if(((board[0]==0&&board[3]==0)||(board[2]==0&&board[4]==0)||(board[7]==0&&board[8]==0))&&board[6]==-1)
{
updateBoard(6);
return 1;
}
if(((board[6]==0&&board[8]==0)||(board[1]==0&&board[4]==0))&&board[7]==-1)
{
updateBoard(7);
return 1;
}
if(((board[0]==0&&board[4]==0)||(board[2]==0&&board[5]==0)||(board[6]==0&&board[7]==0))&&board[8]==-1)
{
updateBoard(8);
return 1;
}
return 0;
}
int tiec()
{
if(((board[1]==1&&board[2]==1)||(board[3]==1&&board[6]==1)||(board[4]==1&&board[8]==1))&&board[0]==-1)
{
updateBoard(0);
return 1;
}
if(((board[0]==1&&board[2]==1)||(board[4]==1&&board[7]==1))&&board[1]==-1)
{
updateBoard(1);
return 1;
}
if(((board[0]==1&&board[1]==1)||(board[4]==1&&board[6]==1)||(board[5]==1&&board[8]==1))&&board[2]==-1)
{
updateBoard(2);
return 1;
}
if(((board[0]==1&&board[6]==1)||(board[4]==1&&board[5]==1))&&board[3]==-1)
{
updateBoard(3);
return 1;
}
if(((board[0]==0&&board[8]==0)||(board[1]==0&&board[7]==0)||(board[2]==0&&board[6]==0)||(board[3]==0&&board[5]==0))&&board[4]==-1)
{
updateBoard(4);
return 1;
}
if(((board[3]==1&&board[4]==1)||(board[2]==1&&board[8]==1))&&board[5]==-1)
{
updateBoard(5);
return 1;
}
if(((board[0]==1&&board[3]==1)||(board[2]==1&&board[4]==1)||(board[7]==1&&board[8]==1))&&board[6]==-1)
{
updateBoard(6);
return 1;
}
if(((board[6]==1&&board[8]==1)||(board[1]==1&&board[4]==1))&&board[7]==-1)
{
updateBoard(7);
return 1;
}
if(((board[0]==1&&board[4]==1)||(board[2]==1&&board[5]==1)||(board[6]==1&&board[7]==1))&&board[8]==-1)
{
updateBoard(8);
return 1;
}
return 0;
}
void caseot()
{
int prior;
if(board[4]==-1)
{
prior=4;
}
else if((board[0]==1&&board[2]==1)||(board[0]==1&&board[6]==1)||(board[0]==1&&board[8]==1))
{
if(board[1]==-1)
{
prior=1;
}
else if(board[3]==-1)
{
prior=3;
}
else if(board[5]==-1)
{
prior=5;
}
else if(board[7]==-1)
{
prior=7;
}
}
else if(board[0]==-1)
{
prior=0;
}
else if(board[2]==-1)
{
prior=2;
}
else if(board[6]==-1)
{
prior=6;
}
else if(board[6]==-1)
{
prior=6;
}
else if(board[1]==-1)
{
prior=1;
}
else if(board[3]==-1)
{
prior=3;
}
else if(board[5]==-1)
{
prior=5;
}
else if(board[7]==-1)
{
prior=7;
}
updateBoard(prior);
}
void compMove()
{
if(winc())
{
return;
}
if(tiec())
{
return;
}
caseot();
}
void result()
{
for(int i=0;i<2;i++)
{
if((board[0]==i&&board[1]==i&&board[2]==i)||(board[3]==i&&board[4]==i&&board[5]==i)||(board[6]==i&&board[7]==i&&board[8]==i)||(board[0]==i&&board[3]==i&&board[6]==i)||(board[1]==i&&board[4]==i&&board[7]==i)||(board[2]==i&&board[5]==i&&board[8]==i)||(board[0]==i&&board[4]==i&&board[8]==i)||(board[2]==i&&board[4]==i&&board[6]==i))
{
pwin=i;
}
}
if(pwin==0)
{
outtextxy(0,400,"Computer Win Press any key To restart and Press Esc To Exit ");
}
else if(pwin==1)
{
outtextxy(0,400,"Player Win Press any key To restart and Press Esc To Exit");
}
int cstage=0;
for(i=0;i<9;i++)
{
if(board[i]!=-1)
{
cstage++;
}
}
if(cstage==9)
{
outtextxy(0,400,"Great !! Tie Between You and Computer press any key to restart and Esc to Exit");
}
}
void main()
{
clrscr();
initgraphics();
initMouse();
int lstate=0;
while(1)
{ cleardevice();
init();
makeBoard();
do
{
initMouseClick();
if(chance==1)
{
playerMove();
}
else
{
compMove();
}
result();
if(pwin==0||pwin==1)
{
break;
}
}
while(!kbhit());
fflush(stdin);
lstate=getch();
if(lstate==27)
{
break;
}
}
closegraph();
}