gpt4 book ai didi

c++ - 霸气C++游戏

转载 作者:行者123 更新时间:2023-12-02 10:19:58 29 4
gpt4 key购买 nike

我正在为自己的类(class)做一个霸气游戏,我一直在尝试通过添加许多void函数来做一些新的事情,但由于某种奇怪的原因,我的开发板无法正常工作,因为它说标识符“board”未定义,但是我有到目前为止,我的董事会从来没有真正遇到过问题,而且我还没有弄清楚。错误在void playAt()函数中。

#include <iostream>
#include <Windows.h>
#include <vector>
using namespace std;
int horizontal = 0;
int vertical = 0;
int row = 0;
int column = 0;
int x = row = 0;
int y = column = 0;
bool gameOver;
bool player = horizontal && vertical;
bool islegal;

void Setup()
{
gameOver = false;
}
void Draw()
{
// specifty default value to fill the vector elements
int mapSize = 0;

cout << "Enter the size of board => ";
cin >> mapSize;

vector<vector<char> > board(mapSize, vector<char>(mapSize, 'e'));
for (int i = 0; i < mapSize; i++) {
for (int j = 0; j < mapSize; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}
void play()
{
if (player == horizontal) {
cout << ("Horizontal to play");
}
else {
cout << ("Vertical to play");
}
}
void playAt(int row, int column, bool player)
{
board[row][column] = true;
if (player == horizontal) {
board[x][y + 1] = true;
board[x][y + 2] = true;
}
else {
board[x + 1][y] = true;
board[x + 2][y] = true;
}
}
void Input()
{
}
void Logic()
{
}
int main()
{
Setup();
while (!gameOver) {
Draw();
Input();
Logic();
}
}

最佳答案

当我们声明name(例如您的board)时,我们会在特定的scope中声明。在此范围之外,该名称不可访问。您的vector<vector<char>> board名称在函数Draw()及其作用域内是本地的,因为它仅在此处定义和声明。在该函数范围之外,代码的其他部分不可见。

一种快速(但值得商de)的解决方案是在全局范围内声明board名称,每个人都可以看到它,例如,在void Setup()函数定义之前:

std::vector<std::vector<char>> board;

更好的解决方案是在 main()函数中声明并初始化它,并将其作为参数传递给其他函数,在这种情况下,您需要更改函数签名。

就是说, using namespace std;的使用是 best avoided

关于c++ - 霸气C++游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60661852/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com