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

发布于:2024-04-30 ⋅ 阅读:(38) ⋅ 点赞:(0)
#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;  
}

编译通过后的执行效果: