?? mantischessstd.cpp
字號:
/***************************************************************
MantisChessStd.cpp : MantisChess 標(biāo)準(zhǔn)函數(shù)
版權(quán)所有(C) 陳成濤
這一程序是自由軟件,你可以遵照自由軟件基金會出版的GNU通用公共
許可證條款來修改和重新發(fā)布這一程序。或者用許可證的第二版,或者
(根據(jù)你的選擇)用任何更新的版本。
發(fā)布這一程序的目的是希望它有用,但沒有任何擔(dān)保。甚至沒有適合特
定目的的隱含的擔(dān)保。更詳細(xì)的情況請參閱GNU通用公共許可證。
你應(yīng)該已經(jīng)和程序一起收到一份GNU通用公共許可證的副本。
如果還沒有,寫信給:
The Free Software Foundation,Inc,,675 Mass Ave, Cambridge,
MAO2139,USA
如果你在使用本軟件時有什么問題或建議,用以下地址可以與我取得聯(lián)
系:
http://thecct.51.net
或發(fā)Email到:
stove@eyou.com
thecct@163.com
------------------------------------------------------------------
MantisChessStd.cpp : MantisChess standard functions
Copyright (C) Chen Chengtao, China
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
If you have any question about this software please visit my hompage:
http://thecct.51.net
or E_mail to:
stove@eyou.com
thecct@163.com
******************************************************************/
#include "StdAfx.h"
#include "MantisChessDef.h"
#include "MantisChessStd.h"
/******************************************************************
CanGo: 判斷一步棋合不合法
參數(shù):
manmap: 棋位狀態(tài)
man: 所移動的棋子
from: 所移動的棋子原始位置
to: 所要移動到的位置
返回值: 合法返回TRUE,不合法返回FALSE
******************************************************************/
BOOL CanGo(int manmap[11][12],int man,const POINT &from,const POINT &to)
{
static int i,j;
if(!IsNormal(ManToType[man],to)) //這個棋子不能放在目標(biāo)位置
{
//如果不是將/帥 (將/帥可以"照相")
if(ManToType[man]!=RED_K&& ManToType[man]!=BLACK_K)return FALSE;
else if(ManToType[man]==RED_K && //走的是帥
ManToType[manmap[to.x][to.y]]==BLACK_K) //目標(biāo)是將
{
BOOL flag=FALSE;
for(j= from.y-1;j>0;j--)
{
if (manmap[from.x][j]!=32)
{
if(ManToType[manmap[from.x][j]]==BLACK_K) //照相
flag=TRUE;
break;
}
}
if(flag)return TRUE;
else return FALSE;
}
else if(ManToType[manmap[to.x][to.y]]==RED_K) //走的是將,目標(biāo)是帥
{
BOOL flag=FALSE;
for(j= from.y+1;j<11;j++)
{
if (manmap[from.x][j]!=32)
{
if(ManToType[manmap[from.x][j]]==RED_K) //照相
flag=TRUE;
break;
}
}
if(flag)return TRUE;
else return FALSE;
}
else return FALSE;
}
//下面幾行判斷目標(biāo)點是否己方的棋子:
if(SideOfMan[man]==0)
{
if(manmap[to.x][to.y]!=32&& SideOfMan[manmap[to.x][to.y]]==0)return FALSE;
}
else if(SideOfMan[man]==1)
{
if(manmap[to.x][to.y]!=32&& SideOfMan[manmap[to.x][to.y]]==1)return FALSE;
}
//--------------------------------
//以下是各棋子的規(guī)則:
switch(ManToType[man])
{
case RED_B:
//兵不回頭:
if(to.y > from.y)return FALSE;
//兵只走一步直線:
if(from.y-to.y+abs(to.x-from.x)>1)return FALSE;
break;
case BLACK_B:
//卒不回頭:
if(to.y < from.y)return FALSE;
//卒只走一步直線:
if(to.y-from.y+abs(to.x-from.x)>1)return FALSE;
break;
case RED_S:
case BLACK_S:
//士走斜線一步:
if(abs(from.y-to.y)>1||abs(to.x-from.x)>1)return FALSE;
break;
case RED_X:
case BLACK_X:
//相走田:
if(abs(from.x-to.x)!=2||abs(from.y-to.y)!=2)return FALSE;
//相心:
if(manmap[(from.x+to.x)/2][(from.y+to.y)/2]!=32)return FALSE;
break;
case RED_K:
case BLACK_K:
//將帥只走一步直線:
if(abs(from.y-to.y)+abs(to.x-from.x)>1)return FALSE;
break;
case RED_J:
case BLACK_J:
//車只能走直線:
if(from.y!=to.y&&from.x!=to.x)return FALSE;
//車經(jīng)過的路線中不能有棋子: -----------
if(from.y==to.y)
{
if(from.x<to.x)
{
for(i=from.x+1;i<to.x;i++)
if(manmap[i][from.y]!=32)return FALSE;
}
else
{
for(i=to.x+1;i<from.x;i++)
if(manmap[i][from.y]!=32)return FALSE;
}
}
else
{
if(from.y<to.y)
{
for(j=from.y+1;j<to.y;j++)
if(manmap[from.x][j]!=32)return FALSE;
}
else
{
for(j=to.y+1;j<from.y;j++)
if(manmap[from.x][j]!=32)return FALSE;
}
}
//以上是車---------------------------------
break;
case RED_P:
case BLACK_P:
//炮只能走直線:
if(from.y!=to.y&&from.x!=to.x)return FALSE;
//炮不吃子時經(jīng)過的路線中不能有棋子:------------------
if(manmap[to.x][to.y]==32)
{
if(from.y==to.y)
{
if(from.x<to.x)
{
for(i=from.x+1;i<to.x;i++)
if(manmap[i][from.y]!=32)return FALSE;
}
else
{
for(i=to.x+1;i<from.x;i++)
if(manmap[i][from.y]!=32)return FALSE;
}
}
else
{
if(from.y<to.y)
{
for(j=from.y+1;j<to.y;j++)
if(manmap[from.x][j]!=32)return FALSE;
}
else
{
for(j=to.y+1;j<from.y;j++)
if(manmap[from.x][j]!=32)return FALSE;
}
}
}
//以上是炮不吃子-------------------------------------
//吃子時:=======================================
else
{
int count=0;
if(from.y==to.y)
{
if(from.x<to.x)
{
for(i=from.x+1;i<to.x;i++)
if(manmap[i][from.y]!=32)count++;
if(count!=1)return FALSE;
}
else
{
for(i=to.x+1;i<from.x;i++)
if(manmap[i][from.y]!=32)count++;
if(count!=1)return FALSE;
}
}
else
{
if(from.y<to.y)
{
for(j=from.y+1;j<to.y;j++)
if(manmap[from.x][j]!=32)count++;
if(count!=1)return FALSE;
}
else
{
for(j=to.y+1;j<from.y;j++)
if(manmap[from.x][j]!=32)count++;
if(count!=1)return FALSE;
}
}
}
//以上是炮吃子時================================
break;
case RED_M:
case BLACK_M:
//馬走日:
if(!(
(abs(to.x-from.x)==1&&abs(to.y-from.y)==2)
||(abs(to.x-from.x)==2&&abs(to.y-from.y)==1)
))return FALSE;
//找馬腳:
if (to.x-from.x==2){i=from.x+1;j=from.y;}
else if (from.x-to.x==2){i=from.x-1;j=from.y;}
else if (to.y-from.y==2){i=from.x;j=from.y+1;}
else if (from.y-to.y==2){i=from.x;j=from.y-1;}
//絆馬腳:
if(manmap[i][j]!=32)return FALSE;
break;
default:
break;
}
return TRUE; //上面的規(guī)則全通過!
}
/******************************************************************
IsNormal: mantype類型的棋子放在point位置是否合法
參數(shù):
mantype: 棋子類型
point: 位置
返回值: 合法返回TRUE,不合法返回FALSE
******************************************************************/
BOOL IsNormal(const int & mantype,const POINT &point)
{
if(point.x<1||point.x>9||point.y<1||point.y>10)return FALSE;
switch(mantype)
{
case RED_K:
//帥不能在紅方宮外:
if( point.x>6|| point.x<4|| point.y<8)return FALSE;
break;
case RED_S:
//仕只能在宮內(nèi)特定點:
if(!(
( point.x==4&& point.y==10)||
( point.x==4&& point.y==8)||
( point.x==5&& point.y==9)||
( point.x==6&& point.y==10)||
( point.x==6&& point.y==8)
))return FALSE;
break;
case RED_X:
//七個相位:
if(!(
( point.x==1&& point.y==8)||
( point.x==3&& point.y==10)||
( point.x==3&& point.y==6)||
( point.x==5&& point.y==8)||
( point.x==7&& point.y==10)||
( point.x==7&& point.y==6)||
( point.x==9&& point.y==8)
))return FALSE;
break;
case RED_B:
//兵不能在兵位后:
if( point.y>7)return FALSE;
//兵過河前不能左右移動:
if( point.y>5&& point.x%2==0)return FALSE;
break;
case BLACK_K:
//帥不能在紅方宮外:
if( point.x>6|| point.x<4|| point.y>3)return FALSE;
break;
case BLACK_S:
//仕只能在宮內(nèi)特定點:
if(!(
( point.x==4&& point.y==1)||
( point.x==4&& point.y==3)||
( point.x==5&& point.y==2)||
( point.x==6&& point.y==1)||
( point.x==6&& point.y==3)
))return FALSE;
break;
case BLACK_X:
//七個相位:
if(!(
( point.x==1&& point.y==3)||
( point.x==3&& point.y==1)||
( point.x==3&& point.y==5)||
( point.x==5&& point.y==3)||
( point.x==7&& point.y==1)||
( point.x==7&& point.y==5)||
( point.x==9&& point.y==3)
))return FALSE;
break;
case BLACK_B:
//兵不能在兵位后:
if( point.y<4)return FALSE;
//兵過河前不能左右移動:
if( point.y<6&& point.x%2==0)return FALSE;
break;
default:
break;
}
return TRUE;
}
/******************************************************************
FixManMap: 根據(jù)棋子坐標(biāo)計算棋位狀態(tài)
參數(shù):
map: 棋位狀態(tài)(存放結(jié)果)
manpoint: 棋子坐標(biāo)
side: 輪到哪一方走
返回值: 無
******************************************************************/
void FixManMap(int map[11][12],POINT manpoint[32],int side)
{
memcpy(map,_defaultmap,132*sizeof(int));
static POINT * pman;
static int i;
for(i=0;i<32;i++)
{
pman = & manpoint[i];
if(pman->x)
map[pman->x][pman->y]=i;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -