Skip to content

Commit 8159c8a

Browse files
committed
fix: remove dead functions and unused imports
1 parent b54426f commit 8159c8a

5 files changed

Lines changed: 153 additions & 206 deletions

File tree

topics/tic-tac-toe/src/ai.rs

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::board::{Board, Player, Cell};
1+
use crate::board::{Cell, Player};
22
use crate::game::Game;
33

44
/// AI player that uses the Minimax algorithm to play optimally
@@ -24,11 +24,11 @@ impl AI {
2424

2525
for &position in &available_moves {
2626
let mut game_copy = self.clone_game(game);
27-
27+
2828
game_copy.make_move(position);
29-
29+
3030
let score = self.minimax(&game_copy, 0, false);
31-
31+
3232
if score > best_score {
3333
best_score = score;
3434
best_move = Some(position);
@@ -45,9 +45,9 @@ impl AI {
4545
// Check terminal states (game over)
4646
if let Some(winner) = game.check_winner() {
4747
return if winner == self.player {
48-
10 - depth as i32 // AI wins: prefer winning sooner (higher score for fewer moves)
48+
10 - depth as i32 // AI wins: prefer winning sooner (higher score for fewer moves)
4949
} else {
50-
depth as i32 - 10 // AI loses: prefer losing later (less negative score)
50+
depth as i32 - 10 // AI loses: prefer losing later (less negative score)
5151
};
5252
}
5353

@@ -56,32 +56,32 @@ impl AI {
5656
}
5757

5858
let available_moves = game.get_available_moves();
59-
59+
6060
if is_maximizing {
6161
// AI's turn - maximize the score
6262
let mut max_score = i32::MIN;
63-
63+
6464
for &position in &available_moves {
6565
let mut game_copy = self.clone_game(game);
6666
game_copy.make_move(position);
67-
67+
6868
let score = self.minimax(&game_copy, depth + 1, false);
6969
max_score = max_score.max(score);
7070
}
71-
71+
7272
max_score
7373
} else {
7474
// Opponent's turn - minimize the score (from AI's perspective)
7575
let mut min_score = i32::MAX;
76-
76+
7777
for &position in &available_moves {
7878
let mut game_copy = self.clone_game(game);
7979
game_copy.make_move(position);
80-
80+
8181
let score = self.minimax(&game_copy, depth + 1, true);
8282
min_score = min_score.min(score);
8383
}
84-
84+
8585
min_score
8686
}
8787
}
@@ -91,17 +91,16 @@ impl AI {
9191
fn clone_game(&self, game: &Game) -> Game {
9292
let mut new_game = Game::new();
9393

94-
let board = game.get_board();
94+
let board = game.get_board();
9595

96-
let mut moves = Vec::new();
96+
let mut moves = Vec::new();
9797
for position in 1..=9 {
9898
if let Some(Cell::Occupied(player)) = board.get_cell(position) {
9999
moves.push((position, player));
100100
}
101101
}
102102

103-
104-
let mut x_moves = Vec::new();
103+
let mut x_moves = Vec::new();
105104
let mut o_moves = Vec::new();
106105

107106
for (pos, player) in moves {
@@ -111,7 +110,7 @@ impl AI {
111110
}
112111
}
113112

114-
let mut move_sequence = Vec::new();
113+
let mut move_sequence = Vec::new();
115114
let max_len = x_moves.len().max(o_moves.len());
116115

117116
for i in 0..max_len {
@@ -123,7 +122,7 @@ impl AI {
123122
}
124123
}
125124

126-
for &position in &move_sequence {
125+
for &position in &move_sequence {
127126
new_game.make_move(position);
128127
}
129128

@@ -140,7 +139,7 @@ mod tests {
140139
fn test_ai_creation() {
141140
let ai = AI::new(Player::O);
142141
assert_eq!(ai.player, Player::O);
143-
142+
144143
let ai_x = AI::new(Player::X);
145144
assert_eq!(ai_x.player, Player::X);
146145
}
@@ -149,13 +148,13 @@ mod tests {
149148
fn test_ai_blocks_winning_move() {
150149
let mut game = Game::new();
151150
let ai = AI::new(Player::O);
152-
151+
153152
// X threatens to win in top row
154153
game.make_move(1); // X
155154
game.make_move(4); // O (AI's previous move)
156155
game.make_move(2); // X
157156
// Now X threatens to win at position 3
158-
157+
159158
let ai_move = ai.get_best_move(&game);
160159
assert_eq!(ai_move, Some(3)); // AI should block at position 3
161160
}
@@ -164,15 +163,15 @@ mod tests {
164163
fn test_ai_takes_winning_move() {
165164
let mut game = Game::new();
166165
let ai = AI::new(Player::O);
167-
166+
168167
// Set up a scenario where AI can win
169168
game.make_move(1); // X
170169
game.make_move(4); // O
171170
game.make_move(2); // X
172171
game.make_move(5); // O
173172
game.make_move(7); // X
174173
// Now O can win at position 6
175-
174+
176175
let ai_move = ai.get_best_move(&game);
177176
assert_eq!(ai_move, Some(6)); // AI should win at position 6
178177
}
@@ -181,7 +180,7 @@ mod tests {
181180
fn test_ai_chooses_valid_move_on_empty_board() {
182181
let game = Game::new();
183182
let ai = AI::new(Player::X);
184-
183+
185184
let ai_move = ai.get_best_move(&game);
186185
// On an empty board, any move should be valid (1-9)
187186
// The AI should choose some valid position
@@ -194,13 +193,17 @@ mod tests {
194193
fn test_ai_no_moves_available() {
195194
let mut game = Game::new();
196195
let ai = AI::new(Player::O);
197-
196+
198197
// Fill the board completely
199198
for position in 1..=9 {
200-
let player = if position % 2 == 1 { Player::X } else { Player::O };
199+
let _player = if position % 2 == 1 {
200+
Player::X
201+
} else {
202+
Player::O
203+
};
201204
game.make_move(position);
202205
}
203-
206+
204207
let ai_move = ai.get_best_move(&game);
205208
assert_eq!(ai_move, None); // No moves available
206209
}
@@ -209,7 +212,7 @@ mod tests {
209212
fn test_minimax_prefers_winning_sooner() {
210213
let mut game = Game::new();
211214
let ai = AI::new(Player::O);
212-
215+
213216
// Create a scenario where AI has multiple ways to win
214217
// AI should prefer the immediate win over a longer path to victory
215218
game.make_move(1); // X
@@ -218,8 +221,8 @@ mod tests {
218221
game.make_move(5); // O
219222
game.make_move(8); // X
220223
// AI can win immediately at position 6
221-
224+
222225
let ai_move = ai.get_best_move(&game);
223226
assert_eq!(ai_move, Some(6)); // Should take the immediate win
224227
}
225-
}
228+
}

topics/tic-tac-toe/src/board.rs

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ impl Board {
3131
if position == 0 || position > 9 {
3232
return false;
3333
}
34-
34+
3535
let index = position - 1;
36-
36+
3737
if self.cells[index] == Cell::Empty {
3838
self.cells[index] = Cell::Occupied(player);
3939
true
@@ -42,14 +42,6 @@ impl Board {
4242
}
4343
}
4444

45-
/// Checks if a position is empty
46-
pub fn is_empty(&self, position: usize) -> bool {
47-
if position == 0 || position > 9 {
48-
return false;
49-
}
50-
self.cells[position - 1] == Cell::Empty
51-
}
52-
5345
/// Gets the cell at a specific position (1-9)
5446
pub fn get_cell(&self, position: usize) -> Option<Cell> {
5547
if position == 0 || position > 9 {
@@ -90,7 +82,7 @@ impl Board {
9082
Some(Cell::Occupied(Player::O)) => "O".to_string(),
9183
None => "?".to_string(),
9284
};
93-
85+
9486
print!(" {} ", display_char);
9587
if col < 2 {
9688
print!("|");
@@ -112,30 +104,27 @@ mod tests {
112104
#[test]
113105
fn test_new_board() {
114106
let board = Board::new();
115-
107+
116108
for position in 1..=9 {
117109
assert_eq!(board.get_cell(position), Some(Cell::Empty));
118-
assert!(board.is_empty(position));
119110
}
120-
111+
121112
assert!(!board.is_full());
122-
113+
123114
assert_eq!(board.get_empty_positions().len(), 9);
124115
assert_eq!(board.get_empty_positions(), vec![1, 2, 3, 4, 5, 6, 7, 8, 9]);
125116
}
126117

127118
#[test]
128119
fn test_place_move_valid() {
129120
let mut board = Board::new();
130-
121+
131122
assert!(board.place_move(5, Player::X));
132123
assert_eq!(board.get_cell(5), Some(Cell::Occupied(Player::X)));
133-
assert!(!board.is_empty(5));
134-
124+
135125
assert!(board.place_move(1, Player::O));
136126
assert_eq!(board.get_cell(1), Some(Cell::Occupied(Player::O)));
137-
assert!(!board.is_empty(1));
138-
127+
139128
assert_eq!(board.get_empty_positions().len(), 7);
140129
assert!(!board.get_empty_positions().contains(&1));
141130
assert!(!board.get_empty_positions().contains(&5));
@@ -144,66 +133,62 @@ mod tests {
144133
#[test]
145134
fn test_place_move_invalid_position() {
146135
let mut board = Board::new();
147-
136+
148137
assert!(!board.place_move(0, Player::X));
149138
assert!(!board.place_move(10, Player::X));
150-
139+
151140
assert_eq!(board.get_empty_positions().len(), 9);
152141
}
153142

154143
#[test]
155144
fn test_place_move_occupied_position() {
156145
let mut board = Board::new();
157-
146+
158147
assert!(board.place_move(5, Player::X));
159-
148+
160149
assert!(!board.place_move(5, Player::O));
161-
150+
162151
assert_eq!(board.get_cell(5), Some(Cell::Occupied(Player::X)));
163152
}
164153

165154
#[test]
166155
fn test_get_cell_invalid_position() {
167156
let board = Board::new();
168-
157+
169158
assert_eq!(board.get_cell(0), None);
170159
assert_eq!(board.get_cell(10), None);
171160
}
172161

173-
#[test]
174-
fn test_is_empty_invalid_position() {
175-
let board = Board::new();
176-
177-
assert!(!board.is_empty(0));
178-
assert!(!board.is_empty(10));
179-
}
180-
181162
#[test]
182163
fn test_is_full() {
183164
let mut board = Board::new();
184-
165+
185166
assert!(!board.is_full());
186-
167+
187168
for position in 1..=9 {
188-
let player = if position % 2 == 1 { Player::X } else { Player::O };
169+
let player = if position % 2 == 1 {
170+
Player::X
171+
} else {
172+
Player::O
173+
};
189174
board.place_move(position, player);
190175
}
191-
176+
192177
assert!(board.is_full());
193178
assert_eq!(board.get_empty_positions().len(), 0);
194179
}
195180

196181
#[test]
197182
fn test_get_empty_positions() {
198183
let mut board = Board::new();
199-
184+
200185
let mut expected = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
201186
assert_eq!(board.get_empty_positions(), expected);
202-
187+
203188
board.place_move(1, Player::X);
204189
board.place_move(5, Player::O);
205190
board.place_move(9, Player::X);
206-
191+
207192
expected.retain(|&x| x != 1 && x != 5 && x != 9);
208193
assert_eq!(board.get_empty_positions(), expected);
209194
assert_eq!(board.get_empty_positions(), vec![2, 3, 4, 6, 7, 8]);
@@ -214,9 +199,9 @@ mod tests {
214199
let mut original = Board::new();
215200
original.place_move(1, Player::X);
216201
original.place_move(5, Player::O);
217-
202+
218203
let cloned = original.clone();
219-
204+
220205
assert_eq!(cloned.get_cell(1), Some(Cell::Occupied(Player::X)));
221206
assert_eq!(cloned.get_cell(5), Some(Cell::Occupied(Player::O)));
222207
assert_eq!(cloned.get_empty_positions().len(), 7);
@@ -237,4 +222,4 @@ mod tests {
237222
assert_ne!(Cell::Empty, Cell::Occupied(Player::X));
238223
assert_ne!(Cell::Occupied(Player::X), Cell::Occupied(Player::O));
239224
}
240-
}
225+
}

0 commit comments

Comments
 (0)