下面是标志类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace ChineseChess
{
public class Flag
{
public Flag(ChessBoard cb)
{
this.ChessBoard = cb;
}
public ChessBoard ChessBoard { get; set; }
public Point LastPoint { get; set; }
public APiece FocusPiece { get; set; }
public void Draw(System.Drawing.Graphics g)
{
if (FocusPiece != null)
{
var p = new Pen(Color.DarkBlue);
p.DashStyle = DashStyle.Dash;
foreach (var pts in this.GetLinePoint(this.LastPoint))
{
g.DrawLines(p, pts);
}
p.DashStyle = DashStyle.Solid;
foreach (var pts in this.GetLinePoint(this.FocusPiece.Location))
{
g.DrawLines(p, pts);
}
}
}
private List<Point[]> GetLinePoint(Point pt)
{
var p = new Point(pt.X * this.ChessBoard.BoxLength, pt.Y * this.ChessBoard.BoxLength);
var r = this.ChessBoard.Radio;
var l = new List<Point[]>();
l.Add(new Point[] { new Point(p.X - r, p.Y - r / 2), new Point(p.X - r, p.Y - r), new Point(p.X - r / 2, p.Y - r) });
l.Add(new Point[] { new Point(p.X - r, p.Y + r / 2), new Point(p.X - r, p.Y + r), new Point(p.X - r / 2, p.Y + r) });
l.Add(new Point[] { new Point(p.X + r, p.Y - r / 2), new Point(p.X + r, p.Y - r), new Point(p.X + r / 2, p.Y - r) });
l.Add(new Point[] { new Point(p.X + r, p.Y + r / 2), new Point(p.X + r, p.Y + r), new Point(p.X + r / 2, p.Y + r) });
return l;
}
}
}
最后是棋盘类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace ChineseChess
{
public class ChessBoard
{
public ChessBoard()
{
this.Radio = 25;
this.BoxLength = 55;
this.IsRed = true;
this.BackColor = Color.White;
PieceList = new List<APiece>();
Flag = new Flag(this);
this.StartStep += new EventHandler(ChessBoard_StartStep);
this.EndStep += new EventHandler(ChessBoard_EndStep);
this.OverGame += new EventHandler(ChessBoard_OverGame);
this.InitPiece();
}
/// <summary>
/// 判定游戏结束
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ChessBoard_OverGame(object sender, EventArgs e)
{
}
/// <summary>
/// 每一步的结束触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ChessBoard_EndStep(object sender, EventArgs e)
{
_selectpiece = null;
}
/// <summary>
/// 每一步开始时触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ChessBoard_StartStep(object sender, EventArgs e)
{
Flag.LastPoint = ((APiece)sender).Location;
Flag.FocusPiece = ((APiece)sender);
}
private void InitPiece()
{
PieceList.AddRange(new APiece[] {
//将
new RBoss(this){Location=new Point(5,1)},
new BBoss(this){Location=new Point(5,10)},
//士
new RShi(this){Location=new Point(4,1)},
new RShi(this){Location=new Point(6,1)},
new BShi(this){Location=new Point(4,10)},
new BShi(this){Location=new Point(6,10)},
//象
new RXiang(this){Location=new Point(3,1)},
new RXiang(this){Location=new Point(7,1)},
new BXiang(this){Location=new Point(3,10)},
new BXiang(this){Location=new Point(7,10)},
//马
new RMa(this){Location=new Point(2,1)},
new RMa(this){Location=new Point(8,1)},
new BMa(this){Location=new Point(2,10)},
new BMa(this){Location=new Point(8,10)},
//车
new RJu(this){Location=new Point(1,1)},
new RJu(this){Location=new Point(9,1)},
new BJu(this){Location=new Point(1,10)},
new BJu(this){Location=new Point(9,10)},
//炮
new RPao(this){Location=new Point(2,3)},
new RPao(this){Location=new Point(8,3)},
new BPao(this){Location=new Point(2,8)},
new BPao(this){Location=new Point(8,8)},
//兵
new RBin(this){Location=new Point(1,4)},
new RBin(this){Location=new Point(3,4)},
new RBin(this){Location=new Point(5,4)},
new RBin(this){Location=new Point(7,4)},
new RBin(this){Location=new Point(9,4)},
new BBin(this){Location=new Point(1,7)},
new BBin(this){Location=new Point(3,7)},
new BBin(this){Location=new Point(5,7)},
new BBin(this){Location=new Point(7,7)},
new BBin(this){Location=new Point(9,7)}
});
}
public event EventHandler Update;
public event EventHandler StartStep;
public event EventHandler EndStep;
public event EventHandler OverGame;
public Flag Flag;
public List<APiece> PieceList { get; private set; }
public int Radio { get; set; }
public int BoxLength { get; set; }
public Color BackColor { get; set; }
public Bitmap Bitmap { get; set; }
private APiece _selectpiece = null;
public APiece SelectPiece
{
get
{
return _selectpiece;
}
private set
{
if (value != _selectpiece)
{
_selectpiece = value;
if (StartStep != null)
{
StartStep(value, EventArgs.Empty);
this.Draw();
}
}
}
}
public bool IsRed { get; set; }
public void ClickNode(Point ppt)
{
var n = this.GetNode(ppt);
var p = this.GetPiece(n);
if (p == null)
{
if (SelectPiece != null)
{
if (SelectPiece.Move(n))
{
this.Draw();
if (EndStep != null)
EndStep(SelectPiece, EventArgs.Empty);
}
}
}
else
{
if (SelectPiece == null)
{
if (p.IsRed == this.IsRed)
{
SelectPiece = p;
}
}
else
{
if (p.IsRed == SelectPiece.IsRed)
{
SelectPiece = p;
}
else
{
if (SelectPiece.Move(n))
{
this.Draw();
if (EndStep != null)
EndStep(SelectPiece, EventArgs.Empty);
}
}
}
}
}
public Rectangle GetPixelRantangle(Point p)
{
var rec = new Rectangle()
{
X = p.X * this.BoxLength - this.Radio,
Y = p.Y * this.BoxLength - this.Radio,
Width = this.Radio * 2,
Height = this.Radio * 2
};
return rec;
}
/// <summary>
/// 绘制界面
/// </summary>
public void Draw()
{
Bitmap bmp = new Bitmap(this.BoxLength * 10, this.BoxLength * 11);
var g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.HighQuality; //高质量
g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量
g.Clear(Color.White);
this.DrawSelf(g);
this.Flag.Draw(g);
foreach (var p in PieceList)
{
p.Draw(g);
}
this.Flag.Draw(g);
this.Bitmap = bmp;
if (Update != null)
{
Update(this, EventArgs.Empty);
}
}
/// <summary>
/// 绘制棋盘
/// </summary>
/// <param name="g"></param>
private void DrawSelf(Graphics g)
{
var p = new Pen(Color.Black);
p.Width = 2;
//Graphics g = this.Graphics;
Point pt1, pt2;
for (int i = 1; i <= 9; i++)
{
pt1 = new Point(i * this.BoxLength, 1 * this.BoxLength);
pt2 = new Point(i * this.BoxLength, 10 * this.BoxLength);
g.DrawLine(p, pt1, pt2);
}
for (int j = 1; j <= 10; j++)
{
pt1 = new Point(1 * this.BoxLength, j * this.BoxLength);
pt2 = new Point(9 * this.BoxLength, j * this.BoxLength);
g.DrawLine(p, pt1, pt2);
}
pt1 = new Point(4 * this.BoxLength, 1 * this.BoxLength);
pt2 = new Point(6 * this.BoxLength, 3 * this.BoxLength);
g.DrawLine(p, pt1, pt2);
pt1 = new Point(4 * this.BoxLength, 3 * this.BoxLength);
pt2 = new Point(6 * this.BoxLength, 1 * this.BoxLength);
g.DrawLine(p, pt1, pt2);
pt1 = new Point(4 * this.BoxLength, 10 * this.BoxLength);
pt2 = new Point(6 * this.BoxLength, 8 * this.BoxLength);
g.DrawLine(p, pt1, pt2);
pt1 = new Point(4 * this.BoxLength, 8 * this.BoxLength);
pt2 = new Point(6 * this.BoxLength, 10 * this.BoxLength);
g.DrawLine(p, pt1, pt2);
p.Color = this.BackColor;
for (int i = 2; i <= 8; i++)
{
pt1 = new Point(i * this.BoxLength, 5 * this.BoxLength);
pt2 = new Point(i * this.BoxLength, 6 * this.BoxLength);
g.DrawLine(p, pt1, pt2);
}
}
public Point GetNode(Point ppt)
{
var pt = new Point()
{
X = (int)Math.Round((ppt.X / (double)this.BoxLength), 0),
Y = (int)Math.Round((ppt.Y / (double)this.BoxLength), 0)
};
return pt;
}
public APiece GetPiece(Point node)
{
var qry = (from pi in this.PieceList
where pi.Location == node && !pi.IsDead
select pi).ToList<APiece>();
if (qry.Count == 0)
return null;
else
return qry[0];
}
public Size GetChessBoardSize()
{
return new Size(this.BoxLength * 10, this.BoxLength * 11);
}
}
}
最后截个图