Tic Tac Toe编程帮助C ++

时间:2014-02-06 02:54:53

标签: c++

我的主要问题是,如何让代币(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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

看起来你应该在'checkMove'函数中插入适当的值:

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

其中'value'为-1或1,具体取决于您是否插入X或O.函数原样没有关于谁添加标记的任何信息,因此您可以添加另一个参数传递'value'的函数。