From afef9f1b190e273a75ae51243ca9e8156ab98c9d Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 28 Jan 2025 13:07:03 -0500 Subject: [PATCH] sample game segment --- src/agent.rs | 9 +++++++++ src/game.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/main.rs | 12 +++++++----- src/repr.rs | 1 + 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/agent.rs b/src/agent.rs index c9c7ba4..97b058f 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -31,6 +31,15 @@ pub struct QueueAgent { color: Piece, } +impl QueueAgent { + pub fn new(moves: impl IntoIterator, 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() diff --git a/src/game.rs b/src/game.rs index f4c1ea4..a3ab673 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,4 +1,5 @@ use crate::{agent::Agent, repr::Board}; +use std::time::Duration; pub struct Game { players: [Box; 2], @@ -19,3 +20,40 @@ impl std::fmt::Display for Game { Ok(()) } } + +impl Game { + pub const fn new(player1: Box, player2: Box) -> 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); + } + } +} diff --git a/src/main.rs b/src/main.rs index 9a94f1c..8c1fa3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(); } diff --git a/src/repr.rs b/src/repr.rs index 35e4594..985d770 100644 --- a/src/repr.rs +++ b/src/repr.rs @@ -45,6 +45,7 @@ impl fmt::Display for Board { } writeln!(f)?; } + writeln!(f, "{}", "-".repeat(BOARD_SIZE * 2 + 1))?; Ok(()) } }