2301_81503119 2024-05-27 09:20 采纳率: 0%
浏览 19
已结题

三子连珠对弈小游戏制作




#include <iostream>
using namespace std;

void PrintBoard();   // game board
void PrintInfo();  // some info about the rules of the game is printed
void PrintStats(int, int, int);  // prints the stats of the  match
void IfError(int&, int&); // When invalid input is entered, prints the error
void ChoiceOfChar(char&);  // Asks the users if they want to play again
void PromptTurnO(int&, int&);  // Prompts the first user for data
void PromptTurnX(int&, int&);  // Second user has to input coordinates
char board[3][3];  // array definition
int main()
{
    int ROW;  // elements of 2 dimensional array!
    int COLUMN;
    int FirstPlayer;  // number of winning games for first player
    int SecondPlayer;
    int Draws;  // number of draws in a game
    FirstPlayer = 0;
    SecondPlayer = 0;
    Draws = 0;
    char choice;
    choice = 'Y'; // Initial value is Y in order to initialize the main loop
    PrintInfo();
    while (choice == 'Y') // In case the players want to play again
    {
        // this is the main loop
        for (ROW = 0; ROW < 3; ROW++)  // these forloops are used to process the array
            for (COLUMN = 0; COLUMN < 3; COLUMN++)  // by row and column
                board[ROW][COLUMN] = ' ';   // blank characters as initial values
        int x;     // (x,y) coordinates entered by the users
        int y;
        int SpotsOnBoard;  // this is the sum of spots available on the game board
        SpotsOnBoard = 0;
        while (SpotsOnBoard <= 9)  // because there is only 9 characters available on the board
        {
            if (SpotsOnBoard == 0)
                PrintBoard();   // This function draws initialy the blank game board
            // so it is easier for the user to pick the x and y values

            PromptTurnO(x, y);

            IfError(x, y); // If the user's input is invalid, this function prints
            // the error message and prompts for correction
            board[x - 1][y - 1] = 'O';   // actually the array starts from 0, NOT from 1
            SpotsOnBoard++;    // (as entered by the user)
            // so the program have to "fix" the input
            PrintBoard();   // If 3 coordintates are O's in one of 8 possible
            //winning combinations, then O wins
            if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
            {
                cout << " O player wins the game" << endl;
                FirstPlayer++;
                break;
            }
            else if (board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == 'O')
            {
                cout << "O player wins the game" << endl;
                FirstPlayer++;
                break;
            }
            else if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')
            {
                cout << "O player wins the game" << endl;
                FirstPlayer++;
                break;
            }
            else if (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')
            {
                cout << "O player wins the game" << endl;
                FirstPlayer++;
                break;
            }
            else if (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')
            {
                cout << "O player wins the game" << endl;
                FirstPlayer++;
                break;
            }
            else if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
            {
                cout << "O player wins the game" << endl;
                FirstPlayer++;
                break;
            }
            else if (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
            {
                cout << "O player wins the game" << endl;
                FirstPlayer++;
                break;
            }
            else if (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
            {
                cout << "O player wins the game" << endl;
                FirstPlayer++;  // counts the number of won games
                break;   // break statement is used to make sure that only one possibility
            }                                   // will be processed
            else if (SpotsOnBoard == 9)
            {
                cout << "Draw!" << endl;
                Draws++;
                break;    // Without this brake statement it was not working propertly
            }

            PromptTurnX(x, y);
            IfError(x, y);
            board[x - 1][y - 1] = 'X';
            SpotsOnBoard++;
            PrintBoard();
            if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
            {
                cout << "X player is victorious" << endl;
                SecondPlayer++;
            }
            else if (board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X')
            {
                cout << "X player is victorious" << endl;
                SecondPlayer++;
            }
            else if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
            {
                cout << "X player is victorious" << endl;
                SecondPlayer++;
            }
            else if (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
            {
                cout << "X player is victorious" << endl;
                SecondPlayer++;
            }
            else if (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
            {
                cout << "X player is victorious" << endl;
                SecondPlayer++;
            }
            else if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
            {
                cout << "X player is victorious" << endl;
                SecondPlayer++;
            }
            else if (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
            {
                cout << "X player is victorious" << endl;
                SecondPlayer++;
            }
            else if (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
            {
                cout << "X player is victorious" << endl;
                SecondPlayer++;
            }
        }
        ChoiceOfChar(choice);
    }

    PrintStats(FirstPlayer, SecondPlayer, Draws);

    system("PAUSE");
    return 0;
}

void PrintBoard()
// Post : The Game board is printed to help with choosing the coordintates
//        Either X or O will be printed on the board.
//        Board is updated while the game progresses
{
    cout << endl;
    cout << "  1   2   3 " << endl;
    cout << "1 " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;
    cout << " ---|---|---" << endl;;
    cout << "2 " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;
    cout << " ---|---|---" << endl;
    cout << "3 " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;
    cout << endl;
    cout << endl;
}

void PrintInfo()
// Post : Some info about the rules of the game is printed
{
    cout << "First player is represented by O, second by X." << endl;
    cout << "Enter the coordinates for Rows and Columns" << endl;
    cout << "Strike enter when Done." << endl;
    cout << endl;
}

void PrintStats(int FirstPlayer, int SecondPlayer, int Draws)
// Pre : Players have to decide to exit the game.
// Post : Returns the number of won games and draws.
{
    cout << "The O player have won " << FirstPlayer << " times" << endl;
    cout << "The X player have won " << SecondPlayer << " times" << endl;
    cout << "There was " << Draws << " draws" << endl;
    cout << "Thanks for using my program" << endl;
}

void IfError(int& x, int& y)
// Pre : User entered invalid data, program doesn't kow where to put them
// Post : Error message is printed, user is prompted to enter the right input
{
    while (x > 3 || x < 1 || y > 3 || y < 1 || ('O' == board[x - 1][y - 1]
            || 'X' == board[x - 1][y - 1]))
    {
        cout << "This coordinate is not allowed, try again" << endl;
        cout << "row: ";
        cin >> x;
        cout << "column: ";
        cin >> y;
    }
}

void ChoiceOfChar(char& choice)
// Pre : Entire program has been executed
// Post : User is given a chance to play again
{
    cout << endl;
    cout << " Press CAPITAL Y if you want to play again." << endl;
    cout << " Otherwise, press any other letter key to exit the loop." << endl;
    cin >> choice;
}

void PromptTurnO(int& x, int& y)
// Post : First player enters the coordinates of a spot on a game board
{
    cout << "Turn of the first player (O), enter the coordinates" << endl;
    cout << "Row: ";
    cin >> x;
    cout << "Column: ";
    cin >> y;
}

void PromptTurnX(int& x, int& y)
// Post : Second player enters the coordinates of a spot on a game board
{
    cout << "Turn of the second player (X), enter the coordinates" << endl;
    cout << "Row: ";
    cin >> x;
    cout << "Column: ";
    cin >> y;

```该程序是TICTACTOE游戏,棋盘为3*3,三个棋子连起来为赢。输入参数时,若输入的数字超出边界或在已有棋子的地方再输入,则提示出错,要求重新输入。 具体要求如下: 将程序改写成类。 将棋盘扩充为4*4;如果棋牌规格玩家可以定制更好,例如,玩家可以定制4*415*15等规格的棋牌。 统计输赢结果 打印输出。对弈算法流程如下: 两个玩家可以下棋,第一次下棋之前首先将棋盘清空;以后每次下棋时都要判断棋盘是否已满。 下棋时需要判断当前棋盘的棋子是否已满。 下棋时指定规则,由谁先下棋,下棋时判断下棋的坐标是否正确,如果不正确提示玩家继续下棋;当下棋时的坐标正确时,保存棋子的坐标和当前已下棋子的总数。 每一次下棋都要判断当前玩家的棋子是否已经连线(横、竖、对角线),当连成线时,则当前玩家赢。 如果当前玩家棋子没有连成线,则提示另一方玩家下棋,下棋过程判断同第34步。 结束一局后,判断是否进行下一局。该程序是TICTACTOE游戏,棋盘为3*3,当三个棋子连起来即为赢。输入参数时,若输入的数字超出边界或在已有棋子的地方再输入,则提示出错,要求重新输入。
具体要求如下:
将程序改写成类。
将棋盘扩充为4*4;如果棋牌规格玩家可以定制更好,例如,玩家可以定制4*415*15等规格的棋牌。
统计输赢结果 打印输出

```

  • 写回答

26条回答 默认 最新

  • 阿里嘎多学长 2024-05-27 09:23
    关注
    获得0.15元问题酬金

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    要将现有的TICTACTOE游戏程序改写为面向对象的形式,并进行扩展和改进,我们可以创建一个名为TicTacToe的类,该类将包含棋盘、玩家统计、游戏逻辑等属性和方法。下面是一个基本的类实现示例:

    #include <iostream>
    #include <vector>
    #include <string>
    
    class TicTacToe {
    private:
        std::vector<std::vector<char>> board;
        int rows, cols;
        int FirstPlayerWins, SecondPlayerWins, Draws;
        bool isFull() const;
        bool checkWin(int row, int col, char player) const;
        void printBoard() const;
        void resetBoard();
    
    public:
        TicTacToe(int rows = 3, int cols = 3) : rows(rows), cols(cols), FirstPlayerWins(0), SecondPlayerWins(0), Draws(0) {
            board.resize(rows, std::vector<char>(cols, ' '));
        }
    
        void startGame();
        void playTurn(int& row, int& col, char player);
        void printStats() const;
    };
    
    bool TicTacToe::isFull() const {
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (board[i][j] == ' ') return false;
            }
        }
        return true;
    }
    
    bool TicTacToe::checkWin(int row, int col, char player) const {
        // Check horizontal, vertical, and diagonal wins
        // ...
        return false; // Placeholder for win condition
    }
    
    void TicTacToe::printBoard() const {
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                std::cout << board[i][j] << " ";
            }
            std::cout << std::endl;
        }
    }
    
    void TicTacToe::resetBoard() {
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                board[i][j] = ' ';
            }
        }
    }
    
    void TicTacToe::startGame() {
        char choice = 'Y';
        while (choice == 'Y') {
            resetBoard();
            int currentPlayer = 'O';
            while (!isFull()) {
                int row, col;
                playTurn(row, col, currentPlayer);
                if (checkWin(row, col, currentPlayer)) {
                    std::cout << currentPlayer << " player wins!" << std::endl;
                    if (currentPlayer == 'O') FirstPlayerWins++;
                    else SecondPlayerWins++;
                    break;
                }
                currentPlayer = (currentPlayer == 'O') ? 'X' : 'O';
            }
            std::cout << "Press CAPITAL Y to play again or any other key to exit: ";
            std::cin >> choice;
            std::cin.ignore(); // Clear input buffer
        }
        printStats();
    }
    
    void TicTacToe::playTurn(int& row, int& col, char player) {
        std::cout << "Player " << player << ", enter your move (row col): ";
        std::cin >> row >> col;
        // Validate input and handle errors
        // ...
    }
    
    void TicTacToe::printStats() const {
        std::cout << "First player wins: " << FirstPlayerWins << std::endl;
        std::cout << "Second player wins: " << SecondPlayerWins << std::endl;
        std::cout << "Draws: " << Draws << std::endl;
    }
    
    int main() {
        TicTacToe game(4, 4); // Initialize game with 4x4 board
        game.startGame();
        return 0;
    }
    

    在这个示例中,TicTacToe类包含了棋盘、玩家统计、游戏逻辑等。startGame方法用于开始游戏循环,playTurn方法用于玩家下棋,checkWin方法用于检查是否有玩家获胜(需要你根据4x4的规则来实现),printBoard方法用于打印棋盘,resetBoard方法用于重置棋盘,printStats方法用于打印统计信息。

    请注意,这个示例是一个框架,你需要根据具体需求来实现checkWin方法和其他可能需要的功能。此外,错误处理和输入验证也需要你进一步完善。

    至于参考资料,你可以查阅以下资源来获取更多关于面向对象编程和C++的信息:

    • C++ Primer (5th Edition) by Stanley B. Lippman, Josée Lajoie, and Barbara E. Moo
    • Effective Modern C++ by Scott Meyers
    • cppreference.com (在线C++标准库参考)

    这些资源将帮助你更好地理解和实现面向对象的设计和C++编程。

    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月4日
  • 创建了问题 5月27日