使用linux,c++,创作一个简单的五子棋游戏

#include <iostream>  
#include <vector>  
#include <unordered_map>  
  
using namespace std;  
  
// 棋盘大小  
const int BOARD_SIZE = 15;  
  
// 棋子类型  
enum ChessType {  
    EMPTY,  
    BLACK,  
    WHITE  
};  
  
// 棋盘类  
class ChessBoard {  
private:  
    vector<vector<ChessType>> board;  
  
public:  
    ChessBoard() {  
        board.resize(BOARD_SIZE, vector<ChessType>(BOARD_SIZE, EMPTY));  
    }  
  
    // 打印棋盘  
    void print() {  
        for (int i = 0; i < BOARD_SIZE; ++i) {  
            for (int j = 0; j < BOARD_SIZE; ++j) {  
                switch (board[i][j]) {  
                    case BLACK: cout << "● "; break;  
                    case WHITE: cout << "○ "; break;  
                    default: cout << "· "; break;  
                }  
            }  
            cout << endl;  
        }  
    }  
  
    // 下棋  
    bool placeChess(ChessType type, int x, int y) {  
        if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE || board[x][y] != EMPTY) {  
            return false;  
        }  
        board[x][y] = type;  
        return true;  
    }  
  
    // 检查是否胜利  
    bool checkWin(ChessType type, int x, int y) {  
        // 检查八个方向(上下左右,四个对角线)  
        vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1},  
                                             {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};  
        for (auto dir : directions) {  
            int count = 1;  
            for (int i = 1; i < 5; ++i) {  
                int nx = x + i * dir.first;  
                int ny = y + i * dir.second;  
                if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE && board[nx][ny] == type) {  
                    ++count;  
                } else {  
                    break;  
                }  
            }  
            for (int i = 1; i < 5; ++i) {  
                int nx = x - i * dir.first;  
                int ny = y - i * dir.second;  
                if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE && board[nx][ny] == type) {  
                    ++count;  
                } else {  
                    break;  
                }  
            }  
            if (count >= 5) {  
                return true;  
            }  
        }  
        return false;  
    }  
};  
  
int main() {  
    ChessBoard board;  
    ChessType currentPlayer = BLACK;  
    bool gameOver = false;  
  
    while (!gameOver) {  
        board.print();  
        cout << (currentPlayer == BLACK ? "Black" : "White") << ", enter your move (x y): ";  
        int x, y;  
        cin >> x >> y;  
  
        if (board.placeChess(currentPlayer, x, y)) {  
            if (board.checkWin(currentPlayer, x, y)) {  
                board.print();  
                cout << (currentPlayer == BLACK ? "Black wins!" : "White wins!") << endl;  
                gameOver = true;  
            } else {  
                currentPlayer = (currentPlayer == BLACK) ? WHITE : BLACK;  
            }  
        } else {  
            cout << "Invalid move. Try again." << endl;  
        }  
    }  
  
    return 0;  
}

编译通过后的执行效果:

相关推荐

  1. Python用Pygame实现一个五子棋游戏

    2024-04-24 13:00:01       35 阅读
  2. 使用Python创建一个简单Discord机器人

    2024-04-24 13:00:01       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-24 13:00:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-24 13:00:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-24 13:00:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-24 13:00:01       18 阅读

热门阅读

  1. React vs React Native写法上的不同

    2024-04-24 13:00:01       14 阅读
  2. 20240423-线程基础

    2024-04-24 13:00:01       13 阅读
  3. C++orm使用插曲——MySQL保留字

    2024-04-24 13:00:01       16 阅读
  4. 如何在 Docker 和 DigitalOcean Kubernetes 上部署 Kafka

    2024-04-24 13:00:01       10 阅读
  5. 深入理解Kubernetes:kube-scheduler源码解析

    2024-04-24 13:00:01       13 阅读
  6. DNS 服务器不同类型有什么作用?

    2024-04-24 13:00:01       15 阅读
  7. 项目开发的详细步骤(精华版)

    2024-04-24 13:00:01       12 阅读
  8. FlinkSQL State的生命周期

    2024-04-24 13:00:01       11 阅读