c++ - 改变数组的值

标签 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';
        }
    }

}

最佳答案

要存储来自 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();

}

然后在玩游戏

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 语句放入带有中断条件的 while 循环中。这意味着您将继续查询玩家的输入(记得切换玩家),读取他的移动并将其放入您的数组中,并显示棋盘。当用户键入 Exit 或类似的内容时,跳出 while 循环。

关于c++ - 改变数组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31702348/

相关文章:

c++ - 用于生成行人检测 ROI 的图像调整大小与图像金字塔 - OpenCV

C++ 延迟日志记录(无 C++11)

java - 获取一个数组,判断该数字是否更接近右/左零

Java - 在无限循环中返回多个字符串用户输入

c++ - 使用 fstream 将文件中的一行读取到不同的变量中

c++ - 尝试在 C++ 中读取一行键盘输入

c++ - 测试 IPv6 应用程序

c++ - 使用(未)捕获的异常进行堆栈展开

java - 使用数组在两组之间进行 IPL 比赛

c++ - 如果用户输入无效值,为什么从 cin 读取的代码会无限递归?