Skip to content

Commit 6011b70

Browse files
committed
Updating reversi example based on latest API changes
1 parent da755cf commit 6011b70

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-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: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ mod board;
88

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

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

1414
use board::{Board, Piece};
1515

@@ -24,11 +24,12 @@ struct Dimensions {
2424
}
2525

2626
fn main() {
27-
let mut turtle = Turtle::new();
28-
turtle.drawing_mut().set_background_color("#B3E5FC");
27+
let mut drawing = Drawing::new();
28+
let mut turtle = drawing.add_turtle();
29+
drawing.set_background_color("#B3E5FC");
2930
turtle.set_pen_color("#757575");
3031
turtle.set_pen_size(2.0);
31-
turtle.set_speed(8);
32+
turtle.set_speed(23);
3233

3334
let width = 580.0;
3435
let height = 580.0;
@@ -59,9 +60,9 @@ fn main() {
5960
draw_valid_moves(&mut turtle, &board, &dim);
6061

6162
// Get rid of any events that may have accumulated while drawing
62-
drain_events(&mut turtle);
63+
drain_events(&mut drawing);
6364

64-
play_game(&mut turtle, board, &dim);
65+
play_game(&mut drawing, &mut turtle, board, &dim);
6566
}
6667

6768
fn draw_board(turtle: &mut Turtle, dim: &Dimensions) {
@@ -99,21 +100,20 @@ fn draw_board_pieces(turtle: &mut Turtle, board: &Board, dim: &Dimensions) {
99100
}
100101
}
101102

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

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

129129
let current = board.current();
130130
let flipped = board.play_piece((row, col));
131131

132132
move_to_tile(turtle, (row, col), &dim);
133133
draw_piece(turtle, current, &dim);
134134

135-
let background = turtle.drawing().background_color();
135+
let background = drawing.background_color();
136136
draw_tile_circles(turtle, 0.9, background, dim, flipped.iter());
137137
draw_tile_circles(turtle, 0.8, current.color(), dim, flipped.iter());
138138

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

143143
// Get rid of any events that may have accumulated while drawing
144-
drain_events(turtle);
144+
drain_events(drawing);
145145
}
146146
}
147147
}
@@ -161,8 +161,8 @@ fn move_to_tile(turtle: &mut Turtle, (row, col): (usize, usize), dim: &Dimension
161161
turtle.pen_down();
162162
}
163163

164-
fn erase_valid_moves(turtle: &mut Turtle, board: &Board, dim: &Dimensions) {
165-
let background = turtle.drawing().background_color();
164+
fn erase_valid_moves(drawing: &Drawing, turtle: &mut Turtle, board: &Board, dim: &Dimensions) {
165+
let background = drawing.background_color();
166166
draw_tile_circles(
167167
turtle,
168168
0.5,
@@ -214,13 +214,14 @@ fn tile_circle(turtle: &mut Turtle, relative_size: f64, fill: Color, dim: &Dimen
214214
fn filled_circle(turtle: &mut Turtle, radius: f64, fill: Color) {
215215
turtle.set_fill_color(fill);
216216
turtle.pen_up();
217-
turtle.begin_fill();
218217

219218
turtle.forward(radius);
220219
turtle.right(90.0);
221-
circle(turtle, radius);
222220

221+
turtle.begin_fill();
222+
circle(turtle, radius);
223223
turtle.end_fill();
224+
224225
turtle.pen_down();
225226
}
226227

@@ -238,6 +239,6 @@ fn circle(turtle: &mut Turtle, radius: f64) {
238239
}
239240

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

0 commit comments

Comments
 (0)