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

本文详细介绍了使用C++编写的一个简单国际象棋游戏,包括棋盘类的定义、棋子操作(放置和检查胜利)、用户交互以及游戏流程。
摘要由CSDN通过智能技术生成
#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;  
}

编译通过后的执行效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑暗森林里的葱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值