Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoDog896 committed Sep 26, 2024
1 parent 409f765 commit 2080e63
Showing 1 changed file with 75 additions and 5 deletions.
80 changes: 75 additions & 5 deletions crates/games/src/sprouts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,81 @@
#![doc = include_str!("./README.md")]

use game_solver::game::Game;
use petgraph::matrix_graph::MatrixGraph;
use game_solver::{game::{Game, StateType}, player::ImpartialPlayer};
use petgraph::{csr::DefaultIx, matrix_graph::{MatrixGraph, NodeIndex}, Undirected};
use thiserror::Error;

type Ix = DefaultIx;

#[derive(Debug, Clone)]
pub struct SproutsMove {
from: NodeIndex<Ix>,
to: NodeIndex<Ix>
}

type SproutsGraph = MatrixGraph<(), (), Undirected, Option<()>>;

#[derive(Clone)]
pub struct Sprouts(MatrixGraph<(), ()>);
pub struct Sprouts(SproutsGraph);

impl Sprouts {
pub fn new(node_count: usize) -> Self {
let mut graph = MatrixGraph::new_undirected();

for _ in 0..node_count {
graph.add_node(());
}

Self(graph)
}
}

#[derive(Error, Debug, Clone)]
pub enum SproutsMoveError {
#[error("chosen index {0} from move {1:?} is out of bounds.")]
MoveOutOfBounds(Ix, SproutsMove),
#[error("chosen index {0} from move {1:?} references a dead sprout.")]
DeadSprout(Ix, SproutsMove),
#[error("a move for {0:?} has already been made")]
SproutsConnected(SproutsMove)
}

impl Game for Sprouts {
type Move = SproutsMove;
type Iter<'a> = std::vec::IntoIter<Self::Move>;

type Player = ImpartialPlayer;
type MoveError = SproutsMoveError;

const STATE_TYPE: Option<StateType> = Some(StateType::Normal);

fn max_moves(&self) -> Option<usize> {
// TODO: i actually want to find what the proper paper is, but
// https://en.wikipedia.org/wiki/Sprouts_(game)#Maximum_number_of_moves
// is where this is from.
Some(self.0.node_count())
}

fn move_count(&self) -> usize {
self.0.edge_count()
}

fn make_move(&mut self, m: &Self::Move) -> Result<(), Self::MoveError> {
if self.0.has_edge(m.from, m.to) {
return Err(SproutsMoveError::SproutsConnected(m.clone()));
}

Ok(())
}

fn player(&self) -> Self::Player {
ImpartialPlayer::Next
}

// impl Game for Sprouts {
fn possible_moves(&self) -> Self::Iter<'_> {

}

// }
fn state(&self) -> game_solver::game::GameState<Self::Player> {
Self::STATE_TYPE.unwrap().state(self)
}
}

0 comments on commit 2080e63

Please sign in to comment.