S
sabirah
My code does not seem to be working properly...
The game is b/w a human and the computer. The computer always goes
first. The computer chooses the first corner in the first turn. in
the comp's 2nd turn, it chooses the opposite corner if possible, if
that corner is not availabe, the computer chooses randomly. In the
computers next turns...it does the following:
1. checks for a winning position
else
2. blocks human from a winning position
else
3. randomly chooses a position
The code is:
//Tic-Tac-Toe program
//
// The game board is a 3x3 array of ints
// board[3][3]:
// board[0][0] is upper left corner
// board[0][1] is top row, middle
// ...
// board[2][2] is bottom right
// 0 means the spot is empty
// 1 means 'X' has the spot
// 2 means 'O' has the spot
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
// drawpos will draw the right character to the screen, depending
// on the value of the parameter val.
// if val is 0, the spot is empty to draw a blank
// if val is 1, the spot belongs to player X, print 'X'
// if val is 2, the spot belongs to player O, print 'O'
void drawpos( int val ) {
if (val==0)
cout << " "; // val is 0, so draw a space
else if (val==1)
cout << "X"; // val is 1, so draw a 'X'
else if (val==2)
cout << "O"; // val is 2, so draw a 'O'
}
// drawrow gets an array of 3 ints and draw a
// row of a tic-tac-toe game using the array to determine
// what should go in each of the three squares.
//
// draws a row of the board
void drawrow( int row[3] ) {
cout << " "; // indent a space
drawpos(row[0]); // draw the leftmost spot
cout << " | "; // draw the vertial bar
drawpos(row[1]); // draw the middle
cout << " | "; // draw the vertial bar
drawpos(row[2]); // draw the rightmost spot
cout << endl; // newline (end of the line)
}
// drawgame will print out a tic-tac-toe game board
//
// the output looks something like this:
//
// | |
// ---|---|---
// X | | O
// ---|---|---
// X | O |
//
//
// the top left corner is board[0][0]
// the top middle is board[0][1]
// the bottom right corner is board[2][2]
void drawgame( int board[3][3] ) {
drawrow(board[0]); // draws the top row
cout << "---|---|---" << endl;
drawrow(board[1]); // draws the middlerow
cout << "---|---|---" << endl;
drawrow(board[2]); // draws the bottom row
}
bool checkPosition ( int board [3][3], int row, int col )
{
if ( ( row > 2 ) || ( row < 0 ) || ( col > 2 ) || ( col < 0 ) // out
of range test
|| ( board [ row ][ col ] != 0 ) ) //position is occupied
return false;
else return true;
}
bool won ( int board [3][3], int player )
{
int i, j;
// ROW TEST
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
if ( board [j] == player )
continue;
else
break;
if ( j == 3 )
return true;
}
// COLUMN TEST
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
if ( board [j] == player )
continue;
else
break;
if ( j == 3 )
return true;
}
//DIAGONAL TEST
if ( ( ( board[0][0] == player ) && ( board[1][1] == player ) &&
( board[2][2] == player ) ) || ( ( board[0][2] == player ) &&
( board[1][1] == player ) && ( board[2][0] == player ) ) )
return true;
return false;
}
void main ()
{
int board [3][3] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int turn = 1;
int flag = 0;
int row, col, player;
bool pos, testWin = 0;
drawgame ( board );
cout << "\nPlayer 1 is X............Computer is 0" << endl;
cout << "\nComputer goes first." << endl;
do
{
if ( ( turn % 2 ) == 1 ) // computer's turn
{
player = 2;
if ( turn == 1 ) // no possibility of any wins yet...choose
strategic position (CORNERS)
board[0][0] = 2;
else if ( turn == 3 ) // no possibility of any wins yet...choose
strategic position
{
if ( board[0][0] == 2 )
pos = checkPosition ( board, 2, 2 );
if ( pos )
board[2][2] = 2;
else if ( board[2][2] == 2 )
{
pos = checkPosition ( board, 0, 0 );
if ( pos )
board[0][0] = 2;
else if ( board[0][2] == 2 )
{
pos = checkPosition ( board, 2, 0 );
if ( pos )
board[2][0] = 2;
else if ( board[2][0] == 2 )
{
pos = checkPosition ( board, 0, 2 );
if ( pos )
board[0][2] = 2;
else
{
int n1, n2;
do
{
n1 = 1 + rand() % 2;
n2 = 1 + rand() % 2;
pos = checkPosition ( board, n1, n2 );
} while ( !pos );
board[n1][n2] = 2;
}
}
}
}
}
else // turn is > 5
{
//check for win situation
int i, j;
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
pos = checkPosition ( board, i , j );
if ( pos )
{
board[j] = 2;
testWin = won ( board, player );
if ( !testWin )
board[j] = 0;
else
break;
}
}
if ( j < 3 ) // computer won and broke from the loop
break;
}
if ( j < 3 )
{
drawgame ( board );
cout << "\nComputer WINS!";
break;
}
// block player 1 from winning
if ( j == 3 )
{
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
pos = checkPosition ( board, i , j );
if ( pos )
{
board[j] = 1;
testWin = won ( board, 1 );
if ( !testWin )
board[j] = 0;
else
{
board[j] = 2;
break;
}
}
}
if ( j < 3 )
break;
}
if ( j == 3 ) // computer chooses a random value
{
int n1, n2;
do
{
n1 = 1 + rand() % 2;
n2 = 1 + rand() % 2;
pos = checkPosition ( board, n1, n2 );
} while ( !pos );
board[n1][n2] = 2;
}
}
}
drawgame ( board );
}
else // player 1's turn
{
player = 1;
do
{
cout << "\nPlayer 1, Enter a position: ";
cin >> row >> col;
pos = checkPosition ( board, row, col );
if ( ! pos )
cout << "\nSorry...choose a different position! " << endl;
} while ( ! pos ); // player entered a wrong position
board [ row ][ col ] = player;
drawgame ( board );
cout<<"\n";
// test for winning position
// tests only if each player has played twice already
if ( turn > 4 )
{
bool testWin = won( board, player );
if ( testWin ) // Player Won!
{
cout << "YOU WIN!" << endl;
break;
}
}
}
turn++; // counter
} while ( turn <= 9 );
if ( turn == 10 )
cout << "\nDRAW"; // no winner
getch();
}
The game is b/w a human and the computer. The computer always goes
first. The computer chooses the first corner in the first turn. in
the comp's 2nd turn, it chooses the opposite corner if possible, if
that corner is not availabe, the computer chooses randomly. In the
computers next turns...it does the following:
1. checks for a winning position
else
2. blocks human from a winning position
else
3. randomly chooses a position
The code is:
//Tic-Tac-Toe program
//
// The game board is a 3x3 array of ints
// board[3][3]:
// board[0][0] is upper left corner
// board[0][1] is top row, middle
// ...
// board[2][2] is bottom right
// 0 means the spot is empty
// 1 means 'X' has the spot
// 2 means 'O' has the spot
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
// drawpos will draw the right character to the screen, depending
// on the value of the parameter val.
// if val is 0, the spot is empty to draw a blank
// if val is 1, the spot belongs to player X, print 'X'
// if val is 2, the spot belongs to player O, print 'O'
void drawpos( int val ) {
if (val==0)
cout << " "; // val is 0, so draw a space
else if (val==1)
cout << "X"; // val is 1, so draw a 'X'
else if (val==2)
cout << "O"; // val is 2, so draw a 'O'
}
// drawrow gets an array of 3 ints and draw a
// row of a tic-tac-toe game using the array to determine
// what should go in each of the three squares.
//
// draws a row of the board
void drawrow( int row[3] ) {
cout << " "; // indent a space
drawpos(row[0]); // draw the leftmost spot
cout << " | "; // draw the vertial bar
drawpos(row[1]); // draw the middle
cout << " | "; // draw the vertial bar
drawpos(row[2]); // draw the rightmost spot
cout << endl; // newline (end of the line)
}
// drawgame will print out a tic-tac-toe game board
//
// the output looks something like this:
//
// | |
// ---|---|---
// X | | O
// ---|---|---
// X | O |
//
//
// the top left corner is board[0][0]
// the top middle is board[0][1]
// the bottom right corner is board[2][2]
void drawgame( int board[3][3] ) {
drawrow(board[0]); // draws the top row
cout << "---|---|---" << endl;
drawrow(board[1]); // draws the middlerow
cout << "---|---|---" << endl;
drawrow(board[2]); // draws the bottom row
}
bool checkPosition ( int board [3][3], int row, int col )
{
if ( ( row > 2 ) || ( row < 0 ) || ( col > 2 ) || ( col < 0 ) // out
of range test
|| ( board [ row ][ col ] != 0 ) ) //position is occupied
return false;
else return true;
}
bool won ( int board [3][3], int player )
{
int i, j;
// ROW TEST
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
if ( board [j] == player )
continue;
else
break;
if ( j == 3 )
return true;
}
// COLUMN TEST
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
if ( board [j] == player )
continue;
else
break;
if ( j == 3 )
return true;
}
//DIAGONAL TEST
if ( ( ( board[0][0] == player ) && ( board[1][1] == player ) &&
( board[2][2] == player ) ) || ( ( board[0][2] == player ) &&
( board[1][1] == player ) && ( board[2][0] == player ) ) )
return true;
return false;
}
void main ()
{
int board [3][3] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int turn = 1;
int flag = 0;
int row, col, player;
bool pos, testWin = 0;
drawgame ( board );
cout << "\nPlayer 1 is X............Computer is 0" << endl;
cout << "\nComputer goes first." << endl;
do
{
if ( ( turn % 2 ) == 1 ) // computer's turn
{
player = 2;
if ( turn == 1 ) // no possibility of any wins yet...choose
strategic position (CORNERS)
board[0][0] = 2;
else if ( turn == 3 ) // no possibility of any wins yet...choose
strategic position
{
if ( board[0][0] == 2 )
pos = checkPosition ( board, 2, 2 );
if ( pos )
board[2][2] = 2;
else if ( board[2][2] == 2 )
{
pos = checkPosition ( board, 0, 0 );
if ( pos )
board[0][0] = 2;
else if ( board[0][2] == 2 )
{
pos = checkPosition ( board, 2, 0 );
if ( pos )
board[2][0] = 2;
else if ( board[2][0] == 2 )
{
pos = checkPosition ( board, 0, 2 );
if ( pos )
board[0][2] = 2;
else
{
int n1, n2;
do
{
n1 = 1 + rand() % 2;
n2 = 1 + rand() % 2;
pos = checkPosition ( board, n1, n2 );
} while ( !pos );
board[n1][n2] = 2;
}
}
}
}
}
else // turn is > 5
{
//check for win situation
int i, j;
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
pos = checkPosition ( board, i , j );
if ( pos )
{
board[j] = 2;
testWin = won ( board, player );
if ( !testWin )
board[j] = 0;
else
break;
}
}
if ( j < 3 ) // computer won and broke from the loop
break;
}
if ( j < 3 )
{
drawgame ( board );
cout << "\nComputer WINS!";
break;
}
// block player 1 from winning
if ( j == 3 )
{
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
pos = checkPosition ( board, i , j );
if ( pos )
{
board[j] = 1;
testWin = won ( board, 1 );
if ( !testWin )
board[j] = 0;
else
{
board[j] = 2;
break;
}
}
}
if ( j < 3 )
break;
}
if ( j == 3 ) // computer chooses a random value
{
int n1, n2;
do
{
n1 = 1 + rand() % 2;
n2 = 1 + rand() % 2;
pos = checkPosition ( board, n1, n2 );
} while ( !pos );
board[n1][n2] = 2;
}
}
}
drawgame ( board );
}
else // player 1's turn
{
player = 1;
do
{
cout << "\nPlayer 1, Enter a position: ";
cin >> row >> col;
pos = checkPosition ( board, row, col );
if ( ! pos )
cout << "\nSorry...choose a different position! " << endl;
} while ( ! pos ); // player entered a wrong position
board [ row ][ col ] = player;
drawgame ( board );
cout<<"\n";
// test for winning position
// tests only if each player has played twice already
if ( turn > 4 )
{
bool testWin = won( board, player );
if ( testWin ) // Player Won!
{
cout << "YOU WIN!" << endl;
break;
}
}
}
turn++; // counter
} while ( turn <= 9 );
if ( turn == 10 )
cout << "\nDRAW"; // no winner
getch();
}