summaryrefslogtreecommitdiff
path: root/src/strategy.rs
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2019-06-24 21:04:27 +0200
committerJustin Worthe <justin@worthe-it.co.za>2019-06-24 21:04:27 +0200
commit1aeab6da05a0c7b7dad4d06a38b282a82d5e1a51 (patch)
treede76a41b53f4ba383bda118dfb97bd2ff53f7a23 /src/strategy.rs
parent6d6cb9e39c4ec3f113833042f1b7bc8082c5d650 (diff)
New command types for select and bombs
Diffstat (limited to 'src/strategy.rs')
-rw-r--r--src/strategy.rs70
1 files changed, 7 insertions, 63 deletions
diff --git a/src/strategy.rs b/src/strategy.rs
index dd5bece..31a1bee 100644
--- a/src/strategy.rs
+++ b/src/strategy.rs
@@ -1,4 +1,4 @@
-use crate::command::Command;
+use crate::command::{Command, Action};
use crate::game::{GameBoard, SimulationOutcome};
use crate::geometry::*;
@@ -179,7 +179,7 @@ fn best_player_move(node: &Node) -> Command {
.iter()
.max_by_key(|(_command, score_sum)| score_sum.avg())
.map(|(command, _score_sum)| *command)
- .unwrap_or(Command::DoNothing)
+ .unwrap_or(Command::new(Action::DoNothing))
}
fn score(state: &GameBoard) -> Score {
@@ -203,11 +203,11 @@ fn rollout(state: &GameBoard) -> Score {
player_moves
.choose(&mut rng)
.cloned()
- .unwrap_or(Command::DoNothing),
+ .unwrap_or(Command::new(Action::DoNothing)),
opponent_moves
.choose(&mut rng)
.cloned()
- .unwrap_or(Command::DoNothing),
+ .unwrap_or(Command::new(Action::DoNothing)),
]);
}
@@ -229,7 +229,7 @@ fn choose_one_existing(node: &Node, player_index: usize) -> Command {
as i32
})
.map(|(command, _score_sum)| *command)
- .unwrap_or(Command::DoNothing)
+ .unwrap_or(Command::new(Action::DoNothing))
}
fn update(node: &mut Node, commands: [Command; 2], score: Score) {
@@ -242,62 +242,6 @@ fn update(node: &mut Node, commands: [Command; 2], score: Score) {
node.score_sum += score;
}
-// fn heuristic_moves(state: &GameBoard, player_index: usize) -> Vec<Command> {
-// let worm = state.players[player_index].active_worm();
-
-// let shoots = state
-// .valid_shoot_commands(player_index, worm.position, worm.weapon_range);
-
-// let closest_powerup = state.powerups
-// .iter()
-// .min_by_key(|p| (p.position - worm.position).walking_distance());
-
-// let average_player_position = Point2d {
-// x: state.players[player_index].worms
-// .iter()
-// .map(|w| w.position.x)
-// .sum::<i8>() / state.players[player_index].worms.len() as i8,
-// y: state.players[player_index].worms
-// .iter()
-// .map(|w| w.position.y)
-// .sum::<i8>() / state.players[player_index].worms.len() as i8
-// };
-
-// let closest_opponent = state.players[GameBoard::opponent(player_index)].worms
-// .iter()
-// .min_by_key(|w| (w.position - average_player_position).walking_distance());
-
-// let mut commands = if !shoots.is_empty() {
-// // we're in combat now. Feel free to move anywhere.
-// let moves = state.valid_move_commands(player_index);
-// moves.iter().chain(shoots.iter()).cloned().collect()
-// } else if let Some(powerup) = closest_powerup {
-// // there are powerups! Let's go grab the closest one.
-// moves_towards(state, player_index, powerup.position)
-// } else if let Some(opponent) = closest_opponent {
-// // we're not currently in combat. Let's go find the closest worm.
-// moves_towards(state, player_index, opponent.position)
-// } else {
-// // this shouldn't happen
-// debug_assert!(false, "No valid heuristic moves");
-// vec!()
-// };
-// commands.push(Command::DoNothing);
-// commands
-// }
-
-// fn moves_towards(state: &GameBoard, player_index: usize, to: Point2d<i8>) -> Vec<Command> {
-// let distance = (to - state.players[player_index].active_worm().position).walking_distance();
-// state.valid_move_commands(player_index)
-// .iter()
-// .filter(|c| match c {
-// Command::Move(p) | Command::Dig(p) => (to - *p).walking_distance() < distance,
-// _ => false
-// })
-// .cloned()
-// .collect()
-// }
-
fn rollout_moves(state: &GameBoard, player_index: usize) -> ArrayVec<[Command; 8]> {
if let Some(worm) = state.players[player_index].active_worm() {
@@ -310,7 +254,7 @@ fn rollout_moves(state: &GameBoard, player_index: usize) -> ArrayVec<[Command; 8
// TODO: More directed destruction movements?
state.valid_move_commands(player_index)
} else {
- [Command::DoNothing].iter().cloned().collect()
+ [Command::new(Action::DoNothing)].into_iter().cloned().collect()
}
}
@@ -323,6 +267,6 @@ fn valid_moves(state: &GameBoard, player_index: usize) -> ArrayVec<[Command; 17]
.cloned()
.collect()
} else {
- [Command::DoNothing].iter().cloned().collect()
+ [Command::new(Action::DoNothing)].into_iter().cloned().collect()
}
}