Implement the basic rules of Go #40

Merged
savanni merged 13 commits from feature/go-rules into main 2023-05-04 02:34:40 +00:00
2 changed files with 30 additions and 10 deletions
Showing only changes of commit 300704297f - Show all commits

View File

@ -14,12 +14,14 @@ pub struct Jitter {
pub struct StoneElement {
pub color: Color,
pub jitter: Jitter,
pub liberties: Option<u8>,
}
impl StoneElement {
pub fn new(color: Color) -> Self {
pub fn new(color: Color, liberties: Option<u8>) -> Self {
Self {
color,
liberties,
jitter: Jitter { x: 0, y: 0 },
}
}
@ -74,6 +76,7 @@ impl From<&Board> for BoardElement {
.map(|column| match board.stone(Coordinate { column, row }) {
Some(color) => IntersectionElement::Filled(StoneElement {
jitter: Jitter { x: 0, y: 0 },
liberties: None,
color,
}),
None => IntersectionElement::Empty(Request::PlayStoneRequest(

View File

@ -141,7 +141,7 @@ impl ObjectImpl for BoardPrivate {
(0..19).for_each(|row| {
match board.stone(row, col) {
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();
}
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 {
Color::White => context.set_source_rgb(0.9, 0.9, 0.9),
Color::Black => context.set_source_rgb(0.0, 0.0, 0.0),
};
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) {
@ -288,13 +303,15 @@ impl Pen {
fn draw_stone(&self, context: &cairo::Context, row: u8, col: u8) {
let radius = self.hspace_between / 2. - 2.;
context.arc(
self.x_offset + (col as f64) * self.hspace_between,
self.y_offset + (row as f64) * self.vspace_between,
radius,
0.0,
2.0 * std::f64::consts::PI,
);
let (x_loc, y_loc) = self.stone_location(row, col);
context.arc(x_loc, y_loc, radius, 0.0, 2.0 * std::f64::consts::PI);
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,
)
}
}