作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为自己的类(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/
我是一名优秀的程序员,十分优秀!