修改数组的值
Changing values of arrays
我是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 << "Othellonn"
<< "1.New Gamen2.Helpn3.QuitnYour Choice: ";
cin >> answer;
system("clear");
return answer;
}
char displayHelp(){
char answer = ' ';
cout << "How to play OthellonnThe object of the game is to have the majority of your colour discs on the board at the end of the game.nnInput the cell where you want to place your disc in the form of (a-z 1-8) without the bracket and includng the space.nnThe one with the most discs wins!!!!nnSo, are you ready to play? (y or n)nnYour 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 << "nnScore: 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之前声明game board数组,使其具有全局作用域,这样你就可以在这个文件中的任何函数中引用它。
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 << "nnScore: 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循环
相关文章:
- 为什么可以修改数组 b?
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 如何修改代码以打印出数组 v2
- 使用指针算法修改函数中的 2D 数组
- 当使用我在一个函数、另一个函数中修改的数组时,在我的输出中得到一个奇怪的负数
- 将数组传递给递归函数,同时C++修改它
- 数组在 C++ 中不使用 static in 函数时不会修改
- C++:通过函数修改数组
- 修改字符串数组中的字符串
- 我在代码中收到一个运行时错误,该错误如何通过修改最多一个元素来查找数组是否可以变得不减少
- 我正在尝试修改一个字节数组并通过 JNI 将其从 c++ 发送到 java
- 修改排序的数组或每次对数组进行排序
- 如何修改数组,从中删除空格,然后将其存储在新数组中
- 从字符串修改char数组
- 循环赋值的多维数组:表达式必须是可修改的左值
- 尝试修改数组时出现写访问冲突
- 当我们在 C++ 中的参数中传递它们时,如何修改一些数组而不修改其他数组
- 修改函数内的多维数组
- 使用指标后数组未修改
- 从uint8_t数组修改数据非常慢