C++ tic tac toe problem
Sep 20, 2014 at 1:32pm UTC
HI, i am trying to make a tic tac toe 2d array game.The game that i am making is actually human vs computer, so this is where the problem arises. I can't seem to validate the players move as in,if the computer places its move somewhere the human cannot place their move in that same place and vice versa. The code is down below, have a look at it and if you can then please alter the code and then post it. Let me know what I was doing wrong, and please make sure to add every new detail in the main function,please don't add a bunch of other functions,just keep it in the main function.Thanks....
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main(){
char board[3][3];
int row,col;
bool Gameover;
bool win;
int player;
int r,c,x,y;
char YorN;
for (row=0;row<3;row++){
for (col=0;col<3;col++){
board[row][col]=' ' ;
}
}
cout << endl << endl;
for (row=0;row<3;row++){
for (col=0;col<3;col++){
cout << "|" << board[row][col] << "|" ;
}
cout << endl << "|-||-||-|" ;
cout << endl;
}
cout << endl << endl << endl;
cout << "Are you player 1 or 2?: " ;
cin >> player;
cout << endl << endl << endl;
if (player==1){
cout << "Your symbol is 'O'.The computer's is 'X' " << endl;
}else if (player==2){
cout << "Your symbol is 'X'.The computer's is 'O' " << endl;
}
cout << endl << endl;
while (Gameover==false ){
while (win==false ){
if (player==1){
cout << "Enter row: " ;
cin >> r;
cout << "Enter column: " ;
cin >> c;
srand(time(0));
x=rand()%3;
y=rand()%3;
cout << endl << endl << endl;
if (board[row][col]!=' ' ){
cout << "Invalid move.That spot is already occupied." ;
cout << endl << endl << endl;
cout << "Enter row: " ;
cin >> r;
cout << "Enter column: " ;
cin >> c;
srand(time(0));
x=rand()%3;
y=rand()%3;
}
board[r][c]='O' ;
board[x][y]='X' ;
}else if (player==2){
cout << "Enter row: " ;
cin >> r;
cout << "Enter column: " ;
cin >> c;
srand(time(0));
x=rand()%3;
y=rand()%3;
if (board[row][col]!=' ' ){
cout << "Invalid move.That spot is already occupied." ;
cout << endl << endl << endl;
cout << "Enter row: " ;
cin >> r;
cout << "Enter column: " ;
cin >> c;
srand(time(0));
x=rand()%3;
y=rand()%3;
}
board[r][c]='X' ;
board[x][y]='O' ;
}
cout << endl << endl << endl;
for (row=0;row<3;row++){
for (col=0;col<3;col++){
cout << "|" << board[row][col] << "|" ;
}
cout << endl << "|-||-||-|" ;
cout << endl;
}
cout << endl << endl << endl;
}
}
return 0;
}
Sep 20, 2014 at 1:36pm UTC
please make sure to add every new detail in the main function,please don't add a bunch of other functions,just keep it in the main function
We'll get onto it right away chief. Can we have your marks as well?
Topic archived. No new replies allowed.