c++实战-多子棋

自创的游戏,可以控制棋盘大小之类的

核心在于控制胜利条件,需要每次扫描

代码如下:

#include <iostream>
#include <vector>
using namespace std;

#define MAX_SIZE 9

// 定义棋盘为MAX_SIZE x MAX_SIZE的二维向量
vector<vector<char>> board(MAX_SIZE, vector<char>(MAX_SIZE));    //稍用一下vector,比较方便
int boardSize;        //小驼峰命名法,全局变量省的来回传来传去
char currentPlayer;   //小驼峰命名法,全局变量省的来回传来传去

// 函数声明
void init(int size);
void printt();
bool check(int size);
void exchange();
void move(int size);
bool judge(int size);

int main()
{
    int size;

    // 提示用户输入棋盘大小
    cout << "请输入棋盘大小(5到9之间):";
    cin >> size;

    // 验证用户输入的棋盘大小是否在有效范围内
    while (1)
    {
        if (size < 5 || size > 9)
        {
            cout << "请正确输入棋盘大小!" << endl;
            cin >> size;
        }
        else
            break;//循环到正确为止
    }

    boardSize = size;
    init(boardSize);     // 初始化棋盘
    currentPlayer = 'X'; // 设置初始玩家为'X'(棋盘行子也用X表示)(五子棋的图案)

    // 主游戏循环
    while (true)
    {
        printt();        // 打印当前棋盘状态
        move(boardSize); // 执行玩家移动

        // 检查当前玩家是否获胜
        if (check(boardSize))
        {
            printt();
            cout << "玩家 " << currentPlayer << " 胜利!" << endl;
            break;
        }

        // 检查棋盘是否已满
        if (judge(boardSize))
        {
            printt();
            cout << "平局!" << endl;
            break;
        }

        exchange(); // 切换玩家,即X 与 O
    }

    return 0;
}

// 初始化棋盘,将所有格子设置为空白
void init(int size)
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            board[i][j] = ' ';
        }
    }
}

// 打印棋盘当前状态
void printt()
{
    cout << "   ";
    for (int i = 0; i < boardSize; i++)
    {
        cout << i + 1 << "   ";
    }
    cout << endl;

    for (int i = 0; i < boardSize; i++)
    {
        cout << i + 1 << " ";
        for (int j = 0; j < boardSize; j++)
        {
            cout << " " << board[i][j] << " ";
            if (j < boardSize - 1)
                cout << " ";
        }
        cout << endl
             << endl; // 添加空行以增加纵向间距,好看的需要,同时连线直
    }
}

// 检查一条线是否有五个连续的相同标志
bool checkLine(vector<char> &line, int size)
{
    int count = 0;
    for (int i = 0; i < size; i++)
    {
        if (line[i] == currentPlayer)
        {
            count++;
            if (count == 5)
            {
                return true;
            }
        }
        else
        {
            count = 0;
        }
    }
    return false;
}

// 检查当前玩家是否获胜
bool check(int size)
{
    vector<char> line(MAX_SIZE);

    // 检查每一行
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            line[j] = board[i][j];
        }
        if (checkLine(line, size))
        {
            return true;
        }
    }

    // 检查每一列
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            line[j] = board[j][i];
        }
        if (checkLine(line, size))
        {
            return true;
        }
    }

    // 检查对角线
    for (int i = -size + 1; i < size; i++)
    {
        int index = 0;
        for (int j = 0; j < size; j++)
        {
            int k = i + j;
            if (k >= 0 && k < size)
            {
                line[index++] = board[j][k];
            }
        }
        if (index >= 5 && checkLine(line, index))
        {
            return true;
        }
    }

    // 检查反对角线
    for (int i = 0; i < 2 * size - 1; i++)
    {
        int index = 0;
        for (int j = 0; j < size; j++)
        {
            int k = i - j;
            if (k >= 0 && k < size)
            {
                line[index++] = board[j][k];
            }
        }
        if (index >= 5 && checkLine(line, index))
        {
            return true;
        }
    }

    return false;
}

// 切换当前玩家
void exchange()
{
    currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}

// 处理玩家输入并更新棋盘
void move(int size)
{
    int row, col;

    while (true)
    {
        cout << "玩家 " << currentPlayer << " 请输入你的移动(行和列):";
        cin >> row >> col;
        row--;
        col--;

        if (row >= 0 && row < size && col >= 0 && col < size && board[row][col] == ' ')
        {
            board[row][col] = currentPlayer;
            break;
        }
        else
        {
            cout << "无效的移动,请重新输入。" << endl;
        }
    }
}

// 检查棋盘是否已满
bool judge(int size)
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (board[i][j] == ' ')
            {
                return false;
            }
        }
    }
    return true;
}

相关推荐

  1. c++实战-多子

    2024-07-10 21:32:04       13 阅读
  2. 乌龟(c++实现)

    2024-07-10 21:32:04       22 阅读
  3. 三子/井字(C语言)

    2024-07-10 21:32:04       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 21:32:04       5 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 21:32:04       5 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 21:32:04       4 阅读
  4. Python语言-面向对象

    2024-07-10 21:32:04       7 阅读

热门阅读

  1. 访问者模式在金融业务中的应用及其框架实现

    2024-07-10 21:32:04       10 阅读
  2. PyTorch清理CPU缓存

    2024-07-10 21:32:04       7 阅读
  3. qt 自定义信号和槽举例

    2024-07-10 21:32:04       9 阅读
  4. 贪吃蛇代码python实现

    2024-07-10 21:32:04       12 阅读
  5. iOS开发语言基础与Xcode工具初探

    2024-07-10 21:32:04       11 阅读
  6. 【面试题】Reactor模型

    2024-07-10 21:32:04       9 阅读
  7. nvm安装node一直没有npm

    2024-07-10 21:32:04       11 阅读
  8. 深入理解model.eval()与torch.no_grad()

    2024-07-10 21:32:04       9 阅读
  9. gusture

    2024-07-10 21:32:04       8 阅读
  10. python的抽象基类

    2024-07-10 21:32:04       10 阅读
  11. 软设之桥接模式

    2024-07-10 21:32:04       12 阅读