iterator fixes
This commit is contained in:
parent
1bed0b61ad
commit
3681fd7e41
11
src/agent.rs
11
src/agent.rs
@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
board::{Board, BOARD_SIZE},
|
board::Board,
|
||||||
piece::Piece,
|
piece::Piece,
|
||||||
};
|
};
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
@ -59,14 +59,7 @@ pub struct RandomAgent {
|
|||||||
|
|
||||||
impl Agent for RandomAgent {
|
impl Agent for RandomAgent {
|
||||||
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
|
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
|
||||||
(0..BOARD_SIZE)
|
board.possible_moves(self.color).choose(&mut rand::rng())
|
||||||
.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 {
|
fn name(&self) -> &'static str {
|
||||||
|
|||||||
@ -74,6 +74,14 @@ impl Board {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn possible_moves(&self, color: Piece) -> Box<dyn Iterator<Item = (usize, usize)> + '_> {
|
||||||
|
Box::new(
|
||||||
|
(0..BOARD_SIZE)
|
||||||
|
.flat_map(|i| (0..BOARD_SIZE).map(move |j| (i, j)))
|
||||||
|
.filter(move |(i, j)| self.what_if(*i, *j, color).is_some()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns a mutable reference to a place on the [`Board`]
|
/// Returns a mutable reference to a place on the [`Board`]
|
||||||
/// at (i, j)
|
/// at (i, j)
|
||||||
pub const fn get_mut(&mut self, i: usize, j: usize) -> &mut Option<Piece> {
|
pub const fn get_mut(&mut self, i: usize, j: usize) -> &mut Option<Piece> {
|
||||||
|
|||||||
@ -1,8 +1,4 @@
|
|||||||
use crate::{
|
use crate::{agent::Agent, board::Board, piece::Piece};
|
||||||
agent::Agent,
|
|
||||||
board::{Board, BOARD_SIZE},
|
|
||||||
piece::Piece,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct ComplexAgent {
|
pub struct ComplexAgent {
|
||||||
color: Piece,
|
color: Piece,
|
||||||
@ -22,7 +18,10 @@ impl Move {
|
|||||||
if i == 0 {
|
if i == 0 {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.next_move = problem_space(&self.board, !self.color);
|
|
||||||
|
if self.next_move.is_empty() {
|
||||||
|
self.next_move = problem_space(&self.board, !self.color).collect();
|
||||||
|
}
|
||||||
self.next_move
|
self.next_move
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.for_each(|x| x.populate_next_moves(i - 1));
|
.for_each(|x| x.populate_next_moves(i - 1));
|
||||||
@ -47,37 +46,33 @@ impl Move {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn problem_space(board: &Board, color: Piece) -> Vec<Move> {
|
fn problem_space(board: &Board, color: Piece) -> Box<dyn Iterator<Item = Move> + '_> {
|
||||||
(0..BOARD_SIZE)
|
Box::new(
|
||||||
.flat_map(|i| {
|
board
|
||||||
(0..BOARD_SIZE)
|
.possible_moves(color)
|
||||||
.map(|j| (i, j))
|
.flat_map(move |(i, j)| board.what_if(i, j, color).map(|x| (i, j, x)))
|
||||||
.collect::<Vec<(usize, usize)>>()
|
.map(move |(i, j, (board, captured))| Move {
|
||||||
})
|
i,
|
||||||
.flat_map(|(i, j)| board.what_if(i, j, color).map(|x| (i, j, x)))
|
j,
|
||||||
.map(|(i, j, (board, captured))| Move {
|
captured,
|
||||||
i,
|
color,
|
||||||
j,
|
board,
|
||||||
captured,
|
next_move: Vec::new(),
|
||||||
color,
|
}),
|
||||||
board,
|
)
|
||||||
next_move: Vec::new(),
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Agent for ComplexAgent {
|
impl Agent for ComplexAgent {
|
||||||
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
|
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
|
||||||
const LOOPS: usize = 5;
|
const LOOPS: usize = 5;
|
||||||
problem_space(board, self.color)
|
problem_space(board, self.color)
|
||||||
.into_iter()
|
|
||||||
.map(|mut x| {
|
.map(|mut x| {
|
||||||
x.populate_next_moves(LOOPS);
|
x.populate_next_moves(LOOPS);
|
||||||
x
|
x
|
||||||
})
|
})
|
||||||
.map(|m| ((m.i, m.j), m.value(self.color)))
|
.map(|m| ((m.i, m.j), m.value(self.color)))
|
||||||
.max_by_key(|&(_, c)| c)
|
.max_by_key(|a| a.1)
|
||||||
.map(|(x, _)| x)
|
.map(|a| a.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name(&self) -> &'static str {
|
fn name(&self) -> &'static str {
|
||||||
|
|||||||
10
src/main.rs
10
src/main.rs
@ -1,5 +1,3 @@
|
|||||||
use agent::{ManualAgent, RandomAgent};
|
|
||||||
use complexagent::ComplexAgent;
|
|
||||||
use game::Game;
|
use game::Game;
|
||||||
use piece::Piece;
|
use piece::Piece;
|
||||||
|
|
||||||
@ -11,10 +9,10 @@ mod misc;
|
|||||||
mod piece;
|
mod piece;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let player1 = ComplexAgent::new(Piece::Black);
|
let player1 = complexagent::ComplexAgent::new(Piece::Black);
|
||||||
// let player2 = ComplexAgent::new(Piece::White);
|
// let player2 = complexagent::ComplexAgent::new(Piece::White);
|
||||||
let player2 = ManualAgent::new(Piece::White);
|
let player2 = agent::ManualAgent::new(Piece::White);
|
||||||
// let player2 = RandomAgent::new(Piece::White);
|
// let player2 = agent::RandomAgent::new(Piece::White);
|
||||||
let mut game = Game::new(Box::new(player1), Box::new(player2));
|
let mut game = Game::new(Box::new(player1), Box::new(player2));
|
||||||
game.game_loop();
|
game.game_loop();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user