woah
This commit is contained in:
18
src/agent.rs
18
src/agent.rs
@@ -52,21 +52,3 @@ impl Agent for QueueAgent {
|
||||
self.color
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AutoAgent {
|
||||
color: Piece,
|
||||
}
|
||||
|
||||
impl Agent for AutoAgent {
|
||||
fn next_move(&mut self, _: &Board) -> Option<(usize, usize)> {
|
||||
todo!("next_move not implemented")
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Auto Agent"
|
||||
}
|
||||
|
||||
fn color(&self) -> Piece {
|
||||
self.color
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::{misc::split_from, piece::Piece};
|
||||
use std::fmt;
|
||||
|
||||
const BOARD_SIZE: usize = 8;
|
||||
pub const BOARD_SIZE: usize = 8;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Board {
|
||||
|
||||
43
src/complexagent.rs
Normal file
43
src/complexagent.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use crate::{
|
||||
agent::Agent,
|
||||
board::{Board, BOARD_SIZE},
|
||||
misc::{offsets, split_from},
|
||||
piece::Piece,
|
||||
};
|
||||
use rand::{seq::IteratorRandom, Rng};
|
||||
|
||||
pub struct ComplexAgent {
|
||||
color: Piece,
|
||||
}
|
||||
|
||||
impl Agent for ComplexAgent {
|
||||
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
|
||||
(0..BOARD_SIZE)
|
||||
.zip(0..BOARD_SIZE)
|
||||
.filter(|&(i, j)| board.get(i, j) == &Some(!self.color()))
|
||||
.flat_map(|(i, j)| offsets(i, j, 1))
|
||||
.filter(|&(i, j)| i < BOARD_SIZE && j < BOARD_SIZE)
|
||||
.filter(|&(i, j)| board.get(i, j).is_none())
|
||||
.choose(&mut rand::rng())
|
||||
.or_else(|| {
|
||||
(0..BOARD_SIZE)
|
||||
.zip(0..BOARD_SIZE)
|
||||
.filter(|&(i, j)| board.get(i, j).is_none())
|
||||
.choose(&mut rand::rng())
|
||||
})
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Complex Agent"
|
||||
}
|
||||
|
||||
fn color(&self) -> Piece {
|
||||
self.color
|
||||
}
|
||||
}
|
||||
|
||||
impl ComplexAgent {
|
||||
pub const fn new(color: Piece) -> Self {
|
||||
Self { color }
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ impl Game {
|
||||
todo!("handle invalid player move");
|
||||
}
|
||||
} else {
|
||||
todo!("handle player not having a move to make");
|
||||
panic!("player {} did not make a move", player_i);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ impl Game {
|
||||
|
||||
println!("{}", self);
|
||||
|
||||
std::thread::sleep(Duration::from_millis(500));
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
21
src/main.rs
21
src/main.rs
@@ -1,22 +1,27 @@
|
||||
use agent::QueueAgent;
|
||||
use complexagent::ComplexAgent;
|
||||
use game::Game;
|
||||
use piece::Piece;
|
||||
|
||||
mod agent;
|
||||
mod board;
|
||||
mod complexagent;
|
||||
mod game;
|
||||
mod misc;
|
||||
mod piece;
|
||||
|
||||
fn main() {
|
||||
let player1 = QueueAgent::new(
|
||||
[(0, 0), (1, 0), (1, 2), (3, 2), (3, 0), (3, 4), (0, 1)],
|
||||
Piece::Black,
|
||||
);
|
||||
let player2 = QueueAgent::new(
|
||||
[(1, 1), (2, 2), (3, 3), (3, 1), (0, 2), (4, 2), (3, 5)],
|
||||
Piece::White,
|
||||
);
|
||||
// let player1 = QueueAgent::new(
|
||||
// [(0, 0), (1, 0), (1, 2), (3, 2), (3, 0), (3, 4), (0, 1)],
|
||||
// Piece::Black,
|
||||
// );
|
||||
// let player2 = QueueAgent::new(
|
||||
// [(1, 1), (2, 2), (3, 3), (3, 1), (0, 2), (4, 2), (3, 5)],
|
||||
// Piece::White,
|
||||
// );
|
||||
|
||||
let player1 = ComplexAgent::new(Piece::Black);
|
||||
let player2 = ComplexAgent::new(Piece::White);
|
||||
let mut game = Game::new(Box::new(player1), Box::new(player2));
|
||||
game.game_loop();
|
||||
}
|
||||
|
||||
22
src/misc.rs
22
src/misc.rs
@@ -1,5 +1,7 @@
|
||||
use std::{iter::Rev, ops::RangeInclusive};
|
||||
|
||||
use num::CheckedSub;
|
||||
|
||||
pub fn split_from<T>(min: T, max: T, x: T) -> Vec<Vec<T>>
|
||||
where
|
||||
T: num::Integer + Copy,
|
||||
@@ -22,6 +24,26 @@ where
|
||||
output
|
||||
}
|
||||
|
||||
pub fn offsets(i: usize, j: usize, range: usize) -> Vec<(usize, usize)> {
|
||||
let mut output = Vec::new();
|
||||
for i_o in [-(range as i16), 0, range as i16] {
|
||||
let new_i = i as i16 + i_o;
|
||||
if 0 > new_i {
|
||||
continue;
|
||||
}
|
||||
|
||||
for j_o in [-(range as i16), 0, range as i16] {
|
||||
let new_j = j as i16 + j_o;
|
||||
if 0 > new_j {
|
||||
continue;
|
||||
}
|
||||
output.push((new_i as usize, new_j as usize));
|
||||
}
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user