【发布时间】:2019-11-27 22:06:08
【问题描述】:
您好,我是 C++ 新手,我想构建一个 tictactoe 游戏,但是当我编译游戏时,它总是给我这个错误,即我的变量未在范围内声明。然后我尝试将 square 作为全局变量,但这给了我错误。
有人可以帮我吗?
include <iostream>
using namespace std;
void Game();
void Spielerfeld();
void Game(){
int player;
int choice;
char mark;
Spielerfeld();
do{
cout << "Wer ist als erstes in der Reihe\n" << choice;
player = 1;
cout << "Player:" << player << "Gebe eine nummer ein";
cin >> choice;
mark = (player);'X';'O';
if(choice == 1 && square[1] == '1')
square[1] == mark;
else if(choice == 2 && square[2] == '2')
square[2] = mark;
else if(choice == 3 && square[3] == "2")
square[3] = mark;
else if(choice == 4 && square[4] == '4')
sqaure[4] = mark;
else if(choice == 5 && square[5] == "5")
sqaure[5] == mark;
else if(choice == 6 && square[6] == "6")
sqaure[6] == mark;
else if(choice == 7 && square[7] == "7")
sqaure[7] == mark;
else if(choice == 8 && square[8] == "8")
sqaure[8] == mark;
else if(choice == 9 && square[9] == "9")
sqaure[9] == mark;
}while(choice == 9);
}
void Spielerfeld(){
int square[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int i;
for(i=0; i<10; i++){
cout << square[i] << endl;
}
cout << square[1] << "|" << square[2] << "|" << sqaure[3];
cout << "-----------|--------";
cout << square[4] << "|" << square[5] << "|" << sqaure[6];
cout << "-----------|--------";
cout << square[7] << "|" << square[8] << "|" << sqaure[9];
}
int main(){
Spielerfeld();``
Game();
}
【问题讨论】:
-
如果您添加了 a) 确切的编译器错误消息 b) 您尝试将 squares 设为全局,将会很有帮助。但是,在 main 中将其作为变量并将其传递给其他两个函数可能更容易
-
您在
Game()中使用square,尽管它是Spielerfeld()中的局部变量。 -
对不起,我忘了。那是错误 tictactoe.cpp:31:5: error: ‘sqaure’ is not declared in this scope sqaure[6] == mark; tictactoe.cpp:49:51: 错误: 'sqaure' 未在此范围内声明 cout
-
按照经典,比较合适的井字游戏looks something like this.
-
感谢兄弟,这对我的游戏运行很有帮助。
标签: c++ tic-tac-toe