summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2018-09-05 20:57:10 +0200
committerJustin Worthe <justin@worthe-it.co.za>2018-09-05 20:57:10 +0200
commitb249c261380323f429883e2364fed0060b2ffd54 (patch)
tree6956f3b4867357b9a0e4734f38bd9959a1a9b230
parente7e948d8861661880d2d283954288d9dba25014f (diff)
Added weighting of victory / defeat board positions
-rw-r--r--src/engine/bitwise_engine.rs4
-rw-r--r--src/strategy/monte_carlo.rs16
2 files changed, 15 insertions, 5 deletions
diff --git a/src/engine/bitwise_engine.rs b/src/engine/bitwise_engine.rs
index b9f08e3..f03f1c0 100644
--- a/src/engine/bitwise_engine.rs
+++ b/src/engine/bitwise_engine.rs
@@ -476,4 +476,8 @@ impl Player {
let mask = ROW_MASKS[y as usize];
(self.occupied & mask).count_ones() as u16
}
+
+ pub fn count_towers(&self) -> u32 {
+ self.occupied.count_ones()
+ }
}
diff --git a/src/strategy/monte_carlo.rs b/src/strategy/monte_carlo.rs
index 6180fce..7baba17 100644
--- a/src/strategy/monte_carlo.rs
+++ b/src/strategy/monte_carlo.rs
@@ -160,8 +160,8 @@ fn simulate_to_endstate<R: Rng>(command_score: &mut CommandScore, state: &Bitwis
let mut next_seed: [u8;16] = [0; 16];
rng.fill_bytes(&mut next_seed);
match status {
- GameStatus::PlayerWon => command_score.add_victory(next_seed),
- GameStatus::OpponentWon => command_score.add_defeat(next_seed),
+ GameStatus::PlayerWon => command_score.add_victory(state_mut.player.count_towers() as i32 - state_mut.opponent.count_towers() as i32, next_seed),
+ GameStatus::OpponentWon => command_score.add_defeat(state_mut.opponent.count_towers() as i32 - state_mut.player.count_towers() as i32, next_seed),
GameStatus::Continue => command_score.add_stalemate(next_seed),
GameStatus::Draw => command_score.add_draw(next_seed)
}
@@ -342,7 +342,9 @@ fn random_move<R: Rng>(player: &Player, _opponent: &Player, rng: &mut R) -> Comm
struct CommandScore {
command: Command,
starts_with_nothing: bool,
+ victory_score: i32,
victories: u32,
+ defeat_score: i32,
defeats: u32,
draws: u32,
stalemates: u32,
@@ -354,7 +356,9 @@ impl CommandScore {
fn new(command: Command, starts_with_nothing: bool) -> CommandScore {
CommandScore {
command, starts_with_nothing,
+ victory_score: 0,
victories: 0,
+ defeat_score: 0,
defeats: 0,
draws: 0,
stalemates: 0,
@@ -363,13 +367,15 @@ impl CommandScore {
}
}
- fn add_victory(&mut self, next_seed: [u8; 16]) {
+ fn add_victory(&mut self, weight: i32, next_seed: [u8; 16]) {
+ self.victory_score += weight;
self.victories += 1;
self.attempts += 1;
self.next_seed = next_seed;
}
- fn add_defeat(&mut self, next_seed: [u8; 16]) {
+ fn add_defeat(&mut self, weight: i32, next_seed: [u8; 16]) {
+ self.defeat_score += weight;
self.defeats += 1;
self.attempts += 1;
self.next_seed = next_seed;
@@ -388,7 +394,7 @@ impl CommandScore {
}
fn win_ratio(&self) -> i32 {
- (self.victories as i32 - self.defeats as i32) * 10000 / (self.attempts as i32)
+ (self.victory_score - self.defeat_score) * 10000 / (self.attempts as i32)
}
fn init_command_scores(state: &BitwiseGameState) -> Vec<CommandScore> {