-
Notifications
You must be signed in to change notification settings - Fork 1
/
tictactoe.rs
180 lines (158 loc) · 5.21 KB
/
tictactoe.rs
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
use std::rc::Rc;
use game::{Action, EndStatus, Player, TictactoeGame};
impl mctser::EndStatus for EndStatus {}
impl mctser::Action for Action {}
impl mctser::Player<EndStatus> for Player {
fn reward_when_outcome_is(&self, outcome: &EndStatus) -> f32 {
match outcome {
EndStatus::Win(winner) => {
if self == winner {
1.
} else {
0.
}
}
EndStatus::Tie => 0.5,
}
}
}
impl mctser::GameState<Player, EndStatus, Action> for TictactoeGame {
fn end_status(&self) -> Option<EndStatus> {
self.end_status
}
fn player(&self) -> Player {
self.player
}
fn possible_actions(&self) -> Vec<Action> {
let mut possible_actions = Vec::new();
for row in 0..3 {
for col in 0..3 {
if !self.occupied(Action(row, col)) {
possible_actions.push(Action(row, col));
}
}
}
possible_actions
}
fn act(&self, selection: &Action) -> Self {
self.place(selection).unwrap()
}
}
fn main() {
let mut game = Rc::new(TictactoeGame::new());
let mut search_tree = mctser::SearchTree::new(game.clone());
while game.end_status.is_none() {
let selected = search_tree.search(1000).unwrap();
search_tree.renew(&selected).unwrap();
game = search_tree.get_game_state();
game.draw_board();
}
}
mod game {
#[derive(Clone, Copy)]
pub enum EndStatus {
Win(Player),
Tie,
}
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum Player {
Player0,
Player1,
}
impl Player {
pub fn next(&self) -> Player {
match self {
Player::Player0 => Player::Player1,
Player::Player1 => Player::Player0,
}
}
}
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct Action(pub usize, pub usize);
pub struct TictactoeGame {
pub board: [[Option<Player>; 3]; 3],
pub player: Player,
pub end_status: Option<EndStatus>,
}
impl TictactoeGame {
pub fn new() -> Self {
Self {
board: [[None; 3]; 3],
player: Player::Player0,
end_status: None,
}
}
fn check_end_status(&mut self) {
for row in 0..3 {
if self.board[row][0].is_some()
&& self.board[row][0] == self.board[row][1]
&& self.board[row][1] == self.board[row][2]
{
let winner = self.board[row][0].unwrap();
self.end_status = Some(EndStatus::Win(winner));
return;
}
}
for col in 0..3 {
if self.board[0][col].is_some()
&& self.board[0][col] == self.board[1][col]
&& self.board[1][col] == self.board[2][col]
{
let winner = self.board[0][col].unwrap();
self.end_status = Some(EndStatus::Win(winner));
return;
}
}
if self.board[1][1].is_some()
&& ((self.board[0][0] == self.board[1][1] && self.board[1][1] == self.board[2][2])
|| (self.board[0][2] == self.board[1][1]
&& self.board[1][1] == self.board[2][0]))
{
let winner = self.board[1][1].unwrap();
self.end_status = Some(EndStatus::Win(winner));
return;
}
if self.board.iter().flatten().all(|p| p.is_some()) {
self.end_status = Some(EndStatus::Tie);
} else {
self.end_status = None;
}
}
pub fn occupied(&self, selection: Action) -> bool {
let Action(row, col) = selection;
self.board[row][col].is_some()
}
pub fn place(&self, selection: &Action) -> Result<TictactoeGame, &str> {
let Action(row, col) = *selection;
match self.board[row][col] {
Some(_) => Err("invalid place"),
None => {
let mut board = self.board;
board[row][col] = Some(self.player);
let mut game = TictactoeGame {
board,
player: self.player.next(),
end_status: None,
};
game.check_end_status();
Ok(game)
}
}
}
pub fn draw_board(&self) {
println!("\u{250C}\u{2500}\u{2500}\u{2500}\u{2510}");
for row in 0..3 {
print!("\u{2502}");
for col in 0..3 {
match self.board[row][col] {
Some(Player::Player0) => print!("O"),
Some(Player::Player1) => print!("X"),
None => print!("-"),
}
}
println!("\u{2502}");
}
println!("\u{2514}\u{2500}\u{2500}\u{2500}\u{2518}");
}
}
}