91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

c++實現五子棋的代碼怎么寫

c++
小億
116
2024-04-26 22:17:11
欄目: 編程語言

以下是一個簡單的C++實現五子棋的代碼示例:

#include <iostream>
#include <vector>

const int BOARD_SIZE = 15;

char board[BOARD_SIZE][BOARD_SIZE];

void initBoard() {
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            board[i][j] = '.';
        }
    }
}

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

bool checkWin(int x, int y, char player) {
    // 檢查橫向
    int count = 1;
    for (int i = 1; i < 5; i++) {
        if (x+i >= BOARD_SIZE || board[x+i][y] != player) {
            break;
        }
        count++;
    }
    for (int i = 1; i < 5; i++) {
        if (x-i < 0 || board[x-i][y] != player) {
            break;
        }
        count++;
    }
    if (count >= 5) {
        return true;
    }

    // 檢查縱向
    count = 1;
    for (int i = 1; i < 5; i++) {
        if (y+i >= BOARD_SIZE || board[x][y+i] != player) {
            break;
        }
        count++;
    }
    for (int i = 1; i < 5; i++) {
        if (y-i < 0 || board[x][y-i] != player) {
            break;
        }
        count++;
    }
    if (count >= 5) {
        return true;
    }

    // 檢查斜向
    count = 1;
    for (int i = 1; i < 5; i++) {
        if (x+i >= BOARD_SIZE || y+i >= BOARD_SIZE || board[x+i][y+i] != player) {
            break;
        }
        count++;
    }
    for (int i = 1; i < 5; i++) {
        if (x-i < 0 || y-i < 0 || board[x-i][y-i] != player) {
            break;
        }
        count++;
    }
    if (count >= 5) {
        return true;
    }

    // 檢查反斜向
    count = 1;
    for (int i = 1; i < 5; i++) {
        if (x+i >= BOARD_SIZE || y-i < 0 || board[x+i][y-i] != player) {
            break;
        }
        count++;
    }
    for (int i = 1; i < 5; i++) {
        if (x-i < 0 || y+i >= BOARD_SIZE || board[x-i][y+i] != player) {
            break;
        }
        count++;
    }
    if (count >= 5) {
        return true;
    }

    return false;
}

int main() {
    initBoard();

    char player = 'X';
    int x, y;

    while (true) {
        printBoard();

        std::cout << "Player " << player << ": ";
        std::cin >> x >> y;

        if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE || board[x][y] != '.') {
            std::cout << "Invalid move, try again." << std::endl;
            continue;
        }

        board[x][y] = player;

        if (checkWin(x, y, player)) {
            printBoard();
            std::cout << "Player " << player << " wins!" << std::endl;
            break;
        }

        player = (player == 'X') ? 'O' : 'X';
    }

    return 0;
}

這段代碼實現了一個簡單的五子棋游戲,玩家可以輪流下棋,當有玩家連成五子時游戲結束并宣布勝利。您可以在此基礎上進行更多的擴展和優化。

0
常山县| 焦作市| 车致| 泰宁县| 华蓥市| 永川市| 墨竹工卡县| 威信县| 洛宁县| 长垣县| 科技| 三穗县| 双牌县| 长宁区| 温泉县| 南安市| 杂多县| 泰兴市| 平昌县| 双牌县| 连平县| 横峰县| 万盛区| 湟源县| 河东区| 西林县| 商南县| 合作市| 连云港市| 偃师市| 上饶市| 唐河县| 静乐县| 陵水| 黄浦区| 辽中县| 华亭县| 且末县| 平顶山市| 昌图县| 江口县|