sample game segment

This commit is contained in:
Simon Gardling 2025-01-28 13:07:03 -05:00
parent ef9a07242d
commit afef9f1b19
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
4 changed files with 55 additions and 5 deletions

View File

@ -31,6 +31,15 @@ pub struct QueueAgent {
color: Piece,
}
impl QueueAgent {
pub fn new(moves: impl IntoIterator<Item = (usize, usize)>, color: Piece) -> Self {
Self {
moves: moves.into_iter().collect(),
color,
}
}
}
impl Agent for QueueAgent {
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
self.moves.pop_front()

View File

@ -1,4 +1,5 @@
use crate::{agent::Agent, repr::Board};
use std::time::Duration;
pub struct Game {
players: [Box<dyn Agent>; 2],
@ -19,3 +20,40 @@ impl std::fmt::Display for Game {
Ok(())
}
}
impl Game {
pub const fn new(player1: Box<dyn Agent>, player2: Box<dyn Agent>) -> Self {
Self {
players: [player1, player2],
board: Board::new(),
}
}
pub fn step(&mut self, player_i: usize) {
let player_move = self.players[player_i].next_move(&self.board);
if let Some((i, j)) = player_move {
if let Ok(()) = self.board.place(i, j, self.players[player_i].color()) {
} else {
todo!("handle invalid player move");
}
} else {
todo!("handle player not having a move to make");
}
}
// TODO! make this loop good
pub fn game_loop(&mut self) {
loop {
println!("{}", self);
std::thread::sleep(Duration::from_millis(500));
self.step(0);
println!("{}", self);
std::thread::sleep(Duration::from_millis(500));
self.step(1);
println!("{}", self);
}
}
}

View File

@ -1,4 +1,6 @@
use repr::{Board, Piece};
use agent::QueueAgent;
use game::Game;
use repr::Piece;
mod agent;
mod game;
@ -6,8 +8,8 @@ mod misc;
mod repr;
fn main() {
let mut board = Board::new();
board.place(0, 1, Piece::Black).unwrap();
board.place(0, 3, Piece::White).unwrap();
println!("{}", board);
let player1 = QueueAgent::new([(0, 0), (1, 0), (1, 2), (3, 2), (3, 0)], Piece::Black);
let player2 = QueueAgent::new([(1, 1), (2, 2), (3, 3), (3, 1), (0, 2)], Piece::White);
let mut game = Game::new(Box::new(player1), Box::new(player2));
game.game_loop();
}

View File

@ -45,6 +45,7 @@ impl fmt::Display for Board {
}
writeln!(f)?;
}
writeln!(f, "{}", "-".repeat(BOARD_SIZE * 2 + 1))?;
Ok(())
}
}