C++实现的五子棋

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

enum ChessmanType{
    BLACK,
    WHITE,
    NONE
};

class Chessman{

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

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


#include "Chessman.h"
class CheckBoard
{
private:
    /* data */
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 <stdio.h>
#include <stdlib.h>
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 <stdio.h>
#include <stdlib.h>
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("黑子win\n");
        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("白子win\n");
        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_Project\C++_Study\wuziqi>

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值