【问题标题】:Tic Tac Toe using C++ and multidimensional arrays [closed]井字游戏使用 C++ 和多维数组 [关闭]
【发布时间】:2016-04-01 20:17:51
【问题描述】:

感谢大家的帮助,我是编程新手,不是很熟练,所以感谢您的耐心等待。我让它工作了!代码中有几个问题,从用于检查“游戏结束”的 for 循环和用于设置棋盘的计数器。但最终的代码似乎没有错误。它在控制台中打印出一个带有标记方块的网格,允许两个人一起玩。现在很开心。

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

bool over = false;

void print_board(char board[3][3])
{
    cout<<"\n------------------\n";
    cout<<"\n  "<<board[0][0]<<"  |  "<<board[0][1]<< "  |  "<<board[0][2];
    cout<<"\n------------------\n";
    cout<<"\n  "<<board[1][0]<<"  |  "<<board[1][1]<< "  |  "<<board[1][2];
    cout<<"\n------------------\n";
    cout<<"\n  "<<board[2][0]<<"  |  "<<board[2][1]<< "  |  "<<board[2][2];
    cout<<"\n------------------\n";
}

bool checkForCorrectMove (char board[3][3], int choice, bool player1Turn)
{
    int xCor;
    int yCor;
    xCor = ((choice + 2) / 3) - 1; 
    yCor = (choice - 1) % 3;
    if (((board[xCor][yCor]) != 'X') && ((board[xCor][yCor]) != 'O'))
    {
        if (player1Turn)
            board[xCor][yCor] = 'X';
        if (!player1Turn)
            board[xCor][yCor] = 'O';
        return true;
    }
    else 
        return false;
}

bool checkRow(char board[3][3])
{
    for (int i = 0; i<3; i++)
    {
        if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]))
        {
            return true;
        }
    }
    return false;
}
bool checkColumn (char board[3][3])
{
    for (int i = 0; i<3; i++)
    {
        if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]))
        {
            return true;
        }
    }
    return false;
}
bool checkForWin (char board[3][3])
{
    bool win = false;
    bool line_win = false;
    if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))
    {
        win = true;
        return win;
    }
    if ((board[2][0] == board[1][1]) && (board[1][1] == board[0][2]))
    {
        win = true;
        return win;
    }

    if (checkColumn(board) || checkRow (board))
    {
        win = true;
        return win;
    }
    return win;
}

int main()
{
    cout << "Welcome to Tic Tac Toe!\n";
    int choice;
    int boardcounter = 1;
    bool isOkayToMove = true;
    bool player1Turn = true;
    char board[3][3];
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            board[i][j] = boardcounter + '0';
            boardcounter++;
        }
    }
    print_board(board);
    do {
        if (player1Turn)
        {
            cout <<"\n\nPlayer 1, please enter a number that corresponds to an open space: ";
        }
        else
        {
            cout <<"\n\nPlayer 2, please enter a number that corresponds to an open space: ";
        }
        cin>>choice;
        isOkayToMove = checkForCorrectMove(board, choice, player1Turn);
        if (isOkayToMove)
        {
            over = checkForWin(board);
            print_board(board);
            player1Turn = !player1Turn;
        }
        else
        {
            cout <<"\n\nYou have attempted to move into a space that is already occupied, please try again.";
            print_board(board);
        }
    }
    while (over == false);
    cout <<"\n\nCongratulations!";
    player1Turn = !player1Turn;
    if (player1Turn)
        cout<<" Player 1 has won!";
    else
        cout<<" Player 2 has won!";
}

我正在尝试用 C++ 创建一个井字游戏,但我很挣扎。我知道以前有人问过这个问题,但是通过查看其他人的示例很难弄清楚我的特定代码(我正在学习 C++)有什么问题。代码如下:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

bool over = false;

void print_board(char board[3][3])
{
    cout<<"\n------------------\n";
    cout<<"\n  "<<board[0][0]<<"  |  "<<board[0][1]<< "  |  "<<board[0][2];
    cout<<"\n------------------\n";
    cout<<"\n  "<<board[1][0]<<"  |  "<<board[1][1]<< "  |  "<<board[1][2];
    cout<<"\n------------------\n";
    cout<<"\n  "<<board[2][0]<<"  |  "<<board[2][1]<< "  |  "<<board[2][2];
    cout<<"\n------------------\n";
}

bool checkForCorrectMove (char board[][3], int choice, bool player1Turn)
{
    int xCor;
    int yCor;
    switch(choice)
    {
        case 1: xCor = 0, yCor = 0;
        case 2: xCor = 0, yCor = 1;
        case 3: xCor = 0, yCor = 2;
        case 4: xCor = 1, yCor = 0;
        case 5: xCor = 1, yCor = 1;
        case 6: xCor = 1, yCor = 2;
        case 7: xCor = 2, yCor = 0;
        case 8: xCor = 2, yCor = 1;
        case 9: xCor = 2, yCor = 2;
    }
    if (((board[xCor][yCor]) != 'X') && ((board[xCor][yCor]) != 'O'))
    {
        if (player1Turn)
            board[xCor][yCor] = 'X';
        if (!player1Turn)
            board[xCor][yCor] = 'O';
        return true;
    }
    else 
        return false;
}

bool checkForWin (char board[][3])
{
    bool win = false;
    if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))
    {
        win = true;
    }
    else if ((board[2][0] == board[1][1]) && (board[1][1] == board[0][2]))
    {
        win = true;
    }
    for (int i = 0; i<3; i++)
    {
        for (int j = 0; i < 3; j++) 
        {
            if (board[i][j] != board [i][0]);
            win = false;
        }
    }
    for (int i = 0; i<3; i++)
    {
        for (int j = 0; i < 3; j++) 
        {
            if (board[i][j] != board [0][j]);
            win = false;
        }
    }
    return win;
}

int main()
{
    cout << "Welcome to Tic Tac Toe!\n";
    int choice;
    int boardcounter = 1;
    bool isOkayToMove = true;
    bool player1Turn = true;
    char board[3][3];
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            board[i][j] = (char) boardcounter;
            boardcounter++;
        }
    }
    print_board(board);
    do {
        if (player1Turn)
        {
            cout <<"\n\nPlayer 1, please enter a number that corresponds to an open space: ";
        }
        else
        {
            cout <<"\n\nPlayer 2, please enter a number that corresponds to an open space: ";
        }
        cin>>choice;
        switch(choice);
        isOkayToMove = checkForCorrectMove(board, choice, player1Turn);
        if (isOkayToMove)
        {
            print_board(board);
            over = checkForWin(board);
            player1Turn = !player1Turn;
        }
        else
        {
            cout <<"\n\nYou have attempted to move into a space that is already occupied, please try again.";
            print_board(board);
        }
    }
    while (over == false);
    cout <<"\n\nCongratulations!";
    if (player1Turn)
        cout<<" Player 1 has won!";
    else
        cout<<" Player 2 has won!";
}

【问题讨论】:

  • “我在挣扎”是一个不合适的问题描述。我可以告诉你,你的switch 在每个case 上都缺少breaks(它们本身有一些有问题的赋值语句——你从哪里听说C++ 用逗号分隔语句?)但是,除此之外,你要去需要提供一个 MCVE 来解释实际问题是什么......
  • 那有什么问题呢?如果它没有编译,您应该发布错误消息。如果它没有按预期运行,请说明您的预期和实际得到的结果。
  • @JonathanMa - 你的switch可以压缩成xCor = ((choice + 2) / 3) - 1; yCor = choice % 3;
  • 它可以编译——但现在,问题在于 print_board 函数以及我如何初始化板。它打印两行乱码和第三行空。
  • 您可以使用for 语句来打印板。循环结构对数组(包括二维数组)非常有用。

标签: c++ arrays multidimensional-array


【解决方案1】:

这是我发现的一个问题:

    int boardcounter = 1;
//...
        for (int j = 0; j < 3; j++)
        {
            board[i][j] = (char) boardcounter;
            boardcounter++;
        }

问题在于演员(char) 不会将int 变量转换为文本 表示。强制转换实际上将整数转换为更小的整数变量。

有很多方法可以将数字转换为字符,例如snprintftostringostringstream

由于您的范围有限,即数字 0 - 9,您可能能够逃脱:

board[row][column] = '0' + boardcounter;

我建议您查看您的代码并找到其他将整数转换为char 的地方并相应地进行更改。

顺便说一句,大多数井字游戏使用 ' '、'X' 和 'O' 作为值。

【讨论】:

  • 感谢您的回复,这是个大问题。但我没有使用空格,因为数字使用户能够输入标签来更改板。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多