Skip to content

Commit b145a02

Browse files
committed
serde is sick
1 parent 622f421 commit b145a02

File tree

7 files changed

+124
-24
lines changed

7 files changed

+124
-24
lines changed

Cargo.lock

+45
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ edition = "2021"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12+
serde = { version = "1.0", features = ["derive"] }
13+
# Each data format lives in its own crate; the sample code below uses JSON
14+
# but you may be using a different one.
15+
serde_json = "1.0"
1216
wasm-bindgen = "0.2"
1317
getrandom = { version = "0.2", features = ["js"] }
1418
rand = "0.8"

crust-ui/src/chess/board.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import init, { greet } from "../../crust";
1+
import init, { greet, parse_fen } from "../../crust";
2+
3+
const initFen = "r1bqkbnr/1pp2p1p/p1n5/1P1P4/P1p1p1p1/5N2/4PPPP/RNBQKB1R b KQkq - 0 1"
24

35
init().then(() => {
46
// console.log("helloooooo ")
5-
greet("Webassambly")
7+
let board = parse_fen(initFen);
8+
console.log('board is', board);
9+
// greet("Webassambly")
10+
611
})
712

813

914
export class Board {
10-
1115
}

src/chess/bboard.rs

+31-10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ use super::board::{Board, CastlingRights};
22
use super::piece::{Color, Direction, Piece, P};
33
use rand::Rng;
44
use std::fmt::Debug; // 0.8.5
5+
// use serde::{Serialize, Deserialize};
6+
use serde::{Deserialize, Serialize};
7+
// use serde::*;
8+
// use serde_derive::{Serialize, Deserialize};
59

6-
#[derive(Clone)]
10+
#[derive(Serialize, Deserialize)]
711
pub struct Move {
812
pub from: u32,
913
pub target: u32,
@@ -187,7 +191,7 @@ fn pawn_attacks(bb: u64, color: Color) -> u64 {
187191
// return (maindia >> sout) << nort;
188192
// }
189193

190-
#[derive(Debug, Clone)]
194+
#[derive(Debug, Clone, Serialize, Deserialize)]
191195
pub struct BBoard {
192196
white: [u64; 7],
193197
black: [u64; 7],
@@ -532,12 +536,12 @@ impl BBoard {
532536
}
533537
}
534538

535-
return alpha
539+
return alpha;
536540
}
537541

538542
fn alpha_beta_min(&self, alpha: i32, mut beta: i32, depth: u32) -> i32 {
539543
if depth == 0 {
540-
return -self.evaluate(self.turn)
544+
return -self.evaluate(self.turn);
541545
}
542546

543547
let moves = self.get_moves_and_captures(self.turn);
@@ -548,15 +552,15 @@ impl BBoard {
548552
let score = c.alpha_beta_max(alpha, beta, depth - 1);
549553

550554
if score <= alpha {
551-
return alpha
555+
return alpha;
552556
}
553557

554558
if score < beta {
555559
beta = score
556560
}
557561
}
558562

559-
return beta
563+
return beta;
560564
}
561565

562566
pub fn eval_side_score(&self, side: Color) -> i32 {
@@ -582,7 +586,6 @@ impl BBoard {
582586
}
583587

584588
pub fn search_good_move(&self, depth: u32) -> Move {
585-
586589
// let mut best_score: i32 = -999999999;
587590
let mut best_score = -f32::INFINITY as i32;
588591
let mut best_score = f32::INFINITY as i32;
@@ -597,9 +600,8 @@ impl BBoard {
597600
},
598601
};
599602

600-
601603
let score = self.alpha_beta_max(-f32::INFINITY as i32, f32::INFINITY as i32, depth);
602-
println!(">> score of alpha beta max is {score}", );
604+
println!(">> score of alpha beta max is {score}",);
603605
self.loop_through_moves_and_captures(self.turn, |m| {
604606
let mut c = self.clone();
605607
c.push_unchecked_move(&m);
@@ -609,7 +611,6 @@ impl BBoard {
609611
best_score = score;
610612
best_move = m;
611613
}
612-
613614
});
614615

615616
best_move
@@ -1009,4 +1010,24 @@ impl BBoard {
10091010

10101011
return panic!("no piece there");
10111012
}
1013+
1014+
1015+
pub fn serialize(&self) -> String {
1016+
// self
1017+
let serialized = serde_json::to_string(self).unwrap();
1018+
1019+
// Prints serialized = {"x":1,"y":2}
1020+
println!("serialized = {}", serialized);
1021+
serialized
1022+
}
1023+
1024+
pub fn from_serialization(val: &str) -> BBoard {
1025+
let b: BBoard = serde_json::from_str(val).unwrap();
1026+
b
1027+
// serde_json::Deserializer
1028+
}
1029+
1030+
pub fn to_fen(&self) -> String {
1031+
todo!()
1032+
}
10121033
}

src/chess/board.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
use std::collections::HashMap;
22

33
use super::piece::{Color, Direction, Piece, P};
4+
use serde::{Deserialize, Serialize};
45
use super::bboard::BBoard;
56

6-
#[derive(Debug, Clone)]
7+
#[derive(Debug, Clone, Serialize, Deserialize)]
78
pub struct Move {
89
pub from: usize,
910
pub target: usize,
1011
// pub capture: Option<usize>
1112
}
1213

13-
#[derive(Debug, Clone)]
14+
#[derive(Debug, Clone,)]
1415
pub struct Board {
1516
pub turn: Color,
1617
pub move_count: u64,
@@ -21,7 +22,7 @@ pub struct Board {
2122
pub black_cr: CastlingRights,
2223
}
2324

24-
#[derive(Debug, Clone)]
25+
#[derive(Debug, Clone, Serialize, Deserialize)]
2526
pub struct CastlingRights {
2627
pub queen: bool,
2728
pub king: bool,

src/chess/piece.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::board::Board;
22
use super::bboard::*;
33
use std::ops::{Add, BitXor, Mul, Shr, ShrAssign};
4+
use serde::{Serialize, Deserialize};
45

56
// A >> B (can A pass through B?)
67
impl Shr for Piece {
@@ -156,7 +157,7 @@ impl Direction {
156157
}
157158
}
158159

159-
#[derive(Debug, Clone, Copy, PartialEq)]
160+
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
160161
pub enum Direction {
161162
Up,
162163
Down, // and in the end its only round and round
@@ -168,7 +169,7 @@ pub enum Direction {
168169
DownRight,
169170
}
170171

171-
#[derive(Debug, Clone, Copy, PartialEq)]
172+
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
172173
pub enum Color {
173174
Black,
174175
White,
@@ -183,7 +184,7 @@ impl Color {
183184
}
184185
}
185186

186-
#[derive(Debug, Clone, Copy, PartialEq)]
187+
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
187188
pub enum P {
188189
Pawn,
189190
Knight,
@@ -196,7 +197,7 @@ pub enum P {
196197
}
197198

198199

199-
#[derive(Debug, Clone, Copy)]
200+
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
200201
pub struct Piece {
201202
pub class: P,
202203
pub color: Color,

src/lib.rs

+28-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,37 @@ use chess::piece::{Color, Piece, P};
99
use wasm_bindgen::prelude::*;
1010

1111
#[wasm_bindgen]
12-
extern {
12+
extern "C" {
1313
pub fn alert(s: &str);
1414
}
1515

16-
1716
#[wasm_bindgen]
1817
pub fn greet(name: &str) -> () {
19-
// return 1
20-
alert(&format!("Hello, {}!", name));
18+
// return 1
19+
alert(&format!("Hello, {}!", name));
20+
}
21+
22+
#[wasm_bindgen]
23+
pub fn parse_fen(fen: &str) -> String {
24+
let fen = fen.to_string();
25+
// let fen = String::from("r1bqkbnr/1pp2p1p/p1n5/1P1P4/P1p1p1p1/5N2/4PPPP/RNBQKB1R b KQkq - 0 1");
26+
let board = Board::from_fen(&fen);
27+
let mut bboard = BBoard::new();
28+
29+
bboard.white_cr = board.white_cr;
30+
bboard.black_cr = board.black_cr;
31+
bboard.en_passant_target = board.en_passant;
32+
bboard.turn = board.turn;
33+
34+
for (i, sq) in board.squares.iter().enumerate() {
35+
if sq.is_none() {
36+
continue;
37+
}
38+
let piece = sq.unwrap();
39+
dbg!(i, piece);
40+
41+
bboard.place(piece, i as u8);
42+
}
43+
44+
bboard.serialize()
2145
}

0 commit comments

Comments
 (0)