📄 chesswhite.cpp
字号:
//#include <QtGui>
#include "myQFrame.h"
#include "chess.h"
#include "board.h"
#include "chesswhite.h"
#include "arithmetic.h"
ChessWhite::ChessWhite(int chessx, int chessy, Board *pb) //初始化棋子
{
status = true;
//color[] = "black";
pboard = pb;
chessIcon = NULL;
SaveChess(chessx,chessy); //保存新棋子指针
}
/* 析构棋子,删除指针和内部对象 */
ChessWhite::~ChessWhite()
{
delete chessIcon;
}
bool ChessWhite::DrawChess(int x, int y) //画棋子
{
int dx, dy;
Arithmetic::AdjustDraw(x, y, dx, dy); //dx,dy通过引用返回,
if( !chessIcon )
{
chessIcon = new QLabel(pboard);
}
chessIcon->setPixmap(QPixmap(":/images/latestwhite.png"));
chessIcon->move(dx, dy);
chessIcon->show();
chessIcon->setAttribute(Qt::WA_DeleteOnClose);
//DrawLaterChess( dx, dy );
return true;
}
void ChessWhite::DrawLaterChess( int &dx, int &dy )
{
QPixmap later = QPixmap(":/images/white.png");
chessIcon->setPixmap(later);
chessIcon->move(dx, dy);
chessIcon->show();
}
char ChessWhite::clstab() //类型标识
{
return 'W'; // 2 标识为棋子
}
/*功能:递归实现,Check方法调用,还用于对象之间递归checknext*/
/*传入参数:int dire查询方向;int count已有相同棋子数*/
/*返回参数:int 相同棋子数*/
int ChessWhite::Check( int dire ,int count)
{
/* 判断棋子颜色,不同色则返回 */
if( clstab() == pboard->chessnow ) //如果this指向的(当前递归的)棋子与刚下的棋子(出发递归搜索的棋子)同颜色,则加1
{
count++;
}
else
{
return count;
}
/* 根据方向,可以算出下一个chess在grid[]中的位置 */
/* 棋盘边缘的棋子有特殊逻辑,防止跨行错误递归,对应每个方向有相应规则 */
int nextplace;
switch(dire) {
case 1 :
{
if( myplace % 15 == 0 )
{
nextplace = -1;
}
else
{
nextplace= myplace - 16;
}
break;
}
case 2 :
{
nextplace= myplace - 15;
break;
}
case 3 :
{
if( ( myplace + 1) % 15 == 0 )
{
nextplace = -1;
}
else
{
nextplace= myplace - 14;
}
break;
}
case 4 :
{
if( myplace%15 == 0 )
{
nextplace = -1;
}
else
{
nextplace= myplace - 1;
}
break;
}
case 5 :
{
if( ( myplace + 1) % 15 == 0 )
{
nextplace = -1;
}
else
{
nextplace= myplace + 1;
}
break;
}
case 6 :
{
if( myplace%15 == 0 )
{
nextplace = -1;
}
else
{
nextplace= myplace + 14;
}
break;
}
case 7 :
{
nextplace= myplace + 15;
break;
}
case 8 :
{
if( ( myplace + 1) % 15 == 0 )
{
nextplace = -1;
}
else
{
nextplace= myplace + 16;
}
}
}
/* 超出棋盘边界,返回 */
if( nextplace < 0 || nextplace > 224 )
{
return count;
}
/* 规定方向上下一个空格没有棋子,返回 */
if( pboard->grid[nextplace] == NULL )
{
return count;
}
/* 按方向查询下一粒chess */
Chess * chessnext = pboard->grid[nextplace];
chessnext->Check( dire , count );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -