栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++实现的五子棋

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++实现的五子棋

第一步:先创建一个Chessman.h主要作用是用于表示棋盘上的每一个棋子。其实现的cpp文件不需要(内容为空)。

enum ChessmanType{
    BLACK,
    WHITE,
    NONE
};

class Chessman{

    public:
        ChessmanType type = ChessmanType::NONE;
};

第二部则需要创建一个棋盘类Chessman.h:

#include "Chessman.h"
class CheckBoard
{
private:
    
public:
    CheckBoard(int w,int h);
   int width = 0;
   int height = 0;

   Chessman* table = nullptr;//table为棋盘数组。

   ~CheckBoard();
   Chessman Get(int x,int y);//得到棋盘上(x,y)位置 的棋子类型。
   int put(int x,int y,ChessmanType type);//落子
   int print();
   int IsWin(int x,int y,ChessmanType type);
};

    其具体的实现放在Chessman.cpp里面:

#include "CheckBoard.h"
#include 
#include 
CheckBoard::CheckBoard(int w, int h)
{
    width = w;
    height = h;
    table = (Chessman *)malloc(sizeof(int) * width * height);
    for (int i = 0; i < width * height; i++)
    {
        table[i].type = ChessmanType::NONE;
    }
}
CheckBoard::~CheckBoard()
{
    if (table != nullptr)
    {
        free(table);
        table = nullptr;
    }
}
Chessman CheckBoard::Get(int x, int y)
{
    if (x < 0 || x > width)
    {
        Chessman res;
        res.type = ChessmanType::NONE;
        return res;
    }
    if (y < 0 || y > height)
    {
        Chessman res;
        res.type = ChessmanType::NONE;
        return res;
    }
    int index = y * width + x;
    return table[index];
}
int CheckBoard::put(int x, int y, ChessmanType type)
{
    Chessman chessman = Get(x, y);
    if (x < 0 || x > width)
    {
        return -1;
    }
    if (y < 0 || y > height)
    {
        return -1;
    }
    if (chessman.type != ChessmanType::NONE)
    {
        return -1;
    }
    int index = y * width + x;
    table[index].type = type;
    return 0;
}
int CheckBoard::print()
{
    for (int j = 0; j < height; j++)
    {
        for (int i = 0; i < width; i++)
        {
            Chessman chessman = Get(i, j);
            if (chessman.type == ChessmanType::BLACK)
            {
                printf("B");
            }
            else if (chessman.type == ChessmanType::WHITE)
            {
                printf("W");
            }
            else
            {
                printf("-");
            }
        }
        printf("n");
    }
    return 0;
}
int CheckBoard::IsWin(int x, int y, ChessmanType type)
{
    //横向。
    int count = 1;
    for (int i = x - 1; i >= 0; i--)
    {
        Chessman chessman = Get(i, y);
        if (chessman.type != type)
        {
            break;
        }
        count++;
    }
    for (int i = x + 1; i < width; i++)
    {
        Chessman chessman = Get(i, y);
        if (chessman.type != type)
        {
            break;
        }
        count++;
    }
    if (count >= 5)
    {
        return 1;
    }
    //纵向
    count = 1;
    for (int j = y - 1; j >= 0; j--)
    {
        Chessman chessman = Get(x, j);
        if (chessman.type != type)
        {
            break;
        }
        count++;
    }
    for (int j = y + 1; j < height; j++)
    {
        Chessman chessman = Get(x, j);
        if (chessman.type != type)
        {
            break;
        }
        count++;
    }
    if (count >= 5)
    {
        return 1;
    }
    //左上-右下
     count = 1;
    for (int i = 1; i < 5; i++)
    {
        Chessman chessman = Get(x - i, y - i);
        if (chessman.type != type)
        {
            break;
        }
        count++;
    }
    for (int i = 1; i < 5; i++)
    {
        Chessman chessman = Get(x + i, y + i);
        if (chessman.type != type)
        {
            break;
        }
        count++;
    }
    if (count >= 5)
    {
        return 1;
    }
    //右上-左下
    count = 1;
    for (int i = 1; i < 5; i++)
    {
        Chessman chessman = Get(x + i, y - i);
         if (chessman.type != type)
        {
            break;
        }
        count++;
    }
    for(int i = 1;i < 5;i ++){
        Chessman chessman = Get(x - i, y + i);
         if (chessman.type != type)
        {
            break;
        }
        count++;
    }
    if (count >= 5)
    {
        return 1;
    }
    return 0;
}

最后是main.cpp

#include "CheckBoard.h"
#include 
#include 
int main(){

    CheckBoard checkboard = CheckBoard(10,10);
    checkboard.print();

    while(1){
        int x,y;
    while(1){
        printf("黑子:");
        scanf("%d,%d",&x,&y);
        int ret = checkboard.put(x,y,ChessmanType::BLACK);
        if(ret){
            printf("错误重新输入:");
            continue;
        }
        break;
    }
    checkboard.print();
    if(checkboard.IsWin(x,y,ChessmanType::BLACK)){
        printf("黑子winn");
        break;
    }

    while(1){
        printf("白子:");    
        scanf("%d,%d",&x,&y);
       int ret = checkboard.put(x,y,ChessmanType::WHITE);
       if(ret){
            printf("错误重新输入:");
            continue;
        }
        break;
    }
        checkboard.print();
      if(checkboard.IsWin(x,y,ChessmanType::WHITE)){
        printf("白子winn");
        break;
    }
    }
    return 0;
}

运行结果展示:
 

黑子:0,0  
B---------
----------
----------
----------
----------
----------
----------
----------
----------
----------
白子:4,5  
B---------
----------
----------
----------
----------
----W-----
----------
----------
----------
----------
黑子:1,0
BB--------
----------
----------
----------
----------
----W-----
----------
----------
----------
----------
白子:6,9
BB--------
----------
----------
----------
----------
----W-----
----------
----------
----------
------W---
黑子:2,0
BBB-------
----------
----------
----------
----------
----W-----
----------
----------
----------
------W---
白子:8,9
BBB-------
----------
----------
----------
----------
----W-----
----------
----------
----------
------W-W-
黑子:3,0
BBBB------
----------
----------
----------
----------
----W-----
----------
----------
----------
------W-W-
白子:7,9
BBBB------
----------
----------
----------
----------
----W-----
----------
----------
----------
------WWW-
黑子:4,0
BBBBB-----
----------
----------
----------
----------
----W-----
----------
----------
----------
------WWW-
黑子win
(base) PS E:VS_ProjectC++_Studywuziqi>

不足之处希望多多指导。时隔3年的第一篇博客。QAQ.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/854154.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号