resolved clippy issues
This commit is contained in:
@@ -157,12 +157,7 @@ fn draw_piece(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_score_bar(
|
fn draw_score_bar(cr: &CairoContext, board: &Rectangle, draughts_game: &DraughtsGame) {
|
||||||
cr: &CairoContext,
|
|
||||||
board: &Rectangle,
|
|
||||||
draughts_game: &DraughtsGame,
|
|
||||||
xform: &Matrix,
|
|
||||||
) {
|
|
||||||
let score_bar = Rectangle::new(
|
let score_bar = Rectangle::new(
|
||||||
board.tl().x() - board.width() / 10.0,
|
board.tl().x() - board.width() / 10.0,
|
||||||
board.tl().y(),
|
board.tl().y(),
|
||||||
@@ -171,17 +166,7 @@ fn draw_score_bar(
|
|||||||
);
|
);
|
||||||
let score_percentage = draughts_game.relative_score(Player::White) as f64;
|
let score_percentage = draughts_game.relative_score(Player::White) as f64;
|
||||||
let tl = score_bar.tl();
|
let tl = score_bar.tl();
|
||||||
let br = score_bar.br();
|
|
||||||
{
|
|
||||||
let (tlx, tly) = xform.transform_point(tl.x(), tl.y());
|
|
||||||
let (brx, bry) = xform.transform_point(br.x(), br.y());
|
|
||||||
println!(
|
|
||||||
"tl: ({}, {}), br: ({}, {}), score: {}",
|
|
||||||
tlx, tly, brx, bry, score_percentage
|
|
||||||
);
|
|
||||||
}
|
|
||||||
cr.save().unwrap();
|
cr.save().unwrap();
|
||||||
//cr.set_matrix(*xform);
|
|
||||||
cr.set_source_rgb(1.0, 1.0, 1.0);
|
cr.set_source_rgb(1.0, 1.0, 1.0);
|
||||||
cr.rectangle(
|
cr.rectangle(
|
||||||
score_bar.tl().x(),
|
score_bar.tl().x(),
|
||||||
@@ -195,7 +180,7 @@ fn draw_score_bar(
|
|||||||
tl.x(),
|
tl.x(),
|
||||||
tl.y() + score_bar.height() * score_percentage,
|
tl.y() + score_bar.height() * score_percentage,
|
||||||
score_bar.width(),
|
score_bar.width(),
|
||||||
score_bar.height(),
|
score_bar.height() * (1.0 - score_percentage),
|
||||||
);
|
);
|
||||||
cr.fill().unwrap();
|
cr.fill().unwrap();
|
||||||
cr.restore().unwrap();
|
cr.restore().unwrap();
|
||||||
@@ -226,6 +211,10 @@ fn on_activate(application: >k::Application) {
|
|||||||
let board_width = SQUARE_SIZE * DraughtsBoard::rows() as f64;
|
let board_width = SQUARE_SIZE * DraughtsBoard::rows() as f64;
|
||||||
let board_height = SQUARE_SIZE * DraughtsBoard::columns() as f64;
|
let board_height = SQUARE_SIZE * DraughtsBoard::columns() as f64;
|
||||||
let board = Rectangle::from_points(Point::new(0.0, 0.0), Point::new(board_width, board_height));
|
let board = Rectangle::from_points(Point::new(0.0, 0.0), Point::new(board_width, board_height));
|
||||||
|
let board_with_bar = Rectangle::from_points(
|
||||||
|
Point::new(-board_width / 10.0, 0.0),
|
||||||
|
Point::new(board_width, board_height),
|
||||||
|
);
|
||||||
let board_clone = board;
|
let board_clone = board;
|
||||||
let crown_red_handle = {
|
let crown_red_handle = {
|
||||||
let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from_static(CROWN_RED));
|
let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from_static(CROWN_RED));
|
||||||
@@ -267,11 +256,11 @@ fn on_activate(application: >k::Application) {
|
|||||||
Point::new(width as f64, height as f64),
|
Point::new(width as f64, height as f64),
|
||||||
);
|
);
|
||||||
let f = f64::min(
|
let f = f64::min(
|
||||||
screen.width() / board.width(),
|
screen.width() / board_with_bar.width(),
|
||||||
screen.height() / board.height(),
|
screen.height() / board_with_bar.height(),
|
||||||
);
|
);
|
||||||
let screen_center = screen.center();
|
let screen_center = screen.center();
|
||||||
let board_center = board.center();
|
let board_center = board_with_bar.center();
|
||||||
let mut xform = xform.borrow_mut();
|
let mut xform = xform.borrow_mut();
|
||||||
*xform = Matrix::multiply(
|
*xform = Matrix::multiply(
|
||||||
&Matrix::multiply(
|
&Matrix::multiply(
|
||||||
@@ -389,7 +378,7 @@ fn on_activate(application: >k::Application) {
|
|||||||
cr.restore().unwrap();
|
cr.restore().unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
draw_score_bar(&cr, &board, &draughts_game.borrow(), &xform);
|
draw_score_bar(cr, &board, &draughts_game.borrow());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
let gesture = gtk::GestureClick::new();
|
let gesture = gtk::GestureClick::new();
|
||||||
|
@@ -105,8 +105,8 @@ impl DraughtsBoard {
|
|||||||
Board::<Piece, 8, 8>::columns()
|
Board::<Piece, 8, 8>::columns()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pieces<'a, const PIECES: usize>(
|
pub fn pieces<const PIECES: usize>(
|
||||||
&'a self,
|
&self,
|
||||||
pieces: Vec<Piece, PIECES>,
|
pieces: Vec<Piece, PIECES>,
|
||||||
) -> impl Iterator<Item = Position> {
|
) -> impl Iterator<Item = Position> {
|
||||||
self.0
|
self.0
|
||||||
@@ -420,7 +420,7 @@ impl DraughtsGame {
|
|||||||
let pos = mv.get_start_position();
|
let pos = mv.get_start_position();
|
||||||
let piece = self.board.get_piece(&pos);
|
let piece = self.board.get_piece(&pos);
|
||||||
let moving_player = piece.player().unwrap();
|
let moving_player = piece.player().unwrap();
|
||||||
self.apply_move(&mv).unwrap();
|
self.apply_move(mv).unwrap();
|
||||||
if depth != 0 {
|
if depth != 0 {
|
||||||
let mut best_score = None;
|
let mut best_score = None;
|
||||||
for mv in self.available_moves() {
|
for mv in self.available_moves() {
|
||||||
@@ -428,21 +428,19 @@ impl DraughtsGame {
|
|||||||
let score = clone.move_score(&mv, player, depth - 1);
|
let score = clone.move_score(&mv, player, depth - 1);
|
||||||
if best_score.is_none() {
|
if best_score.is_none() {
|
||||||
best_score = Some(score);
|
best_score = Some(score);
|
||||||
} else {
|
} else if let Some(bs) = best_score {
|
||||||
if let Some(bs) = best_score {
|
|
||||||
if bs < score {
|
if bs < score {
|
||||||
best_score = Some(bs);
|
best_score = Some(bs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
best_score.unwrap_or_else(|| self.relative_score(player))
|
best_score.unwrap_or_else(|| self.relative_score(player))
|
||||||
} else {
|
} else {
|
||||||
self.relative_score(player)
|
self.relative_score(player)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn available_moves<'a>(&'a self) -> impl Iterator<Item = Move> {
|
fn available_moves(&self) -> impl Iterator<Item = Move> {
|
||||||
let mut pieces = Vec::<Piece, 2>::new();
|
let mut pieces = Vec::<Piece, 2>::new();
|
||||||
match self.next_move {
|
match self.next_move {
|
||||||
Player::White => {
|
Player::White => {
|
||||||
@@ -459,36 +457,6 @@ impl DraughtsGame {
|
|||||||
.flat_map(|pos| self.moves_for_piece(pos))
|
.flat_map(|pos| self.moves_for_piece(pos))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_best_move_rec<const MAX_HEAP_SIZE: usize>(
|
|
||||||
&self,
|
|
||||||
ranking: &mut MoveRanking<MAX_HEAP_SIZE>,
|
|
||||||
current_depth: u32,
|
|
||||||
max_depth: u32,
|
|
||||||
) {
|
|
||||||
let mut pieces = Vec::<Piece, 2>::new();
|
|
||||||
match self.next_move {
|
|
||||||
Player::White => {
|
|
||||||
pieces.push(Piece::SimpleWhitePawn).unwrap();
|
|
||||||
pieces.push(Piece::CrownedWhitePawn).unwrap();
|
|
||||||
}
|
|
||||||
Player::Red => {
|
|
||||||
pieces.push(Piece::SimpleRedPawn).unwrap();
|
|
||||||
pieces.push(Piece::CrownedRedPawn).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.board.pieces(pieces).for_each(|pos| {
|
|
||||||
self.moves_for_piece(pos).for_each(|mv| {
|
|
||||||
let mut game = self.clone();
|
|
||||||
game.apply_move(&mv).unwrap();
|
|
||||||
let new_score = game.score_for_player(self.next_move);
|
|
||||||
ranking.push(MoveHeapEntry {
|
|
||||||
mv,
|
|
||||||
score: new_score,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_best_move(&self, max_depth: u32) -> Option<Move> {
|
pub fn get_best_move(&self, max_depth: u32) -> Option<Move> {
|
||||||
let mut result: Option<(Move, f32)> = None;
|
let mut result: Option<(Move, f32)> = None;
|
||||||
let available_moves = self.available_moves().count();
|
let available_moves = self.available_moves().count();
|
||||||
@@ -500,14 +468,12 @@ impl DraughtsGame {
|
|||||||
let score = self.clone().move_score(&mv, self.next_move, max_depth - 1);
|
let score = self.clone().move_score(&mv, self.next_move, max_depth - 1);
|
||||||
if result.is_none() {
|
if result.is_none() {
|
||||||
result = Some((mv, score));
|
result = Some((mv, score));
|
||||||
} else {
|
} else if let Some((_, best_score)) = result {
|
||||||
if let Some((_, best_score)) = result {
|
|
||||||
if score > best_score {
|
if score > best_score {
|
||||||
result = Some((mv, score));
|
result = Some((mv, score));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
result.map(|(mv, _)| mv)
|
result.map(|(mv, _)| mv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -521,7 +487,7 @@ struct MoveHeapEntry {
|
|||||||
|
|
||||||
impl PartialOrd for MoveHeapEntry {
|
impl PartialOrd for MoveHeapEntry {
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
|
||||||
self.score.partial_cmp(&other.score)
|
Some(self.score.cmp(&other.score))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user