Tic-Tac-Toe阵列错误
Tic-Tac-Toe Array Error
这个程序是玩家和电脑之间的一个简单的Tic-Tac-Toe游戏。如果所述空间还没有被占用,则计算机只生成一个随机空间来移动。此外,我的x坐标在纵轴上,而y坐标在横轴上。我这样做是因为我使用了一个二维数组,这就是它们的结构。
运行程序时,有些空格有问题,我找不出原因。当用户输入点(0,2)时,程序也会填充点(1,0),反之亦然。这也发生在点(1,2)和(2,0)上。
#include<iostream>
#include<string>
#include<stdlib.h>
#include<ctime>
using namespace std;
int board[2][2];
int chooseFirstPlayer();
void userMove(int boardArray[2][2]);
void compMove(int boardArray[2][2]);
int checkIfWinner(int boardArray[2][2]);
void displayBoard(int boardArray[2][2]);
int main(){
srand(time(NULL));
int x,y,winner;
for(x = 0; x <= 2; x++){ //sets the enitre board array to 0
for(y = 0; y <= 2; y++){
board[x][y] = 0;
}
}
if (chooseFirstPlayer() == 1){ //the user gets to movve first
do{//it will loop this until there is a winner
displayBoard(board);
userMove(board);
displayBoard(board);
winner = checkIfWinner(board);
if (winner == 0){//after the player moves, it will see if he won. If not, then the computer willbe able to move.
compMove(board);
displayBoard(board);
winner = checkIfWinner(board);
}
}while (winner == 0);//it will loop until a winner is found
}
else{//same structure as above just slightly altered to allow the computer to move first
do{
compMove(board);
displayBoard(board);
winner = checkIfWinner(board);
if (winner == 0){
userMove(board);
displayBoard(board);
winner = checkIfWinner(board);
}
}while(winner == 0);
}
if (winner = 1){
cout << "Congratulations, you won!";
}
else if (winner = 2){
cout << "Sorry, you lost!";
}
}
int chooseFirstPlayer(){//randomly genereate a number 1 or 2 to choose who moves first
return rand() % 2 + 1;
}
void userMove(int boardArray[2][2]){
int userX, userY;
do{
cout << "Enter an x coordinate: "<<endl;
cin >> userX;
cout << "Enter a y coordinate: "<<endl;
cin >> userY;
if (boardArray[userX][userY] != 0){
cout << "That loaction is already occupied"<<endl;
}
}while(boardArray[userX][userX] != 0);
boardArray[userX][userY] = 1;
}
void compMove(int boardArray[2][2]){
int compX,compY;
do{
compX = rand() % 3;
compY = rand() % 3;
}while(boardArray[compX][compY] != 0);
boardArray[compX][compY] = 2;
}
int checkIfWinner(int boardArray[2][2]){
if(boardArray[0][0] == boardArray[0][1] && boardArray[0][1] == boardArray[0][2]){ //across
return boardArray[0][0];}
else if (boardArray[1][0] == boardArray[1][1] && boardArray[1][1] == boardArray[1][2]){
return boardArray[1][0];}
else if (boardArray[2][0] == boardArray[2][1] && boardArray[2][1] == boardArray[2][2]){
return boardArray[2][0];}
else if (boardArray[0][0] == boardArray[1][0] && boardArray[1][0] == boardArray[2][0]){//down
return boardArray[0][0];}
else if (boardArray[0][1] == boardArray[1][1] && boardArray[1][1] == boardArray[2][1]){
return boardArray[0][1];}
else if (boardArray[0][2] == boardArray[1][2] && boardArray[1][2] == boardArray[2][2]){
return boardArray[0][2];}
else if (boardArray[0][0] == boardArray[1][1] && boardArray[1][1] == boardArray[2][2]){//diagonal
return boardArray[0][0];}
else if (boardArray[2][0] == boardArray[1][1] && boardArray[1][1] == boardArray[0][2]){
return boardArray[2][0];}
else{
return 0;
}
}
void displayBoard(int boardArray[2][2]){
system("CLS");
cout <<" "<<" Y1 "<<" Y2 "<<" Y3 "<<endl;
cout <<" X1 "<< "__"<<boardArray[0][0]<<"__|__"<<boardArray[0][1]<<"__|__"<<boardArray[0][2]<<"__"<<endl;
cout <<" X2 "<< "__"<<boardArray[1][0]<<"__|__"<<boardArray[1][1]<<"__|__"<<boardArray[1][2]<<"__"<<endl;
cout <<" X3 "<< " "<<boardArray[2][0]<<" | "<<boardArray[2][1]<<" | "<<boardArray[2][2]<<" "<<endl;
}
我的IDE是Dev-C++(5.4.2)
你的数组是2x2,你做了:
for(x = 0; x <= 2; x++){ //sets the enitre board array to 0
for(y = 0; y <= 2; y++){
board[x][y] = 0;
}
}
并且你访问了不该访问的内存。这意味着你要出界了!
这里CCD_ 1和CCD_。
数组的索引从0开始,直到其大小为-1。
因此,您可以使用3x3的数组,或者更改代码(以及直到2的函数checkIfWinner
)。
旁注:
您在这里错过了相等运算符:
if (winner = 1){
cout << "Congratulations, you won!";
}
else if (winner = 2){
cout << "Sorry, you lost!";
}
如果你保持原样会发生什么?赋值将发生,并将产生逻辑true,因此第一个条件将始终为true(第二个条件,但代码不会走那么远)。
因此,将=
更改为==
。
相关文章:
- 当我的阵列太大时出现分段错误
- 仅在大型阵列上出现合并排序分段错误
- 访问特定阵列位置/索引时出现分段错误
- 为什么删除分配的阵列会导致内存错误?
- 动态 2D 阵列.为什么分段错误?
- 分配给阵列时出现分段错误?黑客排名 2D 阵列 - DS.
- 删除阵列时出现分段错误
- 保护类内存阵列以检测分段错误
- 3D 阵列中的瓦尔格林德错误C++
- OpenMP 环路阵列访问中的错误共享
- C++ Visual 工作室中的逻辑错误.阵列打印的是地址而不是内容
- 排序阵列会使指针链接错误
- 为什么在迭代中迭代时,为什么我不会出现限制阵列错误
- 问题创建和返回锯齿状阵列(错误std :: bad_array_new_length)
- TBB免费图像lambda阵列比较错误
- 打印我的字符串阵列时,会遇到细分错误
- 删除指向排序的字符串阵列的指针会在运行后会有一个错误
- 在C 中构造函数中初始化C阵列的错误
- 当我尝试创建两个后缀阵列时,为什么要获得EXC_BAD_ACCESS错误
- 分段错误 - C++阵列