using System;
using System.Drawing;
using System.Collections.Generic;
namespace 象棋启蒙
{
/// <summary>
/// ChessItemJu 的摘要说明。
/// </summary>
public class Chess
{
private Game game = null;
private ChessRecord chessRecord = null;
public string BeEatenChess = "";
///<summary>
///棋类
///<summary>
///创建一个棋子
///</summary>
///<param name="location">左上角位置</param>
///<param name="ChessType">棋子类型</param>
public Chess(Game gm,ChessType type0,Level level0,Point location0 )
{
chessRecord = new ChessRecord(gm);
game = gm;
chessType = type0;
chessLevel = level0;
Location_Inner = location0;
string ch = "";
switch (level0)
{
case Level.帅:
ch = (type0 == ChessType.red) ? "帅" : "将"; picID = (chessType == ChessType.red) ? 1 : 8;
break;
case Level.士:
ch = (type0 == ChessType.red) ? "仕" : "士"; picID = (chessType == ChessType.red) ? 2 : 9;
break;
case Level.相:
ch = (type0 == ChessType.red) ? "相" : "象"; picID = (chessType == ChessType.red) ? 3 : 10;
break;
case Level.马:
ch = (type0 == ChessType.red) ? "马" : "码"; picID = (chessType == ChessType.red) ? 4 : 11;
break;
case Level.车:
ch = (type0 == ChessType.red) ? "车" : "砗"; picID = (chessType == ChessType.red) ? 5 : 12;
break;
case Level.炮:
ch = (type0 == ChessType.red) ? "炮" : "砲"; picID = (chessType == ChessType.red) ? 6 : 13;
break;
case Level.兵:
ch = (type0 == ChessType.red) ? "兵" : "卒"; picID = (chessType == ChessType.red) ? 7 : 14;
break;
}
this.ChessName =ch.ToString();
this.LocationOutside = new Point(game.Location.X + Location_Inner.X *Game._unit,game.Location.Y + Location_Inner.Y *Game._unit);
Position_Current = _location_Inner.X + 51 + 16 * _location_Inner.Y;
}
/<summary>
/创建一个棋子
/</summary>
/<param name="location">左上角位置</param>
/<param name="ChessType">棋子类型</param>
public Chess(Game gm,int _picid ,int point)
{
Position_Current = point;
string name = "";
switch (picID)
{
case 1: name = "帅"; _chessLevel = Level.帅; _chessType = ChessType.red; break;
case 2: name = "仕"; _chessLevel = Level.士; _chessType = ChessType.red; break;
case 3: name = "相"; _chessLevel = Level.相; _chessType = ChessType.red; break;
case 4: name = "马"; _chessLevel = Level.马; _chessType = ChessType.red; break;
case 5: name = "车"; _chessLevel = Level.车; _chessType = ChessType.red; break;
case 6: name = "炮"; _chessLevel = Level.炮; _chessType = ChessType.red; break;
case 7: name = "兵"; _chessLevel = Level.兵; _chessType = ChessType.red; break;
//
case 8: name = "将"; _chessLevel = Level.帅; _chessType = ChessType.blue; break;
case 9: name = "士"; _chessLevel = Level.士; _chessType = ChessType.blue; break;
case 10: name = "象"; _chessLevel = Level.相; _chessType = ChessType.blue; break;
case 11: name = "码"; _chessLevel = Level.马; _chessType = ChessType.blue; break;
case 12: name = "砗"; _chessLevel = Level.车; _chessType = ChessType.blue; break;
case 13: name = "砲"; _chessLevel = Level.炮; _chessType = ChessType.blue; break;
case 14: name = "卒"; _chessLevel = Level.兵; _chessType = ChessType.blue; break;
}
this.ChessName = name;
// this.LocationOutside = new Point(game.Location.X + Location_Inner.X * Game._unit, game.Location.Y + Location_Inner.Y * Game._unit);
}
#region 棋子属性
/// <summary>
/// 数组中索引ID
/// </summary>
private int _index = 0;
public int index
{
get { return _index; }
set { _index = value; }
}
/// <summary>
/// 棋子阵营
/// </summary>
private ChessType _chessType;
public ChessType chessType
{
get { return _chessType; }
set { _chessType = value; }
}
/// <summary>
/// 棋子级别
/// </summary>
private Level _chessLevel;
public Level chessLevel
{
get { return _chessLevel; }
set { _chessLevel = value; }
}
/// <summary>
/// 显示名称
/// </summary>
private string _chessname = "";
public string ChessName
{
get { return _chessname; }
set { _chessname = value; }
}
/// <summary>
/// 显示棋谱棋子名称,车=砗,码=马,炮=砲
/// </summary>
public string QPChessName()
{
string ch=this._chessname;
switch (ch)
{
case "砗": ch = "车"; break;
case "码": ch = "马"; break;
case "砲": ch = "炮"; break;
}
return ch;
}
/// <summary>
/// 显示尺寸大小
/// </summary>
private int _Width = 36;//36 //48 IniFile.chessSize;
public int Width
{
get { return _Width; }
set { _Width = value; }
}
private int _Heigh =36;// 36 // 48;IniFile.chessSize;
public int Heigh
{
get { return _Heigh; }
set { _Heigh = value; }
}
/// <summary>
/// 显示在游戏中PANEL中的位置
/// </summary>
private Point _locationOutside;
public Point LocationOutside
{
get {
return new Point(game.Location.X + _location_Inner.X * Game._unit, game.Location.Y +_location_Inner.Y * Game._unit);
}
set { _locationOutside = value; }
}
/// <summary>
/// 内部位置Grid View
/// </summary>
private Point _location_Inner;
public Point Location_Inner
{
get { return _location_Inner; }
set { _location_Inner = value; }
}
/// <summary>
/// FIRST位置 原先内部位置
/// </summary>
private Point _initialPoint;
public Point InitialPoint
{
get { return _initialPoint; }
set { _initialPoint = value; }
}
/// <summary>
/// 移动前位置 16X16=256大图中的Map 位置 INT 51开始 至203 =51+X+16*Y
/// </summary>
private int _position_previous;
public int Position_Previous
{
get { return _position_previous; }
set { _position_previous = value; }
}
/// <summary>
/// 移动后的位置 16X16=256大图中的Map 位置 INT 51开始 至203 =51+X+16*Y
/// </summary>
private int _Position_current;
public int Position_Current
{
get { return _Position_current; }
set
{
_Position_current = value;
_location_Inner.X = (_Position_current - 51) % 16;
_location_Inner.Y = (_Position_current - 51) / 16;
}
}
/// <summary>
/// 图片ID
/// </summary>
private int _picID = 0;
public int picID
{
get { return _picID; }
set { _picID = value; }
}
/// <summary>
/// 激活准备走棋
/// </summary>
private bool _focus = false;
public bool Focus
{
get { return _focus; }
set { _focus = value; }
}
#endregion
#region/// 棋子移动规则
/// <summary>
/// 棋子移动规则
/// </summary>
/// <returns></returns>
public bool Move(int destination)
{
if (destination == Position_Current) { return false; }// System.Windows.Forms.MessageBox.Show("FFF");
if (Map.InBoard[destination]==0) { return false; }
bool isMove = false;
switch (this.chessLevel)
{
case Level.帅:
isMove = SuaiMove(destination);
break;
case Level.士:
isMove = ShiMove(destination);
break;
case Level.相:
isMove = XiangMove(destination);
break;
case Level.马:
isMove = MaMove(destination);
break;
case Level.车:
isMove = CheMove(destination);
break;
case Level.炮:
isMove = PaoMove(destination);
break;
case Level.兵:
isMove = BingMove(destination);
break;
}
if (isMove)
{
//if (CheckKingFaceToFace(destination))
//{ isMove = false; }
}
return isMove;
}
private bool CheckKingFaceToFace(int newStep)
{
Map.map = Map.map;
int temp = Map.map[newStep];
Map.map[Position_Current] = 0;
Map.map[newStep] = picID;
int king1P = 0, king2P = 0;
for (int k = 54; k < 89; k++)
{
if (k % 16 > 8 || k % 16 < 6) continue;
if (Map.map[k] == 1|| Map.map[k] == 8)
{
king1P = k; break;
}
}
for (int k = 166; k < 201; k++)
{
if (k % 16 > 8 || k % 16 < 6) continue;
if (Map.map[k] == 1 || Map.map[k] == 8)
{
king2P = k; break;
}
}
if (king1P % 16 == king2P % 16)
{
if (GetChessCount(king1P, king2P) == 1)
{
System.Windows.Forms.MessageBox.Show("帅将面对面了,请更换棋步!");
Map.map[newStep] = temp;
Map.map[Position_Current] = picID;
return true;
}
}
return false;
}
/// <summary>
/// 帅移动
/// </summary>
/// <returns></returns>
private bool SuaiMove(int destination)
{ //(帅)九宫格
if(!DeltaTable.inFort(destination))return false ;
foreach (int v in DeltaTable.KingDelta)
{
if (_Position_current + v == destination)
{
return true;
}
}
return false;
}
/// <summary>
/// 士移动
/// </summary>
/// <returns></returns>
private bool ShiMove(int destination)
{
//“士”不能出城
if (!DeltaTable.inFort(destination)) return false;
//“士”每次只能走一步且只能是斜线
foreach (int v in DeltaTable.AdvisorDelta)
{
if (_Position_current + v == destination)
{
return true;
}
}
return false;
}
/// <summary>
/// 相移动
/// </summary>
/// <returns></returns>
private bool XiangMove(int destination)
{ //移动范围限制,双方(相)不能过河
//绝对值法
int r = 0;
foreach (int v in DeltaTable.BishopDelta)
{
if (_Position_current + v == destination && destination / 124 == _Position_current /124)
{
int p = _Position_current + DeltaTable.AdvisorDelta[r];
if (Map.map[p] == 0)
{
return true;
}
}
r++;
}
return false;
}
/// <summary>
/// 马移动
/// </summary>
/// <returns></returns>
private bool MaMove(int destination)
{
//蹩脚算法
for (int r = 0; r < 4; r++)
{
for (int c = 0; c < 2; c++)
{
int v = DeltaTable.KnightDelta[r, c];
if (_Position_current + v == destination)
{
int p = _Position_current + DeltaTable.KingDelta[r];
if (Map.map[p] == 0)
{
return true;
}
}
}
}
return false;
}
/// <summary>
/// 车移动
/// </summary>
/// <returns></returns>
private bool CheMove(int destination)
{ //(车)直走
//两点在一条直线上
//中间不能有棋子
int COUNT = GetChessCount(_Position_current, destination);
if (_Position_current % 16 == destination % 16 || _Position_current / 16 == destination / 16)
{
if (COUNT == 0)
{
return true;
}
//中间有1棋子,目的地有1棋子
if (COUNT == 1 && Map.map[destination] > 0)
{
return true;
}
}
return false;
}
/// <summary>
/// 炮移动
/// </summary>
/// <returns></returns>
private bool PaoMove(int destination)
{
//两点在一条直线上 //中间不能有棋子
int COUNT = GetChessCount(_Position_current, destination);
if (_Position_current % 16 == destination % 16 || _Position_current / 16 == destination / 16)
{
if (COUNT == 0 && Map.map[destination]==0)
{
return true;
}
//中间有1棋子,目的地有1棋子
if (COUNT == 2 && Map.map[destination]>0)
{
return true;
}
}
目标点不为空,中间只能由一个棋子
return false;
}
/// <summary>
/// 兵移动
/// </summary>
/// <returns></returns>
/// <summary>
private bool BingMove(int destination)
{
if (chessType==ChessType.red)
{
if (destination > 130)
{
if (destination - _Position_current == -16)
{
return true;
}
}
else
{
if (destination - _Position_current == -16 || Math.Abs(destination - _Position_current) == 1)
{
return true;
}
}
}
else
{
if (destination < 130)
{
if (destination - _Position_current == 16)
{
return true;
}
}
else
{
if (destination - _Position_current == 16 || Math.Abs(destination - _Position_current) == 1)
{
return true;
}
}
}
return false;
}
#endregion
public Point QPpoint(Point _initialPoint0, Point _location_Inner1, bool moved)
{
Point Qp;Point tarGet;
if(moved)
{
tarGet=_location_Inner1;
}
else
{
tarGet=_initialPoint0;
}
if (chessType == ChessType.blue)
{
Qp = new Point(1 +tarGet.X,tarGet.Y);
}
else
{
Qp = new Point(9 -tarGet.X, 9 -tarGet.Y);
}
return Qp;
}
public bool MoveTo(int dest)
{
/被吃掉棋子,或被保护的棋子""
// BeEatenChess = GetName(game.gameView[ChessY,ChessX]);
Map.map[Position_Current] = 0;
_position_previous = _Position_current;
//标记移动后的VIEW图,0和新值
//game.gameView[_location_Inner.Y, _location_Inner.X] = 0;
//移动**************************************************************************************
Position_Current=dest;
//移动**************************************************************************************
Map.map[Position_Current]=picID;
//System.Windows.Forms.MessageBox.Show( game.ChessArr[5].Text);
return true;
}
/// <summary>
/// 获取两点之间的棋子数
/// </summary>
public int GetChessCount(int startAddr, int endAddr)
{
int count =0 ;//自己一个棋子,减1
//如果X相同
if (startAddr % 16 == endAddr % 16)
{
//获取最大X和最小X值
int min = Math.Min(startAddr, endAddr);
int max = Math.Max(startAddr, endAddr);
//棋子计数器
for (int i = min; i < max + 1; i = i + 16)
{
if (Map.map[i] > 0 && i != startAddr)//view 反向
{
count++;
if (count > 2) break;
}
}
}
if (startAddr / 16 == endAddr / 16)
{
//获取最大X和最小X值
int min = Math.Min(startAddr, endAddr);
int max = Math.Max(startAddr, endAddr);
//棋子计数器
for (int i = min; i < max + 1; i = i + 1)
{
if (Map.map[i] > 0 && i != startAddr)//view 反向
{ count++; if (count > 2) break; }
}
}
if (startAddr / 16 == endAddr / 16 && startAddr % 16 == endAddr % 16)
{
count = 99;
}
// System.Windows.Forms.MessageBox.Show(count.ToString());
return count;
}
public int FindotherChess_sameX(Chess theChess)
{
for (int y = theChess.Position_Current % 16; y < 204; y=y+16)
{
if (Map.map[y] == theChess.picID && theChess.Position_Current != y)
{
//MessageBox.Show(theChess.ChessName+ c.ToString(), "提示");
return y;
}
}
return -1;//没找到!
}
public List<string> allMoves = new List<string>();
#region 所有可以走的棋步
/// <summary>
/// 所有可以走的棋步
/// </summary>
/// <returns></returns>
public void CanMoveSteps()
{
allMoves.Clear();
switch (chessLevel)
{
case Level.帅: MoveStep_Suai(); return;
case Level.车: MoveStep_Che(); return;
case Level.炮: MoveStep_Pao(); return;
case Level.兵: MoveStep_Bing(); return;
case Level.士: MoveStep_Shi(); return;
case Level.相: MoveStep_Xiang(); return;
case Level.马: MoveStep_Ma(); return;
}
// return null;
}
private bool isSameChess(int pos)
{
if (Map.map[pos] / 8 == picID / 8 && Map.map[pos] != 0)
{
//或被保护的棋子""
return true;
}
else
{
/被吃掉棋子,
return false;
}
}
private string MoveStep_Suai()
{
Chess ess = new Chess(game, chessType, chessLevel, _location_Inner);
string steps = "";
foreach (int v in DeltaTable.KingDelta)
{
int destination = _Position_current + v;
if(isSameChess(destination)){continue;}
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
//allMoves.Add(chessRecord.QPstep(ess, ess.Location_Inner, new Point(destination%16-3, destination/16-3))) ;
}
}
ess = null;
return steps;
}
private string MoveStep_Shi()
{
Chess ess = new Chess(game, chessType, chessLevel, _location_Inner);
string steps = "";
foreach (int v in DeltaTable.AdvisorDelta)
{
int destination = _Position_current + v;
if (isSameChess(destination)) { continue; }
if (ess.Move(destination))
{
//allMoves.Add(chessRecord.QPstep(ess, ess.Location_Inner, new Point(destination%16-3, destination/16-3))) ;
allMoves.Add(ess.Position_Current + ">" + destination);
}
}
ess = null;
return steps;
}
private string MoveStep_Xiang()
{
Chess ess = new Chess(game, chessType, chessLevel, _location_Inner);
string steps = "";
foreach (int v in DeltaTable.BishopDelta)
{
int destination = _Position_current + v;
if (isSameChess(destination)) { continue; }
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current + ">" + destination);
}
}
ess = null;
return steps;
}
private string MoveStep_Ma()
{
Chess ess = new Chess(game, chessType, chessLevel, _location_Inner);
string steps = "";
foreach (int v in DeltaTable.KnightDelta)
{
int destination = _Position_current + v;
if (isSameChess(destination)) { continue; }
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current + ">" + destination);
}
}
ess = null;
return steps;
}
private string MoveStep_Che()
{
Chess ess = new Chess(game, chessType, chessLevel, _location_Inner);
string steps = "";
int destination = 0; int p = 0; int chesscount = 0;
p = ess.Position_Current % 16;
chesscount = 0;
while (true)
{
if (p > 3)
{
p--;
destination = (ess.Position_Current / 16) * 16 + p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 1) { break; }
// System.Windows.Forms.MessageBox.Show(destination + "+");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
chesscount = 0; p = ess.Position_Current % 16;
while (true)
{
if (p < 11)
{
p++;
destination = (ess.Position_Current / 16) * 16 + p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 1) { break; }
// System.Windows.Forms.MessageBox.Show(destination + "-");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
chesscount = 0; p = ess.Position_Current / 16;
while (true)
{
if (p < 12)
{
p++;
destination = (ess.Position_Current % 16) + 16 * p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 1) { break; }
// System.Windows.Forms.MessageBox.Show(destination + "*");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
chesscount = 0; p = ess.Position_Current / 16;
while (true)
{
if (p > 3)
{
p--;
destination = (ess.Position_Current % 16) + 16 * p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 1)
{ break; }
// System.Windows.Forms.MessageBox.Show(destination + "//");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
ess = null;
return steps;
}
private string MoveStep_Pao()
{
Chess ess = new Chess(game, chessType, chessLevel, _location_Inner);
string steps = "";
int destination = 0; int p = 0; int chesscount = 0;
p = ess.Position_Current % 16;
chesscount = 0;
while (true)
{
if (p > 3)
{
p--;
destination = (ess.Position_Current / 16) * 16 + p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 2) { break; }
// System.Windows.Forms.MessageBox.Show(destination + "+");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
chesscount = 0; p = ess.Position_Current % 16;
while (true)
{
if (p < 11)
{
p++;
destination = (ess.Position_Current / 16) * 16 + p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 2) { break; }
// System.Windows.Forms.MessageBox.Show(destination + "-");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
chesscount = 0; p = ess.Position_Current / 16;
while (true)
{
if (p < 12)
{
p++;
destination = (ess.Position_Current % 16) + 16 * p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 2) { break; }
// System.Windows.Forms.MessageBox.Show(destination + "*");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
chesscount = 0; p = ess.Position_Current / 16;
while (true)
{
if (p > 3)
{
p--;
destination = (ess.Position_Current % 16) + 16 * p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 2){ break; }
// System.Windows.Forms.MessageBox.Show(destination + "//");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
ess = null;
return steps;
}
private string MoveStep_Bing()
{
Chess ess = new Chess(game, chessType, chessLevel, _location_Inner);
string steps = "";
foreach (int v in DeltaTable.PawnDelta)
{
int destination = _Position_current + v;
if (isSameChess(destination)) { continue; }
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
ess = null;
return steps;
}
#endregion 所有可以走的棋步
}
}
using System.Drawing;
using System.Collections.Generic;
namespace 象棋启蒙
{
/// <summary>
/// ChessItemJu 的摘要说明。
/// </summary>
public class Chess
{
private Game game = null;
private ChessRecord chessRecord = null;
public string BeEatenChess = "";
///<summary>
///棋类
///<summary>
///创建一个棋子
///</summary>
///<param name="location">左上角位置</param>
///<param name="ChessType">棋子类型</param>
public Chess(Game gm,ChessType type0,Level level0,Point location0 )
{
chessRecord = new ChessRecord(gm);
game = gm;
chessType = type0;
chessLevel = level0;
Location_Inner = location0;
string ch = "";
switch (level0)
{
case Level.帅:
ch = (type0 == ChessType.red) ? "帅" : "将"; picID = (chessType == ChessType.red) ? 1 : 8;
break;
case Level.士:
ch = (type0 == ChessType.red) ? "仕" : "士"; picID = (chessType == ChessType.red) ? 2 : 9;
break;
case Level.相:
ch = (type0 == ChessType.red) ? "相" : "象"; picID = (chessType == ChessType.red) ? 3 : 10;
break;
case Level.马:
ch = (type0 == ChessType.red) ? "马" : "码"; picID = (chessType == ChessType.red) ? 4 : 11;
break;
case Level.车:
ch = (type0 == ChessType.red) ? "车" : "砗"; picID = (chessType == ChessType.red) ? 5 : 12;
break;
case Level.炮:
ch = (type0 == ChessType.red) ? "炮" : "砲"; picID = (chessType == ChessType.red) ? 6 : 13;
break;
case Level.兵:
ch = (type0 == ChessType.red) ? "兵" : "卒"; picID = (chessType == ChessType.red) ? 7 : 14;
break;
}
this.ChessName =ch.ToString();
this.LocationOutside = new Point(game.Location.X + Location_Inner.X *Game._unit,game.Location.Y + Location_Inner.Y *Game._unit);
Position_Current = _location_Inner.X + 51 + 16 * _location_Inner.Y;
}
/<summary>
/创建一个棋子
/</summary>
/<param name="location">左上角位置</param>
/<param name="ChessType">棋子类型</param>
public Chess(Game gm,int _picid ,int point)
{
Position_Current = point;
string name = "";
switch (picID)
{
case 1: name = "帅"; _chessLevel = Level.帅; _chessType = ChessType.red; break;
case 2: name = "仕"; _chessLevel = Level.士; _chessType = ChessType.red; break;
case 3: name = "相"; _chessLevel = Level.相; _chessType = ChessType.red; break;
case 4: name = "马"; _chessLevel = Level.马; _chessType = ChessType.red; break;
case 5: name = "车"; _chessLevel = Level.车; _chessType = ChessType.red; break;
case 6: name = "炮"; _chessLevel = Level.炮; _chessType = ChessType.red; break;
case 7: name = "兵"; _chessLevel = Level.兵; _chessType = ChessType.red; break;
//
case 8: name = "将"; _chessLevel = Level.帅; _chessType = ChessType.blue; break;
case 9: name = "士"; _chessLevel = Level.士; _chessType = ChessType.blue; break;
case 10: name = "象"; _chessLevel = Level.相; _chessType = ChessType.blue; break;
case 11: name = "码"; _chessLevel = Level.马; _chessType = ChessType.blue; break;
case 12: name = "砗"; _chessLevel = Level.车; _chessType = ChessType.blue; break;
case 13: name = "砲"; _chessLevel = Level.炮; _chessType = ChessType.blue; break;
case 14: name = "卒"; _chessLevel = Level.兵; _chessType = ChessType.blue; break;
}
this.ChessName = name;
// this.LocationOutside = new Point(game.Location.X + Location_Inner.X * Game._unit, game.Location.Y + Location_Inner.Y * Game._unit);
}
#region 棋子属性
/// <summary>
/// 数组中索引ID
/// </summary>
private int _index = 0;
public int index
{
get { return _index; }
set { _index = value; }
}
/// <summary>
/// 棋子阵营
/// </summary>
private ChessType _chessType;
public ChessType chessType
{
get { return _chessType; }
set { _chessType = value; }
}
/// <summary>
/// 棋子级别
/// </summary>
private Level _chessLevel;
public Level chessLevel
{
get { return _chessLevel; }
set { _chessLevel = value; }
}
/// <summary>
/// 显示名称
/// </summary>
private string _chessname = "";
public string ChessName
{
get { return _chessname; }
set { _chessname = value; }
}
/// <summary>
/// 显示棋谱棋子名称,车=砗,码=马,炮=砲
/// </summary>
public string QPChessName()
{
string ch=this._chessname;
switch (ch)
{
case "砗": ch = "车"; break;
case "码": ch = "马"; break;
case "砲": ch = "炮"; break;
}
return ch;
}
/// <summary>
/// 显示尺寸大小
/// </summary>
private int _Width = 36;//36 //48 IniFile.chessSize;
public int Width
{
get { return _Width; }
set { _Width = value; }
}
private int _Heigh =36;// 36 // 48;IniFile.chessSize;
public int Heigh
{
get { return _Heigh; }
set { _Heigh = value; }
}
/// <summary>
/// 显示在游戏中PANEL中的位置
/// </summary>
private Point _locationOutside;
public Point LocationOutside
{
get {
return new Point(game.Location.X + _location_Inner.X * Game._unit, game.Location.Y +_location_Inner.Y * Game._unit);
}
set { _locationOutside = value; }
}
/// <summary>
/// 内部位置Grid View
/// </summary>
private Point _location_Inner;
public Point Location_Inner
{
get { return _location_Inner; }
set { _location_Inner = value; }
}
/// <summary>
/// FIRST位置 原先内部位置
/// </summary>
private Point _initialPoint;
public Point InitialPoint
{
get { return _initialPoint; }
set { _initialPoint = value; }
}
/// <summary>
/// 移动前位置 16X16=256大图中的Map 位置 INT 51开始 至203 =51+X+16*Y
/// </summary>
private int _position_previous;
public int Position_Previous
{
get { return _position_previous; }
set { _position_previous = value; }
}
/// <summary>
/// 移动后的位置 16X16=256大图中的Map 位置 INT 51开始 至203 =51+X+16*Y
/// </summary>
private int _Position_current;
public int Position_Current
{
get { return _Position_current; }
set
{
_Position_current = value;
_location_Inner.X = (_Position_current - 51) % 16;
_location_Inner.Y = (_Position_current - 51) / 16;
}
}
/// <summary>
/// 图片ID
/// </summary>
private int _picID = 0;
public int picID
{
get { return _picID; }
set { _picID = value; }
}
/// <summary>
/// 激活准备走棋
/// </summary>
private bool _focus = false;
public bool Focus
{
get { return _focus; }
set { _focus = value; }
}
#endregion
#region/// 棋子移动规则
/// <summary>
/// 棋子移动规则
/// </summary>
/// <returns></returns>
public bool Move(int destination)
{
if (destination == Position_Current) { return false; }// System.Windows.Forms.MessageBox.Show("FFF");
if (Map.InBoard[destination]==0) { return false; }
bool isMove = false;
switch (this.chessLevel)
{
case Level.帅:
isMove = SuaiMove(destination);
break;
case Level.士:
isMove = ShiMove(destination);
break;
case Level.相:
isMove = XiangMove(destination);
break;
case Level.马:
isMove = MaMove(destination);
break;
case Level.车:
isMove = CheMove(destination);
break;
case Level.炮:
isMove = PaoMove(destination);
break;
case Level.兵:
isMove = BingMove(destination);
break;
}
if (isMove)
{
//if (CheckKingFaceToFace(destination))
//{ isMove = false; }
}
return isMove;
}
private bool CheckKingFaceToFace(int newStep)
{
Map.map = Map.map;
int temp = Map.map[newStep];
Map.map[Position_Current] = 0;
Map.map[newStep] = picID;
int king1P = 0, king2P = 0;
for (int k = 54; k < 89; k++)
{
if (k % 16 > 8 || k % 16 < 6) continue;
if (Map.map[k] == 1|| Map.map[k] == 8)
{
king1P = k; break;
}
}
for (int k = 166; k < 201; k++)
{
if (k % 16 > 8 || k % 16 < 6) continue;
if (Map.map[k] == 1 || Map.map[k] == 8)
{
king2P = k; break;
}
}
if (king1P % 16 == king2P % 16)
{
if (GetChessCount(king1P, king2P) == 1)
{
System.Windows.Forms.MessageBox.Show("帅将面对面了,请更换棋步!");
Map.map[newStep] = temp;
Map.map[Position_Current] = picID;
return true;
}
}
return false;
}
/// <summary>
/// 帅移动
/// </summary>
/// <returns></returns>
private bool SuaiMove(int destination)
{ //(帅)九宫格
if(!DeltaTable.inFort(destination))return false ;
foreach (int v in DeltaTable.KingDelta)
{
if (_Position_current + v == destination)
{
return true;
}
}
return false;
}
/// <summary>
/// 士移动
/// </summary>
/// <returns></returns>
private bool ShiMove(int destination)
{
//“士”不能出城
if (!DeltaTable.inFort(destination)) return false;
//“士”每次只能走一步且只能是斜线
foreach (int v in DeltaTable.AdvisorDelta)
{
if (_Position_current + v == destination)
{
return true;
}
}
return false;
}
/// <summary>
/// 相移动
/// </summary>
/// <returns></returns>
private bool XiangMove(int destination)
{ //移动范围限制,双方(相)不能过河
//绝对值法
int r = 0;
foreach (int v in DeltaTable.BishopDelta)
{
if (_Position_current + v == destination && destination / 124 == _Position_current /124)
{
int p = _Position_current + DeltaTable.AdvisorDelta[r];
if (Map.map[p] == 0)
{
return true;
}
}
r++;
}
return false;
}
/// <summary>
/// 马移动
/// </summary>
/// <returns></returns>
private bool MaMove(int destination)
{
//蹩脚算法
for (int r = 0; r < 4; r++)
{
for (int c = 0; c < 2; c++)
{
int v = DeltaTable.KnightDelta[r, c];
if (_Position_current + v == destination)
{
int p = _Position_current + DeltaTable.KingDelta[r];
if (Map.map[p] == 0)
{
return true;
}
}
}
}
return false;
}
/// <summary>
/// 车移动
/// </summary>
/// <returns></returns>
private bool CheMove(int destination)
{ //(车)直走
//两点在一条直线上
//中间不能有棋子
int COUNT = GetChessCount(_Position_current, destination);
if (_Position_current % 16 == destination % 16 || _Position_current / 16 == destination / 16)
{
if (COUNT == 0)
{
return true;
}
//中间有1棋子,目的地有1棋子
if (COUNT == 1 && Map.map[destination] > 0)
{
return true;
}
}
return false;
}
/// <summary>
/// 炮移动
/// </summary>
/// <returns></returns>
private bool PaoMove(int destination)
{
//两点在一条直线上 //中间不能有棋子
int COUNT = GetChessCount(_Position_current, destination);
if (_Position_current % 16 == destination % 16 || _Position_current / 16 == destination / 16)
{
if (COUNT == 0 && Map.map[destination]==0)
{
return true;
}
//中间有1棋子,目的地有1棋子
if (COUNT == 2 && Map.map[destination]>0)
{
return true;
}
}
目标点不为空,中间只能由一个棋子
return false;
}
/// <summary>
/// 兵移动
/// </summary>
/// <returns></returns>
/// <summary>
private bool BingMove(int destination)
{
if (chessType==ChessType.red)
{
if (destination > 130)
{
if (destination - _Position_current == -16)
{
return true;
}
}
else
{
if (destination - _Position_current == -16 || Math.Abs(destination - _Position_current) == 1)
{
return true;
}
}
}
else
{
if (destination < 130)
{
if (destination - _Position_current == 16)
{
return true;
}
}
else
{
if (destination - _Position_current == 16 || Math.Abs(destination - _Position_current) == 1)
{
return true;
}
}
}
return false;
}
#endregion
public Point QPpoint(Point _initialPoint0, Point _location_Inner1, bool moved)
{
Point Qp;Point tarGet;
if(moved)
{
tarGet=_location_Inner1;
}
else
{
tarGet=_initialPoint0;
}
if (chessType == ChessType.blue)
{
Qp = new Point(1 +tarGet.X,tarGet.Y);
}
else
{
Qp = new Point(9 -tarGet.X, 9 -tarGet.Y);
}
return Qp;
}
public bool MoveTo(int dest)
{
/被吃掉棋子,或被保护的棋子""
// BeEatenChess = GetName(game.gameView[ChessY,ChessX]);
Map.map[Position_Current] = 0;
_position_previous = _Position_current;
//标记移动后的VIEW图,0和新值
//game.gameView[_location_Inner.Y, _location_Inner.X] = 0;
//移动**************************************************************************************
Position_Current=dest;
//移动**************************************************************************************
Map.map[Position_Current]=picID;
//System.Windows.Forms.MessageBox.Show( game.ChessArr[5].Text);
return true;
}
/// <summary>
/// 获取两点之间的棋子数
/// </summary>
public int GetChessCount(int startAddr, int endAddr)
{
int count =0 ;//自己一个棋子,减1
//如果X相同
if (startAddr % 16 == endAddr % 16)
{
//获取最大X和最小X值
int min = Math.Min(startAddr, endAddr);
int max = Math.Max(startAddr, endAddr);
//棋子计数器
for (int i = min; i < max + 1; i = i + 16)
{
if (Map.map[i] > 0 && i != startAddr)//view 反向
{
count++;
if (count > 2) break;
}
}
}
if (startAddr / 16 == endAddr / 16)
{
//获取最大X和最小X值
int min = Math.Min(startAddr, endAddr);
int max = Math.Max(startAddr, endAddr);
//棋子计数器
for (int i = min; i < max + 1; i = i + 1)
{
if (Map.map[i] > 0 && i != startAddr)//view 反向
{ count++; if (count > 2) break; }
}
}
if (startAddr / 16 == endAddr / 16 && startAddr % 16 == endAddr % 16)
{
count = 99;
}
// System.Windows.Forms.MessageBox.Show(count.ToString());
return count;
}
public int FindotherChess_sameX(Chess theChess)
{
for (int y = theChess.Position_Current % 16; y < 204; y=y+16)
{
if (Map.map[y] == theChess.picID && theChess.Position_Current != y)
{
//MessageBox.Show(theChess.ChessName+ c.ToString(), "提示");
return y;
}
}
return -1;//没找到!
}
public List<string> allMoves = new List<string>();
#region 所有可以走的棋步
/// <summary>
/// 所有可以走的棋步
/// </summary>
/// <returns></returns>
public void CanMoveSteps()
{
allMoves.Clear();
switch (chessLevel)
{
case Level.帅: MoveStep_Suai(); return;
case Level.车: MoveStep_Che(); return;
case Level.炮: MoveStep_Pao(); return;
case Level.兵: MoveStep_Bing(); return;
case Level.士: MoveStep_Shi(); return;
case Level.相: MoveStep_Xiang(); return;
case Level.马: MoveStep_Ma(); return;
}
// return null;
}
private bool isSameChess(int pos)
{
if (Map.map[pos] / 8 == picID / 8 && Map.map[pos] != 0)
{
//或被保护的棋子""
return true;
}
else
{
/被吃掉棋子,
return false;
}
}
private string MoveStep_Suai()
{
Chess ess = new Chess(game, chessType, chessLevel, _location_Inner);
string steps = "";
foreach (int v in DeltaTable.KingDelta)
{
int destination = _Position_current + v;
if(isSameChess(destination)){continue;}
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
//allMoves.Add(chessRecord.QPstep(ess, ess.Location_Inner, new Point(destination%16-3, destination/16-3))) ;
}
}
ess = null;
return steps;
}
private string MoveStep_Shi()
{
Chess ess = new Chess(game, chessType, chessLevel, _location_Inner);
string steps = "";
foreach (int v in DeltaTable.AdvisorDelta)
{
int destination = _Position_current + v;
if (isSameChess(destination)) { continue; }
if (ess.Move(destination))
{
//allMoves.Add(chessRecord.QPstep(ess, ess.Location_Inner, new Point(destination%16-3, destination/16-3))) ;
allMoves.Add(ess.Position_Current + ">" + destination);
}
}
ess = null;
return steps;
}
private string MoveStep_Xiang()
{
Chess ess = new Chess(game, chessType, chessLevel, _location_Inner);
string steps = "";
foreach (int v in DeltaTable.BishopDelta)
{
int destination = _Position_current + v;
if (isSameChess(destination)) { continue; }
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current + ">" + destination);
}
}
ess = null;
return steps;
}
private string MoveStep_Ma()
{
Chess ess = new Chess(game, chessType, chessLevel, _location_Inner);
string steps = "";
foreach (int v in DeltaTable.KnightDelta)
{
int destination = _Position_current + v;
if (isSameChess(destination)) { continue; }
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current + ">" + destination);
}
}
ess = null;
return steps;
}
private string MoveStep_Che()
{
Chess ess = new Chess(game, chessType, chessLevel, _location_Inner);
string steps = "";
int destination = 0; int p = 0; int chesscount = 0;
p = ess.Position_Current % 16;
chesscount = 0;
while (true)
{
if (p > 3)
{
p--;
destination = (ess.Position_Current / 16) * 16 + p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 1) { break; }
// System.Windows.Forms.MessageBox.Show(destination + "+");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
chesscount = 0; p = ess.Position_Current % 16;
while (true)
{
if (p < 11)
{
p++;
destination = (ess.Position_Current / 16) * 16 + p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 1) { break; }
// System.Windows.Forms.MessageBox.Show(destination + "-");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
chesscount = 0; p = ess.Position_Current / 16;
while (true)
{
if (p < 12)
{
p++;
destination = (ess.Position_Current % 16) + 16 * p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 1) { break; }
// System.Windows.Forms.MessageBox.Show(destination + "*");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
chesscount = 0; p = ess.Position_Current / 16;
while (true)
{
if (p > 3)
{
p--;
destination = (ess.Position_Current % 16) + 16 * p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 1)
{ break; }
// System.Windows.Forms.MessageBox.Show(destination + "//");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
ess = null;
return steps;
}
private string MoveStep_Pao()
{
Chess ess = new Chess(game, chessType, chessLevel, _location_Inner);
string steps = "";
int destination = 0; int p = 0; int chesscount = 0;
p = ess.Position_Current % 16;
chesscount = 0;
while (true)
{
if (p > 3)
{
p--;
destination = (ess.Position_Current / 16) * 16 + p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 2) { break; }
// System.Windows.Forms.MessageBox.Show(destination + "+");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
chesscount = 0; p = ess.Position_Current % 16;
while (true)
{
if (p < 11)
{
p++;
destination = (ess.Position_Current / 16) * 16 + p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 2) { break; }
// System.Windows.Forms.MessageBox.Show(destination + "-");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
chesscount = 0; p = ess.Position_Current / 16;
while (true)
{
if (p < 12)
{
p++;
destination = (ess.Position_Current % 16) + 16 * p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 2) { break; }
// System.Windows.Forms.MessageBox.Show(destination + "*");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
chesscount = 0; p = ess.Position_Current / 16;
while (true)
{
if (p > 3)
{
p--;
destination = (ess.Position_Current % 16) + 16 * p;
if (isSameChess(destination)) { continue; }
if (Map.map[destination] > 0) { chesscount++; }
if (chesscount > 2){ break; }
// System.Windows.Forms.MessageBox.Show(destination + "//");
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
else
{
break;
}
}
ess = null;
return steps;
}
private string MoveStep_Bing()
{
Chess ess = new Chess(game, chessType, chessLevel, _location_Inner);
string steps = "";
foreach (int v in DeltaTable.PawnDelta)
{
int destination = _Position_current + v;
if (isSameChess(destination)) { continue; }
if (ess.Move(destination))
{
allMoves.Add(ess.Position_Current+">"+destination);
}
}
ess = null;
return steps;
}
#endregion 所有可以走的棋步
}
}