Implement the basic rules of Go #40
|
@ -14,12 +14,14 @@ pub struct Jitter {
|
||||||
pub struct StoneElement {
|
pub struct StoneElement {
|
||||||
pub color: Color,
|
pub color: Color,
|
||||||
pub jitter: Jitter,
|
pub jitter: Jitter,
|
||||||
|
pub liberties: Option<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StoneElement {
|
impl StoneElement {
|
||||||
pub fn new(color: Color) -> Self {
|
pub fn new(color: Color, liberties: Option<u8>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
color,
|
color,
|
||||||
|
liberties,
|
||||||
jitter: Jitter { x: 0, y: 0 },
|
jitter: Jitter { x: 0, y: 0 },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,6 +76,7 @@ impl From<&Board> for BoardElement {
|
||||||
.map(|column| match board.stone(Coordinate { column, row }) {
|
.map(|column| match board.stone(Coordinate { column, row }) {
|
||||||
Some(color) => IntersectionElement::Filled(StoneElement {
|
Some(color) => IntersectionElement::Filled(StoneElement {
|
||||||
jitter: Jitter { x: 0, y: 0 },
|
jitter: Jitter { x: 0, y: 0 },
|
||||||
|
liberties: None,
|
||||||
color,
|
color,
|
||||||
}),
|
}),
|
||||||
None => IntersectionElement::Empty(Request::PlayStoneRequest(
|
None => IntersectionElement::Empty(Request::PlayStoneRequest(
|
||||||
|
|
|
@ -141,7 +141,7 @@ impl ObjectImpl for BoardPrivate {
|
||||||
(0..19).for_each(|row| {
|
(0..19).for_each(|row| {
|
||||||
match board.stone(row, col) {
|
match board.stone(row, col) {
|
||||||
IntersectionElement::Filled(stone) => {
|
IntersectionElement::Filled(stone) => {
|
||||||
pen.stone(&context, row, col, stone.color);
|
pen.stone(&context, row, col, stone.color, stone.liberties);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
|
@ -270,12 +270,27 @@ impl Pen {
|
||||||
let _ = context.fill();
|
let _ = context.fill();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stone(&self, context: &cairo::Context, row: u8, col: u8, color: Color) {
|
fn stone(
|
||||||
|
&self,
|
||||||
|
context: &cairo::Context,
|
||||||
|
row: u8,
|
||||||
|
col: u8,
|
||||||
|
color: Color,
|
||||||
|
liberties: Option<u8>,
|
||||||
|
) {
|
||||||
match color {
|
match color {
|
||||||
Color::White => context.set_source_rgb(0.9, 0.9, 0.9),
|
Color::White => context.set_source_rgb(0.9, 0.9, 0.9),
|
||||||
Color::Black => context.set_source_rgb(0.0, 0.0, 0.0),
|
Color::Black => context.set_source_rgb(0.0, 0.0, 0.0),
|
||||||
};
|
};
|
||||||
self.draw_stone(context, row, col);
|
self.draw_stone(context, row, col);
|
||||||
|
|
||||||
|
if let Some(liberties) = liberties {
|
||||||
|
let stone_location = self.stone_location(row, col);
|
||||||
|
context.set_source_rgb(1., 0., 1.);
|
||||||
|
context.set_font_size(32.);
|
||||||
|
context.move_to(stone_location.0 - 10., stone_location.1 + 10.);
|
||||||
|
let _ = context.show_text(&format!("{}", liberties));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ghost_stone(&self, context: &cairo::Context, row: u8, col: u8, color: Color) {
|
fn ghost_stone(&self, context: &cairo::Context, row: u8, col: u8, color: Color) {
|
||||||
|
@ -288,13 +303,15 @@ impl Pen {
|
||||||
|
|
||||||
fn draw_stone(&self, context: &cairo::Context, row: u8, col: u8) {
|
fn draw_stone(&self, context: &cairo::Context, row: u8, col: u8) {
|
||||||
let radius = self.hspace_between / 2. - 2.;
|
let radius = self.hspace_between / 2. - 2.;
|
||||||
context.arc(
|
let (x_loc, y_loc) = self.stone_location(row, col);
|
||||||
self.x_offset + (col as f64) * self.hspace_between,
|
context.arc(x_loc, y_loc, radius, 0.0, 2.0 * std::f64::consts::PI);
|
||||||
self.y_offset + (row as f64) * self.vspace_between,
|
|
||||||
radius,
|
|
||||||
0.0,
|
|
||||||
2.0 * std::f64::consts::PI,
|
|
||||||
);
|
|
||||||
let _ = context.fill();
|
let _ = context.fill();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn stone_location(&self, row: u8, col: u8) -> (f64, f64) {
|
||||||
|
(
|
||||||
|
self.x_offset + (col as f64) * self.hspace_between,
|
||||||
|
self.y_offset + (row as f64) * self.vspace_between,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue