更改数组的值

时间:2015-07-29 13:47:35

标签: c++ arrays cin reversi

我是c ++的新手,因为我的第一个任务是制作一个逆转游戏,但是当涉及到playGame函数中玩家的输入时,我遇到了问题。因此,当我查看x和y时,它会将板数组[x] [y]的值从空或''更改为'B'。我也不知道如何将数组引用到函数中。如果这些问题对某些人来说很愚蠢,我很抱歉,但请原谅我,我在这里自学。感谢

#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';
        }
    }

}

1 个答案:

答案 0 :(得分:1)

要存储来自cin的输入并在游戏板中保持它,您需要使用playGame()函数来引用游戏板。你可以在main之前声明你的游戏板数组,使其在范围内是全局的,这样你就可以在这个文件的任何函数中引用它。

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();

}

然后在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;
}

显示电路板:

displayBoard(){

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

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

不确定这里的格式,你可以自己解决这个问题。

最后你需要反复调用playGame()和diplayBoard(),所以把你的switch语句放到一个带有break条件的while循环中。这意味着您将继续查询播放器的输入(记得切换播放器),读取他的动作并将其放入阵列中,然后显示电路板。当用户键入Exit时,或者沿着这些行键入某些内容时,会跳出while循环。