简体   繁体   中英

Tic-Tac-Toe Array Error

This program is a simple Tic Tac Toe game between a player and the computer. The computer just generates a random space to move to if said space is not already occupied. Also, I have the x-coordinates on the vertical axis while the y-coordinates are on the horizontal axis. I did this because I use a two dimensional array, and that is just how they are structured.

When running the program, some of the spaces are faulty and I cannot find out why. When the user inputs the point (0,2) the program also fills in the point (1,0) and vise versa. This also occurs with the points (1,2) and (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;
}

My IDE is Dev-C++ (5.4.2)

Your array is 2x2 and you do:

for(x = 0; x <= 2; x++){ //sets the enitre board array to 0
        for(y = 0; y <= 2; y++){
            board[x][y] = 0;
        }
    }

and you access memory you shouldn't. That means you are going out of bounds!

Here x and y will take a value equal to 2 eventually.

Indexing of an array starts from 0 until it's size - 1.

So, you could use an array of 3x3, or change your code (and the function checkIfWinner that goes until 2).


Side note:

You have missed the equality operator here:

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

What will happen if you leave it as is? The assignment will take place and will result in logical true, thus the first condition will be always true (and the second, but the code won't go that far).

So, change = with == .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM