Compare commits

..

41 Commits

Author SHA1 Message Date
Varga Dávid Lajos
7e64a7ca16 fixed incorrect test parameter in bitboard::legality::tests::check_test_test 2025-11-18 16:20:42 +01:00
Varga Dávid Lajos
6e0efc76f3 added missing method place_piece to struct bitboard::Board 2025-11-18 16:18:19 +01:00
Varga Dávid Lajos
1a9d7ad460 added tests for method bitboard::legality::check_test 2025-11-18 16:15:28 +01:00
Varga Dávid Lajos
9209f1c4e0 implemented method check_test in bitboard::checktest.rs 2025-11-18 12:14:22 +01:00
Bence
3ebfc61ac7 Merge pull request #15 from htamas1210/Engine/library-functions
Engine/library functions
2025-11-17 15:04:28 +01:00
Hatvani Tamás
d8d8b58ed7 Uploaded presentation for class 2025-11-17 12:24:42 +01:00
Varga Dávid Lajos
a8970053f9 added constructor for castling moves in chessmove.rs 2025-11-16 19:05:52 +01:00
vargadavidlajos
7cd77ab904 Merge pull request #14 from htamas1210/Docs/kovspec
Added point 3 to kovspec.md
2025-11-16 19:05:07 +01:00
Varga Dávid Lajos
fa0a93172d added constructore for captures in chessmove.rs 2025-11-16 19:01:53 +01:00
Varga Dávid Lajos
bb74a486d8 added constructor for quiet moves in chessmove.rs 2025-11-16 19:00:00 +01:00
vargadavidlajos
10c176a0f5 Added point 3 to kovspec.md 2025-11-16 18:39:48 +01:00
Varga Dávid Lajos
d1932762f9 removed field is_promotion from struct ChessMove and added an Option for field promotion_piece 2025-11-16 18:33:04 +01:00
Varga Dávid Lajos
9181b6c4ca added all arguments constructor for struct BoardSquare 2025-11-16 13:51:10 +01:00
Varga Dávid Lajos
b703d8b9a1 added parameterless constructor for struct BoardSquare 2025-11-16 13:44:41 +01:00
Varga Dávid Lajos
acaa7d078a added new library function: get_board_after_move 2025-11-15 20:40:58 +01:00
Varga Dávid Lajos
30448b5f3d switched return type of is_game_over from bool to GameEnd 2025-11-15 20:37:06 +01:00
Varga Dávid Lajos
ff38c5c475 added struct GameEnd 2025-11-15 20:28:33 +01:00
Varga Dávid Lajos
42daeb9971 added file and module structure for gameend.rs 2025-11-15 20:15:17 +01:00
Bence
fc7ece9e94 Fixed app not starting in fullscreen 2025-11-15 20:09:26 +01:00
Varga Dávid Lajos
c8b6bc35d5 added library functions: get_available_moves and is_game_over 2025-11-15 20:08:54 +01:00
Varga Dávid Lajos
5f2a4e1721 added struct ChessMove 2025-11-15 19:57:11 +01:00
Varga Dávid Lajos
5c42b6e52a added enum MoveType 2025-11-15 19:37:30 +01:00
Varga Dávid Lajos
f7364e7939 added file and module structure for movetype.rs 2025-11-15 19:35:17 +01:00
Varga Dávid Lajos
b4a1f0820f added struct BoardSquare 2025-11-15 19:26:21 +01:00
Varga Dávid Lajos
6d9d5c49ae added file and module structure for boardsquare.rs 2025-11-15 19:24:37 +01:00
Varga Dávid Lajos
1ca44b472b added enum PieceType 2025-11-15 19:21:46 +01:00
Varga Dávid Lajos
79aabeac8c added file and module structure for piecetype.rs 2025-11-15 19:17:50 +01:00
Varga Dávid Lajos
5eb85dab89 added file and module structure for file: chessmove.rs 2025-11-15 19:15:30 +01:00
Varga Dávid Lajos
291b7e71a2 added crate root file lib.rs for library crate 2025-11-15 19:14:08 +01:00
vargadavidlajos
2aaa2aca70 Merge pull request #13 from htamas1210/UI/SettingsAndGame
Created a new branch due to a bug
2025-11-15 19:11:39 +01:00
Bence
456d386551 Created a new branch due to a bug
Previous pull request didn't work (didnt change ui/src/main.rs), created new branch to see if the problem persists
2025-11-15 19:06:25 +01:00
Hatvani Tamás
e663d61b9e Merge pull request #11 from htamas1210/Engine/pseudo-moves
Engine/pseudo moves
2025-11-15 18:05:17 +01:00
Varga Dávid Lajos
659413ca31 implemented pseudo-move generation for queens 2025-11-15 13:47:46 +01:00
Varga Dávid Lajos
4de505bc21 implemented pseudo-move generation for rooks 2025-11-15 13:47:03 +01:00
Varga Dávid Lajos
7c58f0d508 implemented pseudo-move generation for bishops 2025-11-15 13:46:11 +01:00
Varga Dávid Lajos
c9246d1342 added helper function: get_raycast_from_square_in_direction 2025-11-15 13:45:01 +01:00
Varga Dávid Lajos
eab795b6df implemented pseudo-move generation for pawn captures 2025-11-15 13:43:37 +01:00
Varga Dávid Lajos
f8ab14a026 implemented pseudo-move generation for kings 2025-11-15 13:42:41 +01:00
Varga Dávid Lajos
274897a070 implemented pseudo-move generation for knights 2025-11-15 13:41:40 +01:00
Varga Dávid Lajos
8ecbeb9c41 implemented pseudo-move generation for pawn moves 2025-11-15 13:40:40 +01:00
Varga Dávid Lajos
a1482d11f2 added file and module structure for bitboard::attacks.rs 2025-11-15 13:38:48 +01:00
16 changed files with 5209 additions and 3 deletions

View File

@@ -22,6 +22,68 @@ A vágyálom rendszer alapját egy központi szerver képezné, amely kezeli a f
A platform célja a megbízható és folyamatos működés, akár nagyobb terhelés mellett is. A rendszer fejlesztése során kiemelt szempont lenne a biztonság (adatvédelem, csalás elleni védelem), a stabil hálózati kommunikáció, valamint a bővíthetőség például ranglisták, versenyek vagy mobilalkalmazás későbbi integrálásának lehetősége.
Összességében a vágyálom rendszer egy minden szempontból teljes értékű, közösségorientált sakkalkalmazás lenne, amely a mostani, helyi hálózaton működő változatból fejlődne tovább egy interneten keresztül bárhonnan elérhető platformmá.
## 3. Igényelt funkciók
### Alapok
- Két játékos közti sakkjátszma lebonyolítása.
- Teljes szabályrendszer megvalósítása (érvényes lépések, sakk/sakkmatt/patt felismerése).
- Új játék indítása.
- Játékosok nevének megadása a játszma elején.
- Felhasználóbarát grafikus felület (UI) látható tábla, figurák, órák, státuszjelzések.
- Játékoslépések vizuális kiemelése (pl. kijelölt mező, lehetséges lépések megjelenítése).
- A játék állapotának kijelzése (folyamatban, sakk, matt, döntetlen).
### LAN és hálózati funkciók
- „Szerver indítása” funkció a játékos hostként indíthat egy helyi szervert.
- „Csatlakozás” funkció másik játékos IP-cím alapján tud csatlakozni.
- Helyi hálózaton keresztüli valós idejű kommunikáció.
- LAN játék automatikus felfedezése (broadcast keresés).
- Játék mentése és visszatöltése hálózati módban.
### Online vágyálom funkciók
- Felhasználói regisztráció és bejelentkezés.
- Jelszóval védett fiókok, email- vagy OAuth-alapú hitelesítés (Google, GitHub stb.).
- Profiloldal megtekintése (név, avatar, statisztikák, értékszám).
- Automatikus matchmaking rendszer.
- Kézi játékindítás meghívó küldése barátnak.
- Játszmák mentése és visszajátszása.
- Játszmaelemzés lépések listázása, hibák kiemelése.
- Webes felület vagy mobilalkalmazás támogatása.
- Játék előzményeinek és statisztikáinak megtekintése (győzelmek, vereségek, döntetlenek).
- Automatikus szervermentés és adatbázis szinkronizáció.
### Felhasználói felület
- Letisztult, reszponzív, platformfüggetlen felület (asztali és webes verzió).
- Sötét/világos téma lehetősége.
- Egyéni figurakészlet vagy tábla kinézet választása.
- Animált figuramozgások.
- Egérrel és billentyűzettel is vezérelhető játék.
- Hangjelzések a lépésekhez és az idő lejártához.
- Lépéselőzmények (move list) megjelenítése oldalt.
- Tábla forgatásának lehetősége (pl. a fehér vagy fekete nézőpontból).
- Állapotjelzők (sakk, matt, döntetlen, várakozás az ellenfélre).
- Teljes képernyős mód.
### Technikai / fejlesztői funkciók
- Kliensszerver architektúra.
- REST API vagy WebSocket alapú kommunikáció.
- Adatbázis a felhasználói adatok és meccsek tárolására (pl. SQLite, PostgreSQL, MongoDB).
- Logolási és hibakezelési rendszer.
- Automatikus mentés és adatvisszaállítás.
- Verziókezelés (Git).
- Tesztelhető, moduláris kódszerkezet (külön modulok: logika, UI, hálózat, adat).
- Cross-platform működés (Windows, Linux, esetleg web).
### További funkciók
- Egyszemélyes mód (ember vs. gép, AI-bot).
- Több nehézségi szintű AI.
- Oktató mód (javasolt lépések, hibák magyarázata).
- Hivatalos FEN/PGN formátum export/import.
- Beépített sakkfeladványok, kihívások.
- Érintéses vezérlés mobilon.
- Többnyelvű felület (pl. magyar, angol).
## 4. Rendszer követelmények
A rendszer célja egy kétjátékos sakkalkalmazás megvalósítása, amely alapvetően hálózati kapcsolat (LAN vagy internet) segítségével biztosítja a valós idejű játékot. A rendszer kliensszerver architektúrán alapul, ahol az egyes komponensek jól elkülönülten, meghatározott feladatokat látnak el.

BIN
Knightly prezentáció.pptx Normal file

Binary file not shown.

View File

@@ -2,5 +2,6 @@ mod attackmaps;
mod utils;
mod legality;
mod checkinfo;
mod attacks;
pub mod board;

View File

@@ -0,0 +1,89 @@
use super::board::Board;
use super::attackmaps::*;
impl Board {
const RANK_2: u64 = 0x0000_0000_0000_FF00;
const RANK_7: u64 = 0x00FF_0000_0000_0000;
pub fn get_pseudo_pawn_moves(&self, sq: u32) -> u64 {
let pawn: u64 = 1 << sq;
let mut move_mask: u64 = 0u64;
let move_offset: i8 = 8 - 16 * self.side_to_move as i8;
let next_sq: u64 = if move_offset > 0 {pawn << move_offset} else {pawn >> -move_offset};
if (self.occupancy[2] & next_sq) == 0 {
move_mask |= next_sq;
if (self.side_to_move == 0 && pawn & Self::RANK_2 != 0)
|| (self.side_to_move == 1 && pawn & Self::RANK_7 != 0) {
let next_sq: u64 = if move_offset > 0 {next_sq << move_offset} else {next_sq >> -move_offset};
if (self.occupancy[2] & next_sq) == 0 {
move_mask |= next_sq;
}
}
}
return move_mask;
}
#[inline]
pub fn get_pseudo_knight_moves(&self, sq: u32) -> u64 {
return KNIGHT_ATTACK_MAP[sq as usize];
}
#[inline]
pub fn get_pseudo_king_moves(&self, sq: u32) -> u64 {
return KING_ATTACK_MAP[sq as usize];
}
#[inline]
pub fn get_pseudo_pawn_captures(&self, sq: u32) -> u64 {
return PAWN_ATTACK_MAP[sq as usize][self.side_to_move as usize];
}
#[inline]
pub fn get_pseudo_opponent_pawn_captures(&self, sq: u32) -> u64 {
return PAWN_ATTACK_MAP[sq as usize][1 - self.side_to_move as usize];
}
#[inline]
pub fn get_pseudo_bishop_moves(&self, sq: u32) -> u64 {
let mut moves = 0u64;
let sq = sq as usize;
let occupancy = self.occupancy[2];
moves |= get_raycast_from_square_in_direction(occupancy, sq, 1);
moves |= get_raycast_from_square_in_direction(occupancy, sq, 3);
moves |= get_raycast_from_square_in_direction(occupancy, sq, 5);
moves |= get_raycast_from_square_in_direction(occupancy, sq, 7);
return moves;
}
#[inline]
pub fn get_pseudo_rook_moves(&self, sq: u32) -> u64 {
let mut moves: u64 = 0u64;
let occupancy = self.occupancy[2];
let sq = sq as usize;
moves |= get_raycast_from_square_in_direction(occupancy, sq, 0);
moves |= get_raycast_from_square_in_direction(occupancy, sq, 2);
moves |= get_raycast_from_square_in_direction(occupancy, sq, 4);
moves |= get_raycast_from_square_in_direction(occupancy, sq, 6);
return moves;
}
#[inline(always)]
pub fn get_pseudo_queen_moves(&self, sq: u32) -> u64 {
return self.get_pseudo_bishop_moves(sq) | self.get_pseudo_rook_moves(sq);
}
}
#[inline(always)]
pub fn get_raycast_from_square_in_direction(occupancy: u64, sq: usize, dir: usize) -> u64 {
let is_up: bool = dir / 4 == 0;
let mut ray: u64 = RAY_TABLE[sq][dir];
let blockers: u64 = occupancy & ray;
if blockers != 0 {
let first_blocker: u32 = if is_up { blockers.trailing_zeros() } else { 63 - blockers.leading_zeros() };
ray &= !RAY_TABLE[first_blocker as usize][dir];
}
return ray;
}

View File

@@ -64,7 +64,7 @@ impl Board {
for (i, c) in coming_up.chars().enumerate() {
if pieces.contains(&c) {
// board.place_piece(row*8 + col, c);
board.place_piece(row*8 + col, c);
col += 1;
}
else if ('1'..='8').contains(&c) {
@@ -179,4 +179,21 @@ impl Board {
}
}
pub fn place_piece(&mut self, sq: i32, piece: char) {
match piece {
'p' => {self.bitboards[6] |= 1 << sq}
'n' => {self.bitboards[7] |= 1 << sq}
'b' => {self.bitboards[8] |= 1 << sq}
'r' => {self.bitboards[9] |= 1 << sq}
'q' => {self.bitboards[10] |= 1 << sq}
'k' => {self.bitboards[11] |= 1 << sq}
'P' => {self.bitboards[0] |= 1 << sq}
'N' => {self.bitboards[1] |= 1 << sq}
'B' => {self.bitboards[2] |= 1 << sq}
'R' => {self.bitboards[3] |= 1 << sq}
'Q' => {self.bitboards[4] |= 1 << sq}
'K' => {self.bitboards[5] |= 1 << sq}
_ => ()
}
}
}

View File

@@ -1,8 +1,56 @@
use super::board::Board;
use super::attackmaps::RAY_TABLE;
use super::checkinfo::CheckInfo;
use super::attacks::get_raycast_from_square_in_direction;
impl Board {
pub fn check_test(&self) -> CheckInfo {
let mut check_info: CheckInfo = CheckInfo::new();
let offset: usize = 6 * self.side_to_move as usize;
let king: u64 = self.bitboards[5 + offset];
let king_sq = king.trailing_zeros() as usize;
let occupancy = self.occupancy[2];
// queen-rook checks (+)
let attacker_mask = self.bitboards[10 - offset] | self.bitboards[9 - offset];
for dir in [0, 2, 4, 6] {
let threat_mask: u64 = get_raycast_from_square_in_direction(occupancy, king_sq, dir);
if threat_mask & attacker_mask != 0 {
check_info.add_checker(threat_mask);
}
}
// queen-bishop checks (x)
let attacker_mask = self.bitboards[10 - offset] | self.bitboards[8 - offset];
for dir in [1, 3, 5, 7] {
let threat_mask = get_raycast_from_square_in_direction(occupancy, king_sq, dir);
if threat_mask & attacker_mask != 0 {
check_info.add_checker(threat_mask);
}
}
// knight checks (L)
let attacker_mask = self.bitboards[7 - offset];
let threat_mask = self.get_pseudo_knight_moves(king_sq as u32);
let checker = threat_mask & attacker_mask;
if checker != 0 {
check_info.add_checker(checker);
}
// pawn checks (v)
let attacker_mask = self.bitboards[6 - offset];
let threat_mask = self.get_pseudo_pawn_captures(king_sq as u32);
let checker = threat_mask & attacker_mask;
if checker != 0 {
check_info.add_checker(checker);
}
return check_info;
}
pub(in super) fn calc_pinned_squares(&mut self) {
self.pinned_squares = [4; 64];
self.pin_mask = 0u64;
@@ -45,4 +93,35 @@ impl Board {
}
}
}
// <----- TESTS ----->
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_test_test() {
let fens = [
"rnb1k1nr/pppppppp/4q3/8/1b6/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", // no check
"rnb1k1nr/pppppppp/4q3/8/1b1P4/8/PPP1PPPP/RNBQKBNR w KQkq d3 0 1", // single check
"rnb1k1nr/ppp1p2p/3pq1p1/8/1b1P1P2/8/PPP2PPP/RNBQKBNR w KQkq - 0 1" // double check
];
let expected_results = [
CheckInfo { check_count: 0, move_mask: 0xFFFF_FFFF_FFFF_FFFF },
CheckInfo { check_count: 1, move_mask: 0x0000_0000_0204_0800 },
CheckInfo { check_count: 2, move_mask: 0x0000_0000_0000_0000 }
];
for test_nr in 0..3 {
let fen = fens[test_nr];
let board = Board::build(fen);
let check_test_actual = board.check_test();
assert_eq!(check_test_actual.check_count, expected_results[test_nr].check_count);
assert_eq!(check_test_actual.move_mask, expected_results[test_nr].move_mask);
}
}
}

32
engine/src/boardsquare.rs Normal file
View File

@@ -0,0 +1,32 @@
pub struct BoardSquare {
pub x: usize,
pub y: usize
}
impl BoardSquare {
pub fn new() -> Self {
return Self{
x: 0,
y: 0
};
}
pub fn from_coord(x: usize, y: usize) -> Self {
#[cfg(debug_assertions)]
{
if x > 7 {
println!("Warning: x coordinate of square is bigger than 7, it might not be on the board!");
}
if y > 7 {
println!("Warning: y coordinate of square is bigger than 7, it might not be on the board!");
}
}
return Self {
x: x,
y: y
};
}
}

71
engine/src/chessmove.rs Normal file
View File

@@ -0,0 +1,71 @@
use crate::piecetype;
use super::boardsquare::BoardSquare;
use super::piecetype::PieceType;
use super::movetype::MoveType;
pub struct ChessMove {
pub move_type: MoveType,
pub piece_type: PieceType,
pub from_square: BoardSquare,
pub to_square: BoardSquare,
pub rook_from: BoardSquare,
pub rook_to: BoardSquare,
pub promotion_piece: Option<PieceType>
}
impl ChessMove {
pub fn quiet(
piece_type: PieceType,
from_square: BoardSquare,
to_square: BoardSquare,
promotion_piece: Option<PieceType>
) -> Self {
return Self {
move_type: MoveType::Quiet,
piece_type: piece_type,
from_square: from_square,
to_square: to_square,
rook_from: BoardSquare::new(),
rook_to: BoardSquare::new(),
promotion_piece: promotion_piece
}
}
pub fn capture(
piece_type: PieceType,
from_square: BoardSquare,
to_square: BoardSquare,
promotion_piece: Option<PieceType>
) -> Self {
return Self {
move_type: MoveType::Capture,
piece_type: piece_type,
from_square: from_square,
to_square: to_square,
rook_from: BoardSquare::new(),
rook_to: BoardSquare::new(),
promotion_piece: promotion_piece
}
}
pub fn castle(
piece_type: PieceType,
from_square: BoardSquare,
to_square: BoardSquare,
rook_from: BoardSquare,
rook_to: BoardSquare
) -> Self {
return Self {
move_type: MoveType::Quiet,
piece_type: piece_type,
from_square: from_square,
to_square: to_square,
rook_from: rook_from,
rook_to: rook_to,
promotion_piece: None
}
}
}

6
engine/src/gameend.rs Normal file
View File

@@ -0,0 +1,6 @@
pub enum GameEnd {
WhiteWon(String),
BlackWon(String),
Draw(String)
}

24
engine/src/lib.rs Normal file
View File

@@ -0,0 +1,24 @@
mod bitboard;
pub mod chessmove;
pub mod piecetype;
pub mod boardsquare;
pub mod movetype;
pub mod gameend;
use chessmove::ChessMove;
use gameend::GameEnd;
pub fn get_available_moves(fen: &str) -> Vec<ChessMove> {
println!("get_available_moves answered");
return vec![];
}
pub fn is_game_over(fen: &str) -> Option<GameEnd> {
println!("is_game_over answered");
return None;
}
pub fn get_board_after_move(fen: &str, chess_move: &ChessMove) -> String {
println!("get_board_after_move answered");
return String::from("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
}

7
engine/src/movetype.rs Normal file
View File

@@ -0,0 +1,7 @@
pub enum MoveType {
Quiet,
Capture,
Castle,
EnPassant
}

15
engine/src/piecetype.rs Normal file
View File

@@ -0,0 +1,15 @@
pub enum PieceType {
WhitePawn,
WhiteKnight,
WhiteBishop,
WhiteRook,
WhiteQueen,
WhiteKing,
BlackPawn,
BlackKnight,
BlackBishop,
BlackRook,
BlackQueen,
BlackKing
}

4371
ui/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -4,3 +4,7 @@ version = "0.1.0"
edition = "2024"
[dependencies]
eframe = "0.33.0"
egui = "0.33.0"
tokio-tungstenite = "0.28.0"
winit = "0.30.12"

BIN
ui/fonts/DejaVuSans.ttf Normal file

Binary file not shown.

View File

@@ -1,3 +1,431 @@
fn main() {
println!("Hello, world!");
use eframe::egui;
fn main() -> eframe::Result<()> {
let options = eframe::NativeOptions{
viewport: egui::ViewportBuilder::default()
.with_fullscreen(true)
.with_min_inner_size(egui::vec2(800.0, 600.0)) // Minimum width, height
.with_inner_size(egui::vec2(7680.0, 4320.0)), // Initial size
..Default::default()
};
eframe::run_native(
"Knightly",
options,
Box::new(|cc| {
let mut fonts = egui::FontDefinitions::default();
fonts.font_data.insert(
"symbols".to_owned(),
egui::FontData::from_static(include_bytes!("../fonts/DejaVuSans.ttf")).into(),
);
fonts
.families
.entry(egui::FontFamily::Proportional)
.or_default()
.insert(0, "symbols".to_owned());
cc.egui_ctx.set_fonts(fonts);
Ok(Box::new(ChessApp::default()))
}),
)
}
#[derive(Clone, Copy, PartialEq, Debug)]
enum Piece {
King(char),
Queen(char),
Rook(char),
Bishop(char),
Knight(char),
Pawn(char),
Empty,
}
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('b') => "",
Piece::Queen('b') => "",
Piece::Rook('b') => "",
Piece::Bishop('b') => "",
Piece::Knight('b') => "",
Piece::Pawn('b') => "♟︎",
Piece::Empty => "",
_ => "",
}
}
}
#[derive(PartialEq, Debug)]
enum Turn {
White,
Black,
}
enum AppState {
MainMenu,
InGame,
Settings,
}
struct ChessApp {
fullscreen: bool,
resolutions: Vec<(u32, u32)>,
selected_resolution: usize,
state: AppState,
board: [[Piece; 8]; 8],
selected: Option<(usize, usize)>,
turn: Turn,
pending_settings: PendingSettings,
server_port: String,
}
#[derive(Default)]
struct PendingSettings {
fullscreen: bool,
selected_resolution: usize,
server_port: String,
}
impl Default for ChessApp {
fn default() -> Self {
Self {
fullscreen: true,
resolutions: vec![
(1280, 720),
(1600, 900),
(1920, 1080),
(2560, 1440),
(3840, 2160),
(7680, 4320),
],
selected_resolution: 2, // Default to 1920x1080
state: AppState::MainMenu,
board: Self::starting_board(),
selected: None,
turn: Turn::White,
pending_settings: PendingSettings::default(),
server_port: "8080".to_string(), // Default port
}
}
}
impl ChessApp {
fn starting_board() -> [[Piece; 8]; 8] {
use Piece::*;
[
[
Rook('b'),
Knight('b'),
Bishop('b'),
Queen('b'),
King('b'),
Bishop('b'),
Knight('b'),
Rook('b'),
],
[Pawn('b'); 8],
[Empty; 8],
[Empty; 8],
[Empty; 8],
[Empty; 8],
[Pawn('w'); 8],
[
Rook('w'),
Knight('w'),
Bishop('w'),
Queen('w'),
King('w'),
Bishop('w'),
Knight('w'),
Rook('w'),
],
]
}
fn handle_click(&mut self, row: usize, col: usize) {
if let Some((r, c)) = self.selected {
let piece = self.board[r][c];
self.board[r][c] = Piece::Empty;
self.board[row][col] = piece;
self.selected = None;
self.turn = if self.turn == Turn::White {
Turn::Black
} else {
Turn::White
};
} else {
if self.board[row][col] != Piece::Empty {
self.selected = Some((row, col));
}
}
}
fn apply_settings(&mut self, ctx: &egui::Context) {
self.fullscreen = self.pending_settings.fullscreen;
self.selected_resolution = self.pending_settings.selected_resolution;
self.server_port = self.pending_settings.server_port.clone();
if let Some(resolution) = self.resolutions.get(self.selected_resolution) {
ctx.send_viewport_cmd(egui::ViewportCommand::InnerSize(
egui::Vec2::new(resolution.0 as f32, resolution.1 as f32)
));
}
ctx.send_viewport_cmd(egui::ViewportCommand::Fullscreen(self.fullscreen));
}
fn enter_settings(&mut self) {
self.pending_settings.fullscreen = self.fullscreen;
self.pending_settings.selected_resolution = self.selected_resolution;
self.pending_settings.server_port = self.server_port.clone();
self.state = AppState::Settings;
}
}
impl eframe::App for ChessApp {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
match self.state {
AppState::MainMenu => {
egui::CentralPanel::default().show(ctx, |ui| {
ui.vertical_centered(|ui| {
ui.heading("♞ Knightly ♞");
ui.add_space(30.0);
if ui.add_sized([300.0, 60.0], egui::Button::new("Play")).clicked() {
self.state = AppState::InGame;
}
ui.add_space(8.0);
if ui.add_sized([300.0, 60.0], egui::Button::new("Settings")).clicked() {
self.enter_settings();
}
ui.add_space(8.0);
if ui
.add_sized([300.0, 60.0], egui::Button::new("Quit"))
.clicked()
{
std::process::exit(0);
}
});
});
}
AppState::Settings => {
egui::CentralPanel::default().show(ctx, |ui| {
ui.vertical_centered(|ui| {
ui.heading("Settings");
ui.add_space(30.0);
// Fullscreen toggle
ui.horizontal(|ui| {
ui.label("Fullscreen:");
if ui.checkbox(&mut self.pending_settings.fullscreen, "").changed() {
// If enabling fullscreen, we might want to disable resolution selection
}
});
ui.add_space(10.0);
// Resolution dropdown
ui.horizontal(|ui| {
ui.label("Resolution:");
egui::ComboBox::new("resolution_combo", "")
.selected_text(format!(
"{}x{}",
self.resolutions[self.pending_settings.selected_resolution].0,
self.resolutions[self.pending_settings.selected_resolution].1
))
.show_ui(ui, |ui| {
for (i, &(width, height)) in self.resolutions.iter().enumerate() {
ui.selectable_value(
&mut self.pending_settings.selected_resolution,
i,
format!("{}x{}", width, height),
);
}
});
});
ui.add_space(10.0);
// Server port input field
ui.horizontal(|ui| {
ui.label("Local Server Port:");
ui.add(egui::TextEdit::singleline(&mut self.pending_settings.server_port)
.desired_width(100.0)
.hint_text("8080"));
});
ui.add_space(30.0);
// Apply and Cancel buttons
ui.horizontal(|ui| {
if ui.add_sized([140.0, 40.0], egui::Button::new("Apply")).clicked() {
self.apply_settings(ctx);
self.state = AppState::MainMenu;
}
if ui.add_sized([140.0, 40.0], egui::Button::new("Cancel")).clicked() {
self.state = AppState::MainMenu;
}
});
});
});
}
AppState::InGame => {
egui::TopBottomPanel::top("menu_bar").show(ctx, |ui| {
ui.horizontal(|ui| {
if ui.button("Main Menu").clicked() {
self.state = AppState::MainMenu;
}
if ui.button("Settings").clicked() {
self.enter_settings();
}
if ui.button("New Game").clicked() {
*self = ChessApp::default();
self.state = AppState::InGame;
}
ui.separator();
ui.label(format!("Turn: {:?}", self.turn));
});
});
egui::CentralPanel::default().show(ctx, |ui| {
ui.vertical_centered(|ui| {
let full_avail = ui.available_rect_before_wrap();
let board_tile = (full_avail.width().min(full_avail.height())) / 8.0;
let board_size = board_tile * 8.0;
// Create a child UI at the board position
let (response, painter) = ui.allocate_painter(
egui::Vec2::new(board_size, board_size),
egui::Sense::click()
);
let board_rect = egui::Rect::from_center_size(
full_avail.center(),
egui::vec2(board_size, board_size)
);
// Draw the chess board
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)
};
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::WHITE
} else {
egui::Color32::BLACK
}
);
}
// 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);
}
}
}
}
});
});
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_initial_board_setup() {
let app = ChessApp::default();
assert!(matches!(app.board[0][0], Piece::Rook('b')));
assert!(matches!(app.board[7][0], Piece::Rook('w')));
assert!(matches!(app.board[1][0], Piece::Pawn('b')));
assert!(matches!(app.board[6][0], Piece::Pawn('w')));
}
#[test]
fn test_piece_symbols() {
assert_eq!(Piece::King('w').symbol(), "");
assert_eq!(Piece::King('b').symbol(), "");
assert_eq!(Piece::Empty.symbol(), "");
}
#[test]
fn test_piece_selection() {
let mut app = ChessApp::default();
app.handle_click(6, 0);
assert_eq!(app.selected, Some((6, 0)));
app.handle_click(6, 0);
assert_eq!(app.selected, None);
}
#[test]
fn test_piece_movement() {
let mut app = ChessApp::default();
// Select and move a piece
app.handle_click(6, 0); // Select white pawn
app.handle_click(5, 0); // Move to empty square
assert_eq!(app.board[6][0], Piece::Empty);
assert!(matches!(app.board[5][0], Piece::Pawn('w')));
}
#[test]
fn test_turn_switching() {
let mut app = ChessApp::default();
assert_eq!(app.turn, Turn::White);
app.handle_click(6, 0); // White selects
app.handle_click(5, 0); // White moves
assert_eq!(app.turn, Turn::Black); // Should now be Black's turn
}
#[test]
fn test_server_port_default() {
let app = ChessApp::default();
assert_eq!(app.server_port, "8080");
}
}