1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
use rand::seq::SliceRandom;
use rand::Rng;
use std::collections::HashMap;
use std::collections::HashSet;
use std::hash::BuildHasher;
use crate::game;
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct Opponent {
difficulty: Difficulty,
}
impl Opponent {
pub fn new(difficulty: Difficulty) -> Self {
Self { difficulty }
}
pub fn get_move(&self, game: &game::Game) -> Option<game::Position> {
let outcomes = self.evaluate_game(game);
best_position(&outcomes)
}
pub fn evaluate_game(&self, game: &game::Game) -> HashMap<game::Position, Outcome> {
if let Some(outcomes) = self.get_cached_outcomes(&game) {
outcomes
} else {
let mut outcomes = HashMap::new();
let ai_player = AiPlayer::from_game_state(game.state());
for position in game.free_positions() {
let outcome = self.evaluate_position(&game, position, ai_player, 0);
outcomes.insert(position, outcome);
}
outcomes
}
}
fn evaluate_position(
&self,
game: &game::Game,
position: game::Position,
ai_player: AiPlayer,
depth: i32,
) -> Outcome {
const MAX_RECURSION_DEPTH: i32 = 20;
assert!(
depth <= MAX_RECURSION_DEPTH,
"The AI algorithm has reached the maximum recursion limit of {} and \
cannot continue to evaluate the game. This condition is the result \
of a bug in the open_ttt_lib used by this application.",
depth
);
debug_assert!(
game.can_move(position),
"Cannot move into the provided position, {:?}. Thus, the position \
cannot be evaluated. Ensure the game is not over and the position \
is free. This condition is the result of a bug in the open_ttt_lib \
used by this application.",
position
);
if !self.difficulty.should_evaluate_node(depth) {
return Outcome::Unknown;
}
let is_my_turn = ai_player == AiPlayer::from_game_state(game.state());
let mut game = game.clone();
let state = game.do_move(position).unwrap();
if state.is_game_over() {
return Outcome::from_game_state(state, ai_player);
}
let mut outcomes = HashSet::new();
for free_position in game.free_positions() {
let outcome = self.evaluate_position(&game, free_position, ai_player, depth + 1);
if is_worst_outcome(outcome, is_my_turn) {
return outcome;
}
outcomes.insert(outcome);
}
worst_outcome(&outcomes, is_my_turn)
}
fn get_cached_outcomes(&self, game: &game::Game) -> Option<HashMap<game::Position, Outcome>> {
if game.state().is_game_over() {
Some(HashMap::new())
} else if is_new_game(&game) {
let outcomes =
initialize_free_position_outcomes(game.free_positions(), Outcome::CatsGame);
Some(outcomes)
} else {
None
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
pub enum Difficulty {
None,
Easy,
Medium,
Hard,
Unbeatable,
Custom(fn(depth: i32) -> bool),
}
impl Difficulty {
fn should_evaluate_node(&self, depth: i32) -> bool {
match self {
Self::None => Difficulty::none_should_evaluate_node(),
Self::Easy => Difficulty::easy_should_evaluate_node(depth),
Self::Medium => Difficulty::medium_should_evaluate_node(depth),
Self::Hard => Difficulty::hard_should_evaluate_node(depth),
Self::Unbeatable => Difficulty::unbeatable_should_evaluate_node(),
Self::Custom(custom_should_evaluate_node) => custom_should_evaluate_node(depth),
}
}
fn none_should_evaluate_node() -> bool {
false
}
fn easy_should_evaluate_node(depth: i32) -> bool {
if depth == 0 {
rand::thread_rng().gen_bool(0.5)
} else {
false
}
}
fn medium_should_evaluate_node(depth: i32) -> bool {
if depth == 0 {
rand::thread_rng().gen_bool(0.9)
} else {
rand::thread_rng().gen_bool(0.75)
}
}
fn hard_should_evaluate_node(depth: i32) -> bool {
if depth <= 1 {
true
} else {
rand::thread_rng().gen_bool(0.97)
}
}
fn unbeatable_should_evaluate_node() -> bool {
true
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Outcome {
Win,
Loss,
CatsGame,
Unknown,
}
impl Outcome {
fn from_game_state(state: game::State, ai_player: AiPlayer) -> Self {
match state {
game::State::CatsGame => Outcome::CatsGame,
game::State::PlayerXWin(_) => match ai_player {
AiPlayer::PlayerX => Outcome::Win,
AiPlayer::PlayerO => Outcome::Loss,
},
game::State::PlayerOWin(_) => match ai_player {
AiPlayer::PlayerX => Outcome::Loss,
AiPlayer::PlayerO => Outcome::Win,
},
_ => panic!(
"Cannot determine the AI outcome since the game is not over. \
This condition is the result of a bug in the \
open_ttt_lib used by this application."
),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
enum AiPlayer {
PlayerX,
PlayerO,
}
impl AiPlayer {
fn from_game_state(state: game::State) -> Self {
match state {
game::State::PlayerXMove => Self::PlayerX,
game::State::PlayerOMove => Self::PlayerO,
_ => panic!(
"Cannot determine the AI player since the game is over. \
This condition is the result of a bug in the \
open_ttt_lib used by this application."
),
}
}
}
pub fn best_position<S: BuildHasher>(
outcomes: &HashMap<game::Position, Outcome, S>,
) -> Option<game::Position> {
let mut outcome_to_position_map = HashMap::new();
for (position, outcome) in outcomes {
if !outcome_to_position_map.contains_key(outcome) {
outcome_to_position_map.insert(outcome, Vec::new());
}
outcome_to_position_map
.get_mut(outcome)
.unwrap()
.push(position);
}
let best_to_worst_outcomes = [
Outcome::Win,
Outcome::CatsGame,
Outcome::Unknown,
Outcome::Loss,
];
for outcome in &best_to_worst_outcomes {
if outcome_to_position_map.contains_key(outcome) {
let random_position = **outcome_to_position_map
.get(outcome)
.unwrap()
.choose(&mut rand::thread_rng())
.unwrap();
return Some(random_position);
}
}
None
}
fn initialize_free_position_outcomes(
free_positions: game::FreePositions,
outcome: Outcome,
) -> HashMap<game::Position, Outcome> {
let mut outcomes = HashMap::new();
for position in free_positions {
outcomes.insert(position, outcome);
}
outcomes
}
fn worst_to_best_outcomes(is_my_turn: bool) -> [Outcome; 3] {
if is_my_turn {
[Outcome::Loss, Outcome::CatsGame, Outcome::Win]
} else {
[Outcome::Win, Outcome::CatsGame, Outcome::Loss]
}
}
fn is_worst_outcome(outcome: Outcome, is_my_turn: bool) -> bool {
const WORST_OUTCOME_INDEX: usize = 0;
worst_to_best_outcomes(is_my_turn)[WORST_OUTCOME_INDEX] == outcome
}
fn worst_outcome(outcomes: &HashSet<Outcome>, is_my_turn: bool) -> Outcome {
for outcome in &worst_to_best_outcomes(is_my_turn) {
if outcomes.contains(outcome) {
return *outcome;
}
}
Outcome::Unknown
}
fn is_new_game(game: &game::Game) -> bool {
let board_size = game.board().size();
let total_positions = board_size.columns * board_size.rows;
game.free_positions().count() as i32 == total_positions
}
#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
use super::*;
const PLAYER_X_MOVE_WITH_WIN_AVAILABLE: [game::Position; 6] = [
game::Position { row: 0, column: 0 },
game::Position { row: 0, column: 1 },
game::Position { row: 0, column: 2 },
game::Position { row: 1, column: 1 },
game::Position { row: 2, column: 0 },
game::Position { row: 2, column: 2 },
];
const PLAYER_X_WIN: [game::Position; 7] = [
game::Position { row: 0, column: 0 },
game::Position { row: 0, column: 1 },
game::Position { row: 0, column: 2 },
game::Position { row: 1, column: 1 },
game::Position { row: 2, column: 0 },
game::Position { row: 2, column: 2 },
game::Position { row: 1, column: 0 },
];
fn create_game(owned_positions: &[game::Position]) -> game::Game {
let mut game = game::Game::new();
for position in owned_positions {
game.do_move(*position).unwrap();
}
game
}
#[test]
fn opponent_new_should_set_difficulty() {
let expected_difficulty = Difficulty::Medium;
let opponent = Opponent::new(expected_difficulty);
let actual_difficulty = opponent.difficulty;
assert_eq!(expected_difficulty, actual_difficulty);
}
#[test]
fn opponent_get_move_when_game_is_over_should_be_none() {
let game = create_game(&PLAYER_X_WIN);
let opponent = Opponent::new(Difficulty::None);
let expected_position = None;
let actual_position = opponent.get_move(&game);
assert_eq!(
expected_position,
actual_position,
"\nGame board used for this test: \n{}",
game.board()
);
}
#[test]
fn opponent_get_move_when_unbeatable_difficulty_should_pick_wining_position() {
let game = create_game(&PLAYER_X_MOVE_WITH_WIN_AVAILABLE);
let opponent = Opponent::new(Difficulty::Unbeatable);
let expected_position = game::Position { row: 1, column: 0 };
let actual_position = opponent.get_move(&game).unwrap();
assert_eq!(
expected_position,
actual_position,
"\nGame board used for this test: \n{}",
game.board()
);
}
#[test]
fn opponent_evaluate_game_when_new_game_and_unbeatable_difficulty_should_be_cats_game_for_all_positions(
) {
let game = game::Game::new();
let opponent = Opponent::new(Difficulty::Unbeatable);
let expected_outcomes =
initialize_free_position_outcomes(game.free_positions(), Outcome::CatsGame);
let actual_outcomes = opponent.evaluate_game(&game);
assert_eq!(
expected_outcomes,
actual_outcomes,
"\nGame board used for this test: \n{}",
game.board()
);
}
#[test]
fn opponent_evaluate_game_when_game_over_should_be_empty_map() {
let game = create_game(&PLAYER_X_WIN);
let opponent = Opponent::new(Difficulty::Unbeatable);
let expected_outcomes = HashMap::new();
let actual_outcomes = opponent.evaluate_game(&game);
assert_eq!(
expected_outcomes,
actual_outcomes,
"\nGame board used for this test: \n{}",
game.board()
);
}
#[test]
fn opponent_evaluate_game_when_unbeatable_difficulty_should_evaluate_all_positions() {
let game = create_game(&PLAYER_X_MOVE_WITH_WIN_AVAILABLE);
let opponent = Opponent::new(Difficulty::Unbeatable);
let mut expected_outcomes = HashMap::new();
expected_outcomes.insert(game::Position { row: 1, column: 0 }, Outcome::Win);
expected_outcomes.insert(game::Position { row: 1, column: 2 }, Outcome::Loss);
expected_outcomes.insert(game::Position { row: 2, column: 1 }, Outcome::CatsGame);
let actual_outcomes = opponent.evaluate_game(&game);
assert_eq!(
expected_outcomes,
actual_outcomes,
"\nGame board used for this test: \n{}",
game.board()
);
}
#[test]
fn opponent_evaluate_game_when_none_difficulty_should_see_unknown_outcome_for_all_positions() {
let game = create_game(&PLAYER_X_MOVE_WITH_WIN_AVAILABLE);
let opponent = Opponent::new(Difficulty::None);
let mut expected_outcomes = HashMap::new();
expected_outcomes.insert(game::Position { row: 1, column: 0 }, Outcome::Unknown);
expected_outcomes.insert(game::Position { row: 1, column: 2 }, Outcome::Unknown);
expected_outcomes.insert(game::Position { row: 2, column: 1 }, Outcome::Unknown);
let actual_outcomes = opponent.evaluate_game(&game);
assert_eq!(
expected_outcomes,
actual_outcomes,
"\nGame board used for this test: \n{}",
game.board()
);
}
#[test]
fn opponent_evaluate_game_depth_should_start_at_zero() {
let game = create_game(&PLAYER_X_MOVE_WITH_WIN_AVAILABLE);
let opponent = Opponent::new(Difficulty::Custom(|depth| {
assert_eq!(depth, 0);
false
}));
opponent.evaluate_game(&game);
}
#[test]
#[should_panic(expected = "The depth has been incremented.")]
fn opponent_evaluate_game_should_increment_depth() {
let game = create_game(&PLAYER_X_MOVE_WITH_WIN_AVAILABLE);
let opponent = Opponent::new(Difficulty::Custom(|depth| {
if depth > 0 {
panic!("The depth has been incremented.");
}
true
}));
opponent.evaluate_game(&game);
}
#[test]
fn opponent_best_position_when_outcomes_empty_should_none() {
let outcomes = HashMap::new();
let expected_position = None;
let actual_position = best_position(&outcomes);
assert_eq!(expected_position, actual_position);
}
#[test]
fn opponent_best_position_when_win_and_cats_game_should_be_win() {
let mut outcomes = HashMap::new();
outcomes.insert(game::Position { row: 0, column: 0 }, Outcome::CatsGame);
let expected_position = game::Position { row: 0, column: 1 };
outcomes.insert(expected_position, Outcome::Win);
let actual_position = best_position(&outcomes);
assert_eq!(Some(expected_position), actual_position);
}
#[test]
fn opponent_best_position_when_win_and_unknown_should_be_win() {
let mut outcomes = HashMap::new();
outcomes.insert(game::Position { row: 0, column: 0 }, Outcome::Unknown);
let expected_position = game::Position { row: 0, column: 1 };
outcomes.insert(expected_position, Outcome::Win);
let actual_position = best_position(&outcomes);
assert_eq!(Some(expected_position), actual_position);
}
#[test]
fn opponent_best_position_when_win_and_loss_should_be_win() {
let mut outcomes = HashMap::new();
outcomes.insert(game::Position { row: 0, column: 0 }, Outcome::Loss);
let expected_position = game::Position { row: 0, column: 1 };
outcomes.insert(expected_position, Outcome::Win);
let actual_position = best_position(&outcomes);
assert_eq!(Some(expected_position), actual_position);
}
#[test]
fn opponent_best_position_when_cats_game_and_loss_should_be_cats_game() {
let mut outcomes = HashMap::new();
outcomes.insert(game::Position { row: 0, column: 0 }, Outcome::Loss);
let expected_position = game::Position { row: 0, column: 1 };
outcomes.insert(expected_position, Outcome::CatsGame);
let actual_position = best_position(&outcomes);
assert_eq!(Some(expected_position), actual_position);
}
#[test]
fn opponent_best_position_when_cats_game_and_unknown_should_be_cats_game() {
let mut outcomes = HashMap::new();
outcomes.insert(game::Position { row: 0, column: 0 }, Outcome::Unknown);
let expected_position = game::Position { row: 0, column: 1 };
outcomes.insert(expected_position, Outcome::CatsGame);
let actual_position = best_position(&outcomes);
assert_eq!(Some(expected_position), actual_position);
}
#[test]
fn opponent_best_position_when_unknown_and_loss_should_be_unknown() {
let mut outcomes = HashMap::new();
outcomes.insert(game::Position { row: 0, column: 0 }, Outcome::Loss);
let expected_position = game::Position { row: 0, column: 1 };
outcomes.insert(expected_position, Outcome::Unknown);
let actual_position = best_position(&outcomes);
assert_eq!(Some(expected_position), actual_position);
}
#[test]
fn opponent_best_position_when_same_outcome_should_pick_random_position() {
let mut outcomes = HashMap::new();
outcomes.insert(game::Position { row: 0, column: 0 }, Outcome::CatsGame);
outcomes.insert(game::Position { row: 0, column: 1 }, Outcome::CatsGame);
outcomes.insert(game::Position { row: 0, column: 2 }, Outcome::CatsGame);
let mut positions_set = HashSet::new();
const NUM_TIMES: i32 = 300;
for _ in 0..NUM_TIMES {
let position = best_position(&outcomes);
positions_set.insert(position);
}
assert_eq!(
outcomes.len(),
positions_set.len(),
"This test relies on random behavior. Therefore, it is possible, \
although highly unlikely, that the test could fail even if the \
code is working as expected. If this happens try re-running the \
test a few times. Continual failures indicate there is a problem \
that needs addressed in the code as the requirement of picking \
random positions is not being fulfilled."
);
}
#[test]
fn difficulty_when_custom_should_call_provided_function() {
const TRUE_DEPTH_VALUE: i32 = 42_000;
let custom_difficulty = Difficulty::Custom(|depth| depth == TRUE_DEPTH_VALUE);
assert!(custom_difficulty.should_evaluate_node(TRUE_DEPTH_VALUE));
assert!(!custom_difficulty.should_evaluate_node(0));
}
#[test]
fn ai_player_from_game_state_when_player_X_move_should_be_player_X() {
let game_state = game::State::PlayerXMove;
let expected_ai_player = AiPlayer::PlayerX;
let actual_ai_player = AiPlayer::from_game_state(game_state);
assert_eq!(expected_ai_player, actual_ai_player);
}
#[test]
fn ai_player_from_game_state_when_player_O_move_should_be_player_O() {
let game_state = game::State::PlayerOMove;
let expected_ai_player = AiPlayer::PlayerO;
let actual_ai_player = AiPlayer::from_game_state(game_state);
assert_eq!(expected_ai_player, actual_ai_player);
}
#[test]
#[should_panic]
fn ai_player_from_game_state_when_game_over_should_panic() {
let game_state = game::State::CatsGame;
let _actual_ai_player = AiPlayer::from_game_state(game_state);
}
#[test]
fn outcome_from_game_state_when_cats_game_should_be_cats_game() {
let game_state = game::State::CatsGame;
let ai_player = AiPlayer::PlayerX;
let expected_outcome = Outcome::CatsGame;
let actual_outcome = Outcome::from_game_state(game_state, ai_player);
assert_eq!(expected_outcome, actual_outcome);
}
#[test]
fn outcome_from_game_state_when_player_X_win_and_player_X_should_be_win() {
let game_state = game::State::PlayerXWin(Default::default());
let ai_player = AiPlayer::PlayerX;
let expected_outcome = Outcome::Win;
let actual_outcome = Outcome::from_game_state(game_state, ai_player);
assert_eq!(expected_outcome, actual_outcome);
}
#[test]
fn outcome_from_game_state_when_player_X_win_and_player_O_should_be_loss() {
let game_state = game::State::PlayerXWin(Default::default());
let ai_player = AiPlayer::PlayerO;
let expected_outcome = Outcome::Loss;
let actual_outcome = Outcome::from_game_state(game_state, ai_player);
assert_eq!(expected_outcome, actual_outcome);
}
#[test]
fn outcome_from_game_state_when_player_O_win_and_player_O_should_be_win() {
let game_state = game::State::PlayerOWin(Default::default());
let ai_player = AiPlayer::PlayerO;
let expected_outcome = Outcome::Win;
let actual_outcome = Outcome::from_game_state(game_state, ai_player);
assert_eq!(expected_outcome, actual_outcome);
}
#[test]
fn outcome_from_game_state_when_player_O_win_and_player_X_should_be_loss() {
let game_state = game::State::PlayerOWin(Default::default());
let ai_player = AiPlayer::PlayerX;
let expected_outcome = Outcome::Loss;
let actual_outcome = Outcome::from_game_state(game_state, ai_player);
assert_eq!(expected_outcome, actual_outcome);
}
#[test]
fn worst_outcome_when_empty_should_be_unknown() {
let outcomes = Default::default();
let is_my_turn = true;
let expected_outcome = Outcome::Unknown;
let actual_outcome = worst_outcome(&outcomes, is_my_turn);
assert_eq!(expected_outcome, actual_outcome);
}
#[test]
fn worst_outcome_when_my_turn_with_win_and_loss_should_be_loss() {
let outcomes = [Outcome::Win, Outcome::Loss].iter().cloned().collect();
let is_my_turn = true;
let expected_outcome = Outcome::Loss;
let actual_outcome = worst_outcome(&outcomes, is_my_turn);
assert_eq!(expected_outcome, actual_outcome);
}
#[test]
fn worst_outcome_when_my_turn_with_cats_game_and_loss_should_be_loss() {
let outcomes = [Outcome::CatsGame, Outcome::Loss].iter().cloned().collect();
let is_my_turn = true;
let expected_outcome = Outcome::Loss;
let actual_outcome = worst_outcome(&outcomes, is_my_turn);
assert_eq!(expected_outcome, actual_outcome);
}
#[test]
fn worst_outcome_when_my_turn_with_cats_game_and_cats_game_should_be_cats_game() {
let outcomes = [Outcome::Win, Outcome::CatsGame].iter().cloned().collect();
let is_my_turn = true;
let expected_outcome = Outcome::CatsGame;
let actual_outcome = worst_outcome(&outcomes, is_my_turn);
assert_eq!(expected_outcome, actual_outcome);
}
#[test]
fn worst_outcome_when_not_my_turn_with_win_and_loss_should_be_win() {
let outcomes = [Outcome::Win, Outcome::Loss].iter().cloned().collect();
let is_my_turn = false;
let expected_outcome = Outcome::Win;
let actual_outcome = worst_outcome(&outcomes, is_my_turn);
assert_eq!(expected_outcome, actual_outcome);
}
#[test]
fn worst_outcome_when_not_my_turn_with_cats_game_and_loss_should_be_cats_game() {
let outcomes = [Outcome::CatsGame, Outcome::Loss].iter().cloned().collect();
let is_my_turn = false;
let expected_outcome = Outcome::CatsGame;
let actual_outcome = worst_outcome(&outcomes, is_my_turn);
assert_eq!(expected_outcome, actual_outcome);
}
#[test]
fn worst_outcome_when_not_my_turn_with_cats_game_and_cats_game_should_be_win() {
let outcomes = [Outcome::Win, Outcome::CatsGame].iter().cloned().collect();
let is_my_turn = false;
let expected_outcome = Outcome::Win;
let actual_outcome = worst_outcome(&outcomes, is_my_turn);
assert_eq!(expected_outcome, actual_outcome);
}
#[test]
fn initialize_free_position_outcomes_should_set_indicated_outcome() {
let game = game::Game::new();
let expected_outcome = Outcome::Win;
let actual_outcomes =
initialize_free_position_outcomes(game.free_positions(), expected_outcome);
assert!(actual_outcomes
.iter()
.all(|(_position, outcome)| *outcome == expected_outcome));
}
}