Board squares flipping
This commit is contained in:
@@ -53,12 +53,12 @@ enum Piece {
|
||||
impl Piece {
|
||||
fn symbol(&self) -> &'static str {
|
||||
match self {
|
||||
Piece::King('w') => "♔",
|
||||
Piece::Queen('w') => "♕",
|
||||
Piece::Rook('w') => "♖",
|
||||
Piece::Bishop('w') => "♗",
|
||||
Piece::Knight('w') => "♘",
|
||||
Piece::Pawn('w') => "♙",
|
||||
Piece::King('w') => "♚",
|
||||
Piece::Queen('w') => "♛",
|
||||
Piece::Rook('w') => "♜",
|
||||
Piece::Bishop('w') => "♝",
|
||||
Piece::Knight('w') => "♞",
|
||||
Piece::Pawn('w') => "♟︎",
|
||||
Piece::King('b') => "♚",
|
||||
Piece::Queen('b') => "♛",
|
||||
Piece::Rook('b') => "♜",
|
||||
@@ -351,13 +351,15 @@ impl eframe::App for ChessApp {
|
||||
);
|
||||
|
||||
// Draw the chess board
|
||||
let player = "black";
|
||||
if player =="white"{
|
||||
let tile_size = board_size / 8.0;
|
||||
for row in 0..8 {
|
||||
for col in 0..8 {
|
||||
let color = if (row + col) % 2 == 0 {
|
||||
egui::Color32::from_rgb(100, 97, 97)
|
||||
} else {
|
||||
egui::Color32::from_rgb(217, 217, 217)
|
||||
} else {
|
||||
egui::Color32::from_rgb(100, 97, 97)
|
||||
};
|
||||
|
||||
let rect = egui::Rect::from_min_size(
|
||||
@@ -416,6 +418,78 @@ impl eframe::App for ChessApp {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if player=="black"{
|
||||
{
|
||||
let tile_size = board_size / 8.0;
|
||||
for row in 0..8 {
|
||||
for col in 0..8 {
|
||||
let color = if (row + col) % 2 == 0 {
|
||||
egui::Color32::from_rgb(217, 217, 217)
|
||||
} else {
|
||||
egui::Color32::from_rgb(100, 97, 97)
|
||||
};
|
||||
|
||||
let rect = egui::Rect::from_min_size(
|
||||
egui::Pos2::new(
|
||||
board_rect.min.x + col as f32 * tile_size,
|
||||
board_rect.min.y + row as f32 * tile_size,
|
||||
),
|
||||
egui::Vec2::new(tile_size, tile_size),
|
||||
);
|
||||
|
||||
painter.rect_filled(rect, 0.0, color);
|
||||
|
||||
// Draw piece
|
||||
let piece = self.board[row][col];
|
||||
if piece != Piece::Empty {
|
||||
let symbol = piece.symbol();
|
||||
let font_id = egui::FontId::proportional(tile_size * 0.75);
|
||||
painter.text(
|
||||
rect.center(),
|
||||
egui::Align2::CENTER_CENTER,
|
||||
symbol,
|
||||
font_id,
|
||||
if matches!(
|
||||
piece,
|
||||
Piece::King('w')
|
||||
| Piece::Queen('w')
|
||||
| Piece::Rook('w')
|
||||
| Piece::Bishop('w')
|
||||
| Piece::Knight('w')
|
||||
| Piece::Pawn('w')
|
||||
) {
|
||||
egui::Color32::BLACK
|
||||
} else {
|
||||
egui::Color32::WHITE
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// Draw selection highlight
|
||||
if self.selected == Some((row, col)) {
|
||||
painter.rect_stroke(
|
||||
rect,
|
||||
0.0,
|
||||
egui::Stroke::new(3.0, egui::Color32::RED),
|
||||
egui::StrokeKind::Inside,
|
||||
);
|
||||
}
|
||||
|
||||
// Handle clicks
|
||||
if ui.ctx().input(|i| i.pointer.primary_clicked()) {
|
||||
let click_pos =
|
||||
ui.ctx().input(|i| i.pointer.interact_pos()).unwrap();
|
||||
if rect.contains(click_pos) {
|
||||
self.handle_click(row, col);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user