mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
chore: add more debug logs
This commit is contained in:
@@ -9,7 +9,7 @@ use crate::logic::{
|
|||||||
|
|
||||||
use std::ops::{Index, IndexMut};
|
use std::ops::{Index, IndexMut};
|
||||||
|
|
||||||
use log::{debug, error, info, warn};
|
use log::{debug, error, info, trace, warn};
|
||||||
|
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
board: Vec<Field>,
|
board: Vec<Field>,
|
||||||
@@ -71,6 +71,7 @@ impl Board {
|
|||||||
|
|
||||||
/// For testing
|
/// For testing
|
||||||
fn new_empty(player1: &mut Player, player2: &mut Player, size: Option<usize>) -> Self {
|
fn new_empty(player1: &mut Player, player2: &mut Player, size: Option<usize>) -> Self {
|
||||||
|
debug!("Creating new empty board!");
|
||||||
let size = size.unwrap_or(8);
|
let size = size.unwrap_or(8);
|
||||||
|
|
||||||
player1.end_row = Some(size - 1);
|
player1.end_row = Some(size - 1);
|
||||||
@@ -95,6 +96,7 @@ impl Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn place_pawn(&mut self, target: Position, owner: &Player) {
|
fn place_pawn(&mut self, target: Position, owner: &Player) {
|
||||||
|
debug!("Placing new pawn by hand!");
|
||||||
self[target].pawn = Some(Pawn::new(owner));
|
self[target].pawn = Some(Pawn::new(owner));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,6 +128,7 @@ impl Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_available_moves(&self, pos: Position, player: &Player) -> Option<Vec<MovePath>> {
|
pub fn get_available_moves(&self, pos: Position, player: &Player) -> Option<Vec<MovePath>> {
|
||||||
|
debug!("=== Checking available moves for pawn on: {} ===", pos);
|
||||||
let mut available_moves: Vec<MovePath> = vec![];
|
let mut available_moves: Vec<MovePath> = vec![];
|
||||||
let mut possible_directions: Vec<Vector2D> = vec![];
|
let mut possible_directions: Vec<Vector2D> = vec![];
|
||||||
|
|
||||||
@@ -201,7 +204,10 @@ impl Board {
|
|||||||
already_captured: &Vec<Position>,
|
already_captured: &Vec<Position>,
|
||||||
pawn: &Pawn,
|
pawn: &Pawn,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
debug!("=== Checkinf if pawn can capture from: {} to {}", from, to);
|
debug!(
|
||||||
|
"=== Checking if pawn can capture from: {} to {} ===",
|
||||||
|
from, to
|
||||||
|
);
|
||||||
|
|
||||||
if !self.is_player_owner_of_the_pawn(player, &pawn) {
|
if !self.is_player_owner_of_the_pawn(player, &pawn) {
|
||||||
debug!("Player wasn't owner of the pawn...");
|
debug!("Player wasn't owner of the pawn...");
|
||||||
@@ -257,6 +263,7 @@ impl Board {
|
|||||||
all_paths: &mut Vec<CapturePath>,
|
all_paths: &mut Vec<CapturePath>,
|
||||||
pawn: &Pawn,
|
pawn: &Pawn,
|
||||||
) {
|
) {
|
||||||
|
trace!("Searching for captures on: {}...", current_pos);
|
||||||
let mut found_any_capture: bool = false;
|
let mut found_any_capture: bool = false;
|
||||||
|
|
||||||
for direction in available_directions {
|
for direction in available_directions {
|
||||||
@@ -307,6 +314,7 @@ impl Board {
|
|||||||
from: Position,
|
from: Position,
|
||||||
player: &Player,
|
player: &Player,
|
||||||
) -> Option<Vec<CapturePath>> {
|
) -> Option<Vec<CapturePath>> {
|
||||||
|
debug!("=== Searching for available captures from: {} === ", from);
|
||||||
let Some(capturing_pawn) = &self[from].pawn else {
|
let Some(capturing_pawn) = &self[from].pawn else {
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
@@ -356,10 +364,11 @@ impl Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn capture(&mut self, path: &CapturePath, capturing_player: &Player) -> bool {
|
pub fn capture(&mut self, path: &CapturePath, capturing_player: &Player) -> bool {
|
||||||
/*
|
debug!(
|
||||||
debug!("Capturing: {}", path);
|
"Capturing from: {} to: {}!",
|
||||||
*/
|
path.from,
|
||||||
|
path.steps.last().unwrap()
|
||||||
|
);
|
||||||
// TODO! Maybe return false ??
|
// TODO! Maybe return false ??
|
||||||
|
|
||||||
let mut from = path.from;
|
let mut from = path.from;
|
||||||
@@ -390,6 +399,8 @@ impl Board {
|
|||||||
|
|
||||||
/// Returns true if move was successful
|
/// Returns true if move was successful
|
||||||
pub fn move_pawn(&mut self, player: &Player, from: Position, to: Position) -> bool {
|
pub fn move_pawn(&mut self, player: &Player, from: Position, to: Position) -> bool {
|
||||||
|
debug!("Moving pawn from: {} to {}", from, to);
|
||||||
|
|
||||||
if !self.is_move_valid(&from, &to) {
|
if !self.is_move_valid(&from, &to) {
|
||||||
warn!("Tried invalid move: {} -> {}", from, to);
|
warn!("Tried invalid move: {} -> {}", from, to);
|
||||||
return false;
|
return false;
|
||||||
@@ -422,6 +433,10 @@ impl Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn move_pawn_anywhere(&mut self, player: &Player, from: Position, to: Position) -> bool {
|
fn move_pawn_anywhere(&mut self, player: &Player, from: Position, to: Position) -> bool {
|
||||||
|
debug!(
|
||||||
|
"Attempting to move pawn from: {} to: {}. This is a anywhere_move!",
|
||||||
|
from, to
|
||||||
|
);
|
||||||
let Some(pawn) = &self[from].pawn else {
|
let Some(pawn) = &self[from].pawn else {
|
||||||
warn!(
|
warn!(
|
||||||
"Tried moving pawn from: {}, but there is no pawn there!",
|
"Tried moving pawn from: {}, but there is no pawn there!",
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use log::warn;
|
use log::{info, warn};
|
||||||
|
|
||||||
use crate::logic::{
|
use crate::logic::{
|
||||||
board::{
|
board::{
|
||||||
@@ -99,10 +99,13 @@ impl GameMaster {
|
|||||||
let passive_lost: bool = self.is_player_lost(passive);
|
let passive_lost: bool = self.is_player_lost(passive);
|
||||||
|
|
||||||
if active_lost && passive_lost {
|
if active_lost && passive_lost {
|
||||||
|
info!("Game ended at a draw!");
|
||||||
Some(GameResult::Draw)
|
Some(GameResult::Draw)
|
||||||
} else if active_lost {
|
} else if active_lost {
|
||||||
|
info!("Player: {} won", passive.id);
|
||||||
Some(GameResult::Lost(active.id))
|
Some(GameResult::Lost(active.id))
|
||||||
} else if passive_lost {
|
} else if passive_lost {
|
||||||
|
info!("Player: {} won", active.id);
|
||||||
Some(GameResult::Lost(passive.id))
|
Some(GameResult::Lost(passive.id))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use core::fmt;
|
use core::fmt;
|
||||||
use std::ops::Sub;
|
use std::ops::Sub;
|
||||||
|
|
||||||
use log::debug;
|
use log::{debug, trace};
|
||||||
|
|
||||||
use crate::logic::math::vector::Vector2D;
|
use crate::logic::math::vector::Vector2D;
|
||||||
|
|
||||||
@@ -28,8 +28,8 @@ impl Position {
|
|||||||
pub fn checked_add(&self, delta: &Vector2D) -> Option<Position> {
|
pub fn checked_add(&self, delta: &Vector2D) -> Option<Position> {
|
||||||
let new_row = self.row.checked_add_signed(delta.row as isize)?;
|
let new_row = self.row.checked_add_signed(delta.row as isize)?;
|
||||||
let new_column = self.column.checked_add_signed(delta.column as isize)?;
|
let new_column = self.column.checked_add_signed(delta.column as isize)?;
|
||||||
debug!("Adding row: {} + {} = {}", self.row, delta.row, new_row);
|
trace!("Adding row: {} + {} = {}", self.row, delta.row, new_row);
|
||||||
debug!(
|
trace!(
|
||||||
"Adding column: {} + {} = {}",
|
"Adding column: {} + {} = {}",
|
||||||
self.column, delta.column, new_column
|
self.column, delta.column, new_column
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user