第20篇 Qt实现简单五子棋游戏(四)棋盘类编码

第20篇 Qt实现简单五子棋游戏(四)棋盘类编码

2.棋盘类

本身来说棋盘作为一个个体很重要,之前没有设计棋盘类的时候,要添加很多变量来表示棋盘上的一些东西,比如很重要得是表示棋盘的数组,这个可以作为棋盘的一个属性来说明,这样我们用它来判断的时候就很好的知道怎么调用,调用谁的了。

2.1.头文件

没有什么过多得变量说明,只是一些函数看起来多,虽然变量少,但是其中涉及了一些操作,所以函数方法多了。

#ifndef CHESSBOARD_H
#define CHESSBOARD_H

#include <QObject>
#include <chess.h>

class ChessBoard : public QObject
{
    Q_OBJECT
public:
    explicit ChessBoard(QObject *parent = nullptr);
    void setRow(int row);
    void setCol(int col);
    int getRow();
    int getCol();
    void setStart_x(int start_x);
    void setStart_y(int start_y);
    int  getStart_x();
    int  getStart_y();
    void addChess(Chess chess);
    Chess subChess();
    void markLocationColor(int color);
    void markLocationColor(int x,int y,int color);
    int getLocationColor();
    int getLocationColor(int x,int y);
    int getLocation_x();
    int getLocation_y();
    void setChessCount(int count);
    int getChessCount();
    void setLineSpace(int lineSpace);
    int getLineSpace();
    void initChessStore(int length);
    void initPositionRecord(int row,int col);


private:
    int row;
    int col;
    int start_x;
    int start_y;
    Chess* chessStore;
    int **positionRecord;
    int chessCount;
    int lineSpace;

signals:

};

#endif // CHESSBOARD_H

2.2.源文件

删减棋子的时候,先减掉数量,这样直接去调用设置颜色的函数就不会越界。
在这里插入图片描述下面的函数主要用于电脑获取目标位置,当棋盘上有两颗以上的棋子时,它的目标位置是当前棋子的前一颗,所以棋子数量减二对应的棋子的位置就是目标位置;如果只有一颗棋子,那么就以这颗棋子为目标;如果没有棋子就以中间位置为目标。
在这里插入图片描述
代码:

#include "chessboard.h"

ChessBoard::ChessBoard(QObject *parent) : QObject(parent)
{
    this->row = 0;
    this->col = 0;
    this->start_x = 0;
    this->start_y = 0;
    this->chessStore = nullptr;
    this->positionRecord = nullptr;
    this->chessCount = 0;
    this->lineSpace = 0;
}

void ChessBoard::setRow(int row)
{
    this->row = row;
}

void ChessBoard::setCol(int col)
{
    this->col = col;
}

int ChessBoard::getRow()
{
    return this->row;
}

int ChessBoard::getCol()
{
    return this->col;
}

void ChessBoard::setStart_x(int start_x)
{
    this->start_x = start_x;
}

void ChessBoard::setStart_y(int start_y)
{
    this->start_y = start_y;
}

int ChessBoard::getStart_x()
{
    return this->start_x;
}

int ChessBoard::getStart_y()
{
    return this->start_y;
}

void ChessBoard::addChess(Chess chess)
{
    this->chessStore[this->chessCount] = chess;
    this->chessCount++;
}

Chess ChessBoard::subChess()
{
    this->chessCount--;
    markLocationColor(0);
    return this->chessStore[this->chessCount];
}

void ChessBoard::markLocationColor(int color)
{
    int x = this->chessStore[this->chessCount].getX();
    int y = this->chessStore[this->chessCount].getY();
    this->positionRecord[x][y] = color;
}

void ChessBoard::markLocationColor(int x, int y, int color)
{
    this->positionRecord[x][y] = color;
}

int ChessBoard::getLocationColor()
{
    int x = this->chessStore[this->chessCount].getX();
    int y = this->chessStore[this->chessCount].getY();
    return this->positionRecord[x][y];
}

int ChessBoard::getLocationColor(int x, int y)
{
    return this->positionRecord[x][y];
}

int ChessBoard::getLocation_x()
{
    if(this->chessCount > 1){
        return this->chessStore[this->chessCount - 2].getX();
    }
    else if(this->chessCount == 1){
        return this->chessStore[0].getX();
    }
    else{
        return this->row / 2;
    }
}

int ChessBoard::getLocation_y()
{
    if(this->chessCount > 1){
        return this->chessStore[this->chessCount - 2].getY();
    }
    else if(this->chessCount == 1){
        return this->chessStore[0].getY();
    }
    else{
        return this->col / 2;
    }
}

void ChessBoard::setChessCount(int count)
{
    this->chessCount = count;
}

int ChessBoard::getChessCount()
{
    return this->chessCount;
}

void ChessBoard::setLineSpace(int lineSpace)
{
    this->lineSpace = lineSpace;
}

int ChessBoard::getLineSpace()
{
    return this->lineSpace;
}

void ChessBoard::initChessStore(int length)
{
    this->chessStore = new Chess[length];
}

void ChessBoard::initPositionRecord(int row, int col)
{
    this->positionRecord = new int*[row];
    for(int i = 0;i < row;i++){
        this->positionRecord[i] = new int[col];
    }
    for(int i = 0;i < row;i++){
        for(int j = 0;j < col;j++){
            this->positionRecord[i][j] = 0;
        }
    }
}

对了二维数组声明的时候是这样的:int **p;分配内存的方法就在上面。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大唐不良猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值