dimanche 1 juillet 2018

Tic tac toe C++ overwriting the board

Hi so I have it set up the way I want to for the most part. The last part I'm trying to implement is to have it so the input does not overwrite itself when input either Xor 0. If someone could take a look at it and tell me what exactly I did wrong with my noRepeat function I would greatly appreciate it. Thanks.

#include <iostream>

using namespace std;
const int ROWS = 3;
const int COLS = 3;
char Player1 = 'X';
char Player2 = 'O';
int row, col;
char board[3][3]= { '*', '*', '*', '*', '*', '*', '*', '*', '*'};  //Game board


//Prototypes

void noRepeat();
void drawBoard();
void selection(int&, int&);
char winner();
void switchPlayer();

int main()
{
    cout << "Tic Tac Toe!" << endl << endl;

    drawBoard();
    cout << endl << endl;
    while (1)
    {

        if ( noRepeat() )
        {
            selection(row, col);
            drawBoard();
            switchPlayer();
        }
        if (winner() == 'X')
        {
            cout << "Player 1 wins!" << endl;
            break;
        }
        else if (winner() == 'O')
        {
            cout << "Player 2 wins!" << endl;
            break;
        }

    }


    return 0;
}


// input function
void selection(int &row, int &col)
{
    cout << "Enter a number for row between 0 - 2" << endl;
    cin >> row;
    if (row < 0 || row > 2) {
        cout << "Invalid selection select a row between 0 - 2" << endl;
        cin >> row;
    }

    cout << "Enter a number for col between 0 - 2" << endl;
    cin >> col;
    if (col < 0 || col > 2) {
        cout << "Invalid selection select a row between 0 - 2" << endl;
        cin >> col;
    }



    if (row == 0 && col == 0)
        board[0][0] = Player1;
    else if (row == 0 && col == 1)
        board[0][1] = Player1;
    else if (row == 0 && col == 2)
        board[0][2] = Player1;
    else if (row == 1 && col == 0)
        board[1][0] = Player1;
    else if (row == 1 && col == 1)
        board[1][1] = Player1;
    else if (row == 1 && col == 2)
        board[1][2] = Player1;
    else if (row == 2 && col == 0)
        board[2][0] = Player1;
    else if (row == 2 && col == 1)
        board[2][1] = Player1;
    else if (row == 2 && col == 2)
        board[2][2] = Player1;


}


//Board function
 void drawBoard() {
    for (int i = 0; i < ROWS; i++) {

        for (int j = 0; j < COLS; j++) {
            cout << board[i][j];
        }
        cout << endl;
    }
}


//Function to switch between X's and O's

void switchPlayer() {

    char temp = Player1;
    if (Player1) {
        Player1 = Player2;
    }

    if (Player2)
    {
        Player2 = temp;
    }
}


//conditional winner checker
char winner() {
    if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
        return 'X';
    if (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
        return 'X';
    if (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
        return 'X';

    if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
        return 'X';
    if (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
        return 'X';
    if (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
        return 'X';

    if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
        return 'X';
    if (board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X')
        return 'X';

//Player 2
    if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
        return 'O';
    if (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
        return 'O';
    if (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
        return 'O';

    if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')
        return 'O';
    if (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')
        return 'O';
    if (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')
        return 'O';

    if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
        return 'O';
    if (board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == 'O')
        return 'O';
}


//Function to prevent overwriting.
void noRepeat() {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (board[i][j] == = Player1 || board[i][j] == Player2) {
                cout << "Invalid selection, please choose a row and column 
again."
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire