4个版本
0.1.3 | 2022年6月10日 |
---|---|
0.1.2 | 2022年6月7日 |
0.1.1 | 2022年6月7日 |
0.1.0 | 2022年6月6日 |
#940 in 游戏开发
32 个月下载量
在pacifist-chess-simulation中使用
105KB
2K SLoC
回合引擎
回合引擎完全实现了所有棋规则,因此可以用来玩棋或进行棋类模拟。
引擎在每回合玩完后提供一份可用和可玩的回合列表。列表中的任何回合都可以玩。任何玩过的回合都可以撤销。
引擎的输入是使用代数式棋盘表示法编写的棋盘回合。但为了简单起见,可以使用 AvailableTurn
结构将简单的回合“将棋子从方A移动到方B”转换为棋盘表示法回合。
显示
棋盘引擎提供了一套基本的工具,以ASCII格式打印棋盘状态。打印棋盘状态有几个选项。可以打印出回合历史。可以打印出被捕获的棋子。
ViewMode::SimpleAscii
- 非常简单的显示格式
图例
b
:黑方w
:白方P
:兵N
:马B
:象R
:车K
:王Q
:后
示例
8 bR bN bB bQ bK bB bN bR
7 bP bP bP bP bP bP bP bP
6 - + - + - + - +
5 + - + - + - + -
4 - + - + - + - +
3 + - + - + - + -
2 wP wP wP wP wP wP wP wP
1 wR wN wB wQ wK wB wN wR
a b c d e f g h
ViewMode::FancyTui
- 彩色终端ASCII显示格式
注意:颜色在文档页面上不可见
示例
8 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
7 ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
6
5
4
3
2 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
1 ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
a b c d e f g h
玩六个简单回合的示例
use chess_turn_engine::{
ChessTurnEngine, DisplayOption, Setup, ViewMode, Gamestate
};
let mut cte = ChessTurnEngine::new(Setup::Normal).unwrap();
let turns = ["h4", "Na6", "h5", "b5", "d3", "g6"];
turns.iter().for_each(|turn| {
cte.play_turn(turn);
});
for turn in cte.available_turns() {
println!("{}", turn);
}
// Output:
// AvailableTurn (src: h5, dst: h6, piece: Pawn, captured: None, turn: h6
// AvailableTurn (src: h5, dst: g6, piece: Pawn, captured: Some("Pawn"), turn: hxg6
// AvailableTurn (src: d3, dst: d4, piece: Pawn, captured: None, turn: d4
// AvailableTurn (src: a2, dst: a3, piece: Pawn, captured: None, turn: a3
// AvailableTurn (src: a2, dst: a4, piece: Pawn, captured: None, turn: a4
// AvailableTurn (src: b2, dst: b3, piece: Pawn, captured: None, turn: b3
// AvailableTurn (src: b2, dst: b4, piece: Pawn, captured: None, turn: b4
// AvailableTurn (src: c2, dst: c3, piece: Pawn, captured: None, turn: c3
// AvailableTurn (src: c2, dst: c4, piece: Pawn, captured: None, turn: c4
// AvailableTurn (src: e2, dst: e3, piece: Pawn, captured: None, turn: e3
// AvailableTurn (src: e2, dst: e4, piece: Pawn, captured: None, turn: e4
// AvailableTurn (src: f2, dst: f3, piece: Pawn, captured: None, turn: f3
// AvailableTurn (src: f2, dst: f4, piece: Pawn, captured: None, turn: f4
// AvailableTurn (src: g2, dst: g3, piece: Pawn, captured: None, turn: g3
// AvailableTurn (src: g2, dst: g4, piece: Pawn, captured: None, turn: g4
// AvailableTurn (src: b1, dst: c3, piece: Knight, captured: None, turn: Nc3
// AvailableTurn (src: b1, dst: a3, piece: Knight, captured: None, turn: Na3
// AvailableTurn (src: b1, dst: d2, piece: Knight, captured: None, turn: Nd2
// AvailableTurn (src: c1, dst: d2, piece: Bishop, captured: None, turn: Bd2
// AvailableTurn (src: c1, dst: e3, piece: Bishop, captured: None, turn: Be3
// AvailableTurn (src: c1, dst: f4, piece: Bishop, captured: None, turn: Bf4
// AvailableTurn (src: c1, dst: g5, piece: Bishop, captured: None, turn: Bg5
// AvailableTurn (src: c1, dst: h6, piece: Bishop, captured: None, turn: Bh6
// AvailableTurn (src: d1, dst: d2, piece: Queen, captured: None, turn: Qd2
// AvailableTurn (src: e1, dst: d2, piece: King, captured: None, turn: Kd2
// AvailableTurn (src: g1, dst: h3, piece: Knight, captured: None, turn: Nh3
// AvailableTurn (src: g1, dst: f3, piece: Knight, captured: None, turn: Nf3
// AvailableTurn (src: h1, dst: h2, piece: Rook, captured: None, turn: Rh2
// AvailableTurn (src: h1, dst: h3, piece: Rook, captured: None, turn: Rh3
// AvailableTurn (src: h1, dst: h4, piece: Rook, captured: None, turn: Rh4
玩两个回合的示例
let mut cte = ChessTurnEngine::new(Setup::Normal).unwrap();
assert!(cte.play_turn("d4").is_ok()); // Play "d4", starting turn
// Play a random turn for the black player
let mut next_random_turn =
String::from(cte.available_turns().first().unwrap().get_turn());
assert!(cte.play_turn(&next_random_turn).is_ok());
// Play "a3" turn using only info about the source and destination squares
for turn in cte.available_turns() {
if turn.src != String::from("a2") && turn.dst != String::from("a3") {
continue;
}
next_random_turn = String::from(turn.get_turn());
break;
}
assert!(cte.play_turn(&next_random_turn).is_ok());
assert_eq!(cte.gamestate(), Gamestate::Ongoing);
cte.display_on_screen(DisplayOption::BoardView(ViewMode::SimpleAscii));
// 8 bR bN bB bQ bK bB bN bR
// 7 bP bP bP - bP bP bP bP
// 6 - + - + - + - +
// 5 + - + bP + - + -
// 4 - + - wP - + - +
// 3 wP - + - + - + -
// 2 - wP wP + wP wP wP wP
// 1 wR wN wB wQ wK wB wN wR
// a b c d e f g h
关于
我并不真的经常下棋,但我想练习我的Rust技能,所以这是一个有趣的项目。这个特定的包是为了Pacifist chess simulation而创建的。