【问题标题】:Tic Tac Toe Programming help C++井字游戏编程帮助 C++
【发布时间】:2014-02-06 11:33:55
【问题描述】:

我的主要问题是,如何让标记(X 和 O)出现在游戏板上?我必须向用户询问行号和列号,然后令牌就会放在那里。您可能会说,我是编程新手。我已经在这几个小时了,我只是想完成。在这里问是我最后的手段。如果任何代码可以以不同的方式或更有效地完成,或者没有必要,或者我做错了什么,请告诉我。我没主意了。我只是想完成这个程序,所以你越详细越好。我非常感激。我很困惑,真的需要建议来完成这个。

到目前为止我的代码:

#include <iostream>
using namespace std;

void createBoard(int board[][3], char charBoard[][3]);
void printBoard(char charBoard[][3]);
int checkWinner (int board[][3], int player);
bool checkMove(int board[][3], int row, int column);
void resetBoard(int board[][3]);

int main ()
{
    int board[3][3];
    char charBoard[3][3];
    int player;
    int row, column;
    bool win = false;
    char again = 'y';

    while (win == false)
    {
        createBoard(board, charBoard);
        printBoard(charBoard);
        cout << "Player O's turn!" << endl;
        cout << "Enter row number: "; cin >> row;
        cout << "Enter column number: "; cin >> column;
        checkMove(board, row, column);
        //update board
        win = checkWinner(board, -1);
        if (checkWinner (board, -1)  == true)
        {
            cout << "Player O wins!" << endl;
            break;
        }
        //set flag to denote it's x's turn
        printBoard(charBoard);
        cout << "Player X's turn!" << endl;
        cout << "Enter row number: "; cin >> row;
        cout << "Enter column number: "; cin >> column;
        checkMove(board, row, column);
        //update board
        win = checkWinner (board, 1);
        if (checkWinner (board, 1) == true)
        {
            cout << "Player X wins!" << endl;
            break;
        }


        cout << endl << endl;
        cout << "Would you like to play again? (y/n): ";
        cin >> again;

        }
        cout << endl << endl;
        cout << "Good bye!" << endl;

        return 0;
}



void createBoard(int board[][3], char charBoard[][3])
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            // if game board has a -1, put an "O" in the character board
            if (board[i][j]== -1)
            {
                charBoard[i][j] = 'O';
            }
            // if game board has a 1, put an "X" in the character board
            else if (board[i][j]== 1)
            {
                charBoard[i][j] = 'X';
            }
            // if game board has a 0, put an " " (blank space) in the character board
            else
            {
                charBoard[i][j] = ' ';
            }
        }
    }
}

void printBoard(char charBoard[][3])
{
    cout << "       Tic Tac Toe!" << endl << endl;
    cout << "          Column\n";
    cout << "       0     1     2" << endl << endl;
    cout << "R 0    " << charBoard[0][0] << "  |  " << charBoard[0][1] << "  |  " << charBoard[0][2] << endl;
    cout << "    --------------------" << endl;
    cout << "O 1    " << charBoard[1][0] << "  |  " << charBoard[1][1] << "  |  " << charBoard[1][2] << endl;
    cout << "    --------------------" << endl;
    cout << "W 2    " << charBoard[2][0] << "  |  " << charBoard[2][1] << "  |  " << charBoard[2][2] << endl << endl;
}

int checkWinner (int board[][3], int player)
{
    int sum;

    // Check each column for a winner
    for (int i = 0; i < 3; i++)
    {
        sum = board[i][0] + board[i][1] + board[i][2];
        // if O is a winner
        if (sum == 3*player)
        {
            return true;
        }
    }

    // Check each row for a winner
    for (int j = 0; j < 3; j++)
    {
        sum = board[0][j] + board[1][j] + board[2][j];
        // if O is a winner
        if (sum == 3*player)
        {
            return true;
        }
    }
    // Check the back and front diagonal
    sum = board[0][0] + board[1][1] + board[2][2];
    if (sum == 3*player)
    {
        return true;
    }

    sum = board[0][2] + board[1][1] + board[2][0];
    if (sum == 3*player)
    {
        return true;
    }

    return false;
}

bool checkMove(int board[][3], int row, int column)
{
    if ((row < 0) || (row > 2))
    {
        return false;
    }
    else if ((column < 0) || (column > 2))
    {
        return false;
    }
    else if (board[row][column] == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void resetBoard(int board[][3])
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            board[i][j] = 0;
        }
    }
}

【问题讨论】:

  • 我正在考虑你的问题,但应该提到if (checkWinner (board, 1) == true) 是多余的,if (checkWinner (board, 1)) 会做同样的事情。
  • 我认为您还有其他问题。似乎在玩家获胜后,代码退出,并且在每回合之后(在有人获胜之前)他们被询问是否要再次玩。

标签: c++


【解决方案1】:

看起来您应该在“checkMove”函数中插入适当的值:

else if (board[row][column] == 0)
{
    board[row][column] = value;    // value -1 or 1
    return true;
}

“值”是 -1 或 1,具体取决于您插入的是 X 还是 O。函数本身没有任何关于谁添加标记的信息,因此您可以添加另一个参数传入“值”的函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多