From fc7f3c5fca0f26d1c786650d12e7dc5aa22a2e85 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=AD=B1=E5=82=91?= <840465812@qq.com>
Date: Sat, 29 May 2021 00:21:56 +0800
Subject: [PATCH] =?UTF-8?q?1.=20=E4=BF=AE=E6=94=B9=20Chess=20=E7=B1=BB=20?=
=?UTF-8?q?=E5=88=B0=20Chessman=20=E7=B1=BB=202.=20=E5=A2=9E=E5=8A=A0=20?=
=?UTF-8?q?=E9=98=B5=E8=90=A5=E6=9E=9A=E4=B8=BE=E6=89=A9=E5=B1=95=E7=B1=BB?=
=?UTF-8?q?=203.=20=E5=A2=9E=E5=8A=A0=20=E6=A3=8B=E5=B1=80=E8=A3=81?=
=?UTF-8?q?=E5=88=A4=E7=B1=BB=EF=BC=88=E8=A7=84=E5=88=99=E7=B1=BB=EF=BC=89?=
=?UTF-8?q?=204.=20=E5=A2=9E=E5=8A=A0=20=E6=A3=8B=E7=9B=98=E7=9B=B8?=
=?UTF-8?q?=E5=85=B3=E6=93=8D=E4=BD=9C=EF=BC=8C=E5=8C=85=E6=8B=AC=E6=A3=8B?=
=?UTF-8?q?=E8=B0=B1=E6=A0=88=E6=93=8D=E4=BD=9C=205.=20=E4=BF=AE=E6=94=B9?=
=?UTF-8?q?=20=E7=A7=BB=E5=8A=A8=E6=96=B9=E6=B3=95=E4=BB=8E=E7=A7=BB?=
=?UTF-8?q?=E5=8A=A8=E7=B1=BB=E6=94=B9=E5=88=B0=E8=A3=81=E5=88=A4=E7=B1=BB?=
=?UTF-8?q?=E4=B8=AD=EF=BC=8C=E4=BB=8E=E8=A3=81=E5=88=A4=E7=B1=BB=E6=9D=A5?=
=?UTF-8?q?=E5=88=9B=E5=BB=BA=E7=A7=BB=E5=8A=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
ChineseChess.Core/Chess.cs | 9 ---
ChineseChess.Core/ChessCamp.cs | 6 ++
ChineseChess.Core/ChessMove.cs | 19 +-----
ChineseChess.Core/ChessReferee.cs | 19 ++++++
ChineseChess.Core/Chessboard.cs | 103 +++++++++++++++++++++++++++++-
ChineseChess.Core/Chessman.cs | 16 +++++
6 files changed, 144 insertions(+), 28 deletions(-)
delete mode 100644 ChineseChess.Core/Chess.cs
create mode 100644 ChineseChess.Core/ChessReferee.cs
create mode 100644 ChineseChess.Core/Chessman.cs
diff --git a/ChineseChess.Core/Chess.cs b/ChineseChess.Core/Chess.cs
deleted file mode 100644
index 8b0e9dc..0000000
--- a/ChineseChess.Core/Chess.cs
+++ /dev/null
@@ -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; }
- }
-}
\ No newline at end of file
diff --git a/ChineseChess.Core/ChessCamp.cs b/ChineseChess.Core/ChessCamp.cs
index 7faa01b..a78c99b 100644
--- a/ChineseChess.Core/ChessCamp.cs
+++ b/ChineseChess.Core/ChessCamp.cs
@@ -15,4 +15,10 @@
///
Black,
}
+
+ public static class ChessCampExtensions
+ {
+ public static ChessCamp RivalCamp(this ChessCamp camp)
+ => camp == ChessCamp.Red ? ChessCamp.Black : ChessCamp.Red;
+ }
}
\ No newline at end of file
diff --git a/ChineseChess.Core/ChessMove.cs b/ChineseChess.Core/ChessMove.cs
index fd7dd76..baff4df 100644
--- a/ChineseChess.Core/ChessMove.cs
+++ b/ChineseChess.Core/ChessMove.cs
@@ -1,27 +1,10 @@
-using System;
-
-namespace ChineseChess.Core
+namespace ChineseChess.Core
{
///
/// 棋子移动步骤
///
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();
- }
-
///
/// 阵营
///
diff --git a/ChineseChess.Core/ChessReferee.cs b/ChineseChess.Core/ChessReferee.cs
new file mode 100644
index 0000000..61712f9
--- /dev/null
+++ b/ChineseChess.Core/ChessReferee.cs
@@ -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();
+ }
+ }
+}
diff --git a/ChineseChess.Core/Chessboard.cs b/ChineseChess.Core/Chessboard.cs
index 90a7b4e..596fc5c 100644
--- a/ChineseChess.Core/Chessboard.cs
+++ b/ChineseChess.Core/Chessboard.cs
@@ -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 Moves = new Stack();
+
+ private readonly List AlivingChessman = new List(32);
+
+ public event EventHandler 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 GetChessmenByType(ChessType type, ChessCamp camp)
+ => AlivingChessman.Where(chess => chess.Type == type && chess.Camp == camp);
+
+ public IEnumerable GetChessmen() => AlivingChessman;
+
+ public IEnumerable GetMoves() => Moves;
+
+ ///
+ /// 移动棋子到目标位置,若目标位置存在棋子,则移除
+ ///
+ /// 棋子
+ /// 目标位置
+ /// 无法将棋子移动到己方棋子上
+ 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);
+ }
+
+ ///
+ /// 移动一步
+ ///
+ /// 移动方式
+ /// 未找到要进行移动的棋子
+ public void PushMove(ChessMove move)
+ {
+ var chessman = GetChessmanByPos(move.Start);
+ if (chessman == null)
+ throw new MoveException("未找到要进行移动的棋子");
+ MoveChessman(chessman, move.End);
+ Moves.Push(move);
+ }
+
+ ///
+ /// 退回上一步
+ ///
+ /// 已经退回到初始局面 or 未找到要进行移动的棋子
+ 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("移动失败")
+ {
+ }
}
}
\ No newline at end of file
diff --git a/ChineseChess.Core/Chessman.cs b/ChineseChess.Core/Chessman.cs
new file mode 100644
index 0000000..fcb084f
--- /dev/null
+++ b/ChineseChess.Core/Chessman.cs
@@ -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; }
+ }
+}
\ No newline at end of file