1.棋子基类
抽象类,是所有棋子的基类, 注意几个重要的方法:如CanMoveTo移动算法及MoveTo移动棋子。
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using www.vjsdn.com.ChineseChess.Library;
namespace www.vjsdn.com.ChineseChess.Library
{
public abstract class Chess
{
protected ChessColor _color;
protected ChessPoint _currentPoint;
protected ChessBoard _chessboard;
public Chess(ChessColor color, ChessPoint tragpoint, ChessBoard board)
{
this._color = color;
this._currentPoint = tragpoint;
this._chessboard = board;
}
public ChessColor Color
{
get { return _color; }
set { _color = value; }
}
public ChessPoint CurrentPoint
{
get { return _currentPoint; }
set { _currentPoint = value; }
}
public ChessBoard Chessboard
{
get { return _chessboard; }
set { _chessboard = value; }
}
public abstract Image ChessImage { get;}
protected abstract bool CanMoveTo(ChessPoint p);
public void MoveTo(ChessPoint targetPoint)
{
Chess targetChess = _chessboard[targetPoint];
if (targetChess != null && targetChess.Color == this._color) return;
if (!CanMoveTo(targetPoint)) return;
if (_chessboard[targetPoint] is King)
throw new GameLoseException( this.Color == ChessColor.Red ? "红方胜" : "黑方胜");
_chessboard[_currentPoint] = null;
_chessboard[targetPoint] = this;
this._currentPoint = targetPoint;
}
public int GetChessCount(ChessPoint start, ChessPoint end)
{
if (start.Y == end.Y)
{
int min = Math.Min(start.X, end.X);
int max = Math.Max(start.X, end.X);
int count = 0;
for ( int i = min + 1; i < max; i++)
{
if (_chessboard[i, start.Y] != null)
count++;
}
return count;
}
else
{
int min = Math.Min(start.Y, end.Y);
int max = Math.Max(start.Y, end.Y);
int count = 0;
for ( int i = min + 1; i < max; i++)
{
if (_chessboard[start.X, i] != null)
count++;
}
return count;
}
}
}
}
2.车(Rooks)
学习车移动算法CanMoveTo. 移动规则: 1.两点在一条直线上 ,2.中间不能有棋子
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace www.vjsdn.com.ChineseChess.Library
{
public class Rooks : Chess
{
public Rooks(ChessColor color, ChessPoint tragPoint, ChessBoard board)
: base(color, tragPoint, board)
{
}
public override Image ChessImage
{
get
{
if (_color == ChessColor.Red)
return ImageHelper.GetImageByAverageIndex(ChineseChess.Res.Properties.Resources.xchess, 14, 1);
else //黑车
return ImageHelper.GetImageByAverageIndex(ChineseChess.Res.Properties.Resources.xchess, 14, 8);
}
}
protected override bool CanMoveTo(ChessPoint targetPoint)
{
if (targetPoint.X != _currentPoint.X && targetPoint.Y != _currentPoint.Y)
return false;
if (GetChessCount(_currentPoint, targetPoint) > 0)
return false;
return true;
}
}
}
3.马(Knights)
学习马移动算法
移动规则:蹩脚算法,直线一侧不能有棋子
using System;
using System.Collections.Generic;
using System.Text;
namespace www.vjsdn.com.ChineseChess.Library
{
public class Knights : Chess
{
public Knights(ChessColor color, ChessPoint tragPoint, ChessBoard board)
: base(color, tragPoint, board)
{
}
public override System.Drawing.Image ChessImage
{
get
{
if ( this._color == ChessColor.Red)
return ImageHelper.GetImageByAverageIndex(ChineseChess.Res.Properties.Resources.xchess, 14, 2);
return ImageHelper.GetImageByAverageIndex(ChineseChess.Res.Properties.Resources.xchess, 14, 9);
}
}
protected override bool CanMoveTo(ChessPoint p)
{
if (Math.Abs(_currentPoint.X - p.X) == 2 && Math.Abs(_currentPoint.Y - p.Y) == 1)
{
if (_chessboard[(_currentPoint.X + p.X) / 2, _currentPoint.Y] == null)
return true;
}
if (Math.Abs(_currentPoint.X - p.X) == 1 && Math.Abs(_currentPoint.Y - p.Y) == 2)
{
if (_chessboard[_currentPoint.X, (_currentPoint.Y + p.Y) / 2] == null)
return true;
}
return false;
}
}
}
4.象/相(Elephants) 学习移动算法
移动规则:1.田子中间不能有棋子 2.不能过河
using System;
using System.Collections.Generic;
using System.Text;
namespace www.vjsdn.com.ChineseChess.Library
{
public class Elephants : Chess
{
public Elephants(ChessColor color, ChessPoint p, ChessBoard board)
: base(color, p, board)
{
}
public override System.Drawing.Image ChessImage
{
get
{
if ( this._color == ChessColor.Red)
return ImageHelper.GetImageByAverageIndex(ChineseChess.Res.Properties.Resources.xchess, 14, 6);
return ImageHelper.GetImageByAverageIndex(ChineseChess.Res.Properties.Resources.xchess, 14, 13);
}
}
protected override bool CanMoveTo(ChessPoint p)
{
if (Math.Abs(_currentPoint.X - p.X) != 2 || Math.Abs(_currentPoint.Y - p.Y) != 2)
return false;
if (_chessboard[(_currentPoint.X + p.X) / 2, (_currentPoint.Y + p.Y) / 2] != null)
return false;
if (_currentPoint.Y <= 4 && p.Y > 4) return false;
if (_currentPoint.Y >= 5 && p.Y < 5) return false;
return true;
}
}
}
5.士/仕(Mandarins)
移动规则:1.“士”不能出城 2.“士”每次只能走一步且只能是斜线
using System;
using System.Collections.Generic;
using System.Text;
using www.vjsdn.com.ChineseChess.Library;
namespace www.vjsdn.com.ChineseChess.Library
{
public class Mandarins : Chess
{
public Mandarins(ChessColor color, ChessPoint tragPoint, ChessBoard board)
: base(color, tragPoint, board)
{
}
public override System.Drawing.Image ChessImage
{
get
{
if ( this._color == ChessColor.Red)
return ImageHelper.GetImageByAverageIndex(ChineseChess.Res.Properties.Resources.xchess, 14, 5);
return ImageHelper.GetImageByAverageIndex(ChineseChess.Res.Properties.Resources.xchess, 14, 12);
}
}
protected override bool CanMoveTo(ChessPoint tragPoint)
{
if (!((tragPoint.X >= 3 && tragPoint.X <= 5) && (tragPoint.Y <= 2 || tragPoint.Y >= 7)))
return false;
if (!(Math.Abs(tragPoint.X - this._currentPoint.X) == 1 && Math.Abs(tragPoint.Y - this._currentPoint.Y) == 1))
return false;
return true;
}
}
}
6.将/帅(King)
移动规则: 1.“将”不能出城 2.“将”每次只能走一步
using System;
using System.Collections.Generic;
using System.Text;
using www.vjsdn.com.ChineseChess.Library;
namespace www.vjsdn.com.ChineseChess.Library
{
public class King : Chess
{
public King(ChessColor color, ChessPoint tragPoint, ChessBoard board)
: base(color, tragPoint, board)
{
}
public override System.Drawing.Image ChessImage
{
get
{
if ( this._color == ChessColor.Red)
return ImageHelper.GetImageByAverageIndex(ChineseChess.Res.Properties.Resources.xchess, 14, 4);
return ImageHelper.GetImageByAverageIndex(ChineseChess.Res.Properties.Resources.xchess, 14, 11);
}
}
protected override bool CanMoveTo(ChessPoint tragPoint)
{
if (!((tragPoint.X >= 3 && tragPoint.X <= 5) && (tragPoint.Y <= 2 || tragPoint.Y >= 7)))
return false;
if ((Math.Abs(tragPoint.X - this._currentPoint.X) + Math.Abs(tragPoint.Y - this._currentPoint.Y)) != 1)
return false;
return true;
}
}
}
7.炮/砲(Connons)
移动规则: 1.两点在一条直线上 2.目标点不为空,中间只能由一个棋子 3.目标点为空,中间不能有棋子
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace www.vjsdn.com.ChineseChess.Library
{
public class Cannons : Chess
{
public Cannons(ChessColor color, ChessPoint tragPoint, ChessBoard board)
: base(color, tragPoint, board)
{
}
public override Image ChessImage
{
get
{
if (_color == ChessColor.Red)
return ImageHelper.GetImageByAverageIndex(ChineseChess.Res.Properties.Resources.xchess, 14, 3);
return ImageHelper.GetImageByAverageIndex(ChineseChess.Res.Properties.Resources.xchess, 14, 10);
}
}
protected override bool CanMoveTo(ChessPoint targPoint)
{
if (targPoint.X != _currentPoint.X && targPoint.Y != _currentPoint.Y)
return false;
if (_chessboard[targPoint] != null && GetChessCount(_currentPoint, targPoint) != 1)
return false;
if (_chessboard[targPoint] == null && GetChessCount(_currentPoint, targPoint) > 0)
return false;
return true;
}
}
}
8.兵/卒 (Pawns)
移动规则:1.兵不能往后走 2.兵一步一步走 3.过河前只能向前走
using System;
using System.Collections.Generic;
using System.Text;
using www.vjsdn.com.ChineseChess.Library;
namespace www.vjsdn.com.ChineseChess.Library
{
public class Pawns : Chess
{
private bool _isRiverd = false;
private int _step = 0;
public Pawns(ChessColor color, ChessPoint tragPoint, ChessBoard board)
: base(color, tragPoint, board)
{
if (_currentPoint.Y > 4)
_step = -1;
else
_step = 1;
}
public override System.Drawing.Image ChessImage
{
get
{
if ( this._color == ChessColor.Red)
return ImageHelper.GetImageByAverageIndex(ChineseChess.Res.Properties.Resources.xchess, 14, 7);
return ImageHelper.GetImageByAverageIndex(ChineseChess.Res.Properties.Resources.xchess, 14, 14);
}
}
protected override bool CanMoveTo(ChessPoint p)
{
if (p.Y - _currentPoint.Y == -_step)
return false;
if (Math.Abs(_currentPoint.X - p.X) + Math.Abs(_currentPoint.Y - p.Y) != 1)
return false;
if (!_isRiverd)
{
if (_currentPoint.Y == 4 && p.Y == 5) _isRiverd = true;
if (_currentPoint.Y == 5 && p.Y == 4) _isRiverd = true;
}
if (!_isRiverd)
{
if (p.Y - _currentPoint.Y != _step)
return false;
}
return true;
}
}
}