Skip to content

Instantly share code, notes, and snippets.

@mtnbarreto
Last active May 28, 2024 00:29
Show Gist options
  • Select an option

  • Save mtnbarreto/31685eedbd17c8efe8e27f259a7dbecf to your computer and use it in GitHub Desktop.

Select an option

Save mtnbarreto/31685eedbd17c8efe8e27f259a7dbecf to your computer and use it in GitHub Desktop.
#[allow(dead_code)]
#[derive(Named)]
struct RandomStrategy {
random_move: RandomMove,
}
impl Strategy for RandomStrategy {
fn play_for_favoured_move(&mut self, _favoured_move: Move) -> Move {
return self.random_move.get();
}
fn handle_last_round(&mut self, _round: Round, _favoured_move: Move) {
// "Given the last round `round` and given that your favoured move is `favoured_move`, would you adjust your next move? if so, how?"
}
}
#[allow(dead_code)]
#[derive(Named)]
struct SwitchStrategy {
past_move: Move,
}
impl Strategy for SwitchStrategy {
fn play_for_favoured_move(&mut self, _favoured_move: Move) -> Move {
// self.past_move = self.past_move.opposite();
// self.past_move
// _favoured_move
Move::X
}
fn handle_last_round(&mut self, _round: Round, _favoured_move: Move) {
// "Given the last round `round` and given that your favoured move is `favoured_move`, would you adjust your next move? if so, how?"
}
}
#[test]
fn play_agains_random() {
let mut my_stategy = MyStrategy { last_rounds: Vec::new(), opponent_has_static_strategy: false };
// let mut my_stategy2 = MyStrategy { last_rounds: Vec::new(), opponent_has_static_strategy: false };
let mut random_streategy = RandomStrategy { random_move: RandomMove::new(0.5, 0.2) };
// let mut switch_strategy = SwitchStrategy { past_move: Move::Y };
// lets play 100 rounds
for _ in 1..=100 {
let player_a_move = my_stategy.play_for_favoured_move(Move::X);
let player_b_move: Move = random_streategy.play_for_favoured_move(Move::Y);
// let player_b_move: Move = switch_strategy.play_for_favoured_move(Move::Y);
// let player_b_move = my_stategy2.play_for_favoured_move(Move::Y);
my_stategy.handle_last_round(
Round { my_move: player_a_move, opponent_move: player_b_move },
Move::X,
);
// my_stategy2.handle_last_round(
// Round { my_move: player_b_move, opponent_move: player_a_move },
// Move::Y,
// );
random_streategy.handle_last_round(
Round { my_move: player_b_move, opponent_move: player_a_move },
Move::Y,
);
// switch_strategy.handle_last_round(
// Round { my_move: player_b_move, opponent_move: player_a_move },
// Move::Y,
// );
}
fn calculate_points(strategy: MyStrategy) -> (i32, i32) {
strategy.last_rounds.iter().fold((0, 0), |(mut a_points, mut b_points), round| {
let (round_a_points, round_b_points) = match (round.my_move, round.opponent_move) {
(Move::X, Move::X) => (250, 50),
(Move::Y, Move::Y) => (50, 250),
(Move::Z, Move::Z) => (100, 100),
_ => (0, 0),
};
a_points += round_a_points;
b_points += round_b_points;
(a_points, b_points)
})
}
let points = calculate_points(my_stategy);
println!("myPoints: {}, oponentPoints: {}", points.0, points.1);
assert!(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment