C++ 五子棋小游戏

#include <iostream>
using namespace std;

#define BOARD_SIZE 15

void print_board(int board[][BOARD_SIZE]) {
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            cout << board[i][j] << " ";
        }
        cout << endl;
    }
}

bool is_move_valid(int board[][BOARD_SIZE], int x, int y) {
    // check if move is within board bounds
    if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
        return false;
    }
    // check if spot is already taken
    if (board[x][y] != 0) {
        return false;
    }
    return true;
}

int check_win(int board[][BOARD_SIZE], int x, int y) {
    int player = board[x][y];
    int consecutive = 0;

    // check horizontal
    for (int i = max(0, x - 4); i <= min(x + 4, BOARD_SIZE - 1); i++) {
        if (board[i][y] == player) {
            consecutive++;
            if (consecutive >= 5) {
                return player;
            }
        }
        else {
            consecutive = 0;
        }
    }

    // check vertical
    consecutive = 0;
    for (int j = max(0, y - 4); j <= min(y + 4, BOARD_SIZE - 1); j++) {
        if (board[x][j] == player) {
            consecutive++;
            if (consecutive >= 5) {
                return player;
            }
        }
        else {
            consecutive = 0;
        }
    }

    // check diagonal-1
    consecutive = 0;
    for (int i = -4; i <= 4; i++) {
        int new_x = x + i;
        int new_y = y + i;
        if (new_x >= 0 && new_x < BOARD_SIZE && new_y >= 0 && new_y < BOARD_SIZE) {
            if (board[new_x][new_y] == player) {
                consecutive++;
                if (consecutive >= 5) {
                    return player;
                }
            }
            else {
                consecutive = 0;
            }
        }
    }

    // check diagonal-2
    consecutive = 0;
    for (int i = -4; i <= 4; i++) {
        int new_x = x + i;
        int new_y = y - i;
        if (new_x >= 0 && new_x < BOARD_SIZE && new_y >= 0 && new_y < BOARD_SIZE) {
            if (board[new_x][new_y] == player) {
                consecutive++;
                if (consecutive >= 5) {
                    return player;
                }
            }
            else {
                consecutive = 0;
            }
        }
    }

    return 0;
}

int main() {
    int board[BOARD_SIZE][BOARD_SIZE] = { 0 };
    int turn = 1;
    int winner = 0;

    while (winner == 0) {
        cout << "Turn " << turn << endl;
        print_board(board);

        int x, y;
        do {
            cout << "Player " << turn << ", enter x and y for your move:\n";
            cin >> x >> y;
        } while (!is_move_valid(board, x, y));

        board[x][y] = turn;
        winner = check_win(board, x, y);
        turn = 3 - turn; // switch turns between 1 and 2
    }

    cout << "Player " << winner << " wins!" << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值