1
- import { BISHOP , KING , KNIGHT , PAWN , QUEEN , ROOK } from "chess.js" ;
1
+ import { BISHOP , KING , KNIGHT , PAWN , PieceSymbol , QUEEN , ROOK , Move } from "chess.js" ;
2
2
import BoardPosition from "@src/helpers/BoardPosition" ;
3
3
import VirtualBoardController from "./VirtualBoardController" ;
4
4
import LichessGameController from "./LichessControllers/LichessGameController" ;
@@ -7,30 +7,31 @@ import { concatZeroesUntilSizeMatches, hexToBinary64 } from "@src/helpers/util";
7
7
import { ArduinoCommunicator } from "@src/services/ArduinoCommunicator" ;
8
8
9
9
export default class PhysicalBoardController {
10
- static #instance = null ;
10
+ private static instance : PhysicalBoardController ;
11
+ private lastReadPositioning : string | null = null ;
11
12
12
13
constructor ( ) {
13
- if ( PhysicalBoardController . # instance) {
14
+ if ( PhysicalBoardController . instance ) {
14
15
throw new Error ( "Use PhysicalBoardController.getInstance() instead of new." ) ;
15
16
}
16
17
17
18
this . lastReadPositioning = null ;
18
- PhysicalBoardController . # instance = this ;
19
+ PhysicalBoardController . instance = this ;
19
20
}
20
21
21
22
static getInstance ( ) {
22
- if ( ! PhysicalBoardController . # instance) {
23
- PhysicalBoardController . # instance = new PhysicalBoardController ( ) ;
23
+ if ( ! PhysicalBoardController . instance ) {
24
+ PhysicalBoardController . instance = new PhysicalBoardController ( ) ;
24
25
}
25
26
26
- return PhysicalBoardController . # instance;
27
+ return PhysicalBoardController . instance ;
27
28
}
28
29
29
30
/**
30
31
* move: e.g. "a2a3"
31
32
*/
32
33
// eslint-disable-next-line no-unused-vars
33
- async movePiece ( move , pieceType , moveInformation ) {
34
+ async movePiece ( move : string , pieceType : PieceSymbol , moveInformation : Move ) {
34
35
const fromPosition = new BoardPosition ( move [ 0 ] , Number ( move [ 1 ] ) ) ;
35
36
const toPosition = new BoardPosition ( move [ 2 ] , Number ( move [ 3 ] ) ) ;
36
37
@@ -61,24 +62,24 @@ export default class PhysicalBoardController {
61
62
}
62
63
}
63
64
64
- async #movePawn( fromPosition , toPosition ) {
65
+ async #movePawn( fromPosition : BoardPosition , toPosition : BoardPosition ) {
65
66
await this . #executePhysicalMovesInOrder( fromPosition , toPosition ) ;
66
67
}
67
68
68
69
/**
69
70
* Tries to move the knight without moving another piece first.
70
71
* If thats's not possible, it moves a helper piece to a corner, moves the knight, and the moves the helper piece back
71
72
*/
72
- async #moveKnight( fromPosition , toPosition ) {
73
+ async #moveKnight( fromPosition : BoardPosition , toPosition : BoardPosition ) {
73
74
const helperPiecePositions = this . calculateKnightHelperPiecePositions ( fromPosition , toPosition ) ;
74
75
75
76
//if the tile near the from-position is empty
76
- if ( ! VirtualBoardController . getInstance ( ) . getPieceAtPosition ( helperPiecePositions . nearFromPositionFrom ?. toChessNotationString ( ) ) ) {
77
+ if ( ! VirtualBoardController . getInstance ( ) . getPieceAtPosition ( helperPiecePositions . nearFromPositionFrom ?. toSquareString ( ) ) ) {
77
78
return await this . #executePhysicalMovesInOrder( fromPosition , helperPiecePositions . nearFromPositionFrom , toPosition ) ;
78
79
}
79
80
80
81
//if the tile near the to-position is empty
81
- if ( ! VirtualBoardController . getInstance ( ) . getPieceAtPosition ( helperPiecePositions . nearToPositionFrom ?. toChessNotationString ( ) ) ) {
82
+ if ( ! VirtualBoardController . getInstance ( ) . getPieceAtPosition ( helperPiecePositions . nearToPositionFrom ?. toSquareString ( ) ) ) {
82
83
return await this . #executePhysicalMovesInOrder( fromPosition , helperPiecePositions . nearToPositionFrom , toPosition ) ;
83
84
}
84
85
@@ -96,19 +97,19 @@ export default class PhysicalBoardController {
96
97
await this . #executePhysicalMovesInOrder( helperToPositionToUse , helperFromPositionToUse ) ;
97
98
}
98
99
99
- async #moveBishop( fromPosition , toPosition ) {
100
+ async #moveBishop( fromPosition : BoardPosition , toPosition : BoardPosition ) {
100
101
await this . #executePhysicalMovesInOrder( fromPosition , toPosition ) ;
101
102
}
102
103
103
- async #moveRook( fromPosition , toPosition ) {
104
+ async #moveRook( fromPosition : BoardPosition , toPosition : BoardPosition ) {
104
105
await this . #executePhysicalMovesInOrder( fromPosition , toPosition ) ;
105
106
}
106
107
107
- async #moveQueen( fromPosition , toPosition ) {
108
+ async #moveQueen( fromPosition : BoardPosition , toPosition : BoardPosition ) {
108
109
await this . #executePhysicalMovesInOrder( fromPosition , toPosition ) ;
109
110
}
110
111
111
- async #moveKing( fromPosition , toPosition ) {
112
+ async #moveKing( fromPosition : BoardPosition , toPosition : BoardPosition ) {
112
113
await this . #executePhysicalMovesInOrder( fromPosition , toPosition ) ;
113
114
}
114
115
@@ -119,7 +120,7 @@ export default class PhysicalBoardController {
119
120
* along the rest of the positions and is released at the last position in the array
120
121
* @param {BoardPosition } moves
121
122
*/
122
- async #executePhysicalMovesInOrder( ...moves ) {
123
+ async #executePhysicalMovesInOrder( ...moves : BoardPosition [ ] ) {
123
124
if ( moves . length < 2 ) return ;
124
125
const communicator = ArduinoCommunicator . getInstance ( ) ;
125
126
try {
@@ -188,13 +189,13 @@ export default class PhysicalBoardController {
188
189
}
189
190
}
190
191
191
- #getChangedPosition( pos1 , pos2 ) {
192
+ #getChangedPosition( pos1 : string , pos2 : string ) {
192
193
const num1 = BigInt ( "0b" + pos1 ) ;
193
194
const num2 = BigInt ( "0b" + pos2 ) ;
194
195
195
- const position_int = concatZeroesUntilSizeMatches ( ( num1 ^ num2 ) . toString ( 2 ) , 64 ) . indexOf ( 1 ) ; //because of the xor, theres a 1 only at the changed position
196
+ const position_int = concatZeroesUntilSizeMatches ( ( num1 ^ num2 ) . toString ( 2 ) , 64 ) . indexOf ( "1" ) ; //because of the xor, theres a 1 only at the changed position
196
197
const xPositionStr = String . fromCharCode ( "a" . charCodeAt ( 0 ) + ( position_int % 8 ) ) ;
197
- const yPositionStr = parseInt ( position_int / 8 ) + 1 ;
198
+ const yPositionStr = String . fromCharCode ( position_int / 8 + 1 ) ;
198
199
const positionInChessNotation = xPositionStr + yPositionStr ;
199
200
return positionInChessNotation ;
200
201
}
@@ -205,7 +206,7 @@ export default class PhysicalBoardController {
205
206
* @param {BoardPosition } knightToPosition
206
207
* @returns and object containing the from and to positions of the two potential helper pieces
207
208
*/
208
- calculateKnightHelperPiecePositions ( knightFromPosition , knightToPosition ) {
209
+ calculateKnightHelperPiecePositions ( knightFromPosition : BoardPosition , knightToPosition : BoardPosition ) {
209
210
const delta = knightFromPosition . getDelta ( knightToPosition ) ;
210
211
let nearFromDelta = { x : Math . ceil ( delta . x / 2 ) , y : Math . ceil ( delta . y / 2 ) } ;
211
212
let nearToDelta = { x : Math . floor ( delta . x / 2 ) , y : Math . floor ( delta . y / 2 ) } ;
0 commit comments