improve player management

This commit is contained in:
Simon Gardling 2025-01-28 14:16:16 -05:00
parent 17e774ae57
commit 565f638b1b
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
5 changed files with 42 additions and 43 deletions

View File

@ -1,7 +1,6 @@
use crate::{board::Board, piece::Piece};
use std::collections::VecDeque;
use crate::repr::{Board, Piece};
pub trait Agent {
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)>;
fn name(&self) -> &'static str;

View File

@ -1,36 +1,6 @@
use crate::misc::split_from;
use crate::{misc::split_from, piece::Piece};
use std::fmt;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Piece {
Black,
White,
}
impl Piece {
pub const fn flip(&self) -> Self {
match self {
Piece::Black => Piece::White,
Piece::White => Piece::Black,
}
}
pub const fn text(&self) -> &'static str {
match self {
Piece::White => "",
Piece::Black => "",
}
}
}
impl std::ops::Not for Piece {
type Output = Piece;
fn not(self) -> Self::Output {
self.flip()
}
}
const BOARD_SIZE: usize = 8;
#[derive(Copy, Clone)]

View File

@ -1,4 +1,4 @@
use crate::{agent::Agent, repr::Board};
use crate::{agent::Agent, board::Board};
use std::time::Duration;
pub struct Game {
@ -43,17 +43,17 @@ impl Game {
// TODO! make this loop good
pub fn game_loop(&mut self) {
let mut i = 0;
loop {
self.step(i);
// alternate which player plays
i += 1;
i %= self.players.len();
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,11 +1,12 @@
use agent::QueueAgent;
use game::Game;
use repr::Piece;
use piece::Piece;
mod agent;
mod board;
mod game;
mod misc;
mod repr;
mod piece;
fn main() {
let player1 = QueueAgent::new(

29
src/piece.rs Normal file
View File

@ -0,0 +1,29 @@
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Piece {
Black,
White,
}
impl Piece {
pub const fn flip(&self) -> Self {
match self {
Piece::Black => Piece::White,
Piece::White => Piece::Black,
}
}
pub const fn text(&self) -> &'static str {
match self {
Piece::White => "",
Piece::Black => "",
}
}
}
impl std::ops::Not for Piece {
type Output = Piece;
fn not(self) -> Self::Output {
self.flip()
}
}