2D数组的值未被switch语句更改。

huangapple go评论62阅读模式
英文:

Value of 2D Array not being change by switch statement

问题

我尝试编写一个井字棋游戏,但是某种原因我的switch函数似乎没有运行。我做错了什么?

#include <iostream>

using namespace std;

int n = 0;
int Player[2] {1, 2};
int moveChosen=0;
int n1 = 0, n2 = 0;

void Display(int grid[][3], int row, int column)
{
    cout << "Coordinates: \n";
    cout << "1 | 2 | 3\n";
    cout << "--|---|--\n";
    cout << "4 | 5 | 6\n";
    cout << "--|---|--\n";
    cout << "7 | 8 | 9\n";

    cout << "Current Moves: \n";
    cout << grid[0][0] << " | " << grid[0][1] << " | " << grid[0][2] << "\n";
    cout << "--|---|--\n";
    cout << grid[1][0] << " | " << grid[1][1] << " | " << grid[1][2] << "\n";
    cout << "--|---|--\n";
    cout << grid[2][0] << " | " << grid[2][1] << " | " << grid[2][2] << "\n";
}

int moveChoice(int Choice)
{
    cout << "Player " << Player[n] << " please pick where to move. ";
    cin >> Choice;

    if (Choice < 1 || Choice > 9)
    {
        while (Choice < 1 || Choice > 9)
        {
            cout << "Invalid choice, please enter a new number: ";
            cin >> Choice;
        }
    }

    return Choice;
}

void CheckMoveValid(int grid[][3], int Choice)
{
    switch (Choice)
    {
        case 1:
            if(grid[0][0] != 0)
            {
                cout << "Invalid move, space already filled. ";
            }
            else
            {
                moveChosen = Player[n];
                n1 = 0;
                n2 = 0;
                break;
            }
            break;
        case 2:
            if(grid[0][1] != 0)
            {
                cout << "Invalid move, space already filled. ";
            }
            else
            {
                moveChosen = Player[n];
                n1 = 0;
                n2 = 1;
                break;
            }
    }
}

int main()
{
    int grid[3][3]={{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
    int Choice, CheckWin = 0;

    while (CheckWin < 1)
    {
        Display(grid, 3, 3);

        moveChoice(Choice);

        CheckMoveValid(grid, Choice);

        if (n == 0)  // 请注意,这里应该使用双等号来比较,而不是赋值。
        {
            n = 1;
        }
        else
        {
            n = 0;
        }
        grid[n1][n2] = Player[n];
    }
    return 0;
}

在你的代码中,有一个小错误,即在if (n=0)中应该使用双等号if (n == 0)来比较n的值。此外,你的switch语句似乎只包括了两种情况(case 1case 2),你需要继续添加其他情况以处理不同的选择。如果在第一个case中没有触发"Invalid move, space already filled.",那可能是因为grid[0][0]的值一直为0。

英文:

I am trying to code a game of Tic-Tac-Toe, but for some reason my switch function doesn't appear to be running. What am I doing wrong?

#include &lt;iostream&gt;
using namespace std;
int n = 0;
int Player[2] {1, 2};
int moveChosen=0;
int n1 = 0, n2 = 0;
void Display(int grid[][3], int row, int column)
{
cout&lt;&lt;&quot;Coordinates: \n&quot;;
cout&lt;&lt;&quot;1&quot;&lt;&lt;&quot; | &quot;&lt;&lt;&quot;2&quot;&lt;&lt;&quot; | &quot;&lt;&lt;&quot;3&quot;&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;&quot;--|---|--\n&quot;;
cout&lt;&lt;&quot;4&quot;&lt;&lt;&quot; | &quot;&lt;&lt;&quot;5&quot;&lt;&lt;&quot; | &quot;&lt;&lt;&quot;6&quot;&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;&quot;--|---|--\n&quot;;
cout&lt;&lt;&quot;7&quot;&lt;&lt;&quot; | &quot;&lt;&lt;&quot;8&quot;&lt;&lt;&quot; | &quot;&lt;&lt;&quot;9&quot;&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;&quot;Current Moves: \n&quot;;
cout&lt;&lt;grid[0][0]&lt;&lt;&quot; | &quot;&lt;&lt;grid[0][1]&lt;&lt;&quot; | &quot;&lt;&lt;grid[0][2]&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;&quot;--|---|--\n&quot;;
cout&lt;&lt;grid[1][0]&lt;&lt;&quot; | &quot;&lt;&lt;grid[1][1]&lt;&lt;&quot; | &quot;&lt;&lt;grid[1][2]&lt;&lt;&quot;\n&quot;;
cout&lt;&lt;&quot;--|---|--\n&quot;;
cout&lt;&lt;grid[2][0]&lt;&lt;&quot; | &quot;&lt;&lt;grid[2][1]&lt;&lt;&quot; | &quot;&lt;&lt;grid[2][2]&lt;&lt;&quot;\n&quot;;
}
int moveChoice(int Choice)
{
cout&lt;&lt;&quot;Player &quot;&lt;&lt;Player[n]&lt;&lt;&quot; please pick where to move. &quot;;
cin&gt;&gt;Choice;
if (Choice &lt; 1 || Choice &gt; 9)
{
while (Choice &lt; 1 || Choice &gt; 9)
{
cout &lt;&lt; &quot;Invalid choice, please enter a new number: &quot;;
cin &gt;&gt; Choice;
}
}
return Choice;
}
void CheckMoveValid(int grid[][3], int Choice)
{
switch (Choice)
{
case 1:
if(grid[0][0] != 0)
{
cout&lt;&lt;&quot;Invalid move, space already filled. &quot;;
}
else
{
moveChosen = Player[n];
n1 = 0;
n2 = 0;
break;
}
break;           
case 2:
if(grid[0][1] != 0)
{
cout&lt;&lt;&quot;Invalid move, space already filled. &quot;;
}
else
{
moveChosen = Player[n];
n1 = 0;
n2 = 1;
break;
}
}
}
int main()
{
int grid[3][3]={{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
int Choice, CheckWin = 0;
while (CheckWin &lt; 1)
{
Display(grid, 3, 3);
moveChoice(Choice);
CheckMoveValid(grid, Choice);
if (n=0)
{
n = 1;
}
else
{
n = 0;
}
grid[n1][n2] = Player[n];
}
return 0;
}

If I put cout &lt;&lt; &quot;0&quot; &lt;&lt; endl; between line 65 and 66, nothing happens even when I enter 1, and whatever I tried I could not get &quot;Invalid move, space already filled. &quot; to trigger.

答案1

得分: 1

main()函数中,对moveChoice()的调用提示用户输入,但然后丢弃了该输入。main()函数中的Choice变量从未被赋值,因此对CheckMoveValid()的调用会表现出未定义行为

删除moveChoice()中的Choice参数,它是不必要的。让main()函数将moveChoice()的返回值分配给Choice变量,例如:

int moveChoice()
{
    int Choice;
    ...
    cin >> Choice;    
    ...
    return Choice;
}

...

int main()
{
    ...
    int Choice;
    ...
    Choice = moveChoice(); // <--- 这里!!!
    CheckMoveValid(grid, Choice);
    ...
}
英文:

In main(), the call to moveChoice() prompts the user for input, but then discards that input. The Choice variable in main() is never assigned a value, so the call to CheckMoveValid() exhibits undefined behavior.

Get rid of the Choice parameter in moveChoice(), it is not needed. Have main() assign the return value of moveChoice() to the Choice variable, eg:

int moveChoice()
{
int Choice;
...
cin &gt;&gt; Choice;    
...
return Choice;
}
...
int main()
{
...
int Choice;
...
Choice = moveChoice(); // &lt;-- HERE!!!
CheckMoveValid(grid, Choice);
...
}

huangapple
  • 本文由 发表于 2023年2月7日 04:58:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定