我正在从书中学习c ++"跳入c ++"在enum和switch-case章节之后,它说我们应该写一个Tictactoe游戏 我通过声明一个tictactoe枚举器来编写游戏,其中包括三个状态为空白,o和x,并将9个板位置声明为空白。但在测试程序后,我看到我无法将电路板位置从空白改为任何东西。这是我的错误还是枚举的工作方式?
例如:
enum Tictactoe{
TTT_O,
TTT_X,
TTT_Blank};
Tictactoe Board1 = TTT_Blank;
//goes all the way until board position 9
int input;
cout << "X, chose a pile which you want to mark << endl;
cin>> input;
switch (input) {
case 1: { Tictactoe Board1 = TTT_X; break; }
//again, goes all the way until all the board positions are checked.
在代码中,它每两次移动一次打印电路板,但它总是将所有电路板位置显示为空白。
答案 0 :(得分:4)
你想要
case 1: { Board1 = TTT_X; break; }
这样你就改变了现有的变量。使用Tictactoe Board1 = TTT_X;
,您将声明一个新变量,然后立即将其销毁。