c++arrayscinreversi

Changing values of arrays


I'm new to c++ and as my first assignment was to make a game of reversi, but I'm having problems when it comes to the input of the players in the playGame function. So when i cin x and y, it will change the value of the board array[x][y] from empty or '' to 'B'. I also don't know how to refer the array to the function. I'm sorry if these questions are silly to some but forgive me I'm self-learning here. Thanks

#include <iostream>
#include <string>
using namespace std;
void displayTop();
void displayAlpha();
void displayNum();
int displayMenu();
void displayBoard();
char displayHelp();
void playGame();
int num = 8;
char board [8][8] = {

' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ','W','B',' ',' ',' ',
' ',' ',' ','B','W',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',

};

int main() {
    int choice = displayMenu();
    switch (choice) {
        case 1:
            displayBoard();
            break;
        case 2:
            displayHelp();
            break;
        default:
            cout << "Please enter a valid choice." << endl;
            break;
    }
    playGame();

}
void displayBoard(){


    displayTop();

    for (int row = 0; row < 8; row++){
        displayNum();
        cout << "   |";

        for (int column = 0; column < 8; column++){
            cout << board[row][column] << "   |";
        }
        cout << endl;
        displayTop();

    }

    displayAlpha();

}

void displayTop(){
    cout << "    ";
    for (int i = 0; i < 8; i++){
        cout << "+----";
    }
    cout << endl;

}

void displayAlpha(){
    cout << "   ";
    for( char i = 'a'; i <= 'h'; i++ ) {
    cout << "    " << i ;
    }
}

void displayNum(){
    if (num > 0) {
        cout << num;
        num = num - 1;

    }
}

int displayMenu(){
    int answer = 0;
    cout << "Othello\n\n"
    << "1.New Game\n2.Help\n3.Quit\nYour Choice: ";
    cin >> answer;
    system("clear");
    return answer;

}

char displayHelp(){
    char answer = ' ';
    cout << "How to play Othello\n\nThe object of the game is to have     the majority of your colour discs on the board at the end of the     game.\n\nInput the cell where you want to place your disc in the form of     (a-z 1-8) without the bracket and includng the space.\n\nThe one with the     most discs wins!!!!\n\nSo, are you ready to play? (y or n)\n\nYour Choice:    ";
    cin >> answer;
    if (answer == 'y')
        displayBoard();

    return answer;
}

void playGame(){
    int plW = 2;
    int plB = 2;
    int x = 0;
    int y = 0;
    char player = 'B';
    for(;;){
        cout << "\n\nScore: W = " << plW << " B = " << plB;
        cout << "\nPlayer: " << player;
        cout << "\nPlease make your move : ";
        cin >> x >> y;
        cout << endl;

        if (x < 9 && y < 9) {
            board[x-1][y-1] = player;
            displayBoard();
        } else {
            cout << "Invalid Input";
        }

        if (player == 'B') {
            plB++;
            player = 'W';

        } else {
            plW++;
            player = 'B';
        }
    }

}

Solution

  • To store the input from cin and hold on to it in your game board, you need your playGame() function to have a reference to the game board. You could declare your game board array before main to make it global in scope, which would allow you to reference it in any of the functions in this file.

    char board[8][8] = {
    
    {' ',' ',' ',' ',' ',' ',' ',' ',}
    {' ',' ',' ',' ',' ',' ',' ',' ',}
    {' ',' ',' ',' ',' ',' ',' ',' ',}
    {' ',' ',' ','W','B',' ',' ',' ',}
    {' ',' ',' ','B','W',' ',' ',' ',}
    {' ',' ',' ',' ',' ',' ',' ',' ',}
    {' ',' ',' ',' ',' ',' ',' ',' ',}
    {' ',' ',' ',' ',' ',' ',' ',' ',}
    
    };
    
    int main() {
    int choice = displayMenu();
    switch (choice) {
        case 1:
            displayBoard();
            break;
        case 2:
            displayHelp();
            break;
        default:
            cout << "Please enter a valid choice." << endl;
            break;
    }
    playGame();
    
    }
    

    then in playGame

    void playGame(){
    int plW = 0;
    int plB = 0;
    int x = 0;
    int y = 0;
    char player = 'B';
    
    cout << "\n\nScore: W = " << plW << " B = " << plB;
    cout << "\nPlayer: " << player;
    cout << "\nPlease make your move : ";
    cin >> x >> y;
    
    board[x][y]=player;
    }
    

    To display the board:

    displayBoard(){
    
        for (int row = 0; row < 8; row++){
            cout << "   |";
    
            for (int column = 0; column < 8; column++){
                 cout << board[row][column] << "   |";
            }
            cout << endl;
        }
    }
    

    Not sure about the formatting here, you can fix that yourself I think.

    Lastly you need to repeatedly call playGame() and diplayBoard(), so put your switch statement into a while loop with a break condition. This means you will continue to query the player for input (remember to switch the player), read his move and put it in your array, and display the board. Break out of the while loop when the user types Exit, or something along those lines.