Skip to content

Commit 544e7c9

Browse files
committed
Updating reversi example based on latest API changes
1 parent abc3a42 commit 544e7c9

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

examples/reversi/board.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl Board {
209209
fn adjacent_positions(&self, (row, col): Position) -> Vec<Position> {
210210
let rows = self.tiles.len();
211211
let cols = self.tiles[0].len();
212-
[(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)].into_iter()
212+
[(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)].iter()
213213
.map(|&(r, c)| (row as isize + r, col as isize + c))
214214
.filter(|&(r, c)| r >= 0 && c >= 0 && r < rows as isize && c < cols as isize)
215215
.map(|(r, c)| (r as usize, c as usize))

examples/reversi/main.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
//!
33
//! https://en.wikipedia.org/wiki/Reversi
44
5+
// To run this example, use the command: cargo run --features unstable --example reversi
6+
#[cfg(all(not(feature = "unstable")))]
7+
compile_error!("This example relies on unstable features. Run with `--features unstable`");
8+
59
extern crate turtle;
610

711
mod board;
812

913
use std::f64::consts::PI;
1014

11-
use turtle::{Turtle, Event, Color};
12-
use turtle::event::{MouseButton};
15+
use turtle::{Drawing, Turtle, Point, Color, Event};
16+
use turtle::event::{MouseButton, PressedState};
1317

1418
use board::{Board, Piece};
1519

@@ -24,11 +28,12 @@ struct Dimensions {
2428
}
2529

2630
fn main() {
27-
let mut turtle = Turtle::new();
28-
turtle.drawing_mut().set_background_color("#B3E5FC");
31+
let mut drawing = Drawing::new();
32+
let mut turtle = drawing.add_turtle();
33+
drawing.set_background_color("#B3E5FC");
2934
turtle.set_pen_color("#757575");
3035
turtle.set_pen_size(2.0);
31-
turtle.set_speed(8);
36+
turtle.set_speed(23);
3237

3338
let width = 580.0;
3439
let height = 580.0;
@@ -59,9 +64,9 @@ fn main() {
5964
draw_valid_moves(&mut turtle, &board, &dim);
6065

6166
// Get rid of any events that may have accumulated while drawing
62-
drain_events(&mut turtle);
67+
drain_events(&mut drawing);
6368

64-
play_game(&mut turtle, board, &dim);
69+
play_game(&mut drawing, &mut turtle, board, &dim);
6570
}
6671

6772
fn draw_board(turtle: &mut Turtle, dim: &Dimensions) {
@@ -99,21 +104,20 @@ fn draw_board_pieces(turtle: &mut Turtle, board: &Board, dim: &Dimensions) {
99104
}
100105
}
101106

102-
fn play_game(turtle: &mut Turtle, mut board: Board, dim: &Dimensions) {
107+
fn play_game(drawing: &mut Drawing, turtle: &mut Turtle, mut board: Board, dim: &Dimensions) {
103108
println!("Click on a tile to make a move.");
104109
println!("Current Player: {}", board.current().name());
105-
turtle.set_speed(9);
106110

107-
let mut mouse = [0.0, 0.0];
111+
let mut mouse = Point::origin();
108112
loop {
109-
let event = turtle.drawing_mut().poll_event();
113+
let event = drawing.poll_event();
110114
// Sometimes it is more convenient to use `if let` instead of `match`. In this case, it's
111115
// really up to your personal preference. We chose to demonstrate what `if let` would look
112116
// like if used for this code.
113-
if let Some(Event::MouseMove {x, y}) = event {
114-
mouse = [x, y];
117+
if let Some(Event::MouseMove(mouse_pos)) = event {
118+
mouse = mouse_pos;
115119
}
116-
else if let Some(Event::MouseButtonReleased(MouseButton::Left)) = event {
120+
else if let Some(Event::MouseButton(MouseButton::LeftButton, PressedState::Released)) = event {
117121
// Figure out which row and column was clicked
118122
// If these formulas seem unclear, try some example values to see what you get
119123
let row = ((1.0 - (mouse[1] + dim.height/2.0) / dim.height) * dim.rows as f64).floor() as isize;
@@ -124,15 +128,15 @@ fn play_game(turtle: &mut Turtle, mut board: Board, dim: &Dimensions) {
124128
&& board.is_valid_move(&(row as usize, col as usize)) {
125129
let row = row as usize;
126130
let col = col as usize;
127-
erase_valid_moves(turtle, &board, dim);
131+
erase_valid_moves(drawing, turtle, &board, dim);
128132

129133
let current = board.current();
130134
let flipped = board.play_piece((row, col));
131135

132136
move_to_tile(turtle, (row, col), &dim);
133137
draw_piece(turtle, current, &dim);
134138

135-
let background = turtle.drawing().background_color();
139+
let background = drawing.background_color();
136140
draw_tile_circles(turtle, 0.9, background, dim, flipped.iter());
137141
draw_tile_circles(turtle, 0.8, current.color(), dim, flipped.iter());
138142

@@ -141,7 +145,7 @@ fn play_game(turtle: &mut Turtle, mut board: Board, dim: &Dimensions) {
141145
println!("Current Player: {}", board.current().name());
142146

143147
// Get rid of any events that may have accumulated while drawing
144-
drain_events(turtle);
148+
drain_events(drawing);
145149
}
146150
}
147151
}
@@ -161,8 +165,8 @@ fn move_to_tile(turtle: &mut Turtle, (row, col): (usize, usize), dim: &Dimension
161165
turtle.pen_down();
162166
}
163167

164-
fn erase_valid_moves(turtle: &mut Turtle, board: &Board, dim: &Dimensions) {
165-
let background = turtle.drawing().background_color();
168+
fn erase_valid_moves(drawing: &Drawing, turtle: &mut Turtle, board: &Board, dim: &Dimensions) {
169+
let background = drawing.background_color();
166170
draw_tile_circles(
167171
turtle,
168172
0.5,
@@ -214,13 +218,14 @@ fn tile_circle(turtle: &mut Turtle, relative_size: f64, fill: Color, dim: &Dimen
214218
fn filled_circle(turtle: &mut Turtle, radius: f64, fill: Color) {
215219
turtle.set_fill_color(fill);
216220
turtle.pen_up();
217-
turtle.begin_fill();
218221

219222
turtle.forward(radius);
220223
turtle.right(90.0);
221-
circle(turtle, radius);
222224

225+
turtle.begin_fill();
226+
circle(turtle, radius);
223227
turtle.end_fill();
228+
224229
turtle.pen_down();
225230
}
226231

@@ -238,6 +243,6 @@ fn circle(turtle: &mut Turtle, radius: f64) {
238243
}
239244

240245
/// Clear out all events that may have accumulated
241-
fn drain_events(turtle: &mut Turtle) {
242-
while let Some(_) = turtle.drawing_mut().poll_event() {}
246+
fn drain_events(drawing: &mut Drawing) {
247+
while let Some(_) = drawing.poll_event() {}
243248
}

0 commit comments

Comments
 (0)