#include <iostream>
#include <cstdlib>
#include <conio.h>
#include "GUI.cpp"
using namespace std;
void display( char board[3][3][3][3] )
{
for( int R=0; R<3; R++ )
{
for( int r=0; r<3; r++ )
{
for( int C=0; C<3; C++ )
{
for( int c=0; c<3; c++ )
{
setColor(RGBI(1,0,0,1),RGBI(1,1,0,1));
cout << board[R][C][r][c];
setColor(RGBI(1,1,1,0),RGBI(0,0,0,0));
}
cout <<" ";
}
cout << endl;
}
cout << endl;
}
setColor(RGBI(1,1,1,0),RGBI(0,0,0,0));
cout <<"Click on ";
setColor(RGBI(1,0,0,1),RGBI(1,1,0,1));
cout <<"highlighted cell";
setColor(RGBI(1,1,1,0),RGBI(0,0,0,0));
cout <<" above using the [Mouse]"<< endl;
}
bool mouseToBoardCoord( MouseClick const& m, int& R, int& C, int& r, int& c )
{
if( (0 <= m.x) && (m.x < 3) && (0 <= m.y) && (m.y < 3) )
{
R = 0;
C = 0;
if ( m.x == 0 )c = 0;
else if( m.x == 1 )c = 1;
else if( m.x == 2 )c = 2;
if ( m.y == 0 )r = 0;
else if( m.y == 1 )r = 1;
else if( m.y == 2 )r = 2;
}
else if( (4 <= m.x) && (m.x < 7) && (0 <= m.y) && (m.y < 3) )
{
R = 0;
C = 1;
if ( m.x == 4 )c = 0;
else if( m.x == 5 )c = 1;
else if( m.x == 6 )c = 2;
if ( m.y == 0 )r = 0;
else if( m.y == 1 )r = 1;
else if( m.y == 2 )r = 2;
}
else if( (8 <= m.x) && (m.x < 11) && (0 <= m.y) && (m.y < 3) )
{
R = 0;
C = 2;
if ( m.x == 8 )c = 0;
else if( m.x == 9 )c = 1;
else if( m.x == 10 )c = 2;
if ( m.y == 0 )r = 0;
else if( m.y == 1 )r = 1;
else if( m.y == 2 )r = 2;
}
else if( (0 <= m.x) && (m.x < 3) && (4 <= m.y) && (m.y < 7) )
{
R = 1;
C = 0;
if ( m.x == 0 )c = 0;
else if( m.x == 1 )c = 1;
else if( m.x == 2 )c = 2;
if ( m.y == 4 )r = 0;
else if( m.y == 5 )r = 1;
else if( m.y == 6 )r = 2;
}
else if( (4 <= m.x) && (m.x < 7) && (4 <= m.y) && (m.y < 7) )
{
R = 1;
C = 1;
if ( m.x == 4 )c = 0;
else if( m.x == 5 )c = 1;
else if( m.x == 6 )c = 2;
if ( m.y == 4 )r = 0;
else if( m.y == 5 )r = 1;
else if( m.y == 6 )r = 2;
}
else if( (8 <= m.x) && (m.x < 11) && (4 <= m.y) && (m.y < 7) )
{
R = 1;
C = 2;
if ( m.x == 8 )c = 0;
else if( m.x == 9 )c = 1;
else if( m.x == 10 )c = 2;
if ( m.y == 4 )r = 0;
else if( m.y == 5 )r = 1;
else if( m.y == 6 )r = 2;
}
else if( (0 <= m.x) && (m.x < 3) && (8 <= m.y) && (m.y < 11) )
{
R = 2;
C = 0;
if ( m.x == 0 )c = 0;
else if( m.x == 1 )c = 1;
else if( m.x == 2 )c = 2;
if ( m.y == 8 )r = 0;
else if( m.y == 9 )r = 1;
else if( m.y == 10 )r = 2;
}
else if( (4 <= m.x) && (m.x < 7) && (8 <= m.y) && (m.y < 11) )
{
R = 2;
C = 1;
if ( m.x == 4 )c = 0;
else if( m.x == 5 )c = 1;
else if( m.x == 6 )c = 2;
if ( m.y == 8 )r = 0;
else if( m.y == 9 )r = 1;
else if( m.y == 10 )r = 2;
}
else if( (8 <= m.x) && (m.x < 11) && (8 <= m.y) && (m.y < 11) )
{
R = 2;
C = 2;
if ( m.x == 8 )c = 0;
else if( m.x == 9 )c = 1;
else if( m.x == 10 )c = 2;
if ( m.y == 8 )r = 0;
else if( m.y == 9 )r = 1;
else if( m.y == 10 )r = 2;
}
else
{
return false;
}
return true;
}
int main()
{
char board[3][3][3][3] = {};
bool FirstTime = true;
int prev_r;
int prev_c;
while(true)
{
system("CLS");
display(board);
MouseClick m = getMouseClick();
int R,C,r,c;
if( mouseToBoardCoord(m,R,C,r,c) )
{
if (FirstTime)
{
board[R][C][r][c] = 'X';
FirstTime = false;
prev_r = r;
prev_c = c;
}
else
{
if ( R == prev_r && C == prev_c)
{
board[R][C][r][c] = 'o';
prev_r = r;
prev_c = c; }
}
}
}
}
i dont knw how to code for the alternative X and O and for the win / lose/ tie
hope to get ur answer as soon as possible and some explanation on it. really very appreciate