feat: Karel Peeters board game implementation
This commit is contained in:
parent
866ba611a6
commit
f2a89f60bc
10 changed files with 494 additions and 92 deletions
|
|
@ -8,7 +8,7 @@ use std::fmt;
|
|||
pub type Field = usize;
|
||||
pub type FieldWithCount = (Field, i8);
|
||||
|
||||
#[derive(Debug, Copy, Clone, Serialize, PartialEq, Deserialize)]
|
||||
#[derive(Debug, Copy, Clone, Serialize, PartialEq, Eq, Deserialize)]
|
||||
pub struct CheckerMove {
|
||||
from: Field,
|
||||
to: Field,
|
||||
|
|
@ -94,7 +94,7 @@ impl CheckerMove {
|
|||
}
|
||||
|
||||
/// Represents the Tric Trac board
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Board {
|
||||
positions: [i8; 24],
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ impl DiceRoller {
|
|||
/// Represents the two dice
|
||||
///
|
||||
/// Trictrac is always played with two dice.
|
||||
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Deserialize, Default)]
|
||||
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq, Deserialize, Default)]
|
||||
pub struct Dice {
|
||||
/// The two dice values
|
||||
pub values: (u8, u8),
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ impl From<TurnStage> for u8 {
|
|||
}
|
||||
|
||||
/// Represents a TricTrac game
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct GameState {
|
||||
pub stage: Stage,
|
||||
pub turn_stage: TurnStage,
|
||||
|
|
@ -123,6 +123,15 @@ impl GameState {
|
|||
gs
|
||||
}
|
||||
|
||||
pub fn new_with_players(p1_name: &str, p2_name: &str) -> Self {
|
||||
let mut game = Self::default();
|
||||
if let Some(p1) = game.init_player(p1_name) {
|
||||
game.init_player(p2_name);
|
||||
game.consume(&GameEvent::BeginGame { goes_first: p1 });
|
||||
}
|
||||
game
|
||||
}
|
||||
|
||||
fn set_schools_enabled(&mut self, schools_enabled: bool) {
|
||||
self.schools_enabled = schools_enabled;
|
||||
}
|
||||
|
|
@ -707,14 +716,14 @@ impl GameState {
|
|||
}
|
||||
|
||||
/// The reasons why a game could end
|
||||
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Deserialize)]
|
||||
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq, Deserialize)]
|
||||
pub enum EndGameReason {
|
||||
PlayerLeft { player_id: PlayerId },
|
||||
PlayerWon { winner: PlayerId },
|
||||
}
|
||||
|
||||
/// An event that progresses the GameState forward
|
||||
#[derive(Debug, Clone, Serialize, PartialEq, Deserialize)]
|
||||
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Deserialize)]
|
||||
pub enum GameEvent {
|
||||
BeginGame {
|
||||
goes_first: PlayerId,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use std::fmt;
|
|||
// This just makes it easier to dissern between a player id and any ol' u64
|
||||
pub type PlayerId = u64;
|
||||
|
||||
#[derive(Copy, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[derive(Copy, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum Color {
|
||||
White,
|
||||
Black,
|
||||
|
|
@ -20,7 +20,7 @@ impl Color {
|
|||
}
|
||||
|
||||
/// Struct for storing player related data.
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Player {
|
||||
pub name: String,
|
||||
pub color: Color,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue