【发布时间】:2014-09-21 06:44:48
【问题描述】:
您好,我正在尝试制作一个玩家与电脑对战的井字游戏。唯一的问题是我无法验证玩家或 pc 的移动,例如,如果 pc 将其移动放在棋盘上,则玩家无法将其移动放置在将被占用的同一位置。 pc 也是如此.我的代码在下面,所以看看它,让我知道如何纠正它。
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;
}
}
返回 0; }
【问题讨论】:
标签: tic-tac-toe