cleanup
This commit is contained in:
parent
0ca31017b2
commit
74489c43b6
@ -4,4 +4,4 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
num = "0.4.3"
|
||||
num = "0.4"
|
||||
|
||||
@ -22,7 +22,7 @@ impl Agent for ManualAgent {
|
||||
stdin.lock().read_line(&mut input).ok()?;
|
||||
let numbers = input
|
||||
.split_whitespace()
|
||||
.map(|i| usize::from_str_radix(i, 10).ok())
|
||||
.map(|i| i.parse::<usize>().ok())
|
||||
.collect::<Option<Vec<usize>>>();
|
||||
if let Some(numbers) = numbers {
|
||||
if numbers.len() == 2 {
|
||||
|
||||
41
src/board.rs
41
src/board.rs
@ -1,5 +1,5 @@
|
||||
use crate::{misc::split_from, piece::Piece};
|
||||
use std::fmt;
|
||||
use std::{cmp::Ordering, fmt};
|
||||
|
||||
pub const BOARD_SIZE: usize = 8;
|
||||
|
||||
@ -185,38 +185,27 @@ impl Board {
|
||||
captured
|
||||
}
|
||||
|
||||
// keep score of each color
|
||||
/// Returns (White score, Black score)
|
||||
pub fn get_score(&self) -> (usize, usize) {
|
||||
let mut white_score = 0;
|
||||
let mut black_score = 0;
|
||||
|
||||
for row in &self.board {
|
||||
for &cell in row {
|
||||
match cell {
|
||||
Some(Piece::White) => white_score += 1,
|
||||
Some(Piece::Black) => black_score += 1,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
(white_score, black_score)
|
||||
self.board
|
||||
.iter()
|
||||
.flatten()
|
||||
.flatten()
|
||||
.map(|cell| match cell {
|
||||
Piece::White => (1, 0),
|
||||
Piece::Black => (0, 1),
|
||||
})
|
||||
.fold((0_usize, 0usize), |(a, b), (c, d)| (a + c, b + d))
|
||||
}
|
||||
|
||||
// Get the winning piece (for game over screen mainly)
|
||||
pub fn get_winner(&self) -> Option<Piece> {
|
||||
let (white_score, black_score) = self.get_score();
|
||||
|
||||
// White wins
|
||||
if white_score > black_score {
|
||||
Some(Piece::White)
|
||||
}
|
||||
// Black Wins
|
||||
else if black_score > white_score {
|
||||
Some(Piece::Black)
|
||||
}
|
||||
// Tie
|
||||
else {
|
||||
None
|
||||
match white_score.cmp(&black_score) {
|
||||
Ordering::Greater => Some(Piece::White), // White win
|
||||
Ordering::Less => Some(Piece::Black), // Black win
|
||||
Ordering::Equal => None, // Tie
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user