@@ -7,25 +7,27 @@ mod unittest;
77use anyhow:: { anyhow, Context , Result } ;
88use itertools:: Itertools ;
99use std:: cmp:: { Ordering , PartialEq } ;
10+ use std:: fmt;
1011use std:: hash:: { DefaultHasher , Hash , Hasher } ;
1112use std:: ops:: Range ;
12- use wasm_bindgen:: prelude:: wasm_bindgen;
1313
14- #[ wasm_bindgen]
1514#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
1615pub struct Coordinates {
1716 pub x : i32 ,
1817 pub y : i32 ,
1918}
2019
21- #[ wasm_bindgen]
2220#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
2321pub struct Size {
2422 pub x : i32 ,
2523 pub y : i32 ,
2624}
2725
28- #[ wasm_bindgen]
26+ pub enum Axis {
27+ Horizontal ,
28+ Vertical ,
29+ }
30+
2931#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
3032pub enum SlideDirection {
3133 Up ,
@@ -34,15 +36,13 @@ pub enum SlideDirection {
3436 Right ,
3537}
3638
37- #[ wasm_bindgen]
3839#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
3940pub struct SlideMove {
4041 pub start : Coordinates ,
4142 pub direction : SlideDirection ,
4243 pub distance : u8 ,
4344}
4445
45- #[ wasm_bindgen]
4646#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
4747pub struct Piece {
4848 /// The coordinates of the piece's bottom left most tile
@@ -52,13 +52,9 @@ pub struct Piece {
5252}
5353
5454/// A game board filled with all tiles
55- #[ wasm_bindgen]
5655#[ derive( Debug , Clone , Copy ) ]
5756pub struct Board {
5857 pub size : Size ,
59- // TODO(Menno 18.12.2024) https://github.com/rustwasm/wasm-bindgen/issues/122
60- // Wasm bindgen doesn't support arrays at the moment, work around with custom getter.
61- #[ wasm_bindgen( skip) ]
6258 pub pieces : [ Piece ; 10 ] ,
6359}
6460
@@ -75,6 +71,46 @@ pub fn to_id(board: &Board) -> BoardId {
7571/// Standard Klotski board is 4 by 5 tiles
7672const SIZE : Size = Size { x : 4 , y : 5 } ;
7773
74+ impl fmt:: Display for Coordinates {
75+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
76+ write ! (
77+ f,
78+ "{}{}" ,
79+ Self :: axis_to_string( Axis :: Horizontal , self . x as u8 ) ,
80+ Self :: axis_to_string( Axis :: Vertical , self . y as u8 )
81+ )
82+ }
83+ }
84+
85+ impl Coordinates {
86+ pub fn axis_to_string ( axis : Axis , coordinate : u8 ) -> String {
87+ match axis {
88+ Axis :: Horizontal => ( ( b'A' + coordinate) as char ) . to_string ( ) ,
89+ Axis :: Vertical => ( coordinate + 1 ) . to_string ( ) ,
90+ }
91+ }
92+ }
93+
94+ impl fmt:: Display for SlideMove {
95+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
96+ write ! ( f, "{}⮕{}" , self . start, self . get_endpoint( ) )
97+ }
98+ }
99+
100+ impl SlideMove {
101+ fn get_endpoint ( & self ) -> Coordinates {
102+ let mut end = self . start ;
103+ let distance = self . distance as i32 ;
104+ match self . direction {
105+ SlideDirection :: Up => end. y += distance,
106+ SlideDirection :: Down => end. y -= distance,
107+ SlideDirection :: Left => end. x -= distance,
108+ SlideDirection :: Right => end. x += distance,
109+ }
110+ end
111+ }
112+ }
113+
78114impl Eq for Board { }
79115
80116impl PartialEq < Self > for Board {
@@ -102,14 +138,6 @@ impl Hash for Board {
102138 }
103139}
104140
105- #[ wasm_bindgen]
106- impl Board {
107- #[ wasm_bindgen( getter) ]
108- pub fn pieces ( & self ) -> Vec < Piece > {
109- self . pieces . to_vec ( )
110- }
111- }
112-
113141pub fn get_start_board ( ) -> Board {
114142 /// Standard Klotski board is:
115143 /// ABBC
@@ -304,13 +332,7 @@ pub fn make_move(board: &Board, slide_move: &SlideMove) -> Result<Board> {
304332 . context ( "No piece to move" ) ?;
305333
306334 // move the piece by specified distance
307- let distance: i32 = slide_move. distance as i32 ;
308- match slide_move. direction {
309- SlideDirection :: Up => piece_to_move. position . y += distance,
310- SlideDirection :: Down => piece_to_move. position . y -= distance,
311- SlideDirection :: Left => piece_to_move. position . x -= distance,
312- SlideDirection :: Right => piece_to_move. position . x += distance,
313- }
335+ piece_to_move. position = slide_move. get_endpoint ( ) ;
314336
315337 // After modifying the board, we need to sort it to ensure correct ID calculation.
316338 new_board. pieces . sort ( ) ;
0 commit comments