improve player management
This commit is contained in:
parent
17e774ae57
commit
565f638b1b
@ -1,7 +1,6 @@
|
|||||||
|
use crate::{board::Board, piece::Piece};
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
use crate::repr::{Board, Piece};
|
|
||||||
|
|
||||||
pub trait Agent {
|
pub trait Agent {
|
||||||
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)>;
|
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)>;
|
||||||
fn name(&self) -> &'static str;
|
fn name(&self) -> &'static str;
|
||||||
|
|||||||
@ -1,36 +1,6 @@
|
|||||||
use crate::misc::split_from;
|
use crate::{misc::split_from, piece::Piece};
|
||||||
use std::fmt;
|
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;
|
const BOARD_SIZE: usize = 8;
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
16
src/game.rs
16
src/game.rs
@ -1,4 +1,4 @@
|
|||||||
use crate::{agent::Agent, repr::Board};
|
use crate::{agent::Agent, board::Board};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
@ -43,17 +43,17 @@ impl Game {
|
|||||||
|
|
||||||
// TODO! make this loop good
|
// TODO! make this loop good
|
||||||
pub fn game_loop(&mut self) {
|
pub fn game_loop(&mut self) {
|
||||||
|
let mut i = 0;
|
||||||
loop {
|
loop {
|
||||||
|
self.step(i);
|
||||||
|
|
||||||
|
// alternate which player plays
|
||||||
|
i += 1;
|
||||||
|
i %= self.players.len();
|
||||||
|
|
||||||
println!("{}", self);
|
println!("{}", self);
|
||||||
|
|
||||||
std::thread::sleep(Duration::from_millis(500));
|
std::thread::sleep(Duration::from_millis(500));
|
||||||
self.step(0);
|
|
||||||
println!("{}", self);
|
|
||||||
|
|
||||||
std::thread::sleep(Duration::from_millis(500));
|
|
||||||
|
|
||||||
self.step(1);
|
|
||||||
println!("{}", self);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
use agent::QueueAgent;
|
use agent::QueueAgent;
|
||||||
use game::Game;
|
use game::Game;
|
||||||
use repr::Piece;
|
use piece::Piece;
|
||||||
|
|
||||||
mod agent;
|
mod agent;
|
||||||
|
mod board;
|
||||||
mod game;
|
mod game;
|
||||||
mod misc;
|
mod misc;
|
||||||
mod repr;
|
mod piece;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let player1 = QueueAgent::new(
|
let player1 = QueueAgent::new(
|
||||||
|
|||||||
29
src/piece.rs
Normal file
29
src/piece.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user