wip
This commit is contained in:
parent
d67b7a7c01
commit
4da70e21b3
3 changed files with 59 additions and 13 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
use crate::board::{Board, CheckerMove};
|
use crate::board::{Board, CheckerMove};
|
||||||
use crate::dice::Dice;
|
use crate::dice::Dice;
|
||||||
use crate::game_rules_moves::MoveRules;
|
use crate::game_rules_moves::MoveRules;
|
||||||
use crate::game_rules_points::{PointsRules, PossibleJans};
|
use crate::game_rules_points::{PointsRules, PossibleJans, PossibleJansMethods};
|
||||||
use crate::player::{Color, Player, PlayerId};
|
use crate::player::{Color, Player, PlayerId};
|
||||||
use log::{debug, error};
|
use log::{debug, error};
|
||||||
|
|
||||||
|
|
@ -146,19 +146,31 @@ impl GameState {
|
||||||
pub fn mirror(&self) -> GameState {
|
pub fn mirror(&self) -> GameState {
|
||||||
let mirrored_active_player = if self.active_player_id == 1 { 0 } else { 1 };
|
let mirrored_active_player = if self.active_player_id == 1 { 0 } else { 1 };
|
||||||
let mut mirrored_players = HashMap::new();
|
let mut mirrored_players = HashMap::new();
|
||||||
mirrored_players.insert(1, self.players.get(&2).unwrap().mirror());
|
if let Some(p2) = self.players.get(&2) {
|
||||||
mirrored_players.insert(2, self.players.get(&1).unwrap().mirror());
|
mirrored_players.insert(1, p2.mirror());
|
||||||
|
}
|
||||||
|
if let Some(p1) = self.players.get(&1) {
|
||||||
|
mirrored_players.insert(2, p1.mirror());
|
||||||
|
}
|
||||||
|
let mirrored_history = self
|
||||||
|
.history
|
||||||
|
.clone()
|
||||||
|
.iter()
|
||||||
|
.map(|evt| evt.get_mirror())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let (move1, move2) = self.dice_moves;
|
||||||
GameState {
|
GameState {
|
||||||
stage: self.stage,
|
stage: self.stage,
|
||||||
turn_stage: self.turn_stage,
|
turn_stage: self.turn_stage,
|
||||||
board: self.board.mirror(),
|
board: self.board.mirror(),
|
||||||
active_player_id: mirrored_active_player,
|
active_player_id: mirrored_active_player,
|
||||||
players: mirrored_players,
|
players: mirrored_players,
|
||||||
history: Vec::new(),
|
history: mirrored_history,
|
||||||
dice: self.dice,
|
dice: self.dice,
|
||||||
dice_points: self.dice_points,
|
dice_points: self.dice_points,
|
||||||
dice_moves: (CheckerMove::default(), CheckerMove::default()),
|
dice_moves: (move1.mirror(), move2.mirror()),
|
||||||
dice_jans: PossibleJans::default(),
|
dice_jans: self.dice_jans.mirror(),
|
||||||
roll_first: self.roll_first,
|
roll_first: self.roll_first,
|
||||||
schools_enabled: self.schools_enabled,
|
schools_enabled: self.schools_enabled,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -69,10 +69,26 @@ pub type PossibleJans = HashMap<Jan, Vec<(CheckerMove, CheckerMove)>>;
|
||||||
pub trait PossibleJansMethods {
|
pub trait PossibleJansMethods {
|
||||||
fn push(&mut self, jan: Jan, cmoves: (CheckerMove, CheckerMove));
|
fn push(&mut self, jan: Jan, cmoves: (CheckerMove, CheckerMove));
|
||||||
fn merge(&mut self, other: Self);
|
fn merge(&mut self, other: Self);
|
||||||
|
fn mirror(&self) -> Self;
|
||||||
// fn get_points(&self) -> u8;
|
// fn get_points(&self) -> u8;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PossibleJansMethods for PossibleJans {
|
impl PossibleJansMethods for PossibleJans {
|
||||||
|
fn mirror(&self) -> Self {
|
||||||
|
self.clone()
|
||||||
|
.into_iter()
|
||||||
|
.map(|(jan, moves)| {
|
||||||
|
(
|
||||||
|
jan,
|
||||||
|
moves
|
||||||
|
.into_iter()
|
||||||
|
.map(|(m1, m2)| (m1.mirror(), m2.mirror()))
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
fn push(&mut self, jan: Jan, cmoves: (CheckerMove, CheckerMove)) {
|
fn push(&mut self, jan: Jan, cmoves: (CheckerMove, CheckerMove)) {
|
||||||
if let Some(ways) = self.get_mut(&jan) {
|
if let Some(ways) = self.get_mut(&jan) {
|
||||||
if !ways.contains(&cmoves) {
|
if !ways.contains(&cmoves) {
|
||||||
|
|
|
||||||
|
|
@ -50,9 +50,13 @@ impl TricTrac {
|
||||||
self.game_state.active_player_id - 1
|
self.game_state.active_player_id - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_legal_actions(&self, player_id: u64) -> Vec<usize> {
|
fn get_legal_actions(&self, player_idx: u64) -> Vec<usize> {
|
||||||
if player_id == self.current_player_idx() {
|
if player_idx == self.current_player_idx() {
|
||||||
get_valid_action_indices(&self.game_state)
|
if player_idx == 0 {
|
||||||
|
get_valid_action_indices(&self.game_state)
|
||||||
|
} else {
|
||||||
|
get_valid_action_indices(&self.game_state.mirror())
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
vec![]
|
vec![]
|
||||||
}
|
}
|
||||||
|
|
@ -84,6 +88,12 @@ impl TricTrac {
|
||||||
TrictracAction::from_action_index(action_idx).and_then(|a| a.to_event(&self.game_state))
|
TrictracAction::from_action_index(action_idx).and_then(|a| a.to_event(&self.game_state))
|
||||||
{
|
{
|
||||||
println!("get event {:?}", event);
|
println!("get event {:?}", event);
|
||||||
|
|
||||||
|
let event = if self.game_state.active_player_id == 2 {
|
||||||
|
event.get_mirror()
|
||||||
|
} else {
|
||||||
|
event
|
||||||
|
};
|
||||||
if self.game_state.validate(&event) {
|
if self.game_state.validate(&event) {
|
||||||
println!("valid event");
|
println!("valid event");
|
||||||
self.game_state.consume(&event);
|
self.game_state.consume(&event);
|
||||||
|
|
@ -113,12 +123,20 @@ impl TricTrac {
|
||||||
[self.get_score(1), self.get_score(2)]
|
[self.get_score(1), self.get_score(2)]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tensor(&self, player: PlayerId) -> Vec<i8> {
|
fn get_tensor(&self, player_idx: u64) -> Vec<i8> {
|
||||||
self.game_state.to_vec()
|
if player_idx == 0 {
|
||||||
|
self.game_state.to_vec()
|
||||||
|
} else {
|
||||||
|
self.game_state.mirror().to_vec()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_observation_string(&self, player: PlayerId) -> String {
|
fn get_observation_string(&self, player_idx: u64) -> String {
|
||||||
self.__str__()
|
if player_idx == 0 {
|
||||||
|
format!("{}", self.game_state)
|
||||||
|
} else {
|
||||||
|
format!("{}", self.game_state.mirror())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Afficher l'état du jeu (pour le débogage)
|
/// Afficher l'état du jeu (pour le débogage)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue