Skip to content

Commit 846702c

Browse files
committed
Drawing indicators for valid moves
1 parent 78c061d commit 846702c

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

examples/reversi/board.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ impl Board {
7979
self.current
8080
}
8181

82+
pub fn valid_moves(&self) -> &HashSet<Position> {
83+
&self.valid_moves
84+
}
85+
8286
pub fn is_valid_move(&self, position: &Position) -> bool {
8387
self.valid_moves.contains(position)
8488
}
@@ -171,7 +175,7 @@ impl Board {
171175
return false;
172176
}
173177

174-
//TODO: Replace return type with `impl Iterator<Item=(usize, usize)>` when the "impl Trait"
178+
//TODO: Replace return type with `impl Iterator<Item=Position>` when the "impl Trait"
175179
// feature is stable.
176180
fn adjacent_positions(&self, (row, col): Position) -> Vec<Position> {
177181
let rows = self.tiles.len();

examples/reversi/main.rs

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod board;
88

99
use std::f64::consts::PI;
1010

11-
use turtle::{Turtle, Event};
11+
use turtle::{Turtle, Event, Color};
1212
use turtle::event::{MouseButton};
1313

1414
use board::{Board, Piece};
@@ -56,6 +56,8 @@ fn main() {
5656
println!("Drawing the board...\n");
5757
draw_board(&mut turtle, &dim);
5858
draw_board_pieces(&mut turtle, &board, &dim);
59+
draw_valid_moves(&mut turtle, &board, &dim);
60+
5961
// Get rid of any events that may have accumulated while drawing
6062
drain_events(&mut turtle);
6163

@@ -117,16 +119,18 @@ fn play_game(turtle: &mut Turtle, mut board: Board, dim: &Dimensions) {
117119
let row = ((1.0 - (mouse[1] + dim.height/2.0) / dim.height) * dim.rows as f64).floor() as isize;
118120
let col = ((mouse[0] + dim.width/2.0) / dim.width * dim.cols as f64).floor() as isize;
119121

120-
println!("Play {:?}", (row, col));
121122
if row >= 0 && row < dim.rows as isize
122123
&& col >= 0 && col < dim.cols as isize
123124
&& board.is_valid_move(&(row as usize, col as usize)) {
124125
let row = row as usize;
125126
let col = col as usize;
127+
erase_valid_moves(turtle, &board, dim);
128+
126129
board.play_piece((row, col));
127130

128131
move_to_tile(turtle, (row, col), &dim);
129132
draw_piece(turtle, board.current(), &dim);
133+
draw_valid_moves(turtle, &board, dim);
130134

131135
println!("Current Player: {}", board.current().name());
132136

@@ -151,11 +155,58 @@ fn move_to_tile(turtle: &mut Turtle, (row, col): (usize, usize), dim: &Dimension
151155
turtle.pen_down();
152156
}
153157

158+
fn erase_valid_moves(turtle: &mut Turtle, board: &Board, dim: &Dimensions) {
159+
let background = turtle.background_color();
160+
draw_tile_circles(
161+
turtle,
162+
0.5,
163+
background,
164+
dim,
165+
board.valid_moves().iter(),
166+
);
167+
}
168+
169+
fn draw_valid_moves(turtle: &mut Turtle, board: &Board, dim: &Dimensions) {
170+
draw_tile_circles(
171+
turtle,
172+
0.2,
173+
board.current().color().with_alpha(0.8),
174+
dim,
175+
board.valid_moves().iter(),
176+
);
177+
}
178+
179+
fn draw_tile_circles<'a, T: Iterator<Item = &'a (usize, usize)>>(
180+
turtle: &mut Turtle,
181+
relative_size: f64,
182+
fill: Color,
183+
dim: &Dimensions,
184+
tiles: T,
185+
) {
186+
let speed = turtle.speed();
187+
turtle.set_speed("instant");
188+
for pos in tiles {
189+
move_to_tile(turtle, *pos, &dim);
190+
tile_circle(turtle, relative_size, fill, dim);
191+
}
192+
turtle.set_speed(speed);
193+
}
194+
154195
/// Draws the given piece
155196
fn draw_piece(turtle: &mut Turtle, piece: Piece, dim: &Dimensions) {
156-
let radius = dim.tile_width.min(dim.tile_height) / 2.0 * 0.9;
197+
turtle.show();
198+
tile_circle(turtle, 0.8, piece.color(), dim);
199+
turtle.hide();
200+
}
201+
202+
fn tile_circle(turtle: &mut Turtle, relative_size: f64, fill: Color, dim: &Dimensions) {
203+
let radius = dim.tile_width.min(dim.tile_height) / 2.0 * relative_size;
204+
205+
filled_circle(turtle, radius, fill);
206+
}
157207

158-
turtle.set_fill_color(piece.color());
208+
fn filled_circle(turtle: &mut Turtle, radius: f64, fill: Color) {
209+
turtle.set_fill_color(fill);
159210
turtle.pen_up();
160211
turtle.begin_fill();
161212

0 commit comments

Comments
 (0)