#include "chessbroad.h"
#include <qtextcodec.h>
ChessBroad::ChessBroad(QWidget *parent)
: QWidget(parent)
{
//g[0][1]=0;
//如果有文件名为XiangQi.txt删除
QFile::remove("XiangQi.txt");
r = 0;
l = 0;
key = 1;
p_h = 0;
v = 0;
bushu = 0;
for(int h=0; h < NUM+1; h++)
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
p[h][i][j] = 0;
}
initAction = new QAction(tr("&Init"), this);
connect(initAction, SIGNAL(triggered()), this, SLOT(init()));
addAction(initAction);
saveAction = new QAction(tr("&Save"), this);
connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
addAction(saveAction);
loadAction = new QAction(tr("&Load"), this);
connect(loadAction, SIGNAL(triggered()), this, SLOT(load()));
addAction(loadAction);
newAction = new QAction(tr("&BackChess"), this);
connect(newAction, SIGNAL(triggered()), this, SLOT(backchess()));
addAction(newAction);
setContextMenuPolicy(Qt::ActionsContextMenu);
for(int i=0; i < NUMY+1; i++)//初始化g[][]
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = 0;
//qWarning("%d %d %d",i,j,g[i][j]);
}
//黑棋
g[0][0]=24;//车
g[0][1]=22;//马
g[0][2]=20;//象
g[0][3]=18;//士
g[0][4]=17;//将
g[0][5]=19;//士
g[0][6]=21;//象
g[0][7]=23;//马
g[0][8]=25;//车
g[2][1]=26;//炮
g[2][7]=27;//炮
g[3][0]=28;//卒
g[3][2]=29;//卒
g[3][4]=30;//卒
g[3][6]=31;//卒
g[3][8]=32;//卒
//红棋
g[6][0]=12;//兵
g[6][2]=13;//兵
g[6][4]=14;//兵
g[6][6]=15;//兵
g[6][8]=16;//兵
g[7][1]=10;//炮
g[7][7]=11;//炮
g[9][0]=8;//车
g[9][1]=6;//马
g[9][2]=4;//相
g[9][3]=2;//士
g[9][4]=1;//帅
g[9][5]=3;//士
g[9][6]=5;//相
g[9][7]=7;//马
g[9][8]=9;//车
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
jilu[i][j] = g[i][j];
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
p[p_h][i][j] = g[i][j];
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
f[i][j] = NULL;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
d[i][j] = NULL;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
tao[i][j] = 0;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
chi[i][j] = 0;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
gui[i][j] = 0;
}
x_1 = -1;
y_1 = -1;
x_2 = -1;
y_2 = -1;
ta = -1;
for(int i=0; i < N+1; i++)
{
my[i].myi = -1;
my[i].myj = -1;
}
for(int i=0; i < N+1; i++)
for(int j=0; j < N+1; j++)
{
la[i][j] = 0;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
if(g[i][j] != 0)
{
my[(g[i][j])].myi = i;
my[(g[i][j])].myj = j;
//qWarning("my %d %d %d",g[i][j],my[(g[i][j])].myi,my[(g[i][j])].myj);
}
}
for(int i=0; i < N+1; i++)//初始化sbu[]
{
sbu[i]=NULL;
}
/*
//qWarning("my[22].myi = %d",my[22].myi);
//qWarning("my[22].myj = %d", my[22].myj);
chizi(4);
//qWarning("chi 2 0 = %d",chi[2][0]);
//qWarning("chi 2 2 = %d",chi[2][2]);
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
if(chi[i][j] == 4)
{
qWarning("chi %d %d = %d",i,j,chi[i][j]);
}
}
*/
/*
insert(f[0][0],1);
insert(f[0][0],3);
insert(f[0][0],2);
for(chess * p = f[0][0]; p != NULL; p = p->next)
{
qWarning("shuchu 2");
qWarning(" %d ",p->name);
}
*/
//taopao(1);
//qWarning("tao %d",tao[0][0]);
//qWarning("tao %d",tao[0][1]);
}
//pengpeng的单个棋子版
void ChessBroad::chizi(int wochi)
{
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
chi[i][j] = 0;
}
//for(int i = 0; i < NUMY+1; i++)//在f[][]中做标记
//for(int j = 0; j < NUMX+1; j++)
{
//switch( g[i][j] )
int i = my[wochi].myi;
int j = my[wochi].myj;
//qWarning("wochi = %d",wochi);
//qWarning("i = %d j = %d",i,j);
switch(wochi)
{
case 1://红棋
{
//帅
//向上
if((i-1) >= 7)
{
chi[i-1][j] = wochi;
}
//向下
if((i+1) <= 9)
{
chi[i+1][j] = wochi;
}
//向左
if((j-1) >= 3)
{
chi[i][j-1] = wochi;
}
//向右
if((j+1) <= 5)
{
chi[i][j+1] = wochi;
}
//老将对头向上
for(int n = (i-1); n >= 0; n--)
{
if((g[n][j]!=0)&&g[n][j]!=17)
{
break;
}
if(g[n][j]==17)
{
chi[n][j] = wochi;
break;
}
}
break;
}
case 2:
case 3:
{
//仕
if((i == 8)&&(j == 4))//仕在正中
{
//左上
//insert(f[7][3],g[i][j]);
chi[7][3] = wochi;
//右上
//insert(f[7][5],g[i][j]);
chi[7][5] = wochi;
//左下
//insert(f[9][3],g[i][j]);
chi[9][3] = wochi;
//右下
//insert(f[9][5],g[i][j]);
chi[9][5] = wochi;
}
if(((i == 7)&&(j == 3))//仕在左上角
||((i == 7)&&(j == 5))//仕在右上角
||((i == 9)&&(j == 3))//仕在左下角
||((i == 9)&&(j == 5)))//仕在右下角
{
//insert(f[8][4],g[i][j]);
chi[8][4] = wochi;
}
break;
}
case 4:
case 5:
{
//相
//底层左边
if((i == 9)&&(j == 2))
{
if(g[8][1] == 0)//左上
{
chi[7][0] = wochi;
//insert(f[7][0],g[i][j]);
}
if(g[8][3] == 0)//右上
{
chi[7][4] = wochi;
//insert(f[7][4],g[i][j]);
}
}
//底层右边
if((i == 9)&&(j == 6))
{
if(g[8][5] == 0)//左上
{
chi[7][4] = wochi;
//insert(f[7][4],g[i][j]);
}
if(g[8][7] == 0)//右上
{
chi[7][8] = wochi;
//insert(f[7][8],g[i][j]);
}
}
//中层左边
if((i == 7)&&(j == 0))
{
if(g[6][1] == 0)//右上
{
chi[5][2] = wochi;
//insert(f[5][2],g[i][j]);
}
if(g[8][1] == 0)//右下
{
chi[9][2] = wochi;
//insert(f[9][2],g[i][j]);
}
}
//中层中间
if((i == 7)&&(j == 4))
{
if(g[6][5] == 0)//右上
{
chi[5][6] = wochi;
//insert(f[5][6],g[i][j]);
}
if(g[8][5] == 0)//右下
{
chi[9][6] = wochi;
//insert(f[9][6],g[i][j]);
}
if(g[6][3] == 0)//左上
{
chi[5][2] = wochi;
//insert(f[5][2],g[i][j]);
}
if(g[8][3] == 0)//左下
{
chi[9][2] = wochi;
//insert(f[9][2],g[i][j]);
}
}
//中层右边
if((i == 7)&&(j == 8))
{
if(g[6][7] == 0)//左上
{
chi[5][6] = wochi;
//insert(f[5][6],g[i][j]);
}
if(g[8][7] == 0)//左下
{
chi[9][6] = wochi;
//insert(f[9][6],g[i][j]);
}
}
//顶层左边
if((i == 5)&&(j == 2))
{
if(g[6][1] == 0)//左下
{
chi[7][0] = wochi;
//insert(f[7][0],g[i][j]);
}
if(g[6][3] == 0)//右下
{
chi[7][4] = wochi;
//insert(f[7][4],g[i][j]);
}
}
//顶层右边
if((i == 5)&&(j == 6))
{
if(g[6][5] == 0)//左下
{
chi[7][4] = wochi;
//insert(f[7][4],g[i][j]);
}
if(g[6][7] == 0)//右下
{
chi[7][8] = wochi;
//insert(f[7][8],g[i][j]);
}
}
break;
}
case 6:
case 7:
{
//马
//竖日左上角
if(((i-2) >= 0)&&((i-2) <= NUMY)&&((j-1) >= 0)&&((j-1) <= NUMX)&&(g[i-1][j] == 0))
{
chi[i-2][j-1] = wochi;
//insert(f[i-2][j-1],g[i][j]);
}
//竖日右上角
if(((i-2) >= 0)&&((i-2) <= NUMY)&&((j+1) >= 0)&&((j+1) <= NUMX)&&(g[i-1][j] == 0))
{
chi[i-2][j+1] = wochi;
//insert(f[i-2][j+1],g[i][j]);
}
//横日左上角
if(((i-1) >= 0)&&((i-1) <= NUMY)&&((j-2) >= 0)&&((j-2) <= NUMX)&&(g[i][j-1] == 0))
{
chi[i-1][j-2] = wochi;
//insert(f[i-1][j-2],g[i][j]);
}
//横日右上角
if(((i-1) >= 0)&&((i-1) <= NUMY)&&((j+2) >= 0)&&((j+2) <= NUMX)&&(g[i][j+1] == 0))
{
chi[i-1][j+2] = wochi;
//insert(f[i-1][j+2],g[i][j]);
}
//横日左下角
if(((i+1) >= 0)&&((i+1) <= NUMY)&&((j-2) >= 0)&&((j-2) <= NUMX)&&(g[i][j-1] == 0))
{
chi[i+1][j-2] = wochi;
//insert(f[i+1][j-2],g[i][j]);
}
//横日右下角
if(((i+1) >= 0)&&((i+1) <= NUMY)&&((j+2) >= 0)&&((j+2) <= NUMX)&&(g[i][j+1] == 0))
{
chi[i+1][j+2] = wochi;
//insert(f[i+1][j+2],g[i][j]);
}
//竖日左下角
if(((i+2) >= 0)&&((i+2) <= NUMY)&&((j-1) >= 0)&&((j-1) <= NUMX)&&(g[i+1][j] == 0))
{
chi[i+2][j-1] = wochi;
//insert(f[i+2][j-1],g[i][j]);
}
//竖日右下角
if(((i+2) >= 0)&&((i+2) <= NUMY)&&((j+1) >= 0)&&((j+1) <= NUMX)&&(g[i+1][j] == 0))
{
chi[i+2][j+1] = wochi;
//insert(f[i+2][j+1],g[i][j]);
}
break;
}
case 8:
case 9:
{
//车
//向上扫描
for(int t = i-1; t >= 0; t--)
{
{
chi[t][j] = wochi;
//insert(f[t][j],g[i][j]);
}
if((g[t][j] >= 1)&&(g[t][j] <= 16))
{
break;
}
if((g[t][j] >= 17)&&(g[t][j] <= 32))
{
break;
}
}
//向下扫描
for(int t = i+1; t <= 9; t++)
{
chi[t][j] = wochi;
//insert(f[t][j],g[i][j]);
if((g[t][j] >= 1)&&(g[t][j] <= 16))
{
break;
}
if((g[t][j] >= 17)&&(g[t][j] <= 32))
{
break;
}
}
//向左扫描
for(int t = j-1; t >= 0; t--)
{
chi[i][t] = wochi;
//insert(f[i][t],g[i][j]);
if((g[i][t] >= 1)&&(g[i][t] <= 16))
{
break;
}
if((g[i][t] >= 17)&&(g[i][t] <= 32))
{
break;
}
}
//向右扫描
for(int t = j+1; t <= 8; t++)
{
chi[i][t] = wochi;
//insert(f[i][t],g[i][j]);
if((g[i][t] >= 1)&&(g[i][t] <= 16))
{
break;
}
if((g[i][t] >= 17)&&(g[i][t] <= 32))
{
break;
}
}
break;
}
case 10:
case 11:
{
//炮
//向上查找
{
int t = 0;
for(int h = (i-1); h >= 0; h--)
{
if((g[h][j] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
//insert(f[h][j],g[i][j]);
chi[h][j] = wochi;
if(g[h][j] != 0)
{
break;
}
}
}
}
//向下查找
{
int t = 0;
for(int h = (i+1); h <= NUMY; h++)
{
if((g[h][j] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
//insert(f[h][j],g[i][j]);
chi[h][j] = wochi;
if(g[h][j] != 0)
{
break;
}
}
}
}
//向左查找
{
int t = 0;
for(int h = (j-1); h >= 0; h--)
{
if((g[i][h] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
//insert(f[i][h],g[i][j]);
chi[i][h] = wochi;
if(g[i][h] != 0)
{
break;
}
}
}
}
//向右查找
{
int t = 0;
for(int h = (j+1); h <= NUMX; h++)
{
if((g[i][h] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
//insert(f[i][h],g[i][j]);
chi[i][h] = wochi;
if(g[i][h] != 0)
{
break;
}
}
}
}
break;
}
case 12:
case 13:
case 14:
case 15:
case 16:
{
//兵
// 向前
{
chi[i-1][j] = wochi;
//insert(f[i-1][j],g[i][j]);
}
//向左
if((i >= 0)&&(i <= 4))
{
chi[i][j-1] = wochi;
//insert(f[i][j-1],g[i][j]);
}
//向右
if((i >= 0)&&(i <= 4))
{
chi[i][j+1] = wochi;
//insert(f[i][j+1],g[i][j]);
}
break;
}
case 17://黑棋
{
//将
//向上
if((i-1) >= 0)
{
chi[i-1][j] = wochi;
//insert(f[i-1][j],g[i][j]);
}
//向下
if((i+1) <= 2)
{
chi[i+1][j] = wochi;
//insert(f[i+1][j],g[i][j]);
}
//向左
if((j-1) >= 3)
{
chi[i][j-1] = wochi;
//insert(f[i][j-1],g[i][j]);
}
//向右
if((j+1) <= 5)
{
chi[i][j+1] = wochi;
//insert(f[i][j+1],g[i][j]);
}
//老将对头向下
for(int n = (i+1); n <= 9; n++)
{
if((g[n][j]!=0)&&g[n][j]!=1)
{
break;
}
if(g[n][j]==1)
{
chi[n][j] = wochi;
//insert(f[n][j],g[i][j]);
break;
}
}
break;
}
case 18:
case 19:
{
//士
if((i == 1)&&(j == 4))//士在正中
{
//左上
chi[0][3] = wochi;
//insert(f[0][3],g[i][j]);
//右上
chi[0][5] = wochi;
//insert(f[0][5],g[i][j]);
//左下
chi[2][3] = wochi;
//insert(f[2][3],g[i][j]);
//右下
chi[2][5] = wochi;
//insert(f[2][5],g[i][j]);
}
if(((i == 0)&&(j == 3))//士在左上角
||((i == 0)&&(j == 5))//士在右上角
||((i == 2)&&(j == 3))//士在左下角
||((i == 2)&&(j == 5)))//士在右下角
{
chi[1][4] = wochi;
//insert(f[1][4],g[i][j]);
}
break;
}
case 20:
case 21:
{
//象
//河边左边
if((i == 4)&&(j == 2))
{
if(g[3][1] == 0)//左上
{
chi[2][0] = wochi;
//insert(f[2][0],g[i][j]);
}
if(g[3][3] == 0)//右上
{
chi[2][4] = wochi;
//insert(f[2][4],g[i][j]);
}
}
//河边右边
if((i == 4)&&(j == 6))
{
if(g[3][5] == 0)//左上
{
chi[2][4] = wochi;
//insert(f[2][4],g[i][j]);
}
if(g[3][7] == 0)//右上
{
chi[2][8] = wochi;
//insert(f[2][8],g[i][j]);
}
}
//中层左边
if((i == 2)&&(j == 0))
{
if(g[1][1] == 0)//右上
{
chi[0][2] = wochi;
//insert(f[0][2],g[i][j]);
}
if(g[3][1] == 0)//右下
{
chi[3][1] = wochi;
//insert(f[4][2],g[i][j]);
}
}
//中层中间
if((i == 2)&&(j == 4))
{
if(g[1][5] == 0)//右上
{
chi[0][6] = wochi;
//insert(f[0][6],g[i][j]);
}
if(g[3][5] == 0)//右下
{
chi[4][6] = wochi;
//insert(f[4][6],g[i][j]);
}
if(g[1][3] == 0)//左上
{
chi[0][2] = wochi;
//insert(f[0][2],g[i][j]);
}
if(g[3][3] == 0)//左下
{
chi[4][2] = wochi;
//insert(f[4][2],g[i][j]);
}
}
//中层右边
if((i == 2)&&(j == 8))
{
if(g[1][7] == 0)//左上
{
chi[0][6] = wochi;
//insert(f[0][6],g[i][j]);
}
if(g[3][7] == 0)//左下
{
chi[4][6] = wochi;
//insert(f[4][6],g[i][j]);
}
}
//棋盘顶层左边
if((i == 0)&&(j == 2))
{
if(g[1][1] == 0)//左下
{
chi[2][0] = wochi;
//insert(f[2][0],g[i][j]);
}
if(g[1][3] == 0)//右下
{
chi[2][4] = wochi;
//insert(f[2][4],g[i][j]);
}
}
//棋盘顶层右边
if((i == 0)&&(j == 6))
{
if(g[1][5] == 0)//左下
{
chi[2][4] = wochi;
//insert(f[2][4],g[i][j]);
}
if(g[1][7] == 0)//右下
{
chi[2][8] = wochi;
//insert(f[2][8],g[i][j]);
}
}
break;
}
case 22:
case 23:
{
//马
//竖日左上角
if(((i-2) >= 0)&&((i-2) <= NUMY)&&((j-1) >= 0)&&((j-1) <= NUMX)&&(g[i-1][j] == 0))
{
chi[i-2][j-1] = wochi;
//insert(f[i-2][j-1],g[i][j]);
}
//竖日右上角
if(((i-2) >= 0)&&((i-2) <= NUMY)&&((j+1) >= 0)&&((j+1) <= NUMX)&&(g[i-1][j] == 0))
{
chi[i-2][j+1] = wochi;
//insert(f[i-2][j+1],g[i][j]);
}
//横日左上角
if(((i-1) >= 0)&&((i-1) <= NUMY)&&((j-2) >= 0)&&((j-2) <= NUMX)&&(g[i][j-1] == 0))
{
chi[i-1][j-2] = wochi;
//insert(f[i-1][j-2],g[i][j]);
}
//横日右上角
if(((i-1) >= 0)&&((i-1) <= NUMY)&&((j+2) >= 0)&&((j+2) <= NUMX)&&(g[i][j+1] == 0))
{
chi[i-1][j+2] = wochi;
//insert(f[i-1][j+2],g[i][j]);
}
//横日左下角
if(((i+1) >= 0)&&((i+1) <= NUMY)&&((j-2) >= 0)&&((j-2) <= NUMX)&&(g[i][j-1] == 0))
{
chi[i+1][j-2] = wochi;
//insert(f[i+1][j-2],g[i][j]);
}
//横日右下角
if(((i+1) >= 0)&&((i+1) <= NUMY)&&((j+2) >= 0)&&((j+2) <= NUMX)&&(g[i][j+1] == 0))
{
chi[i+1][j+2] = wochi;
//insert(f[i+1][j+2],g[i][j]);
}
//竖日左下角
if(((i+2) >= 0)&&((i+2) <= NUMY)&&((j-1) >= 0)&&((j-1) <= NUMX)&&(g[i+1][j] == 0))
{
chi[i+2][j-1] = wochi;
//insert(f[i+2][j-1],g[i][j]);
}
//竖日右下角
if(((i+2) >= 0)&&((i+2) <= NUMY)&&((j+1) >= 0)&&((j+1) <= NUMX)&&(g[i+1][j] == 0))
{
chi[i+2][j+1] = wochi;
//insert(f[i+2][j+1],g[i][j]);
}
break;
}
case 24:
case 25:
{
//车
//向上扫描
for(int t = i-1; t >= 0; t--)
{
//insert(f[t][j],g[i][j]);
chi[t][j] = wochi;
if((g[t][j] >= 1)&&(g[t][j] <= 16))
{
break;
}
if((g[t][j] >= 17)&&(g[t][j] <= 32))
{
break;
}
}
//向下扫描
for(int t = i+1; t <= 9; t++)
{
//insert(f[t][j],g[i][j]);
chi[t][j] = wochi;
if((g[t][j] >= 1)&&(g[t][j] <= 16))
{
break;
}
if((g[t][j] >= 17)&&(g[t][j] <= 32))
{
break;
}
}
//向左扫描
for(int t = j-1; t >= 0; t--)
{
//insert(f[i][t],g[i][j]);
chi[i][t] = wochi;
if((g[i][t] >= 1)&&(g[i][t] <= 16))
{
break;
}
if((g[i][t] >= 17)&&(g[i][t] <= 32))
{
break;
}
}
//向右扫描
for(int t = j+1; t <= 8; t++)
{
//insert(f[i][t],g[i][j]);
chi[i][t] = wochi;
if((g[i][t] >= 1)&&(g[i][t] <= 16))
{
break;
}
if((g[i][t] >= 17)&&(g[i][t] <= 32))
{
break;
}
}
break;
}
case 26:
case 27:
{
//炮
//向上查找
{
int t = 0;
for(int h = (i-1); h >= 0; h--)
{
if((g[h][j] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
//insert(f[h][j],g[i][j]);
chi[h][j] = wochi;
if(g[h][j] != 0)
{
break;
}
}
}
}
//向下查找
{
int t = 0;
for(int h = (i+1); h <= NUMY; h++)
{
if((g[h][j] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
//insert(f[h][j],g[i][j]);
chi[h][j] = wochi;
if(g[h][j] != 0)
{
break;
}
}
}
}
//向左查找
{
int t = 0;
for(int h = (j-1); h >= 0; h--)
{
if((g[i][h] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
//insert(f[i][h],g[i][j]);
chi[i][h] = wochi;
if(g[i][h] != 0)
{
break;
}
}
}
}
//向右查找
{
int t = 0;
for(int h = (j+1); h <= NUMX; h++)
{
if((g[i][h] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
//insert(f[i][h],g[i][j]);
chi[i][h] = wochi;
if(g[i][h] != 0)
{
break;
}
}
}
}
break;
}
case 28:
case 29:
case 30:
case 31:
case 32:
{
//卒
// 向前
//insert(f[i+1][j],g[i][j]);
chi[i+1][j] = wochi;
//向左
if((i >= 5)&&(i <= 9))
{
chi[i][j+1] = wochi;
//insert(f[i][j+1],g[i][j]);
}
//向右
if((i >= 5)&&(i <= 9))
{
chi[i][j-1] = wochi;
//insert(f[i][j-1],g[i][j]);
}
break;
}
}
}
}
void ChessBroad::guize(int qizi)//刷新了chi数组只是对炮做小的调整
{
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
gui[i][j] = 0;
}
int i = my[qizi].myi;
int j = my[qizi].myj;
chizi(qizi);
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
gui[i][j]=chi[i][j];
}
if((qizi==26)||(qizi == 27)||(qizi == 10)||(qizi==11))
{
//炮
//向上查找
{
int t=0;
for(int h = (i-1); h >= 0; h--)
{
if((g[h][j] == 0)&&(t==0))
{
gui[h][j]=qizi;
}
if((g[h][j] != 0)&&(t==0))
{
t=1;
}
if((g[h][j] == 0)&&(t==1))
{
gui[h][j]=0;
}
}
}
//向下查找
{
int t=0;
for(int h = (i+1); h <= NUMY; h++)
{
if((g[h][j] == 0)&&(t==0))
{
gui[h][j]=qizi;
}
if((g[h][j] != 0)&&(t==0))
{
t=1;
}
if((g[h][j] == 0)&&(t==1))
{
gui[h][j]=0;
}
}
}
//向左查找
{
int t=0;
for(int h = (j-1); h >= 0; h--)
{
if((g[i][h] == 0)&&(t==0))
{
gui[i][h]=qizi;
}
if((g[i][h] != 0)&&(t==0))
{
t=1;
}
if((g[i][h] == 0)&&(t==1))
{
gui[i][h]=0;
}
}
}
//向右查找
{
int t=0;
for(int h = (j+1); h <= NUMX; h++)
{
if((g[i][h] == 0)&&(t==0))
{
gui[i][h]=qizi;
}
if((g[i][h] != 0)&&(t==0))
{
t=1;
}
if((g[i][h] == 0)&&(t==1))
{
gui[i][h]=0;
}
}
}
}
}
void ChessBroad::laole()
{
for(int i=0; i < N+1; i++)
for(int j=0; j < N+1; j++)
{
la[i][j] = 0;
}
//初始化la数组
{
for(int i=1; i < N+1; i++)
{
int a = my[i].myi;
int b = my[i].myj;
for(chess * p = f[a][b]; p != NULL; p = p->next)
{
la[p->name][i] = 1;
//qWarning("la %d %d %d",p->name,i,la[p->name][i]);
}
}
/*
for(int i=0; i < N+1; i++)
for(int j=0; j < N+1; j++)
{
qWarning("la %d %d %d",i,j,la[i][j]);
}*/
}
}
//pengpeng中包含laole
void ChessBroad::pengpeng()
{
//qWarning("pengpeng");
for(int i=0; i < N+1; i++)
{
my[i].myi = -1;
my[i].myj = -1;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
f[i][j] = NULL;
}
for(int i=0; i < N+1; i++)
for(int j=0; j < N+1; j++)
{
la[i][j] = 0;
}
for(int i = 0; i < NUMY+1; i++)//在f[][]中做标记
for(int j = 0; j < NUMX+1; j++)
{
switch( g[i][j] )
{
case 1://红棋
{
//帅
//向上
if((i-1) >= 7)
{
insert(f[i-1][j],g[i][j]);
}
//向下
if((i+1) <= 9)
{
insert(f[i+1][j],g[i][j]);
}
//向左
if((j-1) >= 3)
{
insert(f[i][j-1],g[i][j]);
}
//向右
if((j+1) <= 5)
{
insert(f[i][j+1],g[i][j]);
}
//老将对头向上
for(int n = (i-1); n >= 0; n--)
{
if((g[n][j]!=0)&&g[n][j]!=17)
{
break;
}
if(g[n][j]==17)
{
insert(f[n][j],g[i][j]);
break;
}
}
break;
}
case 2:
case 3:
{
//仕
if((i == 8)&&(j == 4))//仕在正中
{
//左上
insert(f[7][3],g[i][j]);
//右上
insert(f[7][5],g[i][j]);
//左下
insert(f[9][3],g[i][j]);
//右下
insert(f[9][5],g[i][j]);
}
if(((i == 7)&&(j == 3))//仕在左上角
||((i == 7)&&(j == 5))//仕在右上角
||((i == 9)&&(j == 3))//仕在左下角
||((i == 9)&&(j == 5)))//仕在右下角
{
insert(f[8][4],g[i][j]);
}
break;
}
case 4:
case 5:
{
//相
//底层左边
if((i == 9)&&(j == 2))
{
if(g[8][1] == 0)//左上
{
insert(f[7][0],g[i][j]);
}
if(g[8][3] == 0)//右上
{
insert(f[7][4],g[i][j]);
}
}
//底层右边
if((i == 9)&&(j == 6))
{
if(g[8][5] == 0)//左上
{
insert(f[7][4],g[i][j]);
}
if(g[8][7] == 0)//右上
{
insert(f[7][8],g[i][j]);
}
}
//中层左边
if((i == 7)&&(j == 0))
{
if(g[6][1] == 0)//右上
{
insert(f[5][2],g[i][j]);
}
if(g[8][1] == 0)//右下
{
insert(f[9][2],g[i][j]);
}
}
//中层中间
if((i == 7)&&(j == 4))
{
if(g[6][5] == 0)//右上
{
insert(f[5][6],g[i][j]);
}
if(g[8][5] == 0)//右下
{
insert(f[9][6],g[i][j]);
}
if(g[6][3] == 0)//左上
{
insert(f[5][2],g[i][j]);
}
if(g[8][3] == 0)//左下
{
insert(f[9][2],g[i][j]);
}
}
//中层右边
if((i == 7)&&(j == 8))
{
if(g[6][7] == 0)//左上
{
insert(f[5][6],g[i][j]);
}
if(g[8][7] == 0)//左下
{
insert(f[9][6],g[i][j]);
}
}
//顶层左边
if((i == 5)&&(j == 2))
{
if(g[6][1] == 0)//左下
{
insert(f[7][0],g[i][j]);
}
if(g[6][3] == 0)//右下
{
insert(f[7][4],g[i][j]);
}
}
//顶层右边
if((i == 5)&&(j == 6))
{
if(g[6][5] == 0)//左下
{
insert(f[7][4],g[i][j]);
}
if(g[6][7] == 0)//右下
{
insert(f[7][8],g[i][j]);
}
}
break;
}
case 6:
case 7:
{
//马
//竖日左上角
if(((i-2) >= 0)&&((i-2) <= NUMY)&&((j-1) >= 0)&&((j-1) <= NUMX)&&(g[i-1][j] == 0))
{
insert(f[i-2][j-1],g[i][j]);
}
//竖日右上角
if(((i-2) >= 0)&&((i-2) <= NUMY)&&((j+1) >= 0)&&((j+1) <= NUMX)&&(g[i-1][j] == 0))
{
insert(f[i-2][j+1],g[i][j]);
}
//横日左上角
if(((i-1) >= 0)&&((i-1) <= NUMY)&&((j-2) >= 0)&&((j-2) <= NUMX)&&(g[i][j-1] == 0))
{
insert(f[i-1][j-2],g[i][j]);
}
//横日右上角
if(((i-1) >= 0)&&((i-1) <= NUMY)&&((j+2) >= 0)&&((j+2) <= NUMX)&&(g[i][j+1] == 0))
{
insert(f[i-1][j+2],g[i][j]);
}
//横日左下角
if(((i+1) >= 0)&&((i+1) <= NUMY)&&((j-2) >= 0)&&((j-2) <= NUMX)&&(g[i][j-1] == 0))
{
insert(f[i+1][j-2],g[i][j]);
}
//横日右下角
if(((i+1) >= 0)&&((i+1) <= NUMY)&&((j+2) >= 0)&&((j+2) <= NUMX)&&(g[i][j+1] == 0))
{
insert(f[i+1][j+2],g[i][j]);
}
//竖日左下角
if(((i+2) >= 0)&&((i+2) <= NUMY)&&((j-1) >= 0)&&((j-1) <= NUMX)&&(g[i+1][j] == 0))
{
insert(f[i+2][j-1],g[i][j]);
}
//竖日右下角
if(((i+2) >= 0)&&((i+2) <= NUMY)&&((j+1) >= 0)&&((j+1) <= NUMX)&&(g[i+1][j] == 0))
{
insert(f[i+2][j+1],g[i][j]);
}
break;
}
case 8:
case 9:
{
//车
//向上扫描
for(int t = i-1; t >= 0; t--)
{
{
insert(f[t][j],g[i][j]);
}
if((g[t][j] >= 1)&&(g[t][j] <= 16))
{
break;
}
if((g[t][j] >= 17)&&(g[t][j] <= 32))
{
break;
}
}
//向下扫描
for(int t = i+1; t <= 9; t++)
{
insert(f[t][j],g[i][j]);
if((g[t][j] >= 1)&&(g[t][j] <= 16))
{
break;
}
if((g[t][j] >= 17)&&(g[t][j] <= 32))
{
break;
}
}
//向左扫描
for(int t = j-1; t >= 0; t--)
{
insert(f[i][t],g[i][j]);
if((g[i][t] >= 1)&&(g[i][t] <= 16))
{
break;
}
if((g[i][t] >= 17)&&(g[i][t] <= 32))
{
break;
}
}
//向右扫描
for(int t = j+1; t <= 8; t++)
{
insert(f[i][t],g[i][j]);
if((g[i][t] >= 1)&&(g[i][t] <= 16))
{
break;
}
if((g[i][t] >= 17)&&(g[i][t] <= 32))
{
break;
}
}
break;
}
case 10:
case 11:
{
//炮
//向上查找
{
int t = 0;
for(int h = (i-1); h >= 0; h--)
{
if((g[h][j] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
insert(f[h][j],g[i][j]);
if(g[h][j] != 0)
{
break;
}
}
}
}
//向下查找
{
int t = 0;
for(int h = (i+1); h <= NUMY; h++)
{
if((g[h][j] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
insert(f[h][j],g[i][j]);
if(g[h][j] != 0)
{
break;
}
}
}
}
//向左查找
{
int t = 0;
for(int h = (j-1); h >= 0; h--)
{
if((g[i][h] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
insert(f[i][h],g[i][j]);
if(g[i][h] != 0)
{
break;
}
}
}
}
//向右查找
{
int t = 0;
for(int h = (j+1); h <= NUMX; h++)
{
if((g[i][h] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
insert(f[i][h],g[i][j]);
if(g[i][h] != 0)
{
break;
}
}
}
}
break;
}
case 12:
case 13:
case 14:
case 15:
case 16:
{
//兵
// 向前
{
insert(f[i-1][j],g[i][j]);
}
//向左
if((i >= 0)&&(i <= 4))
{
insert(f[i][j-1],g[i][j]);
}
//向右
if((i >= 0)&&(i <= 4))
{
insert(f[i][j+1],g[i][j]);
}
break;
}
case 17://黑棋
{
//将
//向上
if((i-1) >= 0)
{
insert(f[i-1][j],g[i][j]);
}
//向下
if((i+1) <= 2)
{
insert(f[i+1][j],g[i][j]);
}
//向左
if((j-1) >= 3)
{
insert(f[i][j-1],g[i][j]);
}
//向右
if((j+1) <= 5)
{
insert(f[i][j+1],g[i][j]);
}
//老将对头向下
for(int n = (i+1); n <= 9; n++)
{
if((g[n][j]!=0)&&g[n][j]!=1)
{
break;
}
if(g[n][j]==1)
{
insert(f[n][j],g[i][j]);
break;
}
}
break;
}
case 18:
case 19:
{
//士
if((i == 1)&&(j == 4))//士在正中
{
//左上
insert(f[0][3],g[i][j]);
//右上
insert(f[0][5],g[i][j]);
//左下
insert(f[2][3],g[i][j]);
//右下
insert(f[2][5],g[i][j]);
}
if(((i == 0)&&(j == 3))//士在左上角
||((i == 0)&&(j == 5))//士在右上角
||((i == 2)&&(j == 3))//士在左下角
||((i == 2)&&(j == 5)))//士在右下角
{
insert(f[1][4],g[i][j]);
}
break;
}
case 20:
case 21:
{
//象
//河边左边
if((i == 4)&&(j == 2))
{
if(g[3][1] == 0)//左上
{
insert(f[2][0],g[i][j]);
}
if(g[3][3] == 0)//右上
{
insert(f[2][4],g[i][j]);
}
}
//河边右边
if((i == 4)&&(j == 6))
{
if(g[3][5] == 0)//左上
{
insert(f[2][4],g[i][j]);
}
if(g[3][7] == 0)//右上
{
insert(f[2][8],g[i][j]);
}
}
//中层左边
if((i == 2)&&(j == 0))
{
if(g[1][1] == 0)//右上
{
insert(f[0][2],g[i][j]);
}
if(g[3][1] == 0)//右下
{
insert(f[4][2],g[i][j]);
}
}
//中层中间
if((i == 2)&&(j == 4))
{
if(g[1][5] == 0)//右上
{
insert(f[0][6],g[i][j]);
}
if(g[3][5] == 0)//右下
{
insert(f[4][6],g[i][j]);
}
if(g[1][3] == 0)//左上
{
insert(f[0][2],g[i][j]);
}
if(g[3][3] == 0)//左下
{
insert(f[4][2],g[i][j]);
}
}
//中层右边
if((i == 2)&&(j == 8))
{
if(g[1][7] == 0)//左上
{
insert(f[0][6],g[i][j]);
}
if(g[3][7] == 0)//左下
{
insert(f[4][6],g[i][j]);
}
}
//棋盘顶层左边
if((i == 0)&&(j == 2))
{
if(g[1][1] == 0)//左下
{
insert(f[2][0],g[i][j]);
}
if(g[1][3] == 0)//右下
{
insert(f[2][4],g[i][j]);
}
}
//棋盘顶层右边
if((i == 0)&&(j == 6))
{
if(g[1][5] == 0)//左下
{
insert(f[2][4],g[i][j]);
}
if(g[1][7] == 0)//右下
{
insert(f[2][8],g[i][j]);
}
}
break;
}
case 22:
case 23:
{
//马
//竖日左上角
if(((i-2) >= 0)&&((i-2) <= NUMY)&&((j-1) >= 0)&&((j-1) <= NUMX)&&(g[i-1][j] == 0))
{
insert(f[i-2][j-1],g[i][j]);
}
//竖日右上角
if(((i-2) >= 0)&&((i-2) <= NUMY)&&((j+1) >= 0)&&((j+1) <= NUMX)&&(g[i-1][j] == 0))
{
insert(f[i-2][j+1],g[i][j]);
}
//横日左上角
if(((i-1) >= 0)&&((i-1) <= NUMY)&&((j-2) >= 0)&&((j-2) <= NUMX)&&(g[i][j-1] == 0))
{
insert(f[i-1][j-2],g[i][j]);
}
//横日右上角
if(((i-1) >= 0)&&((i-1) <= NUMY)&&((j+2) >= 0)&&((j+2) <= NUMX)&&(g[i][j+1] == 0))
{
insert(f[i-1][j+2],g[i][j]);
}
//横日左下角
if(((i+1) >= 0)&&((i+1) <= NUMY)&&((j-2) >= 0)&&((j-2) <= NUMX)&&(g[i][j-1] == 0))
{
insert(f[i+1][j-2],g[i][j]);
}
//横日右下角
if(((i+1) >= 0)&&((i+1) <= NUMY)&&((j+2) >= 0)&&((j+2) <= NUMX)&&(g[i][j+1] == 0))
{
insert(f[i+1][j+2],g[i][j]);
}
//竖日左下角
if(((i+2) >= 0)&&((i+2) <= NUMY)&&((j-1) >= 0)&&((j-1) <= NUMX)&&(g[i+1][j] == 0))
{
insert(f[i+2][j-1],g[i][j]);
}
//竖日右下角
if(((i+2) >= 0)&&((i+2) <= NUMY)&&((j+1) >= 0)&&((j+1) <= NUMX)&&(g[i+1][j] == 0))
{
insert(f[i+2][j+1],g[i][j]);
}
break;
}
case 24:
case 25:
{
//车
//向上扫描
for(int t = i-1; t >= 0; t--)
{
insert(f[t][j],g[i][j]);
if((g[t][j] >= 1)&&(g[t][j] <= 16))
{
break;
}
if((g[t][j] >= 17)&&(g[t][j] <= 32))
{
break;
}
}
//向下扫描
for(int t = i+1; t <= 9; t++)
{
insert(f[t][j],g[i][j]);
if((g[t][j] >= 1)&&(g[t][j] <= 16))
{
break;
}
if((g[t][j] >= 17)&&(g[t][j] <= 32))
{
break;
}
}
//向左扫描
for(int t = j-1; t >= 0; t--)
{
insert(f[i][t],g[i][j]);
if((g[i][t] >= 1)&&(g[i][t] <= 16))
{
break;
}
if((g[i][t] >= 17)&&(g[i][t] <= 32))
{
break;
}
}
//向右扫描
for(int t = j+1; t <= 8; t++)
{
insert(f[i][t],g[i][j]);
if((g[i][t] >= 1)&&(g[i][t] <= 16))
{
break;
}
if((g[i][t] >= 17)&&(g[i][t] <= 32))
{
break;
}
}
break;
}
case 26:
case 27:
{
//炮
//向上查找
{
int t = 0;
for(int h = (i-1); h >= 0; h--)
{
if((g[h][j] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
insert(f[h][j],g[i][j]);
if(g[h][j] != 0)
{
break;
}
}
}
}
//向下查找
{
int t = 0;
for(int h = (i+1); h <= NUMY; h++)
{
if((g[h][j] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
insert(f[h][j],g[i][j]);
if(g[h][j] != 0)
{
break;
}
}
}
}
//向左查找
{
int t = 0;
for(int h = (j-1); h >= 0; h--)
{
if((g[i][h] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
insert(f[i][h],g[i][j]);
if(g[i][h] != 0)
{
break;
}
}
}
}
//向右查找
{
int t = 0;
for(int h = (j+1); h <= NUMX; h++)
{
if((g[i][h] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
insert(f[i][h],g[i][j]);
if(g[i][h] != 0)
{
break;
}
}
}
}
break;
}
case 28:
case 29:
case 30:
case 31:
case 32:
{
//卒
// 向前
insert(f[i+1][j],g[i][j]);
//向左
if((i >= 5)&&(i <= 9))
{
insert(f[i][j+1],g[i][j]);
}
//向右
if((i >= 5)&&(i <= 9))
{
insert(f[i][j-1],g[i][j]);
}
break;
}
}
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
if(g[i][j] != 0)
{
my[(g[i][j])].myi = i;
my[(g[i][j])].myj = j;
//qWarning("my %d %d %d",g[i][j],my[(g[i][j])].myi,my[(g[i][j])].myj);
}
}
//在d[][]中做标记
for(int i = 0; i < NUMY+1; i++)//遍历f[][]
for(int j = 0; j < NUMX+1; j++)
{
for(chess* p=f[i][j]; p!=NULL; p=p->next)
{
if(g[i][j]!=0)
{
int a,b;
a = my[(p->name)].myi;
b = my[(p->name)].myj;
insert(d[a][b],g[i][j]);
}
}
}
//初始化la数组
{
for(int i=1; i < N+1; i++)
{
int a = my[i].myi;
int b = my[i].myj;
if((a!=-1)&&(b!=-1))
for(chess * p = f[a][b]; p != NULL; p = p->next)
{
la[p->name][i] = 1;
//qWarning("la %d %d %d",p->name,i,la[p->name][i]);
}
}
/*
for(int i=0; i < N+1; i++)
for(int j=0; j < N+1; j++)
{
qWarning("la %d %d %d",i,j,la[i][j]);
}*/
}
}
//逃跑时想着老将,还没实现
//需要修改
void ChessBroad::taopao(int ta ,int wo)
{
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
tao[i][j] = 0;
}
//我想跑 不管有没有保护不走到别人口中
{
for(int i = 0; i < NUMY+1; i++)//在e_r[][]中做标记
for(int j = 0; j < NUMX+1; j++)
{
switch( g[i][j] )
{
case 1://红棋
{
//帅
//向上
if((i-1) >= 7)
{
tao[i-1][j] = -2;//1代表没有空位跑
}
//向下
if((i+1) <= 9)
{
tao[i+1][j] = -2;
}
//向左
if((j-1) >= 3)
{
tao[i][j-1] = -2;
}
//向右
if((j+1) <= 5)
{
tao[i][j+1] = -2;
}
//老将对头向上
for(int n = (i-1); n >= 0; n--)
{
if((g[n][j]!=0)&&g[n][j]!=17)
{
break;
}
if(g[n][j]==17)
{
tao[n][j] = -2;
break;
}
}
break;
}
case 2:
case 3:
{
//仕
if((i == 8)&&(j == 4))//仕在正中
{
//左上
tao[7][3] = -2;
//右上
tao[7][5] = -2;
//左下
tao[9][3] = -2;
//右下
tao[9][5] = -2;
}
if(((i == 7)&&(j == 3))//仕在左上角
||((i == 7)&&(j == 5))//仕在右上角
||((i == 9)&&(j == 3))//仕在左下角
||((i == 9)&&(j == 5)))//仕在右下角
{
tao[8][4] = -2;
}
break;
}
case 4:
case 5:
{
//相
//底层左边
if((i == 9)&&(j == 2))
{
if(g[8][1] == 0)//左上
{
tao[7][0] = -2;
}
if(g[8][3] == 0)//右上
{
tao[7][4] = -2;
}
}
//底层右边
if((i == 9)&&(j == 6))
{
if(g[8][5] == 0)//左上
{
tao[7][4] = -2;
}
if(g[8][7] == 0)//右上
{
tao[7][8] = -2;
}
}
//中层左边
if((i == 7)&&(j == 0))
{
if(g[6][1] == 0)//右上
{
tao[5][2] = -2;
}
if(g[8][1] == 0)//右下
{
tao[9][2] = -2;
}
}
//中层中间
if((i == 7)&&(j == 4))
{
if(g[6][5] == 0)//右上
{
tao[5][6] = -2;
}
if(g[8][5] == 0)//右下
{
tao[9][6] = -2;
}
if(g[6][3] == 0)//左上
{
tao[5][2] = -2;
}
if(g[8][3] == 0)//左下
{
tao[9][2] = -2;
}
}
//中层右边
if((i == 7)&&(j == 8))
{
if(g[6][7] == 0)//左上
{
tao[5][6] = -2;
}
if(g[8][7] == 0)//左下
{
tao[9][6] = -2;
}
}
//顶层左边
if((i == 5)&&(j == 2))
{
if(g[6][1] == 0)//左下
{
tao[7][0] = -2;
}
if(g[6][3] == 0)//右下
{
tao[7][4] = -2;
}
}
//顶层右边
if((i == 5)&&(j == 6))
{
if(g[6][5] == 0)//左下
{
tao[7][4] = -2;
}
if(g[6][7] == 0)//右下
{
tao[7][8] = -2;
}
}
break;
}
case 6:
case 7:
{
//马
//竖日左上角
if(((i-2) >= 0)&&((i-2) <= NUMY)&&((j-1) >= 0)&&((j-1) <= NUMX)&&(g[i-1][j] == 0))
{
tao[i-2][j-1] = -2;
}
//竖日右上角
if(((i-2) >= 0)&&((i-2) <= NUMY)&&((j+1) >= 0)&&((j+1) <= NUMX)&&(g[i-1][j] == 0))
{
tao[i-2][j+1] = -2;
}
//横日左上角
if(((i-1) >= 0)&&((i-1) <= NUMY)&&((j-2) >= 0)&&((j-2) <= NUMX)&&(g[i][j-1] == 0))
{
tao[i-1][j-2] = -2;
}
//横日右上角
if(((i-1) >= 0)&&((i-1) <= NUMY)&&((j+2) >= 0)&&((j+2) <= NUMX)&&(g[i][j+1] == 0))
{
tao[i-1][j+2] = -2;
}
//横日左下角
if(((i+1) >= 0)&&((i+1) <= NUMY)&&((j-2) >= 0)&&((j-2) <= NUMX)&&(g[i][j-1] == 0))
{
tao[i+1][j-2] = -2;
}
//横日右下角
if(((i+1) >= 0)&&((i+1) <= NUMY)&&((j+2) >= 0)&&((j+2) <= NUMX)&&(g[i][j+1] == 0))
{
tao[i+1][j+2] = -2;
}
//竖日左下角
if(((i+2) >= 0)&&((i+2) <= NUMY)&&((j-1) >= 0)&&((j-1) <= NUMX)&&(g[i+1][j] == 0))
{
tao[i+2][j-1] = -2;
}
//竖日右下角
if(((i+2) >= 0)&&((i+2) <= NUMY)&&((j+1) >= 0)&&((j+1) <= NUMX)&&(g[i+1][j] == 0))
{
tao[i+2][j+1] = -2;
}
break;
}
case 8:
case 9:
{
//车 先不考虑他与我
//向上扫描
{
for(int t = i-1; t >= 0; t--)
{
if(g[t][j] == 0)
{
tao[t][j] = -2;
}
else
{
break;
}
}
}
//向下扫描
{
for(int t = i+1; t <= 9; t++)
{
if(g[t][j] == 0)
{
tao[t][j] = -2;
}
else
{
break;
}
}
}
//向左扫描
{
for(int t = j-1; t >= 0; t--)
{
if(g[i][t] == 0)
{
tao[i][t] = -2;
}
else
{
break;
}
}
}
//向右扫描
{
for(int t = j+1; t <= 8; t++)
{
if(g[i][t] == 0)
{
tao[i][t] = -2;
}
else
{
break;
}
}
}
//车 他与我
if(ta == g[i][j])//如果ta等于车
{
int a = my[wo].myi;//wo的位置
int b = my[wo].myj;
//wo在ta上边
if((a < i)&&(b == j))
{
for(int h = a-1; h >= 0; h--)
{
if(g[h][b] == 0)
{
tao[h][b] = -2;
}
if(g[h][b] != 0)
{
break;
}
}
}
//wo在ta下边
if((a > i)&&(b == j))
{
for(int h = a+1; h <= 9; h++)
{
if(g[h][b] == 0)
{
tao[h][b] = -2;
}
if(g[h][b] != 0)
{
break;
}
}
}
//wo在ta左边
if((a == i)&&(b < j))
{
for(int h = b-1; h >= 0; h--)
{
if(g[a][h] == 0)
{
tao[a][h] = -2;
}
if(g[a][h] != 0)
{
break;
}
}
}
//wo在ta右边
if((a == i)&&(b > j))
{
for(int h = b+1; h <= 8; h++)
{
if(g[a][h] == 0)
{
tao[a][h] = -2;
}
if(g[a][h] != 0)
{
break;
}
}
}
}
break;
}
case 10:
case 11:
{
//炮 先不考虑他与我
//向上查找
{
int t = 0;
for(int h = (i-1); h >= 0; h--)
{
if((g[h][j] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
if(g[h][j] == 0)
{
tao[h][j] = -2;
}
if(g[h][j] != 0)
{
break;
}
}
}
}
//向下查找
{
int t = 0;
for(int h = (i+1); h <= NUMY; h++)
{
if((g[h][j] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
if(g[h][j] == 0)
{
tao[h][j] = -2;
}
if(g[h][j] != 0)
{
break;
}
}
}
}
//向左查找
{
int t = 0;
for(int h = (j-1); h >= 0; h--)
{
if((g[i][h] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
if(g[i][h] == 0)
{
tao[i][h] = -2;
}
if(g[i][h] != 0)
{
break;
}
}
}
}
//向右查找
{
int t = 0;
for(int h = (j+1); h <= NUMX; h++)
{
if((g[i][h] != 0)&&(t == 0))
{
t = 1;
continue;
}
if(t == 1)
{
if(g[i][h] == 0)
{
tao[i][h] = -2;
}
if(g[i][h] != 0)
{
break;
}
}
}
}
//炮 考虑他与我
if(ta == g[i][j])//如果ta等于炮
{
int a = my[wo].myi;//wo的位置
int b = my[wo].myj;
//wo在ta上边
if((a < i)&&(b == j))
{
for(int h = a-1; h >= 0; h--)
{
if(g[h][b] == 0)
{
tao[h][b] = -2;
}
if(g[h][b] != 0)
{
break;
}
}
}
//wo在ta下边
if((a > i)&&(b == j))
{
for(int h = a+1; h <= 9; h++)
{
if(g[h][b] == 0)
{
tao[h][b] = -2;
}
if(g[h][b] != 0)
{
break;
}
}
}
//wo在ta左边
if((a == i)&&(b < j))
{
for(int h = b-1; h >= 0; h--)
{
if(g[a][h] == 0)
{
tao[a][h] = -2;
}
if(g[a][h] != 0)
{
break;
}
}
}
//wo在ta右边
if((a == i)&&(b > j))
{
for(int h = b+1; h <= 8; h++)
{
if(g[a][h] == 0)
{
tao[a][h] = -2;
}
if(g[a][h] != 0)
{
break;
}
}
}
}
break;
}
case 12:
case 13:
case 14:
case 15:
case 16:
{
//兵
// 向前
{
tao[i-1][j] = -2;
}
//向左
if((i >= 0)&&(i <= 4))
{
tao[i][j-1] = -2;
}
//向右
if((i >= 0)&&(i <= 4))
{
tao[i][j+1] = -2;
}
break;
}
}
}
//只有g[][]等于0的地才能逃
for(int i = 0; i < NUMY+1; i++)
for(int j = 0; j < NUMX+1; j++)
{
if(g[i][j] != 0)
{
tao[i][j] = -3;
}
//qWarning("tao %d %d %d",i,j,tao[i][j]);
}
{
int i = my[wo].myi;
int j = my[wo].myj;
//上面的都是写的条件
switch(wo)
{//蓝方
case 17:
{
//将
//向上
if((i-1) >= 0)
{
if(tao[i-1][j] == 0)
{
tao[i-1][j] = wo;
}else
{
tao[i-1][j] = -1;
}
}
//向下
if((i+1) <= 2)
{
if(tao[i+1][j] == 0)
{
tao[i+1][j] = wo;
}else
{
tao[i+1][j] = -1;
}
}
//向左
if((j-1) >= 3)
{
if(tao[i][j-1] == 0)
{
tao[i][j-1] = wo;
}else
{
tao[i][j-1] = -1;
}
}
//向右
if((j+1) <= 5)
{
if(tao[i][j+1] == 0)
{
tao[i][j+1] = wo;
}else
{
tao[i][j+1] = -1;
}
}
break;
}
case 18:
case 19://士
{
//士
if((i == 1)&&(j == 4))//士在正中
{
//左上
if(tao[0][3] == 0)
{
tao[0][3] = wo;
}else
{
tao[0][3] = -1;
}
//右上
if(tao[0][5] == 0)
{
tao[0][5] = wo;
}else
{
tao[0][5] = -1;
}
//左下
if(tao[2][3] == 0)
{
tao[2][3] = wo;
}else
{
tao[2][3] = -1;
}
//右下
if(tao[2][5] == 0)
{
tao[2][5] = wo;
}else
{
tao[2][5] = -1;
}
}
if(((i == 0)&&(j == 3))//士在左上角
||((i == 0)&&(j == 5))//士在右上角
||((i == 2)&&(j == 3))//士在左下角
||((i == 2)&&(j == 5)))//士在右下角
{
if(tao[1][4] == 0)
{
tao[1][4] = wo;
}else
{
tao[1][4] = -1;
}
}
break;
}
case 20:
case 21:
{
//象
//河边左边
if((i == 4)&&(j == 2))
{
if(g[3][1] == 0)//左上
{
if(tao[2][0] == 0)
{
tao[2][0] = wo;
}else
{
tao[2][0] = -1;
}
}
if(g[3][3] == 0)//右上
{
if(tao[2][4] == 0)
{
tao[2][4] = wo;
}else
{
tao[2][4] = -1;
}
}
}
//河边右边
if((i == 4)&&(j == 6))
{
if(g[3][5] == 0)//左上
{
if(tao[2][4] == 0)
{
tao[2][4] = wo;
}else
{
tao[2][4] = -1;
}
}
if(g[3][7] == 0)//右上
{
if(tao[2][8] == 0)
{
tao[2][8] = wo;
}else
{
tao[2][8] = -1;
}
}
}
//中层左边
if((i == 2)&&(j == 0))
{
if(g[1][1] == 0)//右上
{
if(tao[0][2] == 0)
{
tao[0][2] = wo;
}else
{
tao[0][2] = -1;
}
}
if(g[3][1] == 0)//右下
{
if(tao[4][2] == 0)
{
tao[4][2] = wo;
}else
{
tao[4][2] = -1;
}
}
}
//中层中间
if((i == 2)&&(j == 4))
{
if(g[1][5] == 0)//右上
{
if(tao[0][6] == 0)
{
tao[0][6] = wo;
}else
{
tao[0][6] = -1;
}
}
if(g[3][5] == 0)//右下
{
if(tao[4][6] == 0)
{
tao[4][6] = wo;
}else
{
tao[4][6] = -1;
}
}
if(g[1][3] == 0)//左上
{
if(tao[0][2] == 0)
{
tao[0][2] = wo;
}else
{
tao[0][2] = -1;
}
}
if(g[3][3] == 0)//左下
{
if(tao[4][2] == 0)
{
tao[4][2] = wo;
}else
{
tao[4][2] = -1;
}
}
}
//中层右边
if((i == 2)&&(j == 8))
{
if(g[1][7] == 0)//左上
{
if(tao[0][6] == 0)
{
tao[0][6] = wo;
}else
{
tao[0][6] = -1;
}
}
if(g[3][7] == 0)//左下
{
if(tao[4][6] == 0)
{
tao[4][6] = wo;
}else
{
tao[4][6] = -1;
}
}
}
//棋盘顶层左边
if((i == 0)&&(j == 2))
{
if(g[1][1] == 0)//左下
{
if(tao[2][0] == 0)
{
tao[2][0] = wo;
}else
{
tao[2][0] = -1;
}
}
if(g[1][3] == 0)//右下
{
if(tao[2][4] == 0)
{
tao[2][4] = wo;
}else
{
tao[2][4] = -1;
}
}
}
//棋盘顶层右边
if((i == 0)&&(j == 6))
{
if(g[1][5] == 0)//左下
{
if(tao[2][4] == 0)
{
tao[2][4] = wo;
}else
{
tao[2][4] = -1;
}
}
if(g[1][7] == 0)//右下
{
if(tao[2][8] == 0)
{
tao[2][8] = wo;
}else
{
tao[2][8] = -1;
}
}
}
break;
}
case 22:
case 23:
{
//马
//竖日左上角
if(((i-2) >= 0)&&((i-2) <= NUMY)&&((j-1) >= 0)&&((j-1) <= NUMX)&&(g[i-1][j] == 0))
{
if(tao[i-2][j-1] == 0)
{
tao[i-2][j-1] = wo;
}else
{
tao[i-2][j-1] = -1;
}
}
//竖日右上角
if(((i-2) >= 0)&&((i-2) <= NUMY)&&((j+1) >= 0)&&((j+1) <= NUMX)&&(g[i-1][j] == 0))
{
if(tao[i-2][j+1] == 0)
{
tao[i-2][j+1] = wo;
}else
{
tao[i-2][j+1] = -1;
}
}
//横日左上角
if(((i-1) >= 0)&&((i-1) <= NUMY)&&((j-2) >= 0)&&((j-2) <= NUMX)&&(g[i][j-1] == 0))
{
if(tao[i-1][j-2] == 0)
{
tao[i-1][j-2] = wo;
}else
{
tao[i-1][j-2] = -1;
}
}
//横日右上角
if(((i-1) >= 0)&&((i-1) <= NUMY)&&((j+2) >= 0)&&((j+2) <= NUMX)&&(g[i][j+1] == 0))
{
if(tao[i-1][j+2] == 0)
{
tao[i-1][j+2] = wo;
}else
{
tao[i-1][j+2] = -1;
}
}
//横日左下角
if(((i+1) >= 0)&&((i+1) <= NUMY)&&((j-2) >= 0)&&((j-2) <= NUMX)&&(g[i][j-1] == 0))
{
if(tao[i+1][j-2] == 0)
{
tao[i+1][j-2] = wo;
}else
{
tao[i+1][j-2] = -1;
}
}
//横日右下角
if(((i+1) >= 0)&&((i+1) <= NUMY)&&((j+2) >= 0)&&((j+2) <= NUMX)&&(g[i][j+1] == 0))
{
if(tao[i+1][j+2] == 0)
{
tao[i+1][j+2] = wo;
}else
{
tao[i+1][j+2] = -1;
}
}
//竖日左下角
if(((i+2) >= 0)&&((i+2) <= NUMY)&&((j-1) >= 0)&&((j-1) <= NUMX)&&(g[i+1][j] == 0))
{
if(tao[i+2][j-1] == 0)
{
tao[i+2][j-1] = wo;
}else
{
tao[i+2][j-1] = -1;
}
}
//竖日右下角
if(((i+2) >= 0)&&((i+2) <= NUMY)&&((j+1) >= 0)&&((j+1) <= NUMX)&&(g[i+1][j] == 0))
{
if(tao[i+2][j+1] == 0)
{
tao[i+2][j+1] = wo;
}else
{
tao[i+2][j+1] = -1;
}
}
break;
}
case 24:
case 25:
{
//车
//向上扫描
for(int t = i-1; t >= 0; t--)
{
if(tao[t][j] == 0)
{
tao[t][j] = wo;
}else
{
tao[t][j] = -1;
}
if((g[t][j] >= 1)&&(g[t][j] <= 16))
{
break;
}
if((g[t][j] >= 17)&&(g[t][j] <= 32))
{
break;
}
}
//向下扫描
for(int t = i+1; t <= 9; t++)
{
if(tao[t][j] == 0)
{
tao[t][j] = wo;
}else
{
tao[t][j] = -1;
}
if((g[t][j] >= 1)&&(g[t][j] <= 16))
{
break;
}
if((g[t][j] >= 17)&&(g[t][j] <= 32))
{
break;
}
}
//向左扫描
for(int t = j-1; t >= 0; t--)
{
if(tao[i][t] == 0)
{
tao[i][t] = wo;
}else
{
tao[i][t] = -1;
}
if((g[i][t] >= 1)&&(g[i][t] <= 16))
{
break;
}
if((g[i][t] >= 17)&&(g[i][t] <= 32))
{
break;
}
}
//向右扫描
for(int t = j+1; t <= 8; t++)
{
if(tao[i][t] == 0)
{
tao[i][t] = wo;
}else
{
tao[i][t] = -1;
}
if((g[i][t] >= 1)&&(g[i][t] <= 16))
{
break;
}
if((g[i][t] >= 17)&&(g[i][t] <= 32))
{
break;
}
}
break;
}
case 26:
case 27:
{
//炮
//向上扫描
for(int t = i-1; t >= 0; t--)
{
if((g[t][j] >= 1)&&(g[t][j] <= 16))
{
break;
}
if((g[t][j] >= 17)&&(g[t][j] <= 32))
{
break;
}
if(tao[t][j] == 0)
{
tao[t][j] = wo;
}else
{
tao[t][j] = -1;
}
}
//向下扫描
for(int t = i+1; t <= 9; t++)
{
if((g[t][j] >= 1)&&(g[t][j] <= 16))
{
break;
}
if((g[t][j] >= 17)&&(g[t][j] <= 32))
{
break;
}
if(tao[t][j] == 0)
{
tao[t][j] = wo;
}else
{
tao[t][j] = -1;
}
}
//向左扫描
for(int t = j-1; t >= 0; t--)
{
if((g[i][t] >= 1)&&(g[i][t] <= 16))
{
break;
}
if((g[i][t] >= 17)&&(g[i][t] <= 32))
{
break;
}
if(tao[i][t] == 0)
{
tao[i][t] = wo;
}else
{
tao[i][t] = -1;
}
}
//向右扫描
for(int t = j+1; t <= 8; t++)
{
if((g[i][t] >= 1)&&(g[i][t] <= 16))
{
break;
}
if((g[i][t] >= 17)&&(g[i][t] <= 32))
{
break;
}
if(tao[i][t] == 0)
{
tao[i][t] = wo;
}else
{
tao[i][t] = -1;
}
}
break;
}
case 28:
case 29:
case 30:
case 31:
case 32:
{
//卒
// 向前
if(tao[i+1][j] == 0)
{
tao[i+1][j] = wo;
}else
{
tao[i+1][j] = -1;
}
//向左
if((i >= 5)&&(i <= 9))
{
if(tao[i][j+1] == 0)
{
tao[i][j+1] = wo;
}else
{
tao[i][j+1] = -1;
}
}
//向右
if((i >= 5)&&(i <= 9))
{
if(tao[i][j-1] == 0)
{
tao[i][j-1] = wo;
}else
{
tao[i][j-1] = -1;
}
}
break;
}
}
}
}
//逃跑时想着老将
/*
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
*/
}
int ChessBroad::priority(int my)
{
switch(my)
{
case 1://老将
case 17:
{
return 10;
}
case 2://士
case 3:
case 18:
case 19:
{
return 3;
}
case 4://相
case 5:
case 20:
case 21:
{
return 3;
}
case 6://马
case 7:
case 22:
case 23:
{
return 4;
}
case 8://车
case 9:
case 24:
case 25:
{
return 5;
}
case 10://炮
case 11:
case 26:
case 27:
{
return 4;
}
case 12://兵
case 13:
case 14:
case 15:
case 16:
case 28:
case 29:
case 30:
case 31:
case 32:
{
return 2;
}
return -1;
}
}
//f与d共用
void ChessBroad::insert(chess* &f ,int a)
{
//qWarning("insert 1");
if(f == NULL)
{
//qWarning("insert 2");
int r=0,b=0;
chess * cp = (chess *)malloc(sizeof(chess));
cp->name = a;
cp->next = NULL;
cp->tail = a;
if((cp->name<17)&&(cp->name>0))//红棋
{
r++;
}
if((cp->name>16)&&(cp->name<=32))
{
b++;
}
cp->red=r;
cp->blue=b;
f = cp;
return;
}
if(f != NULL)
{
//qWarning("insert 3");
for(chess * p = f; p != NULL; p = p->next)
{
chess * q = p->next;
if((a < (p -> name))&&(p == f))
{
//qWarning("insert 4");
chess * cp = (chess *)malloc(sizeof(chess));
cp->name = a;
cp->next = f;
f = cp;
break;
}
if((a > (p -> name))&&(q == NULL))
{
//qWarning("insert 5");
chess * cp = (chess *)malloc(sizeof(chess));
cp->name = a;
cp->next = NULL;
p->next = cp;
break;
}
if((a > (p -> name))&&(a < (q -> name))&&(q != NULL))
{
//qWarning("insert 6");
chess * cp = (chess *)malloc(sizeof(chess));
cp->name = a;
cp->next = p->next;
p->next = cp;
break;
}
}
int temp_t = -1;
for(chess * p = f; p != NULL; p = p->next)
{
if(p->next == NULL)
{
temp_t = p->name;
}
}
for(chess * p = f; p != NULL; p = p->next)
{
p->tail = temp_t;
}
int r=0,b=0;
for(chess * p = f; p != NULL; p = p->next)
{
if((p->name<17)&&(p->name>0))//红棋
{
r++;
}
if((p->name>16)&&(p->name<=32))
{
b++;
}
}
for(chess * p = f; p != NULL; p = p->next)
{
p->red=r;
p->blue=b;
}
}
}
void ChessBroad::init()
{
key = 1;
p_h = 0;
bushu = 0;
for(int h=0; h < NUM+1; h++)
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
p[h][i][j] = 0;
}
for(int i=0; i < NUMY+1; i++)//初始化g[][]
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = 0;
//qWarning("%d %d %d",i,j,g[i][j]);
}
//黑棋
g[0][0]=24;//车
g[0][1]=22;//马
g[0][2]=20;//象
g[0][3]=18;//士
g[0][4]=17;//将
g[0][5]=19;//士
g[0][6]=21;//象
g[0][7]=23;//马
g[0][8]=25;//车
g[2][1]=26;//炮
g[2][7]=27;//炮
g[3][0]=28;//卒
g[3][2]=29;//卒
g[3][4]=30;//卒
g[3][6]=31;//卒
g[3][8]=32;//卒
//红棋
g[6][0]=12;//兵
g[6][2]=13;//兵
g[6][4]=14;//兵
g[6][6]=15;//兵
g[6][8]=16;//兵
g[7][1]=10;//炮
g[7][7]=11;//炮
g[9][0]=8;//车
g[9][1]=6;//马
g[9][2]=4;//相
g[9][3]=2;//士
g[9][4]=1;//帅
g[9][5]=3;//士
g[9][6]=5;//相
g[9][7]=7;//马
g[9][8]=9;//车
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
jilu[i][j] = g[i][j];
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
p[p_h][i][j] = g[i][j];
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
f[i][j] = NULL;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
d[i][j] = NULL;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
tao[i][j] = 0;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
chi[i][j] = 0;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
gui[i][j] = 0;
}
x_1 = -1;
y_1 = -1;
x_2 = -1;
y_2 = -1;
ta = -1;
for(int i=0; i < N+1; i++)
{
my[i].myi = -1;
my[i].myj = -1;
}
for(int i=0; i < N+1; i++)
for(int j=0; j < N+1; j++)
{
la[i][j] = 0;
}
for(int i=0; i < N+1; i++)//初始化sbu[]
{
sbu[i]=NULL;
}
update();
}
void ChessBroad::save()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save XiangQi"), ".",
tr("*.txt"));
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
qWarning("file open error");
}
QTextStream out(&file);
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
out << g[i][j]<< " ";
}
out << p_h<< " ";
for(int k=0; k < NUM; k++)
{
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
out << p[k][i][j]<< " ";
}
}
}
void ChessBroad::load()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Load XiangQi"), ".",
tr("*.txt"));
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
{
qWarning("load error");
return;
}
QTextStream in(&file);
key = 1;
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
in >> g[i][j];
}
in >> p_h;
for(int k=0; k < NUM; k++)
{
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
in >> p[k][i][j];
}
}
update();
}
void ChessBroad::backchess()//人机对弈退棋
{//一共能退九步
qWarning("TuiQi");
//检测是否是第一步
int t = 0;//测试前一步是否为全零,0为全零。
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
if(p[((p_h-1)+NUM+1)%(NUM+1)][i][j] != 0)
{
t = 1;
}
}
if((p_h == 0)&&(t == 0))
{
return;
}
if(t == 1)
{
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
p[p_h][i][j] = 0;
}
p_h = ((--p_h)+NUM+1)%(NUM+1);
qWarning("key %d", key);
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = p[p_h][i][j];
}
key = 1;
if(v == 1)//可以翻转胜负
{
v = 0;
}
bushu--;
update();
}
}
//双人游戏退棋
/*
void ChessBroad::backchess()
{//一共能退九步
qWarning("TuiQi");
//检测是否是第一步
int t = 0;//测试前一步是否为全零,0为全零。
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
if(p[((p_h-1)+NUM+1)%(NUM+1)][i][j] != 0)
{
t = 1;
}
}
if((p_h == 0)&&(t == 0))
{
return;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
p[p_h][i][j] = 0;
}
p_h = ((--p_h)+NUM+1)%(NUM+1);
if(t == 1)
{
qWarning("key %d", key);
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = p[p_h][i][j];
}
if(key == 1)//交换控制权
{
key = 17;
}
else
{
if(key == 17)
{
key = 1;
}
}
if(v == 1)//可以翻转胜负
{
v = 0;
}
update();
}
}
*/
void ChessBroad::paintEvent(QPaintEvent *e)
{
qWarning("Paint");
//受威胁的棋子都记录下来
pengpeng();
/*
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
if(g[i][j]!=0)
{
qWarning("g %d i %d j %d",g[i][j],i,j);
}
for(chess *p = d[i][j]; p != NULL; p= p->next)
{
qWarning("p->name %d",p->name);
qWarning("tail %d",p->tail);
}
}
*/
/*
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
qWarning("g %d i %d j %d",g[i][j],i,j);
for(chess *p = f[i][j]; p != NULL; p= p->next)
{
qWarning("tail %d",p->tail);
}
}
*/
//把那个棋走那步存在一个文件中
{
QString fileName = "XiangQi.txt";
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly|QIODevice::Append))
{
qWarning("file open error");
}
QTextStream out(&file);
int qi = -1, qj = -1, iq = -1, jq = -1, zhi = -1;
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
if((jilu[i][j] != g[i][j])&&(g[i][j] == 0))//所走棋以前的位置
{
//qWarning("jilu");
zhi = jilu[i][j];
qi = i;
qj = j;
iq = my[zhi].myi;
jq = my[zhi].myj;
//qWarning("zhi = %d , qi = %d , qj = %d , iq = %d , jq = %d",zhi,qi,qj,iq,jq);
out<<zhi<<" "<<qi<<","<<qj<<" "<<iq<<","<<jq<<" ";
break;
}
}
if(zhi != -1)
{
break;
}
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
jilu[i][j] = g[i][j];
}
}
QPainter* paint = new QPainter(this);
float max, min;
int flag;
if(this->width() >= this->height())
{
max = this->width();
min = this->height();
flag = 0;
}
else
{
max = this->height();
min = this->width();
flag = 1;
}
float check ;
if(flag == 0)//画棋盘
{
check = min/(NUMY+2);
paint->drawLine((max-(NUMX*check))/2, check, (max-(NUMX*check))/2, min-check);
paint->drawLine((max-(NUMX*check))/2+check*NUMX, check, (max-(NUMX*check))/2+check*NUMX, min-check);
for(int i = 1; i < NUMX; i++)//间断的竖线;
{
paint->drawLine((max-(NUMX*check))/2+check*i, check, (max-(NUMX*check))/2+check*i, check+check*4);
paint->drawLine((max-(NUMX*check))/2+check*i, check+check*5, (max-(NUMX*check))/2+check*i, min-check);
}
for(int i = 0; i <= NUMY; i++)
{
paint->drawLine((max-(NUMX*check))/2, check+i*check, (max-(max-(NUMX*check))/2), check+i*check);
}
paint->drawLine( (max-NUMX*check)/2+3*check, check, (max-NUMX*check)/2+5*check, 3*check);
paint->drawLine( (max-NUMX*check)/2+3*check, 3*check, (max-NUMX*check)/2+5*check, check);
paint->drawLine( (max-NUMX*check)/2+3*check, 8*check, (max-NUMX*check)/2+5*check, 10*check);
paint->drawLine( (max-NUMX*check)/2+3*check, 10*check, (max-NUMX*check)/2+5*check, 8*check);
}
if(flag == 1)
{
check = min/(NUMX+2);
paint->drawLine(check, (max-(check*NUMY))/2, check, (max-(check*NUMY))/2+NUMY*check);
paint->drawLine(check+NUMX*check, (max-(check*NUMY))/2, check+NUMX*check, (max-(check*NUMY))/2+NUMY*check);
for(int i = 1; i < NUMX; i++)//竖线
{
paint->drawLine( check+i*check, (max-(check*NUMY))/2, check+i*check, (max-(check*NUMY))/2+4*check);
paint->drawLine( check+i*check, (max-(check*NUMY))/2+check*5, check+i*check, (max-(check*NUMY))/2+NUMY*check);
}
for(int i = 0; i <= NUMY; i++)//横线
{
paint->drawLine(check, (max-(NUMY*check))/2+i*check, check+NUMX*check, (max-(NUMY*check))/2+i*check);
}
paint->drawLine( check*4, (max-(NUMY*check))/2, check*6, (max-(NUMY*check))/2+check*2);
paint->drawLine( check*4, (max-(NUMY*check))/2+check*2, check*6, (max-(NUMY*check))/2);
paint->drawLine( check*4, (max-(NUMY*check))/2+check*7, check*6, (max-(NUMY*check))/2+check*9);
paint->drawLine( check*4, (max-(NUMY*check))/2+check*9, check*6, (max-(NUMY*check))/2+check*7);
}
if( flag == 0 )//画辅助线
{
paint->setPen(QPen( Qt::red, 1, Qt::DotLine));
for(int i=0; i <= NUMX+1; i++)
{
paint->drawLine((max-NUMX*check)/2-check/2+i*check, check/2, (max-NUMX*check)/2-check/2+i*check, min-check/2);//竖线
}
for(int i=0; i <= NUMY+1; i++)
{
paint->drawLine((max-NUMX*check)/2-check/2, check/2+check*i, (max-NUMX*check)/2+NUMX*check+check/2, check/2+check*i);
}
paint->setPen(QPen( Qt::black, 1, Qt::SolidLine));
}
if( flag == 1 )
{
paint->setPen(QPen( Qt::red, 1, Qt::DotLine));
for(int i=0; i <= NUMX+1; i++)
{
paint->drawLine( check/2+check*i, (max-NUMY*check)/2-check/2, check/2+check*i, (max-NUMY*check)/2+NUMY*check+check/2);
}
for(int i=0; i <= NUMY+1; i++)
{
paint->drawLine( check/2, (max-NUMY*check)/2-check/2+i*check, min-check/2, (max-NUMY*check)/2-check/2+i*check);
}
paint->setPen(QPen( Qt::black, 1, Qt::SolidLine));
}
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));
paint->setFont(QFont("Arial", check/2));
if( flag == 0 )
{
for(int i = 0; i < NUMY+1; i++)//根据g[][]画棋子
for(int j = 0; j < NUMX+1; j++)
{
if( g[i][j]/17 == 0)
{
paint->setPen(Qt::red);
}
if( g[i][j]/17 == 1)
{
paint->setPen(Qt::blue);
}
if( g[i][j] != 0)
{
paint->drawEllipse( (max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check);
switch( g[i][j] )
{
case 1:
{
QString text("帅");
paint->drawText((max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check, Qt::AlignCenter, text);
break;
}
case 2:
case 3:
{
QString text("仕");
paint->drawText((max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check, Qt::AlignCenter, text);
break;
}
case 4:
case 5:
{
QString text("相");
paint->drawText((max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check, Qt::AlignCenter, text);
break;
}
case 6:
case 7:
{
QString text("马");
paint->drawText((max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check, Qt::AlignCenter, text);
break;
}
case 8:
case 9:
{
QString text("车");
paint->drawText((max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check, Qt::AlignCenter, text);
break;
}
case 10:
case 11:
{
QString text("炮");
paint->drawText((max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check, Qt::AlignCenter, text);
break;
}
case 12:
case 13:
case 14:
case 15:
case 16:
{
QString text("兵");
paint->drawText((max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check, Qt::AlignCenter, text);
break;
}
case 17:
{
QString text("将");
paint->drawText((max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check, Qt::AlignCenter, text);
break;
}
case 18:
case 19:
{
QString text("士");
paint->drawText((max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check, Qt::AlignCenter, text);
break;
}
case 20:
case 21:
{
QString text("象");
paint->drawText((max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check, Qt::AlignCenter, text);
break;
}
case 22:
case 23:
{
QString text("马");
paint->drawText((max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check, Qt::AlignCenter, text);
break;
}
case 24:
case 25:
{
QString text("车");
paint->drawText((max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check, Qt::AlignCenter, text);
break;
}
case 26:
case 27:
{
QString text("炮");
paint->drawText((max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check, Qt::AlignCenter, text);
break;
}
case 28:
case 29:
case 30:
case 31:
case 32:
{
QString text("卒");
paint->drawText((max-NUMX*check)/2+j*check-check/2, check/2+i*check, check, check, Qt::AlignCenter, text);
break;
}
}
}
}
//放到flag里比较好
if( key != 0 )//根据key记录的数字画绿框
{
for(int i = 0; i < NUMY+1; i++)
for(int j = 0; j <NUMX+1; j++)
{
if(g[i][j] == key)
{
//qWarning("%d %d",i,j);
paint->setPen(Qt::green);
paint->drawRect( (max-((NUMX+1)*check))/2+j*check, check/2+i*check, check, check);
}
}
}
}
if( flag == 1 )
{
for(int i = 0; i < NUMY+1; i++)
for(int j = 0; j < NUMX+1; j++)
{
if( g[i][j]/17 == 0)
{
paint->setPen(Qt::red);
}
if( g[i][j]/17 == 1)
{
paint->setPen(Qt::blue);
}
if( g[i][j] != 0)
{
paint->drawEllipse( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check);
switch( g[i][j] )
{
case 1:
{
QString text("帅");
paint->drawText( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check, Qt::AlignCenter, text);
break;
}
case 2:
case 3:
{
QString text("仕");
paint->drawText( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check, Qt::AlignCenter, text);
break;
}
case 4:
case 5:
{
QString text("相");
paint->drawText( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check, Qt::AlignCenter, text);
break;
}
case 6:
case 7:
{
QString text("马");
paint->drawText( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check, Qt::AlignCenter, text);
break;
}
case 8:
case 9:
{
QString text("车");
paint->drawText( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check, Qt::AlignCenter, text);
break;
}
case 10:
case 11:
{
QString text("炮");
paint->drawText( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check, Qt::AlignCenter, text);
break;
}
case 12:
case 13:
case 14:
case 15:
case 16:
{
QString text("兵");
paint->drawText( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check, Qt::AlignCenter, text);
break;
}
case 17:
{
QString text("将");
paint->drawText( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check, Qt::AlignCenter, text);
break;
}
case 18:
case 19:
{
QString text("士");
paint->drawText( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check, Qt::AlignCenter, text);
break;
}
case 20:
case 21:
{
QString text("象");
paint->drawText( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check, Qt::AlignCenter, text);
break;
}
case 22:
case 23:
{
QString text("马");
paint->drawText( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check, Qt::AlignCenter, text);
break;
}
case 24:
case 25:
{
QString text("车");
paint->drawText( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check, Qt::AlignCenter, text);
break;
}
case 26:
case 27:
{
QString text("炮");
paint->drawText( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check, Qt::AlignCenter, text);
break;
}
case 28:
case 29:
case 30:
case 31:
case 32:
{
QString text("卒");
paint->drawText( check/2+j*check, (max-NUMX*check)/2+i*check-check, check, check, Qt::AlignCenter, text);
break;
}
}
}
}
if(key != 0)
{
for(int i=0; i < NUMY+1; i++)
for(int j=0; j<NUMX+1; j++)
{
if(g[i][j] == key)
{
//qWarning("%d %d",i,j);
paint->setPen(Qt::green);
paint->drawRect( check/2+j*check, (max-((NUMY+1)*check))/2+i*check, check, check);
}
}
}
}
/*
/*
//显示一下都记录了什么
{
qWarning("shuchu 1");
for(int i = 0; i < NUMY+1; i++)
for(int j = 0; j < NUMX+1; j++)
{
if(f[i][j] != NULL)
{
qWarning("f %d %d",i,j);
for(chess * p = f[i][j]; p != NULL; p = p->next)
{
qWarning("shuchu 2");
qWarning(" %d ",p->name);
}
}
}
}
*/
//如果能吃红方老将
if(key == 17)
{
qWarning("lu 1");
int a = -1, b = -1, c = -1, d = -1, e = -1;
c = my[1].myi;//红方老将的位置
d = my[1].myj;
if((c != -1)&&(d != -1))
{
for(chess * p = f[c][d]; p != NULL; p = p->next)
{
if((p->name >= 17)&&(p->name <=32))
{
e = p->name;
break;
}
}
}
if(e != -1)
{
a = my[e].myi;
b = my[e].myj;
if((a != -1)&&(b != -1))
{
g[a][b] = 0;
g[c][d] = e;
//key = 1;
v = 1;
update();
}
}
}
//蓝方老将受到威胁,棋权在蓝方.杀掉对方,解除危险
if((v == 0)&&(key == 17))
{
qWarning("lu 2");
int a = my[17].myi;//蓝方老将位置
int b = my[17].myj;
if((f[a][b] != NULL)&&(f[a][b]->name < 17)&&(((f[a][b]->next)== NULL)||(((f[a][b]->next)->name) >= 17)))//只有一个红棋威胁蓝方老将
{
int c = my[f[a][b]->name].myi;//唯一威胁蓝方老将的红棋
int d = my[f[a][b]->name].myj;
if((f[c][d] != NULL)&&((f[c][d]->name)>17))//队首的值是蓝棋,没有红棋保护。
{
int i = my[f[c][d]->name].myi;
int j = my[f[c][d]->name].myj;
g[c][d] =g[i][j];
g[i][j] = 0;
key = 1;
update();
}
if((f[c][d] != NULL)&&((f[c][d]->name)==17))//老将不对头
{
int t=1;
int x = my[1].myi;
int y = my[1].myj;
if(y==d)
{
t=0;
for(int i=c+1; i<x; i++)
{
if(g[i][y] != 0)
{
t=1;
}
}
}
if(t==1)
{
g[c][d] =g[a][b];
g[a][b] = 0;
key = 1;
update();
}
}
}
}
//尽量不要用空头炮
if((v == 0)&&(key == 17))
{
qWarning("lu 3");
{
int a = my[17].myi;//蓝棋老将的位置
int b = my[17].myj;
if(g[a][b] == 17)
{
if((f[a][b] != NULL)&&((f[a][b] ->name == 10)||(f[a][b] ->name == 11))&&(f[a][b] ->next == NULL))//如果威胁蓝方老将的只有红炮
{
int pao = f[a][b]->name;
int c = my[pao].myi;//红炮的位置
int d = my[pao].myj;
if(d==4)
{
if((g[1][4]==18)||(g[1][4]==19))//如果上过士
{
if((g[0][2]==20)&&(g[2][4]==0))//上左相
{
g[0][2]=0;
g[2][4]=20;
update();
}
if((g[0][6]==21)&&(g[2][4]==0))//上右相
{
g[0][6]=0;
g[2][4]=21;
update();
}
}
if((g[2][4]==20)||(g[2][4]==21))//如果上过相
{
if((g[0][3]==18)&&(g[1][4]==0))//上左士
{
g[0][3]=0;
g[1][4]=18;
update();
}
if((g[0][5]==19)&&(g[1][4]==0))//上右士
{
g[0][5]=0;
g[1][4]=19;
update();
}
}
}
}
}
}
}
//蓝方老将受到威胁,棋权在蓝方.解棋
//保护老将的棋子过分小心总是怕被别人吃
//在这步确实形成空头炮了
if((v == 0)&&(key == 17))
{
qWarning("lu 4");
int li = my[17].myi;
int lj = my[17].myj;
if((f[li][lj] != NULL)&&(f[li][lj]->name < 17))//蓝方老将受到威胁
{
//动动子如果老将不受威胁
int m = -1, n = -1, s = -1, t = -1, w = -1;
for(int h = 18; h<=32; h++)
{
int a = my[h].myi;//蓝棋h的位置
int b = my[h].myj;
if(h == g[a][b])
{
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
taopao(0,h);//h这个蓝棋可以走到哪
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
if(tao[i][j] == h)
{
int c = my[h].myi;//h的原位置
int d = my[h].myj;
//qWarning("c = %d d = %d",c,d);//c = 2 d = 7
//qWarning("i = %d j = %d",i,j);//i = 2 j =6
//int c =2,d =7,i =2,j =6;
g[c][d] = 0;
g[i][j] = h;
pengpeng();
//如果老将的威胁解除
if(((f[li][lj] != NULL)&&(f[li][lj]->name >= 18))||(f[li][lj] == NULL))
{
m = c;
n = d;
s = i;
t = j;
w = h;
key = 1;
break;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
}
if(key == 1)
{
break;
}
}
if(key == 1)
{
break;
}
}
}
if(key == 1)
{
break;
}
}
if(key == 1)
{
g[m][n] = 0;
g[s][t] = w;
pengpeng();
update();//退出循序后update
}
}
}
//蓝方老将受到威胁,棋权在蓝方.吃子解棋
if((v == 0)&&(key == 17))
{
qWarning("lu 5");
int li = my[17].myi;
int lj = my[17].myj;
if((f[li][lj] != NULL)&&(f[li][lj]->name < 17))//蓝方老将受到威胁
{
int m = -1, n = -1, s = -1, t = -1, w = -1;
for(int h = 18; h<=32; h++)
{
int a = my[h].myi;//蓝棋h的位置
int b = my[h].myj;
if(h == g[a][b])
{
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
chizi(h);
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
if((chi[i][j] == h)&&(g[i][j] <= 16)&&g[i][j] >= 1)
{
int c = my[h].myi;//h的原位置
int d = my[h].myj;
//qWarning("c = %d d = %d",c,d);//c = 2 d = 7
//qWarning("i = %d j = %d",i,j);//i = 2 j =6
//int c =2,d =7,i =2,j =6;
g[c][d] = 0;
g[i][j] = h;
pengpeng();
//如果老将的威胁解除
if(((f[li][lj] != NULL)&&(f[li][lj]->name >= 18))||(f[li][lj] == NULL))
{
m = c;
n = d;
s = i;
t = j;
w = h;
key = 1;
break;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
}
if(key == 1)
{
break;
}
}
if(key == 1)
{
break;
}
}
}
if(key == 1)
{
break;
}
}
if(key == 1)
{
g[m][n] = 0;
g[s][t] = w;
pengpeng();
update();//退出循序后update
}
}
}
//蓝方老将受到威胁,棋权在蓝方.逃跑.
if((v == 0)&&(key == 17))
{
qWarning("lu 6");
int n = 0;
int i = my[17].myi;
int j = my[17].myj;
if((f[i][j] != NULL)&&(f[i][j]->name < 17))//蓝方老将受到威胁
{
//向左移动
if(((j-1) >= 3)&&((g[i][j-1] == 0)||((g[i][j-1] >= 1)&&(g[i][j-1] <= 16))))
{
if((f[i][j-1] != NULL)&&(!(f[i][j-1]->name < 17)))//左边没有受到红棋的威胁
{
for(chess * p = f[i][j]; p != NULL; p = p->next)
{
if(((p->name >=8)&&(p->name <=11)))//红炮红车在蓝方老将的上下位置
{
if(j == y_2)
{
g[i][j-1] = 17;
g[i][j] = 0;
key = 1;
n = 1;
update();
break;
}
}
else if(p->name <17)
{
g[i][j-1] = 17;
g[i][j] = 0;
key = 1;
n = 1;
update();
break;
}
}
}
}
//向右移动
if(((j+1) <= 5)&&((g[i][j+1] == 0)||((g[i][j+1] >= 1)&&(g[i][j+1] <= 16)))&&(n == 0))
{
if((f[i][j+1] != NULL)&&(!(f[i][j+1]->name < 17)))//右边没有受到红棋的威胁
{
{
for(chess * p = f[i][j]; p != NULL; p = p->next)
{
if(((p->name >=8)&&(p->name <=11)))//红炮红车在蓝方老将的上下位置
{
if(j == y_2)
{
g[i][j+1] = 17;
g[i][j] = 0;
key = 1;
n = 1;
update();
break;
}
}
else if(p->name <17)
{
g[i][j+1] = 17;
g[i][j] = 0;
key = 1;
n = 1;
update();
break;
}
}
}
}
}
//向上移动
if(((i+1) <= 2)&&((g[i+1][j] == 0)||((g[i+1][j] >= 1)&&(g[i+1][j] <= 16)))&&(n == 0))
{
if((f[i+1][j] != NULL)&&(!(f[i+1][j]->name < 17)))//下边没有受到红棋的威胁
{
for(chess * p = f[i][j]; p != NULL; p = p->next)
{
if(((p->name >=8)&&(p->name <=11)))//红炮红车在蓝方老将的左右位置
{
if((i == x_2))
{
g[i+1][j] = 17;
g[i][j] = 0;
key = 1;
n = 1;
update();
break;
}
}
else if(p->name <17)
{
g[i+1][j] = 17;
g[i][j] = 0;
key = 1;
n = 1;
update();
break;
}
}
}
}
//向下移动
if(((i-1) >= 0)&&((g[i-1][j] == 0)||((g[i-1][j] >= 1)&&(g[i-1][j] <= 16)))&&(n == 0))
{
if((f[i-1][j] != NULL)&&(!(f[i-1][j]->name < 17)))//下边没有受到红棋的威胁
{
for(chess * p = f[i][j]; p != NULL; p = p->next)
{
if(((p->name >=8)&&(p->name <=11)))//红炮红车在蓝方老将的左右位置
{
if((i == x_2))
{
g[i-1][j] = 17;
g[i][j] = 0;
key = 1;
n = 1;
update();
break;
}
}
else if(p->name <17)
{
g[i-1][j] = 17;
g[i][j] = 0;
key = 1;
n = 1;
update();
break;
}
}
}
}
}
}
//如果上面都没有保住老将写一个只有能走被不被吃无所谓的
if((v == 0)&&(key == 17))
{
qWarning("lu 7");
int li = my[17].myi;
int lj = my[17].myj;
if((f[li][lj] != NULL)&&(f[li][lj]->name < 17))//蓝方老将受到威胁
{
//动动子如果老将不受威胁
int m = -1, n = -1, s = -1, t = -1, w = -1;
for(int h = 18; h<=32; h++)
{
qWarning("h = %d",h);
int a = my[h].myi;//蓝棋h的位置
int b = my[h].myj;
if(h == g[a][b])
{
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
taopao(0,h);//h这个蓝棋可以走到哪
qWarning("tao[3][4] = %d",tao[3][4]);
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
if(tao[i][j] == -1)//能往这走被不被吃无所谓
{
int c = my[h].myi;//h的原位置
int d = my[h].myj;
//qWarning("c = %d d = %d",c,d);//c = 2 d = 7
//qWarning("i = %d j = %d",i,j);//i = 2 j =6
//int c =2,d =7,i =2,j =6;
g[c][d] = 0;
g[i][j] = h;
pengpeng();
//如果老将的威胁解除
if(((f[li][lj] != NULL)&&(f[li][lj]->name >= 18))||(f[li][lj] == NULL))
{
m = c;
n = d;
s = i;
t = j;
w = h;
key = 1;
break;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
}
if(key == 1)
{
break;
}
}
if(key == 1)
{
break;
}
}
}
if(key == 1)
{
break;
}
}
if(key == 1)
{
g[m][n] = 0;
g[s][t] = w;
pengpeng();
update();//退出循序后update
}
}
}
//如果能将军,没有使自己的老将受到威胁,将军吧,只考虑taopao()
//很必要
if((v == 0)&&(key == 17))
{
qWarning("lu 8");
for(int h = 18; h<=32; h++)
{
int a = my[h].myi;//蓝棋h的位置
int b = my[h].myj;
//qWarning("h = %d a = %d b = %d",h,a,b);
if(h == g[a][b])
{
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
taopao(0,h);
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
if(tao[i][j] == g[a][b])
{
g[i][j] = g[a][b];
g[a][b] = 0;
pengpeng();
int bi = my[17].myi;//蓝方老将的位置
int bj = my[17].myj;
int ri = my[1].myi;//红方老将的位置
int rj = my[1].myj;
if((f[bi][bj] == NULL)||((f[bi][bj] != NULL)&&(f[bi][bj]->name >= 18)&&(f[bi][bj]->name <= 32)))//蓝方老将没有受威胁
{
for(chess * p = f[ri][rj]; p != NULL; p = p->next)
{
if(p->name == h)
{
key = 1;
update();
break;
}
}
}
if(key == 1)
{
break;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
}
}
if(key == 1)
{
break;
}
}
if(key == 1)
{
break;
}
}
}
}
//抽将,如果能抽个大子,使自己受到威胁也无所谓
//测试一下
if((v == 0)&&(key == 17))
{
qWarning("lu 9");
int si=-1, i_si=-1, j_si=-1, h_si=-1, a_si=-1, b_si=-1;
for(int h = 18; h<=32; h++)
{
int a = my[h].myi;//蓝棋h的位置
int b = my[h].myj;
//qWarning("hello 0");
//if(h==24)
//qWarning("h = %d a = %d b = %d",h,a,b);
if(h == g[a][b])
{
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
taopao(0,h);
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
if(((tao[i][j] == -1)||(tao[i][j] == h))&&(g[i][j] == 0))//受红方控制空位
{
//qWarning("hello 1");
g[i][j] = h;
g[a][b] = 0;
if(h==24)
//qWarning("i = %d j = %d ",i,j);
//qWarning("a = %d b = %d g[a][b] = %d",a,b,g[a][b]);
pengpeng();
int bi = my[17].myi;//蓝方老将的位置
int bj = my[17].myj;
int ri = my[1].myi;//红方老将的位置
int rj = my[1].myj;
//qWarning("f[bi][bj] == NULL %d",(f[bi][bj] == NULL));
//qWarning("hello 2");
if((f[bi][bj] == NULL)||((f[bi][bj] != NULL)&&(f[bi][bj]->name >= 18)&&(f[bi][bj]->name <= 32)))//蓝方老将没有受威胁
{
//qWarning("biaozi");
if((f[ri][rj]!=NULL)&&(f[ri][rj]->tail>17))//红方老将被蓝棋威胁
{
//qWarning("p->name == %d",p->name);
{
//qWarning("hello 3");
//qWarning("p->name == h %d",p->name == h);
for(int l = 2; l<=16; l++)
{
//qWarning("l = %d",l);
int ci = my[l].myi;
int cj = my[l].myj;
//qWarning("priority(l) = %d",priority(l));
//qWarning("priority(h) = %d",priority(h));
//if(h==24)
//{
//qWarning("g[ci][cj]==l %d",g[ci][cj]==l);
//qWarning("f[ci][cj] != NULL %d",f[ci][cj] != NULL);
//if(((g[ci][cj]==l)&&(f[ci][cj] != NULL))&&((h==24)||(h==25)))
//{
//qWarning("f[ci][cj]->red ==0 %d",f[ci][cj]->red ==0);
//qWarning("f[ci][cj]->blue %d",f[ci][cj]->blue );
//qWarning("f[ci][cj]->name %d",f[ci][cj]->name );
//}
//}
if(((g[ci][cj]==l)&&(f[ci][cj] != NULL)&&(f[ci][cj]->red ==0)&&(f[ci][cj]->blue >= 1))&&(f[ci][cj]->name == h))
{
//qWarning("hello 4");
//qWarning("GAME OVER");
if(priority(l)>si)
{
si=priority(l);
i_si=i;
j_si=j;
a_si=a;
b_si=b;
h_si=h;
}
}
}
}
}
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
}
}
}
if(si!=-1)
{
//qWarning("hello 5");
g[i_si][j_si] = h_si;
g[a_si][b_si] = 0;
key = 1;
update();
break;
}
}
}
}
//吃子将军
//没有保护的红子不吃白不吃不能让老将受威胁
if((v == 0)&&(key == 17))
{
qWarning("lu 10");
int t = 0;
int m = -1, n = -1;
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
if((g[i][j] >= 2)&&(g[i][j] <= 16)&&(f[i][j] != NULL)&&(f[i][j]->name >= 17))
{
if(priority(g[i][j])>t)
{
m = i;
n = j;
t = priority(g[i][j]);
}
}
}
}
if((m!=-1)&&(n!=-1))
{
int a = my[f[m][n]->name].myi;
int b = my[f[m][n]->name].myj;
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
g[m][n] = g[a][b];
g[a][b] = 0;
pengpeng();
int li = my[17].myi;
int lj = my[17].myj;
if(g[li][lj]==17)
{
if((f[li][lj]==NULL)||((f[li][lj]!=NULL)&&(f[li][lj]->name >= 17)))
{
key = 1;
update();
}
if((f[li][lj]!=NULL)&&(f[li][lj]->name < 17))
{
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
}
}
}
}
//我方(蓝方)能用小子吃大子,吃吧
if((v == 0)&&(key == 17))//不要用重复的变量
{
qWarning("lu 11");
for(int l = 2; l<=16; l++)
{
//qWarning("l %d",l);
int a = my[l].myi;//红棋的位置
int b = my[l].myj;
if(l ==g[a][b])//防止l刚被吃掉,出现错误.
{
//qWarning("a %d b %d g[a][b] %d",a,b,g[a][b]);
int h= priority(g[a][b]);//红棋本身的优先级
//如果蓝子的优先级小于等于红棋吃子
{
for(chess *p = f[a][b]; p != NULL; p= p->next)
{
if((p->name <=32)&&(p->name >=17)&&(h > priority(p->name)))
{
//qWarning("a %d b %d g[a][b] %d",a,b,g[a][b]);
int i = my[p->name].myi;//蓝子的位置
int j = my[p->name].myj;
//qWarning("i %d j %d g[i][j] %d",i,j,g[i][j]);
g[i][j] = 0;
g[a][b] = p->name;
key = 1;
update();
break;
}
}
}
if(key == 1)
{
break;
}
}
}
}
//没有保护的蓝子被威胁, 这个子寻找吃子的机会
if((v == 0)&&(key == 17))
{
qWarning("lu 12");
int m = -1, n = -1, s = -1, t = -1, w = -1;
for(int h = 18; h<=32; h++)
{
int a = my[h].myi;//蓝棋的位置
int b = my[h].myj;
//qWarning("i = %d a = %d b = %d",i,a,b);
if(h == g[a][b])
{
if(f[a][b] != NULL)
{
int e = 0;
for(chess *p = f[a][b]; p != NULL; p= p->next)
{
if((p->next == NULL)&&(p->name <= 16))
{
e = 1;//只被红棋威胁
}
}
if(e == 1)
{
//qWarning("a %d b %d g[a][b] %d",a,b,g[a][b]);
//qWarning("f[a][b]->name %d i %d",f[a][b]->name,i);
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
taopao(f[a][b]->name,h);
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
if(tao[i][j] == h)
{
int c = my[h].myi;//h的原位置
int d = my[h].myj;
//qWarning("h = %d c = %d d = %d",h,c,d);//c = 2 d = 7
//qWarning("i = %d j = %d",i,j);//i = 2 j =6
//int c =2,d =7,i =2,j =6;
g[c][d] = 0;
g[i][j] = h;
pengpeng();
//qWarning("la[27][15] = %d",la[27][15]);
for(int k=0; k < N+1; k++)
{
//qWarning("tian 4");
//qWarning("h %d k %d la[h][k] %d",h,k,la[h][k]);
if((la[h][k] == 1)&&(k >= 1)&&(k <= 16))//再测试一下这个红子是不是没有保护
{
int ri = my[k].myi;
int rj = my[k].myj;
if((f[ri][rj] != NULL)&&(f[ri][rj]->name >= 18)&&(f[ri][rj]->name <=32))
{
//qWarning("f[ri][rj]->name %d",f[ri][rj]->name);
//在这考虑一下蓝方老将是否受到威胁
int wi = my[17].myi;//蓝方老将的位置
int wj = my[17].myj;
//qWarning("wi = %d wj = %d",wi,wj);
if(((f[wi][wj] != NULL)&&(f[wi][wj]->name >= 18)&&(f[wi][wj]->name <= 32))||(f[wi][wj] == NULL))//老将没有被威胁
{
m = c;
n = d;
s = i;
t = j;
w = h;
key = 1;
//qWarning("h %d k %d la[h][k] %d",h,k,la[h][k]);
break;
}
}
}
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
if(key == 1)
{
break;
}
}
}
if(key == 1)
{
break;
}
}
}
}
}
if(key == 1)
{
break;
}
}
if(key == 1)
{
g[m][n] = 0;
g[s][t] = w;
pengpeng();
update();//退出循序后update
}
}
//对方(红方)想用小子吃大子,逃跑
if((v == 0)&&(key == 17))
{
qWarning("lu 13");
for(int i = 18; i <= 32; i++)
{
int a = my[i].myi;//蓝棋的位置
int b = my[i].myj;
int j= priority(g[a][b]);//蓝棋本身的优先级
//如果红子的优先级小于等于蓝棋的优先级,蓝棋逃跑
{
for(chess *p = f[a][b]; p != NULL; p= p->next)
{
if((p->name <=16)&&(p->name >=2)&&(j > priority(p->name)))//过分小心
{
//逃跑
//qWarning("f[a][b]->name %d i %d",f[a][b]->name,i);
taopao(p->name,g[a][b]);
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
//如果能在tao中随机选一个数就好了
if(tao[i][j] == g[a][b])
{
g[i][j] = g[a][b];
g[a][b] = 0;
key = 1;
update();
break;
//用蓝框框住可以逃跑的地方
//qWarning("tao[i][j] == %d",g[a][b]);
//if(flag == 0)
//{
// paint->setPen(Qt::blue);
// paint->drawRect( (max-((NUMX+1)*check))/2+j*check, check/2+i*check, check, check);
//}
//if(flag == 1)
//{
// paint->setPen(Qt::blue);
// paint->drawRect( check/2+j*check, (max-((NUMY+1)*check))/2+i*check, check, check);
//}
}
}
if(key == 1)
{
break;
}
}
}
if(key == 1)
{
break;
}
}
}
}
}
//没有保护的蓝子被威胁,寻求保护
//寻求保护时老将不能被吃
if((v == 0)&&(key == 17))
{
qWarning("lu 14");
for(int i = 18; i<=32; i++)//遍历所有的蓝棋
{
int a = my[i].myi;//蓝棋的位置
int b = my[i].myj;
//qWarning("i = %d a = %d b = %d",i,a,b);
if(i == g[a][b])
{
if(f[a][b] != NULL)
{
int e = 0;
for(chess *p = f[a][b]; p != NULL; p= p->next)
{
if((p->next == NULL)&&(p->name <= 16))
{
e = 1;//某个蓝棋i a b只被红棋威胁
}
}
if(e == 1)
{
//qWarning("a %d b %d g[a][b] %d",a,b,g[a][b]);
//qWarning("f[a][b]->name %d i %d",f[a][b]->name,i);
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
taopao(f[a][b]->name,i);//某个蓝棋i a b只被红棋威胁
//当头炮,把马跳
if(key == 17)
{
//有问题
//qWarning("pengpeng 1");
//qWarning("g %d %d %d",a,b,g[a][b]);
//除了自己的棋子在蓝棋中寻找
for(int h = 18; h <= 32; h++)//遍历所有的蓝棋h,看看谁能救蓝棋i
{
qWarning("h = %d",h);
int csi = my[h].myi;
int csj = my[h].myj;
//qWarning("h %d",h);
//qWarning("csi = %d csj = %d",csi,csj);
if((h != g[a][b])&&(csi != -1)&&(csj != -1))//
{
//qWarning("pengpeng 2");
//把g[][]复制下来,带入pengpeng函数
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
taopao(0,h);//h这个蓝棋可以走到哪?
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
if(tao[i][j] == h)
{
//qWarning("pengpeng 3");
int c = my[h].myi;//h的原位置
int d = my[h].myj;
//不走卧槽马
if((i == 1)&&(j == 4)&&((h == 22)||(h == 23)))
{
continue;
}
//不原地悔棋
if((sbu[h]!=NULL)&&(sbu[h]->huii==i)&&(sbu[h]->huij==j))
{
if((h!=20)&&(h!=21))
{
continue;
}
}
//相不上飞
if(((h==20)||(h==21))&&(c==2)&&(d==4)&&(((i==4)&&(j==2))||((i==4)&&(j==6))))
{
qWarning("helloxiang");
qWarning("i %d j %d",i,j);
continue;
}
g[c][d] = 0;
g[i][j] = h;
pengpeng();
int bs=0;
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
for(chess *p = f[i][j]; p != NULL; p= p->next)//如果有蓝棋没有保护
{
if((g[i][j]>17)&&(g[i][j]<=32)&&(p->tail<17))
{
bs=1;
break;
}
}
if(bs==1)
{
break;
}
}
if(bs==1)
{
break;
}
}
if(bs==1)
{
//qWarning("bs");
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
continue;
}
for(chess *p = f[a][b]; p != NULL; p= p->next)//测试一下是否被保护
{
if(p->next == NULL)
{
//qWarning("pengpeng 4");
//qWarning("p->name %d",p->name);
if(p->name > 16)//有蓝棋保护
{
//qWarning("pengpeng 5");
int wi = my[17].myi;//蓝方老将的位置
int wj = my[17].myj;
//qWarning("wi = %d wj = %d",wi,wj);
if(((f[wi][wj] != NULL)&&(f[wi][wj]->name >= 18)&&(f[wi][wj]->name <= 32))||(f[wi][wj] == NULL))//老将没有被威胁
{
//qWarning("nihao");
key = 1;
update();
break;
}
}
}
}
if(key == 1)
{
break;
}
//qWarning("pengpeng 6");
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
}
}
if(key == 1)
{
break;
}
}
}
if(key == 1)
{
break;
}
}
}
if(key == 1)
{
break;
}
}
}
}
}
}
//没有保护的蓝子被威胁,寻求保护,可以顶着上
if((v == 0)&&(key == 17))
{
qWarning("lu 15");
for(int h = 18; h<=32; h++)//遍历所有的蓝棋
{
int a = my[h].myi;//蓝棋h的位置
int b = my[h].myj;
if(h == g[a][b])
{
//qWarning("h %d",h);
if((f[a][b]!=NULL)&&(f[a][b]->tail<17)&&(f[a][b]->red==1))//如果蓝棋h只被一个红棋威胁
{
//qWarning("h %d",h);
for(int k = 18; k<=32; k++)//遍历所有的蓝棋
{
int c = my[k].myi;//蓝棋k的位置
int d = my[k].myj;
if(k == g[c][d])
{
//qWarning("k %d",k);
guize(k);
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
if((gui[i][j]==k)&&(g[i][j]<17))
{
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
g[c][d]=0;
g[i][j]=k;
//if(k==29)
//qWarning("i %d j %d k %d",i ,j ,k);
pengpeng();
if((f[a][b]!=NULL)&&(f[a][b]->tail>17)&&(k!=h))//如果有蓝棋保护h了并且不是自己保护自己
{
//qWarning("baohu");
//qWarning("k %d",k);
int kg = 0;
for(int l = 18; l<=32; l++)//遍历一下蓝棋l
{
int p = my[l].myi;//蓝棋l的位置
int q = my[l].myj;
if(l == g[p][q])
{
//qWarning("l %d",l);
if((f[p][q]!=NULL)&&(f[p][q]->tail<17))//如果蓝棋l只被红棋威胁
{
kg = 1;
//qWarning("kg %d",kg);
break;
}
}
}
if(kg==0)
{
int m=my[17].myi;
int n=my[17].myj;
if((g[m][n]==17)&&((f[m][n]==NULL)||((f[m][n]!=NULL))&&(f[m][n]->red==0)))
{
key=1;
//qWarning("key %d",key);
update();
break;
}
}
}
if(k==h)//如果动的是蓝棋h
{
//qWarning("hello");
//qWarning("k %d",k);
if((f[i][j]!=NULL)&&(f[i][j]->tail>17))//如果有蓝棋保护h了
{
//qWarning("bing");
int kg = 0;
for(int l = 18; l<=32; l++)//遍历一下蓝棋l
{
int p = my[l].myi;//蓝棋l的位置
int q = my[l].myj;
if(l == g[p][q])
{
if((f[p][q]!=NULL)&&(f[p][q]->tail<17))//如果蓝棋l只被红棋威胁
{
kg = 1;
//qWarning("kg %d",kg);
break;
}
}
}
if(kg==0)
{
int m=my[17].myi;
int n=my[17].myj;
if((g[m][n]==17)&&((f[m][n]==NULL)||((f[m][n]!=NULL))&&(f[m][n]->red==0)))
{
key=1;
//qWarning("key %d",key);
update();
break;
}
}
}
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
}
}
if(key==1)
{
break;
}
}
}
if(key==1)
{
break;
}
}
}
}
if(key==1)
{
break;
}
}
}
//没有保护的蓝子被威胁,逃跑.
//逃跑时老将不能被吃
if((v == 0)&&(key == 17))
{
qWarning("lu 16");
for(int i = 18; i<=32; i++)
{
int a = my[i].myi;//蓝棋的位置
int b = my[i].myj;
//qWarning("i = %d a = %d b = %d",i,a,b);
if(i == g[a][b])
{
if(f[a][b] != NULL)
{
int e = 0;
for(chess *p = f[a][b]; p != NULL; p= p->next)
{
if((p->next == NULL)&&(p->name <= 16))
{
e = 1;//只被红棋威胁
}
}
if(e == 1)
{
//qWarning("a %d b %d g[a][b] %d",a,b,g[a][b]);
//qWarning("f[a][b]->name %d i %d",f[a][b]->name,i);
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
taopao(f[a][b]->name,i);
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
//如果能在tao中随机选一个数就好了
if(tao[i][j] == g[a][b])
{
g[i][j] = g[a][b];
g[a][b] = 0;
pengpeng();
int wi = my[17].myi;//蓝方老将的位置
int wj = my[17].myj;
//qWarning("wi = %d wj = %d",wi,wj);
if(((f[wi][wj] != NULL)&&(f[wi][wj]->name >= 18)&&(f[wi][wj]->name <= 32))||(f[wi][wj] == NULL))//老将没有被威胁
{
key = 1;
update();
break;
}
//用蓝框框住可以逃跑的地方
//qWarning("tao[i][j] == %d",g[a][b]);
//if(flag == 0)
//{
// paint->setPen(Qt::blue);
// paint->drawRect( (max-((NUMX+1)*check))/2+j*check, check/2+i*check, check, check);
//}
//if(flag == 1)
//{
// paint->setPen(Qt::blue);
// paint->drawRect( check/2+j*check, (max-((NUMY+1)*check))/2+i*check, check, check);
//}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
}
}
if(key == 1)
{
break;
}
}
if(key == 1)
{
break;
}
}
}
}
}
}
//三步不出车,臭棋。
if((v == 0)&&(key == 17))
{
qWarning("lu 17");
if(bushu < 15 )
{
if((l == 0)&&(g[0][0]==24)&&(key == 17))//左边的车没动
{
if(g[0][1]==0)
{
//taopao(,)用逃跑他我不合适
//qWarning("hello1");
g[0][0] = 0;
g[0][1] = 24;
pengpeng();
if((f[0][1]==NULL)||(f[0][1]->name >= 17))
{
l = 1;
key = 1;
update();
}
else
{
g[0][0] = 24;
g[0][1] = 0;
pengpeng();
}
}
if(g[0][1]==22)//跳马
{
//qWarning("hello2");
taopao(0,22);
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
if(tao[i][j]==22)
{
g[0][1]=0;
g[i][j]=22;
pengpeng();
key = 1;
update();
break;
}
}
}
}
if((r == 0)&&(g[0][8]==25)&&(key == 17))//右边的车没动
{
if(g[0][7] == 0)
{
//qWarning("hello3");
g[0][8] = 0;
g[0][7] = 25;
pengpeng();
if((f[0][7]==NULL)||(f[0][7]->name >= 17))
{
key = 1;
r = 1;
update();
}
else
{
g[0][8] = 25;
g[0][7] = 0;
pengpeng();
}
}
if(g[0][7]==23)
{
//qWarning("hello4");
taopao(0,23);
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
if(tao[i][j]==23)
{
g[0][7]=0;
g[i][j]=23;
pengpeng();
key = 1;
update();
break;
}
}
}
}
}
//for(int i=0; i < NUMY+1; i++)
//for(int j=0; j < NUMX+1; j++)
//{
//if(g[i][j]!=0)
//qWarning("i = %d j = %d g = %d",i,j,g[i][j]);
//}
}
//蓝棋没事找红子吃
//不能让其他子陷入危险,如果有棋子没有被保护(和上面结合)
if((v == 0)&&(key == 17))
{
//int count=0;
qWarning("lu 18");
//qWarning("hi 17");
//qWarning("count = %d",count);
//for(int i=0; i < NUMY+1; i++)
//for(int j=0; j < NUMX+1; j++)
//{
//if(g[i][j]!=0)
//qWarning("i = %d j = %d g = %d",i,j,g[i][j]);
//}
int m = -1, n = -1, s = -1, t = -1, w = -1;
//int h = 27;
for(int h = 18; h<=32; h++)
{
int a = my[h].myi;//蓝棋h的位置
int b = my[h].myj;
qWarning("h = %d a = %d b = %d",h,a,b);
//qWarning("h %d",h);
if(h == g[a][b])
{
//qWarning("tian 1");
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
taopao(0,h);//h这个蓝棋可以走到哪
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
//qWarning("i %d",i);
//qWarning("j %d",j);
if(tao[i][j] == h)
{
//qWarning("tian 2");
int c = my[h].myi;//h的原位置
int d = my[h].myj;
//qWarning("h = %d c = %d d = %d",h,c,d);//c = 2 d = 7
//qWarning("i = %d j = %d",i,j);//i = 2 j =6
//int c =2,d =7,i =2,j =6;
g[c][d] = 0;
g[i][j] = h;
pengpeng();
//qWarning("la[27][15] = %d",la[27][15]);
for(int k=0; k < N+1; k++)
{
//qWarning("tian 3");
//qWarning("h %d k %d la[h][k] %d",h,k,la[h][k]);
if((la[h][k] == 1)&&(k >= 1)&&(k <= 16)&&(my[k].myi != -1)&&(my[k].myj != -1))//再测试一下这个红子是不是没有保护
{
int ri = my[k].myi;
int rj = my[k].myj;
if((f[ri][rj] != NULL)&&(f[ri][rj]->name >= 18)&&(f[ri][rj]->name <=32))
{
//qWarning("f[ri][rj]->name %d",f[ri][rj]->name);
//在这考虑一下蓝方老将是否受到威胁
int wi = my[17].myi;//蓝方老将的位置
int wj = my[17].myj;
//qWarning("wi = %d wj = %d",wi,wj);
if(((f[wi][wj] != NULL)&&(f[wi][wj]->name >= 18)&&(f[wi][wj]->name <= 32))||(f[wi][wj] == NULL))//老将没有被威胁
{
//count++;
//qWarning("count = %d",count);
//qWarning("tian 4");
int tian = 0;
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
//qWarning("tian 5");
//qWarning("i %d j %d",i,j);
if((f[i][j]!=NULL)&&((f[i][j]->name >= 18)&&(f[i][j]->name <= 32))&&(f[i][j]->tail < 17))//蓝棋都安全
{
tian = 1;
break;
}
}
if(tian==1)
{
break;
}
}
//qWarning("tian 6");
if(tian == 0)
{
//qWarning("tian 7");
m = c;
n = d;
s = i;
t = j;
w = h;
key = 1;
//qWarning("h %d k %d la[h][k] %d",h,k,la[h][k]);
break;
}
}
}
}
}
if(key == 1)
{
break;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
}
}
if(key == 1)
{
break;
}
}
}
if(key == 1)
{
break;
}
}
if(key == 1)
{
g[m][n] = 0;
g[s][t] = w;
//qWarning("m = %d n = %d",m,n);
//qWarning("s = %d t = %d w = %d",s,t,w);
pengpeng();
//for(int i=0; i < NUMY+1; i++)
//for(int j=0; j < NUMX+1; j++)
//{
//if(g[i][j]!=0)
//qWarning("i = %d j = %d g = %d",i,j,g[i][j]);
//}
update();//退出循序后update
}
}
//随便走,逃跑还有什么选择吗?
if((v == 0)&&(key == 17))
{
qWarning("lu 19");
//不留顶头小卒
for(int a=0; a < NUMY+1; a++)
for(int b=0; b < NUMX+1; b++)
{
if((g[a][b]==28)&&(g[a+1][b]==12))
{
g[a][b]=0;
g[a+1][b]=28;
pengpeng();
key=1;
update();
break;
}
if((g[a][b]==29)&&(g[a+1][b]==13))
{
g[a][b]=0;
g[a+1][b]=29;
pengpeng();
key=1;
update();
break;
}
if((g[a][b]==30)&&(g[a+1][b]==14))
{
g[a][b]=0;
g[a+1][b]=30;
pengpeng();
key=1;
update();
break;
}
if((g[a][b]==31)&&(g[a+1][b]==15))
{
g[a][b]=0;
g[a+1][b]=31;
pengpeng();
key=1;
update();
break;
}
if((g[a][b]==32)&&(g[a+1][b]==16))
{
g[a][b]=0;
g[a+1][b]=32;
pengpeng();
key=1;
update();
break;
}
}
for(int h = 18; h<=32; h++)
{
int a = my[h].myi;//蓝棋h的位置
int b = my[h].myj;
if(h == g[a][b])
{
qWarning("h %d",h);
//上马前卒
if((h==22)&&(g[a+1][b]==29)&&(g[a+2][b]==0))
{
//qWarning("hello22");
g[a+1][b]=0;
g[a+2][b]=29;
pengpeng();
key=1;
update();
break;
}
if((h==23)&&(g[a+1][b]==31)&&(g[a+2][b]==0))
{
//qWarning("hello23");
g[a+1][b]=0;
g[a+2][b]=31;
pengpeng();
key=1;
update();
break;
}
//if(sbu[20]!=NULL)
//{
//qWarning("sbu[20]->huii %d sbu[20]->huij %d",sbu[20]->huii,sbu[20]->huij);
//}
//相归原位
if((h == 20)&&(g[2][4]==0)&&(sbu[20]!=NULL)&&(sbu[20]->huii==2)&&(sbu[20]->huij==4)&&(!((a==0)&&(b==2))))
{
qWarning("xiang20_1");
g[a][b]=0;
g[2][4]=20;
pengpeng();
key=1;
update();
break;
}
if((h == 21)&&(g[2][4]==0)&&(sbu[21]!=NULL)&&(sbu[21]->huii==2)&&(sbu[21]->huij==4)&&(!((a==0)&&(b==6))))
{
qWarning("xiang21_1");
g[a][b]=0;
g[2][4]=21;
pengpeng();
key=1;
update();
break;
}
//不乱飞相
if((h == 20)&&(g[2][4]==0)&&(g[1][3]==0)&&(a==0)&&(b==2))
{
//qWarning("hello20_2");
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
g[0][2]= 0;
g[2][4]= 20;
pengpeng();
int kg = 0;
for(int l = 18; l<=32; l++)//遍历一下蓝棋l
{
int p = my[l].myi;//蓝棋l的位置
int q = my[l].myj;
if(l == g[p][q])
{
//qWarning("l %d",l);
if((f[p][q]!=NULL)&&(f[p][q]->tail<17))//如果蓝棋l只被红棋威胁
{
kg = 1;
//qWarning("kg1 %d",kg);
break;
}
}
}
if(kg==1)
{
//qWarning("kg2 %d",kg);
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
continue;
}
if(kg==0)
{
//qWarning("kg3 %d",kg);
key=1;
//qWarning("key %d",key);
update();
break;
}
}
if((h == 21)&&(g[2][4]==0)&&(g[1][5]==0)&&(a==0)&&(b==6))
{
//qWarning("hello21_2");
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
g[0][6]= 0;
g[2][4]= 21;
pengpeng();
int kg = 0;
for(int l = 18; l<=32; l++)//遍历一下蓝棋l
{
int p = my[l].myi;//蓝棋l的位置
int q = my[l].myj;
if(l == g[p][q])
{
if((f[p][q]!=NULL)&&(f[p][q]->tail<17))//如果蓝棋l只被红棋威胁
{
kg = 1;
//qWarning("kg %d",kg);
break;
}
}
}
if(kg==1)
{
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
continue;
}
if(kg==0)
{
key=1;
update();
break;
}
}
if((h==20)&&(a==0)&&(b==2)&&(g[2][4]==21))
{
continue;
}
if((h==21)&&(a==0)&&(b==6)&&(g[2][4]==20))
{
continue;
}
taopao(0,h);//h这个蓝棋可以走到哪
int rc=0;
for(int i=0; i < NUMY+1; i++)
{
for(int j=0; j < NUMX+1; j++)
{
if(tao[i][j] == h)
{
//qWarning("i %d j %d",i,j);
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
//不乱上士
if(((h==18)||(h==19))&&(a==1)&&(b==4))
{
continue;
}
//不乱上相
if(((h==20)||(h==21))&&(a==2)&&(b==4))
{
continue;
}
//不原地悔棋
if((sbu[h]!=NULL)&&(sbu[h]->huii==i)&&(sbu[h]->huij==j))
{
continue;
}
//马尽量不后退
if(((h==22)||(h==23))&&(i<a))
{
continue;
}
g[a][b] = 0;
g[i][j] = h;
pengpeng();
for(int l = 18; l<=32; l++)
{
int a = my[l].myi;//蓝棋l的位置
int b = my[l].myj;
if(l == g[a][b])
{
if((f[a][b]!=NULL)&&(f[a][b]->tail<17))//蓝棋l只被红棋威胁
{
rc=1;
break;
}
}
}
if(rc == 1)
{
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
break;
}
int ti = my[17].myi;
int tj = my[17].myj;
if((f[ti][tj] == NULL)||((f[ti][tj] != NULL)&&(f[ti][tj]->name >= 18)&&(f[ti][tj]->name <= 32)))//
{
key = 1;
break;
}
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
g[i][j] = temp[i][j];
}
pengpeng();
}
}
if(rc == 1)
{
break;
}
if(key == 1)
{
break;
}
}
if(rc == 1)
{
//qWarning("rc %d",rc);
continue;
}
}
if(key == 1)
{
update();
break;
}
}
}
paint->~QPainter();
//qWarning("Paintend");
//记录每一步棋
{
int t;//标志是否移动棋子
t = 0;
//qWarning("t = %d",t);
//qWarning("%d",p_h);
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
if(p[p_h][i][j] != g[i][j])
{
t =1;
}
}
if(t == 1)
{
//qWarning("t = %d",t);
bushu++;
qWarning("bushu = %d",bushu);
p_h = (++p_h)%(NUM+1);
//bushu = bushu++;
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
p[p_h][i][j] = g[i][j];
}
}
//记录每个棋子上一步的位置
if(t == 1)
{
for(int i=0; i < NUMY+1; i++)
for(int j=0; j < NUMX+1; j++)
{
if((p[p_h][i][j]!=p[((p_h+NUM)%(NUM+1))][i][j])&&(p[p_h][i][j]==0))
{
sbu[(p[((p_h+NUM)%(NUM+1))][i][j])] = (hui *)malloc(sizeof(hui));
sbu[(p[((p_h+NUM)%(NUM+1))][i][j])]->huii=i;
sbu[(p[((p_h+NUM)%(NUM+1))][i][j])]->huij=j;
qWarning("sbu %d i %d j %d",p[((p_h+NUM)%(NUM+1))][i][j],i,j);
}
}
}
}
}
void ChessBroad::mousePressEvent(QMouseEvent *e)
{
int temp[NUMY+1][NUMX+1];
for(int i=0; i < NUMY+1; i++)//初始化temp[][]
for(int j=0; j < NUMX+1; j++)
{
temp[i][j] = g[i][j];
}
float max, min;
int flag;
if(this->width() >= this->height())
{
max = this->width();
min = this->height();
flag = 0;
}
else
{
max = this->height();
min = this->width();
flag = 1;
}
float check ;
if( flag == 0 )
{
check = min/(NUMY+2);
if((e->pos().x() > (max-(NUMX+1)*check)/2)&&(e->pos().x() < max-(max-(NUMX+1)*check)/2)&&(e->pos().y() > check/2)&&(e->pos().y() < (min-check/2)))
{
int px = (e->pos().y()-check/2)/check;
int py = (e->pos().x()-(max-(NUMX+1)*check)/2)/check;
//落子
if(key != 0)
{
int x, y;
for(int i=0; i<NUMY+1; i++)
for(int j=0; j<NUMX+1; j++)
{
if( g[i][j] == key )
{
x = i;
y = j;
}
}
switch(key)
{//红方
case 1://帅
{
int x1, y1;
for(int i=0; i<NUMY+1; i++)
for(int j=0; j<NUMX+1; j++)
{
if(g[i][j] == 17)
{
x1 = i;
y1 = j;
}
}
//qWarning("%d %d", x1, y1);
if((y1 == y))
{
int i;
for( i = 1; (i <= x-x1)&&(g[x-i][y] == 0); i++);
//qWarning("%d", i);
if((i == x-x1)&&(g[px][py] == 17))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
if((((px == x+1)&&(py == y))||((px == x-1)&&(py == y))
||((px == x)&&(py ==y+1))||((px == x)&&(py == y-1)))
&&((px >=7)&&(py >= 3)&&(py <= 5)))
{
if((g[px][py]/17 != 0)&&(g[px][py] != 0))//吃子
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
break;
}
case 2://仕
case 3:
{
if(((px >=7)&&(py >= 3)&&(py <= 5))
&&(((px == x+1)&&(py == y+1))||((px == x-1)&&(py == y-1))||((px == x+1)&&(py == y-1))||((px == x-1)&&(py == y+1))))
{
if((g[px][py]/17 != 0)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
break;
}
case 4://相
case 5:
{
if((((px == x+2)&&(py == y+2)&&(g[x+1][y+1] == 0))||((px == x-2)&&(py == y-2)&&(g[x-1][y-1] == 0))
||((px == x+2)&&(py == y-2)&&(g[x+1][y-1] == 0))||((px ==x-2)&&(py == y+2)&&(g[x-1][y+1] == 0)))
&&(px >= 5))
{
if((g[px][py]/17 != 0)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
break;
}
case 6://马
case 7:
{
if(((px == x+2)&&(py == y+1)&&(g[x+1][y] == 0))||((px == x+2)&&(py == y-1)&&(g[x+1][y] == 0))
||((px == x-2)&&(py == y+1)&&(g[x-1][y] == 0))||((px == x-2)&&(py == y-1)&&(g[x-1][y] == 0))
||((px == x+1)&&(py == y+2)&&(g[x][y+1] == 0))||((px == x+1)&&(py == y-2)&&(g[x][y-1] == 0))
||((px == x-1)&&(py == y+2)&&(g[x][y+1] == 0))||((px == x-1)&&(py == y-2)&&(g[x][y-1] == 0)))
{
if((g[px][py]/17 != 0)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
break;
}
case 8://车
case 9:
{
if(px == x)
{
if(py > y)
{
int i;
for( i=1; (i < (py-y))&&(g[x][y+i] == 0); i++);
if(i == (py-y))
{
if((g[px][py]/17 != 0)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
if(py < y)
{
int i;
for( i=y-py-1; (i > 0)&&(g[x][y-i] == 0); i--);
if(i == 0)
{
if((g[px][py]/17 != 0)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
}
if(py == y)
{
if(px > x)
{
int i;
for( i=1; (i < (px-x))&&(g[x+i][y] == 0); i++);
if(i == (px-x))
{
if((g[px][py]/17 != 0)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
if(px < x)
{
int i;
for( i=x-px-1; (i > 0)&&(g[x-i][y] == 0); i--);
if(i == 0)
{
if((g[px][py]/17 != 0)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
}
break;
}
case 10://炮
case 11:
{
if(px == x)
{
if(g[px][py] == 0)
{
if(py > y)
{
int i;
for( i = 1; (i < (py-y))&&(g[x][y+i] == 0); i++);
if(i == (py-y))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
if(py < y)
{
int i;
for( i = (y-py-1); (i > 0)&&(g[x][y-i] == 0); i--);
if(i == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
if((g[px][py]/17 != 0)&&(g[px][py] != 0))
{//吃子
//qWarning("%d %d",px,py);
if(py > y)//向右吃子
{
int i, j;
for( i = 1; (i < py-y)&&(g[x][y+i] == 0); i++);
//qWarning("%d %d",px, py);
//qWarning("%d",i);
if(i != py-y)
{
for(j = i+1; (j < py-y)&&(g[x][y+j] == 0); j++);
//qWarning("%d",j);
if(j == py-y)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
if(py < y)//向左吃子
{
int i, j;
for( i = 1; (i < y-py)&&(g[x][y-i] == 0); i++);
//qWarning("%d %d", px, py);
//qWarning("%d", i);
if(i != y-py)
{
for( j = i+1; (j < y-py)&&(g[x][y-j] == 0); j++);
//qWarning("%d",j);
if(j == y-py)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
}
}
if(py == y)
{
if(g[px][py] == 0)
{
if(px > x)
{
int i;
for( i = 1; (i < (px-x))&&(g[x+i][y] == 0); i++);
if(i == (px-x))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
if(px < x)
{
int i;
for( i = (x-px-1); (i > 0)&&(g[x-i][y] == 0); i--);
if(i == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
if((g[px][py]/17 != 0)&&(g[px][py] != 0))
{
if(px > x)
{
int i, j;
for( i = 1; (i < px-x)&&(g[x+i][y] == 0); i++);
//qWarning("%d %d",px, py);
//qWarning("%d",i);
if(i != px-x)
{
for(j = i+1; (j < px-x)&&(g[x+j][y] == 0); j++);
//qWarning("%d",j);
if(j == px-x)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
if(px < x)//向上吃子
{
int i, j;
for( i = 1; (i < x-px)&&(g[x-i][y] == 0); i++);
//qWarning("%d %d", px, py);
//qWarning("%d", i);
if(i != x-px)
{
for( j = i+1; (j < x-px)&&(g[x-j][y] == 0); j++);
//qWarning("%d",j);
if(j == x-px)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
}
}
break;
}
case 12://兵
case 13:
case 14:
case 15:
case 16:
{
if((px == 5)&&(px == (x-1))&&(py == y))
{
if(((g[px][py]/17 != key/17)&&(g[px][py] != 0))||(g[px][py] == 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
if(px < 5)
{
if(((px == x)&&(py == y+1))||((px == x)&&(py == y-1))
||((px == x-1)&&(py == y)))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
break;
}
case 17://黑将
{
int x1, y1;
for(int i=0; i<NUMY+1; i++)
for(int j=0; j<NUMX+1; j++)
{
if(g[i][j] == 1)
{
x1 = i;
y1 = j;
}
}
//qWarning("%d %d", x1, y1);
if(y1 == y)
{
int i;
for( i = 1; (i <= x1-x)&&(g[x+i][y] == 0); i++);
//qWarning("%d", i);
if((i == x1-x)&&(g[px][py] == 1))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
if((((px == x+1)&&(py == y))||((px == x-1)&&(py == y))
||((px == x)&&(py ==y+1))||((px == x)&&(py == y-1)))
&&((px <= 2)&&(py >= 3)&&(py <= 5)))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))//吃子
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
break;
}
case 18://士
case 19:
{
//qWarning("%d %d",px,py);
//qWarning("%d",g[x][y]);
//qWarning("%d",g[px][py]);
if(((px <= 2)&&(py >= 3)&&(py <= 5))
&&(((px == x+1)&&(py == y+1))||((px == x-1)&&(py == y-1))||((px == x+1)&&(py == y-1))||((px == x-1)&&(py == y+1))))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
//qWarning("e");
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
//qWarning("%d %d",px,py);
//qWarning("%d",g[x][y]);
//qWarning("%d",g[px][py]);
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
//qWarning("%d %d",px,py);
//qWarning("%d",g[x][y]);
//qWarning("%d",g[px][py]);
break;
}
case 20://象
case 21:
{
if((((px == x+2)&&(py == y+2)&&(g[x+1][y+1] == 0))||((px == x-2)&&(py == y-2)&&(g[x-1][y-1] == 0))
||((px == x+2)&&(py == y-2)&&(g[x+1][y-1] == 0))||((px ==x-2)&&(py == y+2)&&(g[x-1][y+1] == 0)))
&&(px <= 4))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
break;
}
case 22://马
case 23:
{
if(((px == x+2)&&(py == y+1)&&(g[x+1][y] == 0))||((px == x+2)&&(py == y-1)&&(g[x+1][y] == 0))
||((px == x-2)&&(py == y+1)&&(g[x-1][y] == 0))||((px == x-2)&&(py == y-1)&&(g[x-1][y] == 0))
||((px == x+1)&&(py == y+2)&&(g[x][y+1] == 0))||((px == x+1)&&(py == y-2)&&(g[x][y-1] == 0))
||((px == x-1)&&(py == y+2)&&(g[x][y+1] == 0))||((px == x-1)&&(py == y-2)&&(g[x][y-1] == 0)))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
break;
}
case 24://车
case 25:
{
if(px == x)
{
if(py > y)
{
int i;
for( i=1; (i < (py-y))&&(g[x][y+i] == 0); i++);
if(i == (py-y))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
if(py < y)
{
int i;
for( i=y-py-1; (i > 0)&&(g[x][y-i] == 0); i--);
if(i == 0)
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
}
if(py == y)
{
if(px > x)
{
int i;
for( i=1; (i < (px-x))&&(g[x+i][y] == 0); i++);
if(i == (px-x))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
if(px < x)
{
int i;
for( i=x-px-1; (i > 0)&&(g[x-i][y] == 0); i--);
if(i == 0)
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
}
break;
}
case 26://炮
case 27:
{
if(px == x)
{
if(g[px][py] == 0)
{
if(py > y)
{
int i;
for( i = 1; (i < (py-y))&&(g[x][y+i] == 0); i++);
if(i == (py-y))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
if(py < y)
{
int i;
for( i = (y-py-1); (i > 0)&&(g[x][y-i] == 0); i--);
if(i == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{//吃子
//qWarning("%d %d",px,py);
if(py > y)//向右吃子
{
int i, j;
for( i = 1; (i < py-y)&&(g[x][y+i] == 0); i++);
//qWarning("%d %d",px, py);
//qWarning("%d",i);
if(i != py-y)
{
for(j = i+1; (j < py-y)&&(g[x][y+j] == 0); j++);
//qWarning("%d",j);
if(j == py-y)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
if(py < y)//向左吃子
{
int i, j;
for( i = 1; (i < y-py)&&(g[x][y-i] == 0); i++);
//qWarning("%d %d", px, py);
//qWarning("%d", i);
if(i != y-py)
{
for( j = i+1; (j < y-py)&&(g[x][y-j] == 0); j++);
//qWarning("%d",j);
if(j == y-py)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
}
}
if(py == y)
{
if(g[px][py] == 0)
{
if(px > x)
{
int i;
for( i = 1; (i < (px-x))&&(g[x+i][y] == 0); i++);
if(i == (px-x))
{
g[px][py] = key;
g[x][y] = 0;
key = 1;
}
}
if(px < x)
{
int i;
for( i = (x-px-1); (i > 0)&&(g[x-i][y] == 0); i--);
if(i == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 1;
}
}
}
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
if(px > x)
{
int i, j;
for( i = 1; (i < px-x)&&(g[x+i][y] == 0); i++);
//qWarning("%d %d",px, py);
//qWarning("%d",i);
if(i != px-x)
{
for(j = i+1; (j < px-x)&&(g[x+j][y] == 0); j++);
//qWarning("%d",j);
if(j == px-x)
{
g[px][py] = key;
g[x][y] = 0;
key = 1;
}
}
}
if(px < x)//向上吃子
{
int i, j;
for( i = 1; (i < x-px)&&(g[x-i][y] == 0); i++);
//qWarning("%d %d", px, py);
//qWarning("%d", i);
if(i != x-px)
{
for( j = i+1; (j < x-px)&&(g[x-j][y] == 0); j++);
//qWarning("%d",j);
if(j == x-px)
{
g[px][py] = key;
g[x][y] = 0;
key = 1;
}
}
}
}
}
break;
}
case 28://卒
case 29:
case 30:
case 31:
case 32:
{
if((px == 4)&&(px == (x+1))&&(py == y))
{
if(((g[px][py]/17 != key/17)&&(g[px][py] != 0))||(g[px][py] == 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
if(px > 4)
{
if(((px == x)&&(py == y+1))||((px == x)&&(py == y-1))
||((px == x+1)&&(py == y)))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
break;
}
}
}
if((g[px][py] != 0)&&(key == 0))//第一次点击棋子
{
key = g[px][py];
}
if((key != 0)&&(key/17 == g[px][py]/17)&&(g[px][py] != 0))//再次点击的颜色与选中的颜色相同,框住点选的棋子。
{
key = g[px][py];
}
}
}
if( flag == 1)
{
check = min/(NUMX+2);
if((e->pos().x() > check/2)&&(e->pos().x() < (min - check/2))&&(e->pos().y() > (max-(NUMY+1)*check)/2)&&(e->pos().y() < max-(max-(NUMY+1)*check)/2))
{
int px = (e->pos().y()-(max-(NUMY+1)*check)/2)/check;
int py = (e->pos().x()-check/2)/check;
//落子从flag==0完全复制
if(key != 0)
{
int x, y;
for(int i=0; i<NUMY+1; i++)
for(int j=0; j<NUMX+1; j++)
{
if( g[i][j] == key )
{
x = i;
y = j;
}
}
switch(key)
{//红方
case 1://帅
{
int x1, y1;
for(int i=0; i<NUMY+1; i++)
for(int j=0; j<NUMX+1; j++)
{
if(g[i][j] == 17)
{
x1 = i;
y1 = j;
}
}
//qWarning("%d %d", x1, y1);
if((y1 == y))
{
int i;
for( i = 1; (i <= x-x1)&&(g[x-i][y] == 0); i++);
//qWarning("%d", i);
if((i == x-x1)&&(g[px][py] == 17))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
if((((px == x+1)&&(py == y))||((px == x-1)&&(py == y))
||((px == x)&&(py ==y+1))||((px == x)&&(py == y-1)))
&&((px >=7)&&(py >= 3)&&(py <= 5)))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))//吃子
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
break;
}
case 2://仕
case 3:
{
if(((px >=7)&&(py >= 3)&&(py <= 5))
&&(((px == x+1)&&(py == y+1))||((px == x-1)&&(py == y-1))||((px == x+1)&&(py == y-1))||((px == x-1)&&(py == y+1))))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
break;
}
case 4://相
case 5:
{
if((((px == x+2)&&(py == y+2)&&(g[x+1][y+1] == 0))||((px == x-2)&&(py == y-2)&&(g[x-1][y-1] == 0))
||((px == x+2)&&(py == y-2)&&(g[x+1][y-1] == 0))||((px ==x-2)&&(py == y+2)&&(g[x-1][y+1] == 0)))
&&(px >= 5))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
break;
}
case 6://马
case 7:
{
if(((px == x+2)&&(py == y+1)&&(g[x+1][y] == 0))||((px == x+2)&&(py == y-1)&&(g[x+1][y] == 0))
||((px == x-2)&&(py == y+1)&&(g[x-1][y] == 0))||((px == x-2)&&(py == y-1)&&(g[x-1][y] == 0))
||((px == x+1)&&(py == y+2)&&(g[x][y+1] == 0))||((px == x+1)&&(py == y-2)&&(g[x][y-1] == 0))
||((px == x-1)&&(py == y+2)&&(g[x][y+1] == 0))||((px == x-1)&&(py == y-2)&&(g[x][y-1] == 0)))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
break;
}
case 8://车
case 9:
{
if(px == x)
{
if(py > y)
{
int i;
for( i=1; (i < (py-y))&&(g[x][y+i] == 0); i++);
if(i == (py-y))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
if(py < y)
{
int i;
for( i=y-py-1; (i > 0)&&(g[x][y-i] == 0); i--);
if(i == 0)
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
}
if(py == y)
{
if(px > x)
{
int i;
for( i=1; (i < (px-x))&&(g[x+i][y] == 0); i++);
if(i == (px-x))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
if(px < x)
{
int i;
for( i=x-px-1; (i > 0)&&(g[x-i][y] == 0); i--);
if(i == 0)
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
}
break;
}
case 10://炮
case 11:
{
if(px == x)
{
if(g[px][py] == 0)
{
if(py > y)
{
int i;
for( i = 1; (i < (py-y))&&(g[x][y+i] == 0); i++);
if(i == (py-y))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
if(py < y)
{
int i;
for( i = (y-py-1); (i > 0)&&(g[x][y-i] == 0); i--);
if(i == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{//吃子
if(py > y)//向右吃子
{
int i, j;
for( i = 1; (i < py-y)&&(g[x][y+i] == 0); i++);
//qWarning("%d %d",px, py);
//qWarning("%d",i);
if(i != py-y)
{
for(j = i+1; (j < py-y)&&(g[x][y+j] == 0); j++);
//qWarning("%d",j);
if(j == py-y)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
if(py < y)//向左吃子
{
int i, j;
for( i = 1; (i < y-py)&&(g[x][y-i] == 0); i++);
//qWarning("%d %d", px, py);
//qWarning("%d", i);
if(i != y-py)
{
for( j = i+1; (j < y-py)&&(g[x][y-j] == 0); j++);
//qWarning("%d",j);
if(j == y-py)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
}
}
if(py == y)
{
if(g[px][py] == 0)
{
if(px > x)
{
int i;
for( i = 1; (i < (px-x))&&(g[x+i][y] == 0); i++);
if(i == (px-x))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
if(px < x)
{
int i;
for( i = (x-px-1); (i > 0)&&(g[x-i][y] == 0); i--);
if(i == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
if(px > x)
{
int i, j;
for( i = 1; (i < px-x)&&(g[x+i][y] == 0); i++);
//qWarning("%d %d",px, py);
//qWarning("%d",i);
if(i != px-x)
{
for(j = i+1; (j < px-x)&&(g[x+j][y] == 0); j++);
//qWarning("%d",j);
if(j == px-x)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
if(px < x)//向上吃子
{
int i, j;
for( i = 1; (i < x-px)&&(g[x-i][y] == 0); i++);
//qWarning("%d %d", px, py);
//qWarning("%d", i);
if(i != x-px)
{
for( j = i+1; (j < x-px)&&(g[x-j][y] == 0); j++);
//qWarning("%d",j);
if(j == x-px)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
}
}
break;
}
case 12://兵
case 13:
case 14:
case 15:
case 16:
{
//qWarning("%d %d", px, py);
if((px == 5)&&(px == (x-1))&&(py == y))
{
if(((g[px][py]/17 != key/17)&&(g[px][py] != 0))||(g[px][py] == 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
if(px < 5)
{
if(((px == x)&&(py == y+1))||((px == x)&&(py == y-1))
||((px == x-1)&&(py == y)))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
if(g[px][py] == 0)
{
g[px][py] = key;
g[x][y] = 0;
key = 17;
}
}
}
break;
}
case 17://黑将
{
int x1, y1;
for(int i=0; i<NUMY+1; i++)
for(int j=0; j<NUMX+1; j++)
{
if(g[i][j] == 1)
{
x1 = i;
y1 = j;
}
}
//qWarning("%d %d", x1, y1);
if(y1 == y)
{
int i;
for( i = 1; (i <= x1-x)&&(g[x+i][y] == 0); i++);
//qWarning("%d", i);
if((i == x1-x)&&(g[px][py] == 1))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
if((((px == x+1)&&(py == y))||((px == x-1)&&(py == y))
||((px == x)&&(py ==y+1))||((px == x)&&(py == y-1)))
&&((px <= 2)&&(py >= 3)&&(py <= 5)))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))//吃子
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
break;
}
case 18://士
case 19:
{
if(((px <= 2)&&(py >= 3)&&(py <= 5))
&&(((px == x+1)&&(py == y+1))||((px == x-1)&&(py == y-1))||((px == x+1)&&(py == y-1))||((px == x-1)&&(py == y+1))))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
break;
}
case 20://象
case 21:
{
if((((px == x+2)&&(py == y+2)&&(g[x+1][y+1] == 0))||((px == x-2)&&(py == y-2)&&(g[x-1][y-1] == 0))
||((px == x+2)&&(py == y-2)&&(g[x+1][y-1] == 0))||((px ==x-2)&&(py == y+2)&&(g[x-1][y+1] == 0)))
&&(px <= 4))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
break;
}
case 22://马
case 23:
{
if(((px == x+2)&&(py == y+1)&&(g[x+1][y] == 0))||((px == x+2)&&(py == y-1)&&(g[x+1][y] == 0))
||((px == x-2)&&(py == y+1)&&(g[x-1][y] == 0))||((px == x-2)&&(py == y-1)&&(g[x-1][y] == 0))
||((px == x+1)&&(py == y+2)&&(g[x][y+1] == 0))||((px == x+1)&&(py == y-2)&&(g[x][y-1] == 0))
||((px == x-1)&&(py == y+2)&&(g[x][y+1] == 0))||((px == x-1)&&(py == y-2)&&(g[x][y-1] == 0)))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
break;
}
case 24://车
case 25:
{
if(px == x)
{
if(py > y)
{
int i;
for( i=1; (i < (py-y))&&(g[x][y+i] == 0); i++);
if(i == (py-y))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
if(py < y)
{
int i;
for( i=y-py-1; (i > 0)&&(g[x][y-i] == 0); i--);
if(i == 0)
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
}
if(py == y)
{
if(px > x)
{
int i;
for( i=1; (i < (px-x))&&(g[x+i][y] == 0); i++);
if(i == (px-x))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
if(px < x)
{
int i;
for( i=x-px-1; (i > 0)&&(g[x-i][y] == 0); i--);
if(i == 0)
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
}
break;
}
case 26://炮
case 27:
{
if(px == x)
{
if(g[px][py] == 0)
{
if(py > y)
{
int i;
for( i = 1; (i < (py-y))&&(g[x][y+i] == 0); i++);
if(i == (py-y))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
if(py < y)
{
int i;
for( i = (y-py-1); (i > 0)&&(g[x][y-i] == 0); i--);
if(i == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{//吃子
if(py > y)//向右吃子
{
int i, j;
for( i = 1; (i < py-y)&&(g[x][y+i] == 0); i++);
//qWarning("%d %d",px, py);
//qWarning("%d",i);
if(i != py-y)
{
for(j = i+1; (j < py-y)&&(g[x][y+j] == 0); j++);
//qWarning("%d",j);
if(j == py-y)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
if(py < y)//向左吃子
{
int i, j;
for( i = 1; (i < y-py)&&(g[x][y-i] == 0); i++);
//qWarning("%d %d", px, py);
//qWarning("%d", i);
if(i != y-py)
{
for( j = i+1; (j < y-py)&&(g[x][y-j] == 0); j++);
//qWarning("%d",j);
if(j == y-py)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
}
}
if(py == y)
{
if(g[px][py] == 0)
{
if(px > x)
{
int i;
for( i = 1; (i < (px-x))&&(g[x+i][y] == 0); i++);
if(i == (px-x))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
if(px < x)
{
int i;
for( i = (x-px-1); (i > 0)&&(g[x-i][y] == 0); i--);
if(i == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
if(px > x)
{
int i, j;
for( i = 1; (i < px-x)&&(g[x+i][y] == 0); i++);
//qWarning("%d %d",px, py);
//qWarning("%d",i);
if(i != px-x)
{
for(j = i+1; (j < px-x)&&(g[x+j][y] == 0); j++);
//qWarning("%d",j);
if(j == px-x)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
if(px < x)//向上吃子
{
int i, j;
for( i = 1; (i < x-px)&&(g[x-i][y] == 0); i++);
//qWarning("%d %d", px, py);
//qWarning("%d", i);
if(i != x-px)
{
for( j = i+1; (j < x-px)&&(g[x-j][y] == 0); j++);
//qWarning("%d",j);
if(j == x-px)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
}
}
break;
}
case 28://卒
case 29:
case 30:
case 31:
case 32:
{
//qWarning("%d %d", px, py);
if((px == 4)&&(px == (x+1))&&(py == y))
{
if(((g[px][py]/17 != key/17)&&(g[px][py] != 0))||(g[px][py] == 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
if(px > 4)
{
if(((px == x)&&(py == y+1))||((px == x)&&(py == y-1))
||((px == x+1)&&(py == y)))
{
if((g[px][py]/17 != key/17)&&(g[px][py] != 0))
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
if(g[px][py] == 0)
{
g[px][py] = g[x][y];
g[x][y] = 0;
key = 1;
}
}
}
break;
}
}
}//end落子
if((g[px][py] != 0)&&(key == 0))
{
key = g[px][py];
}
if((key != 0)&&(key/17 == g[px][py]/17)&&(g[px][py] != 0))
{
key = g[px][py];
}
}
}
//qWarning("%d",key);
for(int i=0; i < NUMY+1; i++)//提取起子落子位置
for(int j=0; j < NUMX+1; j++)
{
if((temp[i][j] != g[i][j])&&(g[i][j] == 0))
{
x_1 = i;
y_1 = j;
//qWarning("x_1 %d, y_1 %d",x_1,y_1);
}
if((temp[i][j] != g[i][j])&&(g[i][j] != 0))
{
x_2 = i;
y_2 = j;
ta = g[i][j];
//qWarning("x_2 %d, y_2 %d, ta %d",x_2,y_2,ta);
}
}
update();
}
ChessBroad::~ChessBroad()
{
}
Qt编写的复杂象棋程序 chessbroad.cpp 错误比较多
最新推荐文章于 2022-12-03 19:35:56 发布