add RandomAgent

This commit is contained in:
2025-02-04 19:49:51 -05:00
parent 24ad9cfafd
commit 1bed0b61ad
4 changed files with 274 additions and 2 deletions

View File

@@ -1,4 +1,8 @@
use crate::{board::Board, piece::Piece};
use crate::{
board::{Board, BOARD_SIZE},
piece::Piece,
};
use rand::prelude::*;
use std::io;
use std::io::prelude::*;
@@ -48,3 +52,34 @@ impl ManualAgent {
Self { color }
}
}
pub struct RandomAgent {
color: Piece,
}
impl Agent for RandomAgent {
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
(0..BOARD_SIZE)
.flat_map(|i| {
(0..BOARD_SIZE)
.map(|j| (i, j))
.collect::<Vec<(usize, usize)>>()
})
.flat_map(|(i, j)| board.what_if(i, j, self.color).map(|_| (i, j)))
.choose(&mut rand::rng())
}
fn name(&self) -> &'static str {
"Random Agent"
}
fn color(&self) -> Piece {
self.color
}
}
impl RandomAgent {
pub const fn new(color: Piece) -> Self {
Self { color }
}
}

View File

@@ -1,4 +1,4 @@
use agent::ManualAgent;
use agent::{ManualAgent, RandomAgent};
use complexagent::ComplexAgent;
use game::Game;
use piece::Piece;
@@ -14,6 +14,7 @@ fn main() {
let player1 = ComplexAgent::new(Piece::Black);
// let player2 = ComplexAgent::new(Piece::White);
let player2 = ManualAgent::new(Piece::White);
// let player2 = RandomAgent::new(Piece::White);
let mut game = Game::new(Box::new(player1), Box::new(player2));
game.game_loop();
}