c++实战-多子棋

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

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

代码如下:

cpp 复制代码
#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;
}
相关推荐
Charles Ray12 分钟前
C++学习笔记 —— 内存分配 new
c++·笔记·学习
重生之我在20年代敲代码13 分钟前
strncpy函数的使用和模拟实现
c语言·开发语言·c++·经验分享·笔记
爱上语文15 分钟前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
limingade2 小时前
手机实时提取SIM卡打电话的信令和声音-新的篇章(一、可行的方案探讨)
物联网·算法·智能手机·数据分析·信息与通信
编程零零七2 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
2401_858286113 小时前
52.【C语言】 字符函数和字符串函数(strcat函数)
c语言·开发语言
铁松溜达py3 小时前
编译器/工具链环境:GCC vs LLVM/Clang,MSVCRT vs UCRT
开发语言·网络
everyStudy3 小时前
JavaScript如何判断输入的是空格
开发语言·javascript·ecmascript
jiao000015 小时前
数据结构——队列
c语言·数据结构·算法
C-SDN花园GGbond5 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法