add RandomAgent
This commit is contained in:
37
src/agent.rs
37
src/agent.rs
@@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user