c++ - 井字棋数组错误

标签 c++ arrays 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;
        }
    }

然后你访问了你不应该访问的内存。这意味着你要出界了!

这里 xy 最终将取一个等于 2 的值。

数组的索引从 0 开始,直到它的大小为 - 1。

因此,您可以使用 3x3 数组,或更改您的代码(以及直到 2 的函数 checkIfWinner)。


旁注:

你在这里错过了相等运算符:

if (winner = 1){
    cout << "Congratulations, you won!";
}
else if (winner = 2){
    cout << "Sorry, you lost!";
}

如果你保持原样,会发生什么?分配将发生并导致逻辑真,因此第一个条件将始终为真(第二个条件将始终为真,但代码不会走那么远)。

因此,将 = 更改为 ==

关于c++ - 井字棋数组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26741530/

相关文章:

c++ - 清理字符串以打印到终端的惯用方法?

python - 同时平铺和修改 Numpy 数组

c++ - 使用 Builder 6 应用程序形式的井字游戏

确定井字游戏结束的算法

c++ - std::unordered_map::merge() 的安全性

C++ 在#include 指令中使用 ..(点)是否合法?

c++ - 在 C++ 中生成证书请求

javascript - 如何比较两个对象数组并纠正相同的键值?

javascript - 如何对合并数组进行重复数据删除?

c++ - 井字游戏帮助