中国象棋对战-类结构设计

一、象棋结构设计

   1. 基本对象

    1)棋盘

    

    横纵:9x10

棋盘类 ChessBoard

View Code
    /// <summary>
/// 棋盘类
/// </summary>
public class ChessBoard : ISlopOver
{
/// <summary>
/// 棋盘横向格子数
/// </summary>
public static readonly Int32 horizon;

/// <summary>
/// 棋盘纵向格子数
/// </summary>
public static readonly Int32 vertical;

/// <summary>
/// 棋盘中的所有棋子
/// </summary>
private List<Chess> chessList;

/// <summary>
/// 棋盘中的所有格子
/// </summary>
private Grid[,] gridList;

public Grid this[Point p]
{
get
{
if (!IsOvered(p)) // 坐标未越界
return gridList[p.X, p.Y];
throw new Exception("坐标越界");
}
}

static ChessBoard()
{
horizon = 9;
vertical = 10;
}

public ChessBoard()
{
this.gridList = new Grid[horizon, vertical];
for (int i = 0; i < horizon; i++)
{
for (int j = 0; j < vertical; j++)
{
gridList[i, j] = new Grid(i, j);
}
}

this.chessList = new List<Chess>();
}

#region ISlopOver Members

public bool IsOvered(Point p)
{
if (p.X < 0 || p.X > horizon || p.Y < 0 || p.Y > horizon) // 越界
return true;
return false;
}

#endregion

/// <summary>
/// 初始化棋盘
/// </summary>
/// <param name="playerCamp">玩家阵营</param>
public void InitBoard(object sender, InitBoardArgs e)
{
try
{
if (chessList != null && chessList.Count > 0) // 清空当前棋盘上所有的棋子
chessList.Clear();
}
catch
{
}
InitGridNull(this.gridList);
InitGridWithCamp(e.playerCamp);
InitChessList(this.gridList);
}

/// <summary>
/// 初始化ChessList值
/// </summary>
/// <param name="grid"></param>
private void InitChessList(Grid[,] gridList)
{
for (int i = 0; i < horizon; i++)
{
for (int j = 0; j < vertical; j++)
{
if (gridList[i, j].HasChess && gridList[i, j].Chess != null)
this.chessList.Add(gridList[i, j].Chess);
}
}
}

/// <summary>
/// 根据阵营初始化棋局
/// </summary>
/// <param name="campType"></param>
private void InitGridWithCamp(CampType playerCamp)
{
if (playerCamp == CampType.红方)
{
#region 红方
// 车的位置
gridList[0, 9].Chess = ChessFactory.Create(CampType.红方, ChessType.Car);
gridList[8, 9].Chess = ChessFactory.Create(CampType.红方, ChessType.Car);
gridList[0, 9].HasChess = gridList[8, 9].HasChess = true;
// 马的位置
gridList[1, 9].Chess = ChessFactory.Create(CampType.红方, ChessType.Horse);
gridList[7, 9].Chess = ChessFactory.Create(CampType.红方, ChessType.Horse);
gridList[1, 9].HasChess = gridList[7, 9].HasChess = true;
// 相的位置
gridList[2, 9].Chess = ChessFactory.Create(CampType.红方, ChessType.Ancient);
gridList[6, 9].Chess = ChessFactory.Create(CampType.红方, ChessType.Ancient);
gridList[2, 9].HasChess = gridList[6, 9].HasChess = true;
// 士的位置
gridList[3, 9].Chess = ChessFactory.Create(CampType.红方, ChessType.Kngiht);
gridList[5, 9].Chess = ChessFactory.Create(CampType.红方, ChessType.Kngiht);
gridList[3, 9].HasChess = gridList[5, 9].HasChess = true;
// 帅的位置
gridList[4, 9].Chess = ChessFactory.Create(CampType.红方, ChessType.General);
gridList[4, 9].HasChess = true;
// 炮的位置
gridList[1, 7].Chess = ChessFactory.Create(CampType.红方, ChessType.Cannon);
gridList[7, 7].Chess = ChessFactory.Create(CampType.红方, ChessType.Cannon);
gridList[1, 7].HasChess = gridList[7, 7].HasChess = true;
// 兵的位置
gridList[0, 6].Chess = ChessFactory.Create(CampType.红方, ChessType.Soldier);
gridList[2, 6].Chess = ChessFactory.Create(CampType.红方, ChessType.Soldier);
gridList[4, 6].Chess = ChessFactory.Create(CampType.红方, ChessType.Soldier);
gridList[6, 6].Chess = ChessFactory.Create(CampType.红方, ChessType.Soldier);
gridList[8, 6].Chess = ChessFactory.Create(CampType.红方, ChessType.Soldier);
gridList[0, 6].HasChess = gridList[2, 6].HasChess = gridList[4, 6].HasChess
= gridList[6, 6].HasChess = gridList[8, 6].HasChess = true;
#endregion
#region 黑方
// 车的位置
gridList[0, 0].Chess = ChessFactory.Create(CampType.黑方, ChessType.Car);
gridList[8, 0].Chess = ChessFactory.Create(CampType.黑方, ChessType.Car);
gridList[0, 0].HasChess = gridList[8, 0].HasChess = true;
// 马的位置
gridList[1, 0].Chess = ChessFactory.Create(CampType.黑方, ChessType.Horse);
gridList[7, 0].Chess = ChessFactory.Create(CampType.黑方, ChessType.Horse);
gridList[1, 0].HasChess = gridList[7, 0].HasChess = true;
// 象的位置
gridList[2, 0].Chess = ChessFactory.Create(CampType.黑方, ChessType.Ancient);
gridList[6, 0].Chess = ChessFactory.Create(CampType.黑方, ChessType.Ancient);
gridList[2, 0].HasChess = gridList[6, 0].HasChess = true;
// 士的位置
gridList[3, 0].Chess = ChessFactory.Create(CampType.黑方, ChessType.Kngiht);
gridList[5, 0].Chess = ChessFactory.Create(CampType.黑方, ChessType.Kngiht);
gridList[3, 0].HasChess = gridList[5, 0].HasChess = true;
// 将的位置
gridList[4, 0].Chess = ChessFactory.Create(CampType.黑方, ChessType.General);
gridList[4, 0].HasChess = true;
// 炮的位置
gridList[1, 2].Chess = ChessFactory.Create(CampType.黑方, ChessType.Cannon);
gridList[7, 2].Chess = ChessFactory.Create(CampType.黑方, ChessType.Cannon);
gridList[1, 2].HasChess = gridList[7, 2].HasChess = true;
// 卒的位置
gridList[0, 3].Chess = ChessFactory.Create(CampType.黑方, ChessType.Soldier);
gridList[2, 3].Chess = ChessFactory.Create(CampType.黑方, ChessType.Soldier);
gridList[4, 3].Chess = ChessFactory.Create(CampType.黑方, ChessType.Soldier);
gridList[6, 3].Chess = ChessFactory.Create(CampType.黑方, ChessType.Soldier);
gridList[8, 3].Chess = ChessFactory.Create(CampType.黑方, ChessType.Soldier);
gridList[0, 3].HasChess = gridList[2, 3].HasChess = gridList[4, 3].HasChess
= gridList[6, 3].HasChess = gridList[8, 3].HasChess = true;
#endregion
}
else
{
#region 黑方
// 车的位置
gridList[0, 9].Chess = ChessFactory.Create(CampType.黑方, ChessType.Car);
gridList[8, 9].Chess = ChessFactory.Create(CampType.黑方, ChessType.Car);
gridList[0, 9].HasChess = gridList[8, 9].HasChess = true;
// 马的位置
gridList[1, 9].Chess = ChessFactory.Create(CampType.黑方, ChessType.Horse);
gridList[7, 9].Chess = ChessFactory.Create(CampType.黑方, ChessType.Horse);
gridList[1, 9].HasChess = gridList[7, 9].HasChess = true;
// 象的位置
gridList[2, 9].Chess = ChessFactory.Create(CampType.黑方, ChessType.Ancient);
gridList[6, 9].Chess = ChessFactory.Create(CampType.黑方, ChessType.Ancient);
gridList[2, 9].HasChess = gridList[6, 9].HasChess = true;
// 士的位置
gridList[3, 9].Chess = ChessFactory.Create(CampType.黑方, ChessType.Kngiht);
gridList[5, 9].Chess = ChessFactory.Create(CampType.黑方, ChessType.Kngiht);
gridList[3, 9].HasChess = gridList[5, 9].HasChess = true;
// 将的位置
gridList[4, 9].Chess = ChessFactory.Create(CampType.黑方, ChessType.General);
gridList[4, 9].HasChess = true;
// 炮的位置
gridList[1, 7].Chess = ChessFactory.Create(CampType.黑方, ChessType.Cannon);
gridList[7, 7].Chess = ChessFactory.Create(CampType.黑方, ChessType.Cannon);
gridList[1, 7].HasChess = gridList[7, 7].HasChess = true;
// 卒的位置
gridList[0, 6].Chess = ChessFactory.Create(CampType.黑方, ChessType.Soldier);
gridList[2, 6].Chess = ChessFactory.Create(CampType.黑方, ChessType.Soldier);
gridList[4, 6].Chess = ChessFactory.Create(CampType.黑方, ChessType.Soldier);
gridList[6, 6].Chess = ChessFactory.Create(CampType.黑方, ChessType.Soldier);
gridList[8, 6].Chess = ChessFactory.Create(CampType.黑方, ChessType.Soldier);
gridList[0, 6].HasChess = gridList[2, 6].HasChess = gridList[4, 6].HasChess
= gridList[6, 6].HasChess = gridList[8, 6].HasChess = true;
#endregion
#region 红方
// 车的位置
gridList[0, 0].Chess = ChessFactory.Create(CampType.红方, ChessType.Car);
gridList[8, 0].Chess = ChessFactory.Create(CampType.红方, ChessType.Car);
gridList[0, 0].HasChess = gridList[8, 0].HasChess = true;
// 马的位置
gridList[1, 0].Chess = ChessFactory.Create(CampType.红方, ChessType.Horse);
gridList[7, 0].Chess = ChessFactory.Create(CampType.红方, ChessType.Horse);
gridList[1, 0].HasChess = gridList[7, 0].HasChess = true;
// 象的位置
gridList[2, 0].Chess = ChessFactory.Create(CampType.红方, ChessType.Ancient);
gridList[6, 0].Chess = ChessFactory.Create(CampType.红方, ChessType.Ancient);
gridList[2, 0].HasChess = gridList[6, 0].HasChess = true;
// 士的位置
gridList[3, 0].Chess = ChessFactory.Create(CampType.红方, ChessType.Kngiht);
gridList[5, 0].Chess = ChessFactory.Create(CampType.红方, ChessType.Kngiht);
gridList[3, 0].HasChess = gridList[5, 0].HasChess = true;
// 帅的位置
gridList[4, 0].Chess = ChessFactory.Create(CampType.红方, ChessType.General);
gridList[4, 0].HasChess = true;
// 炮的位置
gridList[1, 2].Chess = ChessFactory.Create(CampType.红方, ChessType.Cannon);
gridList[7, 2].Chess = ChessFactory.Create(CampType.红方, ChessType.Cannon);
gridList[1, 2].HasChess = gridList[7, 2].HasChess = true;
// 兵的位置
gridList[0, 3].Chess = ChessFactory.Create(CampType.红方, ChessType.Soldier);
gridList[2, 3].Chess = ChessFactory.Create(CampType.红方, ChessType.Soldier);
gridList[4, 3].Chess = ChessFactory.Create(CampType.红方, ChessType.Soldier);
gridList[6, 3].Chess = ChessFactory.Create(CampType.红方, ChessType.Soldier);
gridList[8, 3].Chess = ChessFactory.Create(CampType.红方, ChessType.Soldier);
gridList[0, 3].HasChess = gridList[2, 3].HasChess = gridList[4, 3].HasChess
= gridList[6, 3].HasChess = gridList[8, 3].HasChess = true;
#endregion
}
}

/// <summary>
/// 清空盘面
/// </summary>
/// <param name="gridList"></param>
private void InitGridNull(Grid[,] gridList)
{
for (int i = 0; i < horizon; i++)
{
for (int j = 0; j < vertical; j++)
{
gridList[i, j].Chess = null;
gridList[i, j].HasChess = false;
}
}
}
}

public class InitBoardArgs : EventArgs
{
public readonly CampType playerCamp;
public InitBoardArgs(CampType playerCamp)
{
this.playerCamp = playerCamp;
}
}

棋盘格子类 Grid

View Code
/// <summary>
/// 棋盘格子类
/// </summary>
public class Grid
{
/// <summary>
/// X坐标
/// </summary>
private readonly Int32 x;

/// <summary>
/// Y坐标
/// </summary>
private readonly Int32 y;

/// <summary>
/// 是否有棋子
/// </summary>
private Boolean hasChess;

public Boolean HasChess { get; set; }

/// <summary>
/// 格子上的棋子
/// </summary>
private Chess chess;

public Chess Chess
{
get { return this.chess; }
set { this.chess = value; }
}

public Grid()
{
this.x = 0;
this.y = 0;
this.HasChess = false;
}

public Grid(Int32 x, Int32 y)
{
this.x = x;
this.y = y;
this.HasChess = false;
}

public Grid(Int32 x, Int32 y, Chess chess)
{
this.x = x;
this.y = y;
this.HasChess = true;
this.chess = chess;
}
}

 

    2)棋子

    

    红方:兵兵兵兵兵、炮炮、车车、马马、相相、士士、帅

    黑方:卒卒卒卒卒、炮炮、车车、马马、象象、士士、将

棋子抽象基类 Chess

View Code
public enum ChessType
{
Soldier = 0, //
Cannon = 1, //
Car = 2, //
Horse = 3, //
Ancient = 4, //
Kngiht = 5, //
General = 6, //
}

public enum CampType
{
红方 = 0,
黑方 = 1,
}

public abstract class Chess
{
/// <summary>
/// 名称
/// </summary>
private String name;

/// <summary>
/// 阵营
/// </summary>
private CampType camp;

/// <summary>
/// 类型
/// </summary>
private ChessType type;

public String Name
{
get { return this.name; }
set { this.name = value; }
}

public CampType Camp
{
get { return this.camp; }
set { this.camp = value; }
}

public ChessType Type
{
get { return this.type; }
set { this.type = value; }
}

public Chess()
{
this.name = "";
this.Camp = CampType.红方;
this.type = ChessType.Soldier;
}

public Chess(String name, CampType camp, ChessType type)
{
this.name = name;
this.camp = camp;
this.type = type;
}

/// <summary>
/// 走位
/// </summary>
/// <param name="from">起点</param>
/// <param name="to">终点</param>
/// <returns></returns>
public abstract bool GoNext(Point from, Point to);
}

棋子"兵"类

View Code
public class ChessSoldier : Chess
{
// 不允许使用无参数构造函数创建对象
private ChessSoldier(){ }

public ChessSoldier(CampType camp)
: base("", camp, ChessType.Soldier)
{
if (camp == CampType.红方)
{
this.Name = "";
}
else
{
this.Name = "";
}
this.Camp = camp;
}

public override bool GoNext(Point from, Point to)
{
throw new NotImplementedException();
}
}

棋子"炮"类

View Code
public class ChessCannon : Chess
{
// 不允许使用无参数构造函数创建对象
private ChessCannon() { }

public ChessCannon(CampType camp)
: base("", camp, ChessType.Cannon)
{ }

public override bool GoNext(System.Drawing.Point from, System.Drawing.Point to)
{
throw new NotImplementedException();
}
}

棋子"车"类

View Code
public class ChessCar : Chess
{
// 不允许使用无参数构造函数创建对象
private ChessCar() { }

public ChessCar(CampType camp)
: base("", camp, ChessType.Car)
{ }

public override bool GoNext(System.Drawing.Point from, System.Drawing.Point to)
{
throw new NotImplementedException();
}
}

棋子"马"类

View Code
public class ChessHorse : Chess
{
// 不允许使用无参数构造函数创建对象
private ChessHorse() { }

public ChessHorse(CampType camp)
: base("", camp, ChessType.Horse)
{ }

public override bool GoNext(System.Drawing.Point from, System.Drawing.Point to)
{
throw new NotImplementedException();
}
}

棋子"相"类

View Code
public class ChessAncient : Chess
{
// 不允许使用无参数构造函数创建对象
private ChessAncient() { }

public ChessAncient(CampType camp)
: base("", camp, ChessType.Ancient)
{
if (camp == CampType.黑方)
this.Name = "";
else
this.Name = "";
}

public override bool GoNext(System.Drawing.Point from, System.Drawing.Point to)
{
throw new NotImplementedException();
}
}

棋子"士"类

View Code
public class ChessKngiht : Chess
{
private ChessKngiht() { }

public ChessKngiht(CampType camp)
: base("", camp, ChessType.Kngiht)
{ }

public override bool GoNext(System.Drawing.Point from, System.Drawing.Point to)
{
throw new NotImplementedException();
}
}

棋子"帅"类

View Code
public class ChessGeneral : Chess
{
private ChessGeneral() { }

public ChessGeneral(CampType camp)
: base("", camp, ChessType.General)
{
if (camp == CampType.黑方)
this.Name = "";
else
this.Name = "";
}

public override bool GoNext(System.Drawing.Point from, System.Drawing.Point to)
{
throw new NotImplementedException();
}
}

 棋子工厂类

View Code
public class ChessFactory
{
public static Chess Create(CampType camp, ChessType type)
{
switch (type)
{
case ChessType.Soldier:
return new ChessSoldier(camp);
case ChessType.Cannon:
return new ChessCannon(camp);
case ChessType.Car:
return new ChessCar(camp);
case ChessType.Horse:
return new ChessHorse(camp);
case ChessType.Ancient:
return new ChessAncient(camp);
case ChessType.Kngiht:
return new ChessKngiht(camp);
case ChessType.General:
return new ChessGeneral(camp);
default:
return null;
}
}
}

 

   2. 功能接口

    越界判断接口:ISlopOver

View Code
    /// <summary>
/// 越界判断接口
/// </summary>
public interface ISlopOver
{
/// <summary>
/// 越界判断
/// </summary>
/// <param name="p">坐标</param>
/// <returns>是否越界</returns>
Boolean IsOvered(Point p);
}

 

二、通信结构设计

   1. 消息

消息抽象基类

View Code
    public enum MessageType
{
Chat = 0, // 聊天
Play = 1, // 下棋
}

public abstract class MessageBase
{
/// <summary>
/// 消息产生时间
/// </summary>
private readonly DateTime time;

/// <summary>
/// 消息类型
/// </summary>
private readonly MessageType type;

/// <summary>
/// 消息来源IP
/// </summary>
private IPAddress fromAddress;

/// <summary>
/// 消息目标IP
/// </summary>
private IPAddress toAddress;

public DateTime Time
{
get { return this.time; }
}

public MessageType Type
{
get { return this.type; }
}

public IPAddress FromAddress
{
get { return this.fromAddress; }
set { this.fromAddress = value; }
}

public IPAddress ToAddress
{
get { return this.toAddress; }
set { this.toAddress = value; }
}

public MessageBase(MessageType type)
{
this.time = DateTime.Now;
this.type = type;
}
}

聊天消息

View Code
    public class ChatMessage : MessageBase
{
/// <summary>
/// 聊天内容
/// </summary>
private String content;

/// <summary>
/// 消息来自方
/// </summary>
private String fromPerson;

/// <summary>
/// 消息接收方
/// </summary>
private String toPerson;

public String Content
{
get { return this.content; }
set { this.content = value; }
}

public String FromPerson
{
get { return this.fromPerson; }
set { this.fromPerson = value; }
}

public String ToPerson
{
get { return this.toPerson; }
set { this.toPerson = value; }
}

public ChatMessage(String fromPerson, String content)
: base(MessageType.Chat)
{
this.fromPerson = fromPerson;
this.content = content;
}

public ChatMessage(String fromPerson, String toPerson, String content)
: base(MessageType.Chat)
{
this.fromPerson = fromPerson;
this.toPerson = toPerson;
this.content = content;
}

public override string ToString()
{
StringBuilder result = new StringBuilder();
result.Append("[").Append(this.Time.ToString("hh:mm:ss")).Append("]")
.Append(this.fromPerson).Append(": ")
.Append(this.content)
.Append("\n");
return result.ToString();
}
}

下棋消息

View Code
    public class PlayMessage : MessageBase
{
/// <summary>
/// 棋子起点
/// </summary>
private Point fromPoint;

/// <summary>
/// 棋子终点
/// </summary>
private Point toPoint;

public Point FromPoint
{
get { return this.fromPoint; }
set { this.fromPoint = value; }
}

public Point ToPoint
{
get { return this.toPoint; }
set { this.toPoint = value; }
}

public PlayMessage(Point fromPoint, Point toPoint)
: base(MessageType.Play)
{
this.fromPoint = fromPoint;
this.toPoint = toPoint;
}
}

 

   2. 发送

View Code
   public interface ISend
{
/// <summary>
/// 发送消息
/// </summary>
/// <returns></returns>
Boolean Send(MessageBase message);
}
View Code
    public class Sender : ISend
{
#region ISend Members

public bool Send(MessageBase message)
{
throw new NotImplementedException();
}

#endregion
}

 

   3. 接收

View Code
    public interface IReceive
{
/// <summary>
/// 接收消息
/// </summary>
/// <returns></returns>
MessageBase Receive();
}
View Code
    public class Receiver : IReceive
{

#region IReceive Members

public MessageBase Receive()
{
throw new NotImplementedException();
}

#endregion
}

 

三、玩家结构设计

   1. 玩家

View Code
public class ChessPlayer : IPlay
{
/// <summary>
/// 账号
/// </summary>
private String userName;

/// <summary>
/// 密码
/// </summary>
private String password;

/// <summary>
/// 昵称
/// </summary>
private String nickName;

/// <summary>
/// 头像
/// </summary>
private String imgUrl;

public String UserName
{
get { return this.userName; }
set { this.userName = value; }
}

public String Password
{
get { return this.password; }
set { this.password = value; }
}

public String NickName
{
get { return this.nickName; }
set { this.nickName = value; }
}

public String ImgUrl
{
get { return this.imgUrl; }
set { this.imgUrl = value; }
}

#region IPlay Members

public void InitBoard()
{
throw new NotImplementedException();
}

public bool Retract()
{
throw new NotImplementedException();
}

public bool StandOff()
{
throw new NotImplementedException();
}

public bool GiveUp()
{
throw new NotImplementedException();
}

public bool Over()
{
throw new NotImplementedException();
}

#endregion
}
View Code
public interface IPlay
{
/// <summary>
/// 初始化棋盘
/// </summary>
void InitBoard();

/// <summary>
/// 悔棋
/// </summary>
/// <returns>操作是否成功</returns>
Boolean Retract();

/// <summary>
/// 和局
/// </summary>
/// <returns></returns>
Boolean StandOff();

/// <summary>
/// 认输
/// </summary>
/// <returns></returns>
Boolean GiveUp();

/// <summary>
/// 棋局结束
/// </summary>
/// <returns></returns>
Boolean Over();


}

 

   2. AI(有时间再做吧)

posted @ 2012-01-03 17:11  xanthodont  阅读(1773)  评论(0编辑  收藏  举报