TIC TAC TOE automized player....is me!!

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 22
Reputation: mnv29brt is an unknown quantity at this point 
Solved Threads: 0
mnv29brt mnv29brt is offline Offline
Newbie Poster

TIC TAC TOE automized player....is me!!

 
0
  #1
Mar 14th, 2008
//To check if there is a win
#include <iostream>
#include "tictactoe.h"
using namespace std;

int win(char board[3][3])
{
for(int i=0;i<3;i=i+1)

{
if ((board[i][0] == 'x' && board[i][1] == 'x' && board[i][2] == 'x')|| (board[i][0] == 'o' && board[i][1] == 'o'&& board [i][2]== 'o'))
return 1;
else if((board[0][i] == 'x' && board[1][i] == 'x' && board[2][i] == 'x')||(board[0][i] == 'o' && board[1][i] == 'o' && board[2][i] == 'o'))
return 1;
else if((board[0][0]=='x' && board[1][1] =='x' && board[2][2] =='x')||(board[0][0] == 'o' && board[1][1] == 'o' && board[2][2]=='o')||(board[0][2] =='x' && board[1][1] =='x' && board[2][0] =='x')||(board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o'))
return 1;
else
return 0;
}

// Algorithm: if any horizonal, vertical, or diagonal row is all 'x' or 'o' (not 'e'),
// a win has occurred.
return 0; // No win yet
}

// C++ Project 1 -- tictactoe.cpp
//
#include <iostream>
using namespace std;

// Included for random number generation to see who goes first
#include <time.h>
#include <stdlib.h>

// Function prototypes common to all source code files
#include "tictactoe.h"

int main()
{
char board[3][3]; // Current tic-tac-toe board
int i, j, num;

// Initialize board to empty spaces. Key: 'e' = empty; 'x' = player 1 mark; 'o' = player 2 mark
for (i=0;i<3;i++) { // rows
for (j=0;j<3;j++) { // columns
board[i][j] = 'e';
}
}

// First, use random number generator to choose which player starts first.
// If the integer number returned by rand() is even, player 1 starts first.
// Otherwise (number is odd), player 2 starts first.
//
srand((unsigned int) time(NULL));
num = rand()%2; // randomly sets num to 0 or 1
if (!num) cout << "Player 1 starts first.\n";
else cout << "Player 2 starts first.\n";

// Let the game begin! Take turns until either the board is filled (tie) or
// one player occupies an entire row, column, or diagonal. If player overwrites
// space already filled with an 'x' or 'o', that player automatically loses.
//
for (i=1;i<=9;i++) { // Board contains 9 spaces total to fill

// Make the next move, check its legality, and check for a win
if (((!num) && i%2) || (num && !(i%2))) { // Player 1
player1_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 1 made an illegal move. Player 2 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 1 has resulted in a win
cout << "\n*** Player 1 has won the game!!!\n";
print_board(board);
return 0;
}

} else { // Player 2
player2_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 2 made an illegal move. Player 1 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 2 has resulted in a win
cout << "\n*** Player 2 has won the game!!!\n";
print_board(board);
return 0;
}
}

} // End of "for" loop

cout << "\n*** The game is a draw!!!\n";
print_board(board);
return 0;
}

// Function to print the current board configuration
void print_board(char board[3][3])
{
int i, j;
for (i=0;i<3;i++) {
for (j=0;j<3;j++) {
if (board[i][j] == 'e') cout << "e "; // Leave blank
else cout << board[i][j] << " ";
}
cout << endl;
}
return;
}



------------------------------------------------------------------------------------------------------
// Check for illegal move overwriting previous 'x' or 'o'
// Algorithm: look for exactly (9-i) 'e' (empty) board spaces
// where i = number of moves made so far
int illegal_move(int i, char board[3][3])
{
static char oldboard[3][3];
static int firstflag = 1;
int j, k, ecount=0, newcount=0;

// Initialize oldboard, reset firstflag
if (firstflag) {
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
oldboard[j][k] = 'e';
}
}
firstflag = 0;
}

// Now check that board matches except for the one new move
// Includes "old" code to ensure correct number of x/o values present.
//
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
if (board[j][k] == 'e') ecount++;
if (oldboard[j][k] != board[j][k]) {
if (oldboard[j][k] != 'e') return(1); // Illegal to overwrite anything but 'e'
oldboard[j][k] = board[j][k];
newcount++;
}
}
}

if ((ecount == (9-i)) && (newcount == 1))
return(0); // OK
return(1); // Illegal move made
}

-------------------------------------------------------------------------------------------------------
// C++ Project 1 -- tictactoe.h
//

// Function prototypes
int illegal_move(int, char [3][3]); // Checks for an illegal board move (return 0=OK; 1=bad move)
int win(char [3][3]); // Checks whether a player has won the game (0=no win; 1=win)
void print_board(char [3][3]); // Prints the current board configuration
void player1_move(int, char [3][3]); // Player 1 (instructor) selects the next move, marked with an 'x'
void player2_move(int, char [3][3]); // Player 2 (student) selects the next move, marked with an 'o'
------------------------------------------------------------------------------------------
now player 2 is me....but here's a swist its auctually an automized me as in I have to predict and block/win against player 1 who is the instructor.I want to enter into the center postion if its my chance 1st(note that my code is devised in a way that the chance as to who gets to go 1st is random...as I was instructed) Also I want to try and win against the instructor's code
Any help guys
ASAP!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,831
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: TIC TAC TOE automized player....is me!!

 
0
  #2
Mar 14th, 2008
That's way too much to read without code tags and formatting.


[code]
// Paste your code here.
[/code]

or

[code=cplusplus]
// Paste your code here. Adds line numbers and syntax highlighting.
[/code]
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: mnv29brt is an unknown quantity at this point 
Solved Threads: 0
mnv29brt mnv29brt is offline Offline
Newbie Poster

Re: TIC TAC TOE automized player....is me!!

 
0
  #3
Mar 14th, 2008
#include <iostream>
#include "win.cpp"
using namespace std;

int win(char board[3][3])
{
for(int i=0;i<3;i=i+1)

{
if ((board[i][0] == 'x' && board[i][1] == 'x' && board[i][2] == 'x')|| (board[i][0] == 'o' && board[i][1] == 'o'&& board [i][2]== 'o'))
return 1;
else if((board[0][i] == 'x' && board[1][i] == 'x' && board[2][i] == 'x')||(board[0][i] == 'o' && board[1][i] == 'o' && board[2][i] == 'o'))
return 1;
else if((board[0][0]=='x' && board[1][1] =='x' && board[2][2] =='x')||(board[0][0] == 'o' && board[1][1] == 'o' && board[2][2]=='o')||(board[0][2] =='x' && board[1][1] =='x' && board[2][0] =='x')||(board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o'))
return 1;
else
return 0;
}

// Algorithm: if any horizonal, vertical, or diagonal row is all 'x' or 'o' (not 'e'),
// a win has occurred.
return 0; // No win yet
}
-------------------------------------------------------
this is to check if 'x'||'o' is filled in all 3 rows or columns or diagnol as in board[0][0]&&board[1][1]&&[2][2]||board[0][2]&&[1][1]&&[2][0]Then its a win
Last edited by mnv29brt; Mar 14th, 2008 at 9:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: mnv29brt is an unknown quantity at this point 
Solved Threads: 0
mnv29brt mnv29brt is offline Offline
Newbie Poster

Re: TIC TAC TOE automized player....is me!!

 
0
  #4
Mar 14th, 2008
//F// ENGR 101, Winter 2008
// C++ Project 1 -- tictactoe.h//

// Function prototypes
1)int illegal_move(int, char [3][3]); // Checks for an illegal board move (return 0=OK; 1=bad move)
2)int win(char [3][3]); // Checks whether a player has won the game (0=no win; 1=win)
3)void print_board(char [3][3]); // Prints the current board configuration
4)void player1_move(int, char [3][3]); // Player 1 (instructor) selects the next move, marked with an 'x'
5)void player2_move(int, char [3][3]); // Player 2 (student) selects the next move, marked with an 'o'unction prototypes
Last edited by mnv29brt; Mar 14th, 2008 at 9:31 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: mnv29brt is an unknown quantity at this point 
Solved Threads: 0
mnv29brt mnv29brt is offline Offline
Newbie Poster

Re: TIC TAC TOE automized player....is me!!

 
0
  #5
Mar 14th, 2008
// ENGR 101, Winter 2008
// C++ Project 1 -- tictactoe.cpp
//
#include <iostream>
using namespace std;

// Included for random number generation to see who goes first
#include <time.h>
#include <stdlib.h>

// Function prototypes common to all source code files
#include "tictactoe.h"

int main()
{
char board[3][3]; // Current tic-tac-toe board
int i, j, num;

// Initialize board to empty spaces. Key: 'e' = empty; 'x' = player 1 mark; 'o' = player 2 mark
for (i=0;i<3;i++) { // rows
for (j=0;j<3;j++) { // columns
board[i][j] = 'e';
}
}

// First, use random number generator to choose which player starts first.
// If the integer number returned by rand() is even, player 1 starts first.
// Otherwise (number is odd), player 2 starts first.
//
srand((unsigned int) time(NULL));
num = rand()%2; // randomly sets num to 0 or 1
if (!num) cout << "Player 1 starts first.\n";
else cout << "Player 2 starts first.\n";

// Let the game begin! Take turns until either the board is filled (tie) or
// one player occupies an entire row, column, or diagonal. If player overwrites
// space already filled with an 'x' or 'o', that player automatically loses.
//
for (i=1;i<=9;i++) { // Board contains 9 spaces total to fill

// Make the next move, check its legality, and check for a win
if (((!num) && i%2) || (num && !(i%2))) { // Player 1
player1_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 1 made an illegal move. Player 2 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 1 has resulted in a win
cout << "\n*** Player 1 has won the game!!!\n";
print_board(board);
return 0;
}

} else { // Player 2
player2_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 2 made an illegal move. Player 1 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 2 has resulted in a win
cout << "\n*** Player 2 has won the game!!!\n";
print_board(board);
return 0;
}
}

} // End of "for" loop

cout << "\n*** The game is a draw!!!\n";
print_board(board);
return 0;
}

// Function to print the current board configuration
void print_board(char board[3][3])
{
int i, j;
for (i=0;i<3;i++) {
for (j=0;j<3;j++) {
if (board[i][j] == 'e') cout << "e "; // Leave blank
else cout << board[i][j] << " ";
}
cout << endl;
}
return;
}

// Check for illegal move overwriting previous 'x' or 'o'
// Algorithm: look for exactly (9-i) 'e' (empty) board spaces
// where i = number of moves made so far
int illegal_move(int i, char board[3][3])
{
static char oldboard[3][3];
static int firstflag = 1;
int j, k, ecount=0, newcount=0;

// Initialize oldboard, reset firstflag
if (firstflag) {
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
oldboard[j][k] = 'e';
}
}
firstflag = 0;
}

// Now check that board matches except for the one new move
// Includes "old" code to ensure correct number of x/o values present.
//
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
if (board[j][k] == 'e') ecount++;
if (oldboard[j][k] != board[j][k]) {
if (oldboard[j][k] != 'e') return(1); // Illegal to overwrite anything but 'e'
oldboard[j][k] = board[j][k];
newcount++;
}
}
}

if ((ecount == (9-i)) && (newcount == 1))
return(0); // OK
return(1); // Illegal move made
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: mnv29brt is an unknown quantity at this point 
Solved Threads: 0
mnv29brt mnv29brt is offline Offline
Newbie Poster

Re: TIC TAC TOE automized player....is me!!

 
0
  #6
Mar 14th, 2008
now player 2 is me....but here's a swist its auctually an automized me as in I have to predict and block/win against player 1 who is the instructor.I want to enter into the center postion if its my chance 1st(note that my code is devised in a way that the chance as to who gets to go 1st is random...as I was instructed) Also I want to try and win against the instructor's code
Any help guys
ASAP!



Guys I have tried and edited it,any other confusion or discomfort in reading plz tell me i'll email you or so..
but hopefully I hear from you guys
there are 3 FUNCTIONS that I am done with:
1)win.cpp
2)tictactoe.h
3)tictactoe.cpp

Now I need to create a player_2_move function...which is me predicting almost every move of player1(instructor),when I turn this in the instructor tests my code
whether it detects a win,knows when whose chance is,and also whether my player1 code is peffect!!!!and can beat her code
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: mnv29brt is an unknown quantity at this point 
Solved Threads: 0
mnv29brt mnv29brt is offline Offline
Newbie Poster

Re: TIC TAC TOE automized player....is me!!

 
0
  #7
Mar 14th, 2008
FORGET THIS MESSAGE I HAE SPLIT IT UP IN THE REPLYS,EXATLY THE SAME(almost),so please make your time available in reading this,thanks so much u guys



Originally Posted by Pranay L View Post
//To check if there is a win
#include <iostream>
#include "tictactoe.h"
using namespace std;

int win(char board[3][3])
{
for(int i=0;i<3;i=i+1)

{
if ((board[i][0] == 'x' && board[i][1] == 'x' && board[i][2] == 'x')|| (board[i][0] == 'o' && board[i][1] == 'o'&& board [i][2]== 'o'))
return 1;
else if((board[0][i] == 'x' && board[1][i] == 'x' && board[2][i] == 'x')||(board[0][i] == 'o' && board[1][i] == 'o' && board[2][i] == 'o'))
return 1;
else if((board[0][0]=='x' && board[1][1] =='x' && board[2][2] =='x')||(board[0][0] == 'o' && board[1][1] == 'o' && board[2][2]=='o')||(board[0][2] =='x' && board[1][1] =='x' && board[2][0] =='x')||(board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o'))
return 1;
else
return 0;
}

// Algorithm: if any horizonal, vertical, or diagonal row is all 'x' or 'o' (not 'e'),
// a win has occurred.
return 0; // No win yet
}

// C++ Project 1 -- tictactoe.cpp
//
#include <iostream>
using namespace std;

// Included for random number generation to see who goes first
#include <time.h>
#include <stdlib.h>

// Function prototypes common to all source code files
#include "tictactoe.h"

int main()
{
char board[3][3]; // Current tic-tac-toe board
int i, j, num;

// Initialize board to empty spaces. Key: 'e' = empty; 'x' = player 1 mark; 'o' = player 2 mark
for (i=0;i<3;i++) { // rows
for (j=0;j<3;j++) { // columns
board[i][j] = 'e';
}
}

// First, use random number generator to choose which player starts first.
// If the integer number returned by rand() is even, player 1 starts first.
// Otherwise (number is odd), player 2 starts first.
//
srand((unsigned int) time(NULL));
num = rand()%2; // randomly sets num to 0 or 1
if (!num) cout << "Player 1 starts first.\n";
else cout << "Player 2 starts first.\n";

// Let the game begin! Take turns until either the board is filled (tie) or
// one player occupies an entire row, column, or diagonal. If player overwrites
// space already filled with an 'x' or 'o', that player automatically loses.
//
for (i=1;i<=9;i++) { // Board contains 9 spaces total to fill

// Make the next move, check its legality, and check for a win
if (((!num) && i%2) || (num && !(i%2))) { // Player 1
player1_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 1 made an illegal move. Player 2 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 1 has resulted in a win
cout << "\n*** Player 1 has won the game!!!\n";
print_board(board);
return 0;
}

} else { // Player 2
player2_move(i, board);
if (illegal_move(i, board)) {
cout << "\n*** Player 2 made an illegal move. Player 1 wins!!!\n";
return 0;
}
if (win(board)) { // New move by player 2 has resulted in a win
cout << "\n*** Player 2 has won the game!!!\n";
print_board(board);
return 0;
}
}

} // End of "for" loop

cout << "\n*** The game is a draw!!!\n";
print_board(board);
return 0;
}

// Function to print the current board configuration
void print_board(char board[3][3])
{
int i, j;
for (i=0;i<3;i++) {
for (j=0;j<3;j++) {
if (board[i][j] == 'e') cout << "e "; // Leave blank
else cout << board[i][j] << " ";
}
cout << endl;
}
return;
}



------------------------------------------------------------------------------------------------------
// Check for illegal move overwriting previous 'x' or 'o'
// Algorithm: look for exactly (9-i) 'e' (empty) board spaces
// where i = number of moves made so far
int illegal_move(int i, char board[3][3])
{
static char oldboard[3][3];
static int firstflag = 1;
int j, k, ecount=0, newcount=0;

// Initialize oldboard, reset firstflag
if (firstflag) {
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
oldboard[j][k] = 'e';
}
}
firstflag = 0;
}

// Now check that board matches except for the one new move
// Includes "old" code to ensure correct number of x/o values present.
//
for (j=0;j<3;j++) {
for (k=0;k<3;k++) {
if (board[j][k] == 'e') ecount++;
if (oldboard[j][k] != board[j][k]) {
if (oldboard[j][k] != 'e') return(1); // Illegal to overwrite anything but 'e'
oldboard[j][k] = board[j][k];
newcount++;
}
}
}

if ((ecount == (9-i)) && (newcount == 1))
return(0); // OK
return(1); // Illegal move made
}

-------------------------------------------------------------------------------------------------------
// C++ Project 1 -- tictactoe.h
//

// Function prototypes
int illegal_move(int, char [3][3]); // Checks for an illegal board move (return 0=OK; 1=bad move)
int win(char [3][3]); // Checks whether a player has won the game (0=no win; 1=win)
void print_board(char [3][3]); // Prints the current board configuration
void player1_move(int, char [3][3]); // Player 1 (instructor) selects the next move, marked with an 'x'
void player2_move(int, char [3][3]); // Player 2 (student) selects the next move, marked with an 'o'
------------------------------------------------------------------------------------------
now player 2 is me....but here's a swist its auctually an automized me as in I have to predict and block/win against player 1 who is the instructor.I want to enter into the center postion if its my chance 1st(note that my code is devised in a way that the chance as to who gets to go 1st is random...as I was instructed) Also I want to try and win against the instructor's code
Any help guys
ASAP!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,831
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: TIC TAC TOE automized player....is me!!

 
0
  #8
Mar 15th, 2008
  1. // win.cpp
  2. #include <iostream>
  3. #include "win.cpp"
  4. using namespace std;
  5.  
  6. int win(char board[3][3])
  7. {
  8. for(int i=0;i<3;i=i+1)
  9.  
  10. {
  11. if ((board[i][0] == 'x' && board[i][1] == 'x' && board[i][2] == 'x')|| (board[i][0] == 'o' && board[i][1] == 'o'&& board [i][2]== 'o'))
  12. return 1;
  13. else if((board[0][i] == 'x' && board[1][i] == 'x' && board[2][i] == 'x')||(board[0][i] == 'o' && board[1][i] == 'o' && board[2][i] == 'o'))
  14. return 1;
  15. else if((board[0][0]=='x' && board[1][1] =='x' && board[2][2] =='x')||(board[0][0] == 'o' && board[1][1] == 'o' && board[2][2]=='o')||(board[0][2] =='x' && board[1][1] =='x' && board[2][0] =='x')||(board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o'))
  16. return 1;
  17. else
  18. return 0;
  19. }
  20.  
  21. // Algorithm: if any horizonal, vertical, or diagonal row is all 'x' or 'o' (not 'e'),
  22. // a win has occurred.
  23. return 0; // No win yet
  24. }// -------------------------------------------------------
  25. // this is to check if 'x'||'o' is filled in all 3 rows or columns or diagnol as in board[0][0]&&board[1][1]&&[2][2]||board[0][2]&&[1][1]&&[2][0]Then its a win

  1. //F// ENGR 101, Winter 2008
  2. // C++ Project 1 -- tictactoe.h//
  3.  
  4. // Function prototypes
  5. int illegal_move(int, char [3][3]); // Checks for an illegal board move (return 0=OK; 1=bad move)
  6. int win(char [3][3]); // Checks whether a player has won the game (0=no win; 1=win)
  7. void print_board(char [3][3]); // Prints the current board configuration
  8. void player1_move(int, char [3][3]); // Player 1 (instructor) selects the next move, marked with an 'x'
  9. void player2_move(int, char [3][3]); // Player 2 (student) selects the next move, marked with an 'o'unction prototypes

  1. // ENGR 101, Winter 2008
  2. // C++ Project 1 -- tictactoe.cpp
  3. //
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. // Included for random number generation to see who goes first
  8. #include <time.h>
  9. #include <stdlib.h>
  10.  
  11. // Function prototypes common to all source code files
  12. #include "tictactoe.h"
  13.  
  14. int main()
  15. {
  16. char board[3][3]; // Current tic-tac-toe board
  17. int i, j, num;
  18.  
  19. // Initialize board to empty spaces. Key: 'e' = empty; 'x' = player 1 mark; 'o' = player 2 mark
  20. for (i=0;i<3;i++) { // rows
  21. for (j=0;j<3;j++) { // columns
  22. board[i][j] = 'e';
  23. }
  24. }
  25.  
  26. // First, use random number generator to choose which player starts first.
  27. // If the integer number returned by rand() is even, player 1 starts first.
  28. // Otherwise (number is odd), player 2 starts first.
  29. //
  30. srand((unsigned int) time(NULL));
  31. num = rand()%2; // randomly sets num to 0 or 1
  32. if (!num) cout << "Player 1 starts first.\n";
  33. else cout << "Player 2 starts first.\n";
  34.  
  35. // Let the game begin! Take turns until either the board is filled (tie) or
  36. // one player occupies an entire row, column, or diagonal. If player overwrites
  37. // space already filled with an 'x' or 'o', that player automatically loses.
  38. //
  39. for (i=1;i<=9;i++) { // Board contains 9 spaces total to fill
  40.  
  41. // Make the next move, check its legality, and check for a win
  42. if (((!num) && i%2) || (num && !(i%2))) { // Player 1
  43. player1_move(i, board);
  44. if (illegal_move(i, board)) {
  45. cout << "\n*** Player 1 made an illegal move. Player 2 wins!!!\n";
  46. return 0;
  47. }
  48. if (win(board)) { // New move by player 1 has resulted in a win
  49. cout << "\n*** Player 1 has won the game!!!\n";
  50. print_board(board);
  51. return 0;
  52. }
  53.  
  54. } else { // Player 2
  55. player2_move(i, board);
  56. if (illegal_move(i, board)) {
  57. cout << "\n*** Player 2 made an illegal move. Player 1 wins!!!\n";
  58. return 0;
  59. }
  60. if (win(board)) { // New move by player 2 has resulted in a win
  61. cout << "\n*** Player 2 has won the game!!!\n";
  62. print_board(board);
  63. return 0;
  64. }
  65. }
  66.  
  67. } // End of "for" loop
  68.  
  69. cout << "\n*** The game is a draw!!!\n";
  70. print_board(board);
  71. return 0;
  72. }
  73.  
  74. // Function to print the current board configuration
  75. void print_board(char board[3][3])
  76. {
  77. int i, j;
  78. for (i=0;i<3;i++) {
  79. for (j=0;j<3;j++) {
  80. if (board[i][j] == 'e') cout << "e "; // Leave blank
  81. else cout << board[i][j] << " ";
  82. }
  83. cout << endl;
  84. }
  85. return;
  86. }
  87.  
  88. // Check for illegal move overwriting previous 'x' or 'o'
  89. // Algorithm: look for exactly (9-i) 'e' (empty) board spaces
  90. // where i = number of moves made so far
  91. int illegal_move(int i, char board[3][3])
  92. {
  93. static char oldboard[3][3];
  94. static int firstflag = 1;
  95. int j, k, ecount=0, newcount=0;
  96.  
  97. // Initialize oldboard, reset firstflag
  98. if (firstflag) {
  99. for (j=0;j<3;j++) {
  100. for (k=0;k<3;k++) {
  101. oldboard[j][k] = 'e';
  102. }
  103. }
  104. firstflag = 0;
  105. }
  106.  
  107. // Now check that board matches except for the one new move
  108. // Includes "old" code to ensure correct number of x/o values present.
  109. //
  110. for (j=0;j<3;j++) {
  111. for (k=0;k<3;k++) {
  112. if (board[j][k] == 'e') ecount++;
  113. if (oldboard[j][k] != board[j][k]) {
  114. if (oldboard[j][k] != 'e') return(1); // Illegal to overwrite anything but 'e'
  115. oldboard[j][k] = board[j][k];
  116. newcount++;
  117. }
  118. }
  119. }
  120.  
  121. if ((ecount == (9-i)) && (newcount == 1))
  122. return(0); // OK
  123. return(1); // Illegal move made

O.K., here it is formatted and in code tags. All formatting is lost when you don't use code tags. You have a choice. The first way leaves formatting, but doesn't add line numbers or highlight syntax. The second adds the line numbers and highlights syntax.


[code]
// paste code here
[/code]

or

[code=cplusplus]
// paste code here
[/code]


Now what specifically is the problem you are having?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: mnv29brt is an unknown quantity at this point 
Solved Threads: 0
mnv29brt mnv29brt is offline Offline
Newbie Poster

Re: TIC TAC TOE automized player....is me!!

 
0
  #9
Apr 11th, 2008
,jxbn ,k
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: mnv29brt is an unknown quantity at this point 
Solved Threads: 0
mnv29brt mnv29brt is offline Offline
Newbie Poster

Re: TIC TAC TOE automized player....is me!!

 
0
  #10
Apr 11th, 2008
zm
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC