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;分配内存的方法就在上面。