1. 修改 Chess 类 到 Chessman 类

2. 增加 阵营枚举扩展类
3. 增加 棋局裁判类(规则类)
4. 增加 棋盘相关操作,包括棋谱栈操作
5. 修改 移动方法从移动类改到裁判类中,从裁判类来创建移动
This commit is contained in:
筱傑 2021-05-29 00:21:56 +08:00
parent c6bdd63d2e
commit fc7f3c5fca
6 changed files with 144 additions and 28 deletions

View File

@ -1,9 +0,0 @@
namespace ChineseChess.Core
{
public class Chess
{
public ChessType Type { get; set; }
public ChessCamp Camp { get; set; }
public ChessboardPosition Position { get; set; }
}
}

View File

@ -15,4 +15,10 @@
/// </summary>
Black,
}
public static class ChessCampExtensions
{
public static ChessCamp RivalCamp(this ChessCamp camp)
=> camp == ChessCamp.Red ? ChessCamp.Black : ChessCamp.Red;
}
}

View File

@ -1,27 +1,10 @@
using System;
namespace ChineseChess.Core
namespace ChineseChess.Core
{
/// <summary>
/// 棋子移动步骤
/// </summary>
public class ChessMove
{
private ChessMove()
{
}
public static ChessMove GenMove(Chessboard chessboard, string move)
{
throw new NotImplementedException();
}
public static ChessMove GenMove(Chessboard chessboard, ChessCamp camp, ChessboardPosition start, ChessboardPosition end)
{
throw new NotImplementedException();
}
/// <summary>
/// 阵营
/// </summary>

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace ChineseChess.Core
{
class ChessReferee
{
public static ChessMove Move(Chessboard chessboard, string move)
{
throw new NotImplementedException();
}
public static ChessMove Move(Chessboard chessboard, ChessCamp camp, ChessboardPosition start, ChessboardPosition end)
{
throw new NotImplementedException();
}
}
}

View File

@ -1,6 +1,107 @@
namespace ChineseChess.Core
using System;
using System.Collections.Generic;
using System.Linq;
namespace ChineseChess.Core
{
public class Chessboard
{
private readonly Stack<ChessMove> Moves = new Stack<ChessMove>();
private readonly List<Chessman> AlivingChessman = new List<Chessman>(32);
public event EventHandler<ChessmanMovedEventArgs> ChessmanMovedEvent;
private void OnChessmanMoved(Chessman chessman, Chessman chessmanKilled)
=> ChessmanMovedEvent?.Invoke(this, new ChessmanMovedEventArgs(chessman, chessmanKilled));
public Chessman GetChessmanByPos(ChessboardPosition position)
=> AlivingChessman.FirstOrDefault(c => c.Position == position);
public IEnumerable<Chessman> GetChessmenByType(ChessType type, ChessCamp camp)
=> AlivingChessman.Where(chess => chess.Type == type && chess.Camp == camp);
public IEnumerable<Chessman> GetChessmen() => AlivingChessman;
public IEnumerable<ChessMove> GetMoves() => Moves;
/// <summary>
/// 移动棋子到目标位置,若目标位置存在棋子,则移除
/// </summary>
/// <param name="chessman">棋子</param>
/// <param name="target">目标位置</param>
/// <exception cref="MoveException">无法将棋子移动到己方棋子上</exception>
private void MoveChessman(Chessman chessman, ChessboardPosition target)
{
var tar = GetChessmanByPos(target);
if (tar != null)
{
if (chessman.Camp == tar.Camp)
throw new MoveException("无法将棋子移动到己方棋子上");
AlivingChessman.Remove(tar);
}
chessman.Position = target;
OnChessmanMoved(chessman, tar);
}
/// <summary>
/// 移动一步
/// </summary>
/// <param name="move">移动方式</param>
/// <exception cref="MoveException">未找到要进行移动的棋子</exception>
public void PushMove(ChessMove move)
{
var chessman = GetChessmanByPos(move.Start);
if (chessman == null)
throw new MoveException("未找到要进行移动的棋子");
MoveChessman(chessman, move.End);
Moves.Push(move);
}
/// <summary>
/// 退回上一步
/// </summary>
/// <exception cref="MoveException">已经退回到初始局面 or 未找到要进行移动的棋子</exception>
public void PopMove()
{
if (Moves.Count == 0)
throw new MoveException("已经退回到初始局面");
var move = Moves.Pop();
var chessman = GetChessmanByPos(move.End);
if (chessman == null)
throw new MoveException("未找到要进行移动的棋子");
MoveChessman(chessman, move.Start);
if (move.Killed != null)
AlivingChessman.Add(new Chessman((ChessType)move.Killed, chessman.Camp.RivalCamp(), move.End));
}
}
public class ChessmanMovedEventArgs : EventArgs
{
public ChessmanMovedEventArgs(Chessman chessman, Chessman chessmanKilled)
{
Chessman = chessman;
ChessmanKilled = chessmanKilled;
}
public Chessman Chessman { get; set; }
public Chessman ChessmanKilled { get; set; }
}
public class MoveException : Exception
{
public MoveException(string message) : base(message)
{
}
public MoveException(string message, Exception innerException) : base(message, innerException)
{
}
public MoveException() : this("移动失败")
{
}
}
}

View File

@ -0,0 +1,16 @@
namespace ChineseChess.Core
{
public class Chessman
{
public Chessman(ChessType type, ChessCamp camp, ChessboardPosition position)
{
Type=type;
Camp=camp;
Position=position;
}
public ChessType Type { get; set; }
public ChessCamp Camp { get; set; }
public ChessboardPosition Position { get; set; }
}
}