【问题标题】:tictactoe game variable not declared in the scope [closed]tictactoe 游戏变量未在范围内声明[关闭]
【发布时间】: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


【解决方案1】:

1) Square 是 Spielerfeld() 函数中的一个局部变量,它在函数结束后销毁,所以你不能使用它。

2) 您将“Square”定义为内置数组,并且内置数组元素从零索引开始。所以当你想访问第一个元素时,你应该使用 Square[0]。

我将 Square 定义为全局变量并修改了您的代码。 现在试试看。


#include <iostream>

using namespace std;
void Game();
void Spielerfeld();
int square[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

void Game() 
{
    int player = 0;
    int choice = 0;
    char mark = 0;

    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[0] == 1)
            square[0] = mark;
        else if (choice == 2 && square[1] == 2)
            square[1] = mark;
        else if (choice == 3 && square[2] == 2)
            square[2] = mark;
        else if (choice == 4 && square[3] == 4)
            square[3] = mark;
        else if (choice == 5 && square[4] == 5)
            square[4] = mark;
        else if (choice == 6 && square[5] == 6)
            square[5] = mark;
        else if (choice == 7 && square[6] == 7)
            square[6] = mark;
        else if (choice == 8 && square[7] == 8)
            square[7] = mark;
        else if (choice == 9 && square[8] == 9)
            square[8] = mark;
    } while (choice == 9);
}


void Spielerfeld() {

    int i;

    for (i = 0; i < 9; i++) 
    {
        cout << square[i] << endl;
    }

    cout << square[0] << "|" << square[1] << "|" << square[2];
    cout << "-----------|--------";
    cout << square[3] << "|" << square[4] << "|" << square[5];
    cout << "-----------|--------";
    cout << square[6] << "|" << square[7] << "|" << square[8];

}

int main() 
{
    Spielerfeld();
    Game();
}

【讨论】:

    【解决方案2】:

    您遇到的简单问题。在 C++ 中,== 运算符和 = 运算符之间存在差异。简单地说,== 用于相等比较= 用于变量赋值。所有那些说square[i] == mark; 的行都应该说square[i] = mark;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 2016-09-18
      • 2017-02-12
      • 1970-01-01
      相关资源
      最近更新 更多