1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
void computer_move()
{
cls();
int comp_col[9] = {1,0,0,2,2,0,2,1,2};
int comp_row[9] = {1,0,2,0,2,1,1,2,1};
int cols = 0;
int rows = 0;
int up = 0;
bool found = false;
string temp;
//check for O winner
while(!found && up<8)
{
cols = comp_col[up];
rows = comp_row[up];
if(validate(rows,cols))
{
TicTac[rows][cols] = "O";
temp = Check("O",2);
if(temp == "winner")
{
found = true;
player ="X";
}
}
else
{
TicTac[rows][cols] = " ";
up++;
}
}
//reset
cols = 0;
rows = 0;
up = 0;
//check for X winner
while(!found && up<8)
{
cols = comp_col[up];
rows = comp_row[up];
if(validate(rows,cols))
{
TicTac[rows][cols] = "X";
temp = Check("X",2);
if(temp == "winner")
{
TicTac[rows][cols] = "O";
player ="X";
found = true;
}
}
else
{
TicTac[rows][cols] = " ";
up++;
}
}
cols = 0;
rows = 0;
up = 0;
//best move if no win can be found
while(!found)
{
cols = comp_col[up];
rows = comp_row[up];
if(validate(rows,cols))
{
TicTac[rows][cols] = "O";
found = true;
}
else{up++;}
}
}
|