📄 chess.cpp
字号:
//#include <QtGui>
#include "myQFrame.h"
#include "chess.h"
#include "board.h"
#include "arithmetic.h"
//Chess::Chess(int chessx, int chessy, Board *pb) //初始化棋子
//{
// status = true;
// //color[] = "black";
// pboard = pb;
//
// SaveChess(chessx,chessy); //保存新棋子指针
//}
Chess::Chess()
{
}
/* 析构棋子,删除指针和内部对象 */
Chess::~Chess()
{
//delete chessIcon;
}
bool Chess::DrawChess(int x, int y) //画棋子
{
//int dx, dy;
//Arithmetic::AdjustDraw(x, y, dx, dy); //dx,dy通过引用返回,
// chessIcon = new QLabel(pboard);
// chessIcon->setPixmap(QPixmap(":/images/lastwhite.bmp"));
// chessIcon->move(dx, dy);
// chessIcon->show();
// chessIcon->setAttribute(Qt::WA_DeleteOnClose);
return NULL;
}
void Chess::DrawLaterChess( int &dx, int &dy )
{
//QPixmap later = QPixmap(":/images/white.png");
// chessIcon->setPixmap(later);
// chessIcon->move(dx, dy);
// chessIcon->show();
}
void Chess::SaveChess(int x, int y) //保存新棋子指针
{
myplace = Arithmetic::ChessPlace(x,y);
n = (x - 30.0) / 35 + 0.5; //棋子的横纵坐标,m\n >= 1;
m = (y - 30.0) / 35 + 0.5; //32.5边距;35行距;+0.5四省五入
pboard->realgrid[m][n] = pboard->chessnow; //保存新棋子指针到多维网格数组,字符型,存当前棋子颜色
pboard->grid[myplace] = this; //保存新棋子指针到网格数组
//棋子顺序的保存
pboard->whosturn++;
pboard->order[m][n] = pboard->whosturn; //棋子总数加一,且保存在数组相应位置
}
char Chess::clstab() //类型标识
{
return 'C'; // C 标识为棋子
}
/*功能:递归入口,board.cpp中调用*/
/*传入参数:int dire查询方向*/
/*返回参数:int 相同棋子数*/
int Chess::CheckStart( int dire )
{
int num = Check( dire , 0 );
return num;
}
/*功能:递归实现,Check方法调用,还用于对象之间递归checknext*/
/*传入参数:int dire查询方向;int count已有相同棋子数*/
/*返回参数:int 相同棋子数*/
int Chess::Check( int dire ,int count)
{
/* 判断棋子颜色,不同色则返回 */
if( clstab() == 'C' )
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 + -