This commit is contained in:
Simon Gardling 2025-02-03 20:03:38 -05:00
parent 0ca31017b2
commit 74489c43b6
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
3 changed files with 17 additions and 28 deletions

View File

@ -4,4 +4,4 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
num = "0.4.3" num = "0.4"

View File

@ -22,7 +22,7 @@ impl Agent for ManualAgent {
stdin.lock().read_line(&mut input).ok()?; stdin.lock().read_line(&mut input).ok()?;
let numbers = input let numbers = input
.split_whitespace() .split_whitespace()
.map(|i| usize::from_str_radix(i, 10).ok()) .map(|i| i.parse::<usize>().ok())
.collect::<Option<Vec<usize>>>(); .collect::<Option<Vec<usize>>>();
if let Some(numbers) = numbers { if let Some(numbers) = numbers {
if numbers.len() == 2 { if numbers.len() == 2 {

View File

@ -1,5 +1,5 @@
use crate::{misc::split_from, piece::Piece}; use crate::{misc::split_from, piece::Piece};
use std::fmt; use std::{cmp::Ordering, fmt};
pub const BOARD_SIZE: usize = 8; pub const BOARD_SIZE: usize = 8;
@ -185,38 +185,27 @@ impl Board {
captured captured
} }
// keep score of each color /// Returns (White score, Black score)
pub fn get_score(&self) -> (usize, usize) { pub fn get_score(&self) -> (usize, usize) {
let mut white_score = 0; self.board
let mut black_score = 0; .iter()
.flatten()
for row in &self.board { .flatten()
for &cell in row { .map(|cell| match cell {
match cell { Piece::White => (1, 0),
Some(Piece::White) => white_score += 1, Piece::Black => (0, 1),
Some(Piece::Black) => black_score += 1, })
_ => {} .fold((0_usize, 0usize), |(a, b), (c, d)| (a + c, b + d))
}
}
}
(white_score, black_score)
} }
// Get the winning piece (for game over screen mainly) // Get the winning piece (for game over screen mainly)
pub fn get_winner(&self) -> Option<Piece> { pub fn get_winner(&self) -> Option<Piece> {
let (white_score, black_score) = self.get_score(); let (white_score, black_score) = self.get_score();
// White wins match white_score.cmp(&black_score) {
if white_score > black_score { Ordering::Greater => Some(Piece::White), // White win
Some(Piece::White) Ordering::Less => Some(Piece::Black), // Black win
} Ordering::Equal => None, // Tie
// Black Wins
else if black_score > white_score {
Some(Piece::Black)
}
// Tie
else {
None
} }
} }