Compare commits
No commits in common. "06157604b9b42b8c6925631f4f3de63abafd8ff1" and "78af31f6f907d48a7a4ea74666af4bde414c72df" have entirely different histories.
06157604b9
...
78af31f6f9
14
Makefile
14
Makefile
|
@ -35,17 +35,3 @@ ifc-dev:
|
||||||
ifc-test:
|
ifc-test:
|
||||||
cd ifc && make test
|
cd ifc && make test
|
||||||
|
|
||||||
kifu-core/dev:
|
|
||||||
cd kifu/kifu-core && make test
|
|
||||||
|
|
||||||
kifu-core/test:
|
|
||||||
cd kifu/kifu-core && make test
|
|
||||||
|
|
||||||
kifu-core/test-oneshot:
|
|
||||||
cd kifu/kifu-core && make test-oneshot
|
|
||||||
|
|
||||||
kifu-gtk:
|
|
||||||
cd kifu/kifu-gtk && make release
|
|
||||||
|
|
||||||
kifu-gtk/dev:
|
|
||||||
cd kifu/kifu-gtk && make dev
|
|
||||||
|
|
|
@ -49,7 +49,6 @@ name = "kifu-core"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"grid",
|
"grid",
|
||||||
"thiserror",
|
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -210,37 +209,6 @@ dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "syn"
|
|
||||||
version = "2.0.12"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "79d9531f94112cfc3e4c8f5f02cb2b58f72c97b7efd85f70203cc6d8efda5927"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"unicode-ident",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "thiserror"
|
|
||||||
version = "1.0.40"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac"
|
|
||||||
dependencies = [
|
|
||||||
"thiserror-impl",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "thiserror-impl"
|
|
||||||
version = "1.0.40"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn 2.0.12",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.26.0"
|
version = "1.26.0"
|
||||||
|
@ -269,7 +237,7 @@ checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 1.0.109",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -8,4 +8,3 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tokio = { version = "1.26", features = [ "full" ] }
|
tokio = { version = "1.26", features = [ "full" ] }
|
||||||
grid = { version = "0.9" }
|
grid = { version = "0.9" }
|
||||||
thiserror = { version = "1" }
|
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
|
|
||||||
test:
|
|
||||||
cargo watch -x 'nextest run'
|
|
||||||
|
|
||||||
test-oneshot:
|
|
||||||
cargo nextest run
|
|
|
@ -1,23 +1,30 @@
|
||||||
use crate::{BoardError, Color, Size};
|
use crate::{Color, Size};
|
||||||
|
use grid::Grid;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
InvalidPosition,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
pub size: Size,
|
pub size: Size,
|
||||||
|
pub grid: Grid<Option<Color>>,
|
||||||
pub groups: Vec<Group>,
|
pub groups: Vec<Group>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Board {
|
impl std::fmt::Display for Board {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||||
write!(f, " ")?;
|
print!(" ");
|
||||||
// for c in 'A'..'U' {
|
// for c in 'A'..'U' {
|
||||||
for c in 0..19 {
|
for c in 0..19 {
|
||||||
write!(f, "{:2}", c)?;
|
print!("{:2}", c);
|
||||||
}
|
}
|
||||||
writeln!(f, "")?;
|
println!("");
|
||||||
|
|
||||||
for row in 0..self.size.height {
|
for row in 0..self.size.height {
|
||||||
write!(f, " {:2}", row)?;
|
print!(" {:2}", row);
|
||||||
for column in 0..self.size.width {
|
for column in 0..self.size.width {
|
||||||
match self.stone(&Coordinate { column, row }) {
|
match self.stone(&Coordinate { column, row }) {
|
||||||
None => write!(f, " .")?,
|
None => write!(f, " .")?,
|
||||||
|
@ -31,26 +38,6 @@ impl std::fmt::Display for Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Board {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
if self.size != other.size {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for group in self.groups.iter() {
|
|
||||||
if !other.groups.contains(&group) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for group in other.groups.iter() {
|
|
||||||
if !self.groups.contains(&group) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -58,16 +45,17 @@ impl Board {
|
||||||
width: 19,
|
width: 19,
|
||||||
height: 19,
|
height: 19,
|
||||||
},
|
},
|
||||||
|
grid: Grid::new(19, 19),
|
||||||
groups: Vec::new(),
|
groups: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_coordinates(
|
pub fn from_coordinates(coordinates: impl Iterator<Item = (Coordinate, Color)>) -> Self {
|
||||||
mut coordinates: impl Iterator<Item = (Coordinate, Color)>,
|
let mut s = Self::new();
|
||||||
) -> Result<Self, BoardError> {
|
for (coordinate, color) in coordinates {
|
||||||
coordinates.try_fold(Self::new(), |board, (coordinate, color)| {
|
s.place_stone(coordinate, color);
|
||||||
board.place_stone(coordinate, color)
|
}
|
||||||
})
|
s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,9 +66,9 @@ pub struct Coordinate {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
pub fn place_stone(mut self, coordinate: Coordinate, color: Color) -> Result<Self, BoardError> {
|
pub fn place_stone(&mut self, coordinate: Coordinate, color: Color) -> Result<(), Error> {
|
||||||
if let Some(_) = self.stone(&coordinate) {
|
if let Some(_) = self.stone(&coordinate) {
|
||||||
return Err(BoardError::InvalidPosition);
|
return Err(Error::InvalidPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut friendly_group = self
|
let mut friendly_group = self
|
||||||
|
@ -102,6 +90,11 @@ impl Board {
|
||||||
};
|
};
|
||||||
self.groups.push(friendly_group.clone());
|
self.groups.push(friendly_group.clone());
|
||||||
|
|
||||||
|
match self.grid.get_mut(coordinate.row, coordinate.column) {
|
||||||
|
None => return Err(Error::InvalidPosition),
|
||||||
|
Some(space) => *space = Some(color),
|
||||||
|
}
|
||||||
|
|
||||||
let adjacent_groups = self.adjacent_groups(&friendly_group);
|
let adjacent_groups = self.adjacent_groups(&friendly_group);
|
||||||
for group in adjacent_groups {
|
for group in adjacent_groups {
|
||||||
if self.liberties(&group) == 0 {
|
if self.liberties(&group) == 0 {
|
||||||
|
@ -109,18 +102,14 @@ impl Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.liberties(&friendly_group) == 0 {
|
Ok(())
|
||||||
return Err(BoardError::SelfCapture);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(self)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stone(&self, coordinate: &Coordinate) -> Option<Color> {
|
pub fn stone(&self, coordinate: &Coordinate) -> Option<Color> {
|
||||||
self.groups
|
match self.grid.get(coordinate.row, coordinate.column) {
|
||||||
.iter()
|
None => None,
|
||||||
.find(|g| g.contains(coordinate))
|
Some(val) => *val,
|
||||||
.map(|g| g.color)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn group(&self, coordinate: &Coordinate) -> Option<&Group> {
|
pub fn group(&self, coordinate: &Coordinate) -> Option<&Group> {
|
||||||
|
@ -131,15 +120,23 @@ impl Board {
|
||||||
|
|
||||||
pub fn remove_group(&mut self, group: &Group) {
|
pub fn remove_group(&mut self, group: &Group) {
|
||||||
self.groups.retain(|g| g != group);
|
self.groups.retain(|g| g != group);
|
||||||
|
for coord in group.coordinates.iter() {
|
||||||
|
match self.grid.get_mut(coord.row, coord.column) {
|
||||||
|
None => (),
|
||||||
|
Some(v) => *v = None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn adjacent_groups(&self, group: &Group) -> Vec<Group> {
|
pub fn adjacent_groups(&self, group: &Group) -> Vec<Group> {
|
||||||
let adjacent_spaces = self.group_halo(group).into_iter();
|
let adjacent_spaces = self.group_halo(group).into_iter();
|
||||||
|
println!("adjacent spaces: {:?}", adjacent_spaces);
|
||||||
let mut grps: Vec<Group> = Vec::new();
|
let mut grps: Vec<Group> = Vec::new();
|
||||||
|
|
||||||
adjacent_spaces.for_each(|coord| match self.group(&coord) {
|
adjacent_spaces.for_each(|coord| match self.group(&coord) {
|
||||||
None => return,
|
None => return,
|
||||||
Some(adj) => {
|
Some(adj) => {
|
||||||
|
println!("group found: {:?}", adj.color);
|
||||||
if group.color == adj.color {
|
if group.color == adj.color {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -167,6 +164,32 @@ impl Board {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|c| self.stone(&c) == None)
|
.filter(|c| self.stone(&c) == None)
|
||||||
.count()
|
.count()
|
||||||
|
|
||||||
|
// let adjacencies: HashSet<Coordinate> = self.adjacencies(group.coordinates.iter()).collect();
|
||||||
|
/*
|
||||||
|
println!("adjacencies: {:?}", adjacencies);
|
||||||
|
let opposing_spaces = adjacencies
|
||||||
|
.iter()
|
||||||
|
.filter(|coord| match self.grid.get(coord.row, coord.column) {
|
||||||
|
None => true,
|
||||||
|
Some(&None) => false,
|
||||||
|
Some(&Some(c)) => c != group.color,
|
||||||
|
})
|
||||||
|
.cloned()
|
||||||
|
.collect::<HashSet<Coordinate>>();
|
||||||
|
println!("opposition: {:?}", opposing_spaces);
|
||||||
|
|
||||||
|
adjacencies.len() - opposing_spaces.len()
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
group
|
||||||
|
.adjacencies(self.size.width as u8 - 1, self.size.height as u8 - 1)
|
||||||
|
.into_iter()
|
||||||
|
.filter(|coordinate| self.stone(*coordinate).is_none())
|
||||||
|
.collect::<Vec<Coordinate>>()
|
||||||
|
.len()
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn adjacencies(&self, coordinate: &Coordinate) -> Vec<Coordinate> {
|
pub fn adjacencies(&self, coordinate: &Coordinate) -> Vec<Coordinate> {
|
||||||
|
@ -194,6 +217,41 @@ impl Board {
|
||||||
v.into_iter().filter(|c| self.within_board(c)).collect()
|
v.into_iter().filter(|c| self.within_board(c)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
pub fn adjacencies<'a>(
|
||||||
|
&'a self,
|
||||||
|
coordinates: impl Iterator<Item = &'a Coordinate> + 'a,
|
||||||
|
) -> impl Iterator<Item = Coordinate> + 'a {
|
||||||
|
coordinates
|
||||||
|
.map(|coordinate| {
|
||||||
|
let mut v = Vec::new();
|
||||||
|
if coordinate.column > 0 {
|
||||||
|
v.push(Coordinate {
|
||||||
|
column: coordinate.column - 1,
|
||||||
|
row: coordinate.row,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if coordinate.row > 0 {
|
||||||
|
v.push(Coordinate {
|
||||||
|
column: coordinate.column,
|
||||||
|
row: coordinate.row - 1,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
v.push(Coordinate {
|
||||||
|
column: coordinate.column + 1,
|
||||||
|
row: coordinate.row,
|
||||||
|
});
|
||||||
|
v.push(Coordinate {
|
||||||
|
column: coordinate.column,
|
||||||
|
row: coordinate.row + 1,
|
||||||
|
});
|
||||||
|
v
|
||||||
|
})
|
||||||
|
.flatten()
|
||||||
|
.filter(|coordinate| self.within_board(coordinate))
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
pub fn within_board(&self, coordinate: &Coordinate) -> bool {
|
pub fn within_board(&self, coordinate: &Coordinate) -> bool {
|
||||||
coordinate.column < self.size.width && coordinate.row < self.size.height
|
coordinate.column < self.size.width && coordinate.row < self.size.height
|
||||||
}
|
}
|
||||||
|
@ -205,11 +263,17 @@ pub struct Group {
|
||||||
coordinates: HashSet<Coordinate>,
|
coordinates: HashSet<Coordinate>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
impl Group {
|
impl Group {
|
||||||
fn contains(&self, coordinate: &Coordinate) -> bool {
|
pub fn adjacencies(&self, max_column: usize, max_row: usize) -> HashSet<Coordinate> {
|
||||||
self.coordinates.contains(coordinate)
|
self.coordinates
|
||||||
|
.iter()
|
||||||
|
.map(|stone| stone.adjacencies(max_column, max_row))
|
||||||
|
.flatten()
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
@ -272,14 +336,9 @@ mod test {
|
||||||
(Coordinate { column: 6, row: 16 }, Color::White),
|
(Coordinate { column: 6, row: 16 }, Color::White),
|
||||||
(Coordinate { column: 7, row: 17 }, Color::White),
|
(Coordinate { column: 7, row: 17 }, Color::White),
|
||||||
(Coordinate { column: 7, row: 18 }, Color::White),
|
(Coordinate { column: 7, row: 18 }, Color::White),
|
||||||
/* */
|
|
||||||
(Coordinate { column: 17, row: 0 }, Color::White),
|
|
||||||
(Coordinate { column: 17, row: 1 }, Color::White),
|
|
||||||
(Coordinate { column: 18, row: 1 }, Color::White),
|
|
||||||
]
|
]
|
||||||
.into_iter(),
|
.into_iter(),
|
||||||
)
|
);
|
||||||
.unwrap();
|
|
||||||
test(board);
|
test(board);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,8 +377,7 @@ mod test {
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
.into_iter(),
|
.into_iter(),
|
||||||
)
|
);
|
||||||
.unwrap();
|
|
||||||
assert!(board.group(&Coordinate { column: 18, row: 3 }).is_none());
|
assert!(board.group(&Coordinate { column: 18, row: 3 }).is_none());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
board
|
board
|
||||||
|
@ -506,7 +564,8 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_finds_adjacent_groups() {
|
fn it_finds_adjacent_groups() {
|
||||||
with_example_board(|board| {
|
with_example_board(|mut board| {
|
||||||
|
println!("{}", board);
|
||||||
let group = board
|
let group = board
|
||||||
.group(&Coordinate { column: 0, row: 0 })
|
.group(&Coordinate { column: 0, row: 0 })
|
||||||
.cloned()
|
.cloned()
|
||||||
|
@ -523,23 +582,17 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn surrounding_a_group_removes_it() {
|
fn surrounding_a_group_removes_it() {
|
||||||
with_example_board(|board| {
|
with_example_board(|mut board| {
|
||||||
let board = board
|
board.place_stone(Coordinate { column: 10, row: 9 }, Color::Black);
|
||||||
.place_stone(Coordinate { column: 10, row: 9 }, Color::Black)
|
|
||||||
.unwrap();
|
|
||||||
assert!(board.stone(&Coordinate { column: 9, row: 9 }).is_none());
|
assert!(board.stone(&Coordinate { column: 9, row: 9 }).is_none());
|
||||||
|
|
||||||
let board = board
|
board.place_stone(Coordinate { column: 2, row: 18 }, Color::Black);
|
||||||
.place_stone(Coordinate { column: 2, row: 18 }, Color::Black)
|
|
||||||
.unwrap();
|
|
||||||
assert!(board.stone(&Coordinate { column: 0, row: 18 }).is_none());
|
assert!(board.stone(&Coordinate { column: 0, row: 18 }).is_none());
|
||||||
assert!(board.stone(&Coordinate { column: 1, row: 18 }).is_none());
|
assert!(board.stone(&Coordinate { column: 1, row: 18 }).is_none());
|
||||||
assert!(board.stone(&Coordinate { column: 0, row: 17 }).is_none());
|
assert!(board.stone(&Coordinate { column: 0, row: 17 }).is_none());
|
||||||
assert!(board.group(&Coordinate { column: 0, row: 18 }).is_none());
|
assert!(board.group(&Coordinate { column: 0, row: 18 }).is_none());
|
||||||
|
|
||||||
let board = board
|
board.place_stone(Coordinate { column: 5, row: 18 }, Color::White);
|
||||||
.place_stone(Coordinate { column: 5, row: 18 }, Color::White)
|
|
||||||
.unwrap();
|
|
||||||
assert!(board.stone(&Coordinate { column: 4, row: 17 }).is_none());
|
assert!(board.stone(&Coordinate { column: 4, row: 17 }).is_none());
|
||||||
assert!(board.stone(&Coordinate { column: 5, row: 17 }).is_none());
|
assert!(board.stone(&Coordinate { column: 5, row: 17 }).is_none());
|
||||||
assert!(board.stone(&Coordinate { column: 6, row: 17 }).is_none());
|
assert!(board.stone(&Coordinate { column: 6, row: 17 }).is_none());
|
||||||
|
@ -548,86 +601,11 @@ mod test {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
fn suicide_is_forbidden() {
|
||||||
fn self_capture_is_forbidden() {
|
assert!(false);
|
||||||
with_example_board(|board| {
|
|
||||||
{
|
|
||||||
let board = board.clone();
|
|
||||||
let res = board.place_stone(Coordinate { column: 18, row: 0 }, Color::Black);
|
|
||||||
assert_eq!(res, Err(BoardError::SelfCapture));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
fn captures_preceed_self_capture() {
|
||||||
let board = board.clone();
|
assert!(false);
|
||||||
let res = board.place_stone(Coordinate { column: 5, row: 18 }, Color::Black);
|
|
||||||
assert_eq!(res, Err(BoardError::SelfCapture));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn validate_group_comparisons() {
|
|
||||||
{
|
|
||||||
let b1 = Board::from_coordinates(
|
|
||||||
vec![(Coordinate { column: 7, row: 9 }, Color::White)].into_iter(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
let b2 = Board::from_coordinates(
|
|
||||||
vec![(Coordinate { column: 7, row: 9 }, Color::White)].into_iter(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(b1, b2);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
let b1 = Board::from_coordinates(
|
|
||||||
vec![
|
|
||||||
(Coordinate { column: 7, row: 9 }, Color::White),
|
|
||||||
(Coordinate { column: 8, row: 10 }, Color::White),
|
|
||||||
]
|
|
||||||
.into_iter(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
let b2 = Board::from_coordinates(
|
|
||||||
vec![
|
|
||||||
(Coordinate { column: 8, row: 10 }, Color::White),
|
|
||||||
(Coordinate { column: 7, row: 9 }, Color::White),
|
|
||||||
]
|
|
||||||
.into_iter(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(b1, b2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn two_boards_can_be_compared() {
|
|
||||||
let board = Board::from_coordinates(
|
|
||||||
vec![
|
|
||||||
(Coordinate { column: 7, row: 9 }, Color::White),
|
|
||||||
(Coordinate { column: 8, row: 8 }, Color::White),
|
|
||||||
(Coordinate { column: 8, row: 10 }, Color::White),
|
|
||||||
(Coordinate { column: 9, row: 9 }, Color::White),
|
|
||||||
(Coordinate { column: 10, row: 9 }, Color::Black),
|
|
||||||
(Coordinate { column: 9, row: 8 }, Color::Black),
|
|
||||||
(Coordinate { column: 9, row: 10 }, Color::Black),
|
|
||||||
]
|
|
||||||
.into_iter(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let b1 = board
|
|
||||||
.clone()
|
|
||||||
.place_stone(Coordinate { column: 8, row: 9 }, Color::Black)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let b2 = b1
|
|
||||||
.clone()
|
|
||||||
.place_stone(Coordinate { column: 9, row: 9 }, Color::White)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(board, b2);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ mod api;
|
||||||
pub use api::{CoreApp, Request, Response};
|
pub use api::{CoreApp, Request, Response};
|
||||||
|
|
||||||
mod types;
|
mod types;
|
||||||
pub use types::{BoardError, Color, Size};
|
pub use types::{Color, Size};
|
||||||
pub mod ui;
|
pub mod ui;
|
||||||
|
|
||||||
mod board;
|
mod board;
|
||||||
|
|
|
@ -3,17 +3,6 @@ use crate::{
|
||||||
board::{Board, Coordinate},
|
board::{Board, Coordinate},
|
||||||
};
|
};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Error)]
|
|
||||||
pub enum BoardError {
|
|
||||||
#[error("Position is invalid")]
|
|
||||||
InvalidPosition,
|
|
||||||
#[error("Self-capture is forbidden")]
|
|
||||||
SelfCapture,
|
|
||||||
#[error("Ko")]
|
|
||||||
Ko,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Hash, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Hash, Eq)]
|
||||||
pub enum Color {
|
pub enum Color {
|
||||||
|
@ -91,8 +80,6 @@ pub struct Player {
|
||||||
|
|
||||||
pub struct GameState {
|
pub struct GameState {
|
||||||
pub board: Board,
|
pub board: Board,
|
||||||
pub past_positions: Vec<Board>,
|
|
||||||
|
|
||||||
pub conversation: Vec<String>,
|
pub conversation: Vec<String>,
|
||||||
pub current_player: Color,
|
pub current_player: Color,
|
||||||
|
|
||||||
|
@ -107,7 +94,6 @@ impl GameState {
|
||||||
fn new() -> GameState {
|
fn new() -> GameState {
|
||||||
GameState {
|
GameState {
|
||||||
board: Board::new(),
|
board: Board::new(),
|
||||||
past_positions: vec![],
|
|
||||||
conversation: vec![],
|
conversation: vec![],
|
||||||
current_player: Color::Black,
|
current_player: Color::Black,
|
||||||
white_player: Player {
|
white_player: Player {
|
||||||
|
@ -123,79 +109,11 @@ impl GameState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn place_stone(&mut self, coordinate: Coordinate) -> Result<(), BoardError> {
|
fn place_stone(&mut self, coordinate: Coordinate) {
|
||||||
let board = self.board.clone();
|
self.board.place_stone(coordinate, self.current_player);
|
||||||
let new_board = board.place_stone(coordinate, self.current_player)?;
|
|
||||||
|
|
||||||
println!("past positions: {}", self.past_positions.len());
|
|
||||||
if self.past_positions.contains(&new_board) {
|
|
||||||
return Err(BoardError::Ko);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.past_positions.push(self.board.clone());
|
|
||||||
self.board = new_board;
|
|
||||||
match self.current_player {
|
match self.current_player {
|
||||||
Color::White => self.current_player = Color::Black,
|
Color::White => self.current_player = Color::Black,
|
||||||
Color::Black => self.current_player = Color::White,
|
Color::Black => self.current_player = Color::White,
|
||||||
};
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn current_player_changes_after_move() {
|
|
||||||
let mut state = GameState::new();
|
|
||||||
assert_eq!(state.current_player, Color::Black);
|
|
||||||
state.place_stone(Coordinate { column: 9, row: 9 }).unwrap();
|
|
||||||
assert_eq!(state.current_player, Color::White);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn current_player_remains_the_same_after_self_capture() {
|
|
||||||
let mut state = GameState::new();
|
|
||||||
state.board = Board::from_coordinates(
|
|
||||||
vec![
|
|
||||||
(Coordinate { column: 17, row: 0 }, Color::White),
|
|
||||||
(Coordinate { column: 17, row: 1 }, Color::White),
|
|
||||||
(Coordinate { column: 18, row: 1 }, Color::White),
|
|
||||||
]
|
|
||||||
.into_iter(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
state.current_player = Color::Black;
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
state.place_stone(Coordinate { column: 18, row: 0 }),
|
|
||||||
Err(BoardError::SelfCapture)
|
|
||||||
);
|
|
||||||
assert_eq!(state.current_player, Color::Black);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn ko_rules_are_enforced() {
|
|
||||||
let mut state = GameState::new();
|
|
||||||
state.board = Board::from_coordinates(
|
|
||||||
vec![
|
|
||||||
(Coordinate { column: 7, row: 9 }, Color::White),
|
|
||||||
(Coordinate { column: 8, row: 8 }, Color::White),
|
|
||||||
(Coordinate { column: 8, row: 10 }, Color::White),
|
|
||||||
(Coordinate { column: 9, row: 9 }, Color::White),
|
|
||||||
(Coordinate { column: 10, row: 9 }, Color::Black),
|
|
||||||
(Coordinate { column: 9, row: 8 }, Color::Black),
|
|
||||||
(Coordinate { column: 9, row: 10 }, Color::Black),
|
|
||||||
]
|
|
||||||
.into_iter(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
state.place_stone(Coordinate { column: 8, row: 9 }).unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
state.place_stone(Coordinate { column: 9, row: 9 }),
|
|
||||||
Err(BoardError::Ko)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -667,7 +667,6 @@ name = "kifu-core"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"grid",
|
"grid",
|
||||||
"thiserror",
|
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
release:
|
|
||||||
cargo build --release
|
|
||||||
|
|
||||||
dev:
|
|
||||||
cargo watch -x 'run --bin kifu-gtk'
|
|
Loading…
Reference in New Issue