• 个人简介

    加油,现场考试一定能过

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main() {
        // 初始化随机数生成器
        srand(time(0));
        
        // 生成1-100的随机数
        int secretNumber = rand() % 100 + 1;
        int guess = 0;
        int attempts = 0;
        
        cout << "欢迎来到猜数字游戏!\n";
        cout << "我已经想了一个1到100之间的数字,你能猜出来吗?\n\n";
        
        do {
            cout << "请输入你的猜测: ";
            cin >> guess;
            attempts++;
            
            if (guess > secretNumber) {
                cout << "太大了!再试一次。\n";
            } else if (guess < secretNumber) {
                cout << "太小了!再试一次。\n";
            } else {
                cout << "\n恭喜你!你在" << attempts << "次尝试后猜对了!\n";
            }
        } while (guess != secretNumber);
        
        cout << "\n游戏结束,谢谢参与!\n";
        return 0;
    }
    
    
    
    ```language
    #include <iostream>
    #include <vector>
    #include <cctype>
    
    using namespace std;
    
    // 游戏板类
    class TicTacToe {
    private:
        vector<vector<char> > board;  // 3x3游戏板
        char currentPlayer;          // 当前玩家 (X或O)
        int moves;                  // 移动次数
    
    public:
        // 构造函数
        TicTacToe() : board(3, vector<char>(3, ' ')), currentPlayer('X'), moves(0) {}
    
        // 打印游戏板
        void printBoard() {
            cout << "\n   0   1   2\n";
            cout << "  -----------\n";
            for (int i = 0; i < 3; i++) {
                cout << i << " ";
                for (int j = 0; j < 3; j++) {
                    cout << " " << board[i][j] << " ";
                    if (j < 2) cout << "|";
                }
                cout << "\n";
                if (i < 2) cout << "  ---+---+---\n";
            }
            cout << "\n";
        }
    
        // 检查是否获胜
        bool checkWin() {
            // 检查行
            for (int i = 0; i < 3; i++) {
                if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
                    return true;
                }
            }
            
            // 检查列
            for (int j = 0; j < 3; j++) {
                if (board[0][j] != ' ' && board[0][j] == board[1][j] && board[1][j] == board[2][j]) {
                    return true;
                }
            }
            
            // 检查对角线
            if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
                return true;
            }
            
            if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
                return true;
            }
            
            return false;
        }
    
        // 检查是否平局
        bool checkDraw() {
            return moves == 9;
        }
    
        // 进行移动
        bool makeMove(int row, int col) {
            if (row < 0 || row > 2 || col < 0 || col > 2) {
                cout << "无效的位置!请选择0-2之间的行和列。\n";
                return false;
            }
            
            if (board[row][col] != ' ') {
                cout << "该位置已被占用!请选择其他位置。\n";
                return false;
            }
            
            board[row][col] = currentPlayer;
            moves++;
            return true;
        }
    
        // 切换玩家
        void switchPlayer() {
            currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
        }
    
        // 获取当前玩家
        char getCurrentPlayer() {
            return currentPlayer;
        }
    };
    
    // 主游戏函数
    int main() {
        TicTacToe game;
        int row, col;
        char playAgain;
        
        cout << "欢迎来到井字棋游戏!\n";
        cout << "玩家X先开始,玩家O随后。\n";
        cout << "输入行和列(0-2)进行移动。\n";
        
        do {
            game = TicTacToe(); // 重置游戏
            
            while (true) {
                game.printBoard();
                cout << "玩家 " << game.getCurrentPlayer() << " 的回合。\n";
                
                // 获取玩家输入
                cout << "输入行和列(空格分隔):";
                cin >> row >> col;
                
                // 处理无效输入
                while (cin.fail()) {
                    cin.clear();
                    cin.ignore(10000, '\n');
                    cout << "无效输入!请输入数字(0-2)。\n";
                    cout << "输入行和列(空格分隔):";
                    cin >> row >> col;
                }
                
                // 尝试移动
                if (game.makeMove(row, col)) {
                    // 检查游戏状态
                    if (game.checkWin()) {
                        game.printBoard();
                        cout << "玩家 " << game.getCurrentPlayer() << " 获胜!\n";
                        break;
                    } else if (game.checkDraw()) {
                        game.printBoard();
                        cout << "平局!\n";
                        break;
                    } else {
                        game.switchPlayer();
                    }
                }
            }
            
            // 询问是否再玩一次
            cout << "再玩一次?(y/n): ";
            cin >> playAgain;
            
        } while (tolower(playAgain) == 'y');
        
        cout << "感谢游玩!\n";
        return 0;
    }
    
    
    #include <iostream>
    #include <vector>
    #include <cstdlib>
    #include <ctime>
    #include <iomanip>
    #include <algorithm>
    
    using namespace std;
    
    const int BOARD_SIZE = 8;
    const int MINE_COUNT = 10;
    
    class Minesweeper {
    private:
        vector<vector<char> > board;      // 玩家可见的棋盘
        vector<vector<char> > solution;   // 包含地雷的解决方案
        vector<vector<bool> > revealed;   // 记录格子是否已翻开
        int remainingCells;
    
        // 初始化棋盘
        void initializeBoard() {
            board = vector<vector<char> >(BOARD_SIZE, vector<char>(BOARD_SIZE, '-'));
            solution = vector<vector<char> >(BOARD_SIZE, vector<char>(BOARD_SIZE, '0'));
            revealed = vector<vector<bool> >(BOARD_SIZE, vector<bool>(BOARD_SIZE, false));
            remainingCells = BOARD_SIZE * BOARD_SIZE - MINE_COUNT;
            
            // 随机放置地雷
            srand(time(0));
            int minesPlaced = 0;
            while (minesPlaced < MINE_COUNT) {
                int row = rand() % BOARD_SIZE;
                int col = rand() % BOARD_SIZE;
                if (solution[row][col] != 'X') {
                    solution[row][col] = 'X';
                    minesPlaced++;
                    
                    // 更新周围格子的数字
                    for (int dr = -1; dr <= 1; dr++) {
                        for (int dc = -1; dc <= 1; dc++) {
                            if (dr == 0 && dc == 0) continue;
                            int nr = row + dr;
                            int nc = col + dc;
                            if (nr >= 0 && nr < BOARD_SIZE && nc >= 0 && nc < BOARD_SIZE) {
                                if (solution[nr][nc] != 'X') {
                                    solution[nr][nc]++;
                                }
                            }
                        }
                    }
                }
            }
        }
    
        // 显示当前棋盘状态
        void displayBoard() {
            cout << "  ";
            for (int i = 0; i < BOARD_SIZE; i++) {
                cout << setw(2) << i << " ";
            }
            cout << "\n";
            
            for (int i = 0; i < BOARD_SIZE; i++) {
                cout << i << " ";
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (revealed[i][j]) {
                        cout << setw(2) << solution[i][j] << " ";
                    } else {
                        cout << setw(2) << board[i][j] << " ";
                    }
                }
                cout << "\n";
            }
        }
    
        // 翻开格子
        void revealCell(int row, int col) {
            if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE || revealed[row][col]) {
                return;
            }
            
            revealed[row][col] = true;
            if (solution[row][col] == 'X') {
                return;
            }
            
            remainingCells--;
            
            // 如果格子周围没有地雷,自动翻开周围的格子
            if (solution[row][col] == '0') {
                for (int dr = -1; dr <= 1; dr++) {
                    for (int dc = -1; dc <= 1; dc++) {
                        if (dr == 0 && dc == 0) continue;
                        revealCell(row + dr, col + dc);
                    }
                }
            }
        }
    
    public:
        Minesweeper() {
            initializeBoard();
        }
    
        // 开始游戏
        void play() {
            cout << "欢迎来到扫雷游戏!\n";
            cout << "输入行和列来翻开格子(例如:3 4)\n";
            cout << "棋盘大小: " << BOARD_SIZE << "x" << BOARD_SIZE << ",地雷数量: " << MINE_COUNT << "\n\n";
            
            while (true) {
                displayBoard();
                
                if (remainingCells == 0) {
                    cout << "\n恭喜!你赢了!\n";
                    break;
                }
                
                int row, col;
                cout << "\n请输入行和列 (0-" << BOARD_SIZE - 1 << "): ";
                cin >> row >> col;
                
                if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
                    cout << "无效的输入,请重新输入!\n";
                    continue;
                }
                
                if (solution[row][col] == 'X') {
                    cout << "\n游戏结束!你踩到地雷了!\n";
                    // 显示所有地雷位置
                    for (int i = 0; i < BOARD_SIZE; i++) {
                        for (int j = 0; j < BOARD_SIZE; j++) {
                            if (solution[i][j] == 'X') {
                                revealed[i][j] = true;
                            }
                        }
                    }
                    displayBoard();
                    break;
                }
                
                revealCell(row, col);
            }
        }
    };
    
    int main() {
        Minesweeper game;
        game.play();
        
        char playAgain;
        cout << "\n再玩一次?(y/n): ";
        cin >> playAgain;
        
        if (playAgain == 'y' || playAgain == 'Y') {
            Minesweeper newGame;
            newGame.play();
        }
        
        return 0;
    }
    
    
    游戏
    
  • 最近活动

  • Stat

  • Rating