@@ -5,85 +5,87 @@ import {Chessboard} from './chessboard';
5
5
import { Suggestor } from './suggestor' ;
6
6
7
7
export module ChessEngineAPI {
8
- @Injectable ( ) export class ChessboardUI {
9
- selectedPieceRow : number ;
10
- selectedPieceCol : number ;
11
- isPieceSelected : boolean = false ;
12
- lookahead : number = 3 ;
13
- breadth : number = 7 ;
8
+ @Injectable ( ) export class ChessboardUI {
9
+ selectedPieceRow : number ;
10
+ selectedPieceCol : number ;
11
+ isPieceSelected : boolean = false ;
12
+ lookahead : number = 3 ;
13
+ breadth : number = 7 ;
14
14
15
- private chessboard : Chessboard = new Chessboard ( new Array < Move > ( ) ) ;
15
+ private chessboard : Chessboard = new Chessboard ( new Array < Move > ( ) ) ;
16
16
17
- get fields ( ) : number [ ] [ ] {
18
- return this . chessboard . fields ;
19
- }
17
+ get fields ( ) : number [ ] [ ] {
18
+ return this . chessboard . fields ;
19
+ }
20
20
21
- get isWhitePlaying ( ) : boolean { return this . chessboard . isWhitePlaying }
21
+ get isWhitePlaying ( ) : boolean { return this . chessboard . isWhitePlaying }
22
22
23
- get capturedPieces ( ) : Array < number > { return this . chessboard . capturedPieces }
23
+ get capturedPieces ( ) : Array < number > { return this . chessboard . capturedPieces }
24
24
25
- get check ( ) : boolean { return this . chessboard . check }
26
- get checkMate ( ) : boolean { return this . chessboard . checkMate }
27
- get staleMate ( ) : boolean { return this . chessboard . staleMate }
28
- get ownCheck ( ) : boolean { return this . chessboard . ownCheck }
29
- get ownCheckMate ( ) : boolean { return this . chessboard . ownCheckMate }
25
+ get check ( ) : boolean { return this . chessboard . check }
26
+ get checkMate ( ) : boolean { return this . chessboard . checkMate }
27
+ get staleMate ( ) : boolean { return this . chessboard . staleMate }
28
+ get ownCheck ( ) : boolean { return this . chessboard . ownCheck }
29
+ get ownCheckMate ( ) : boolean { return this . chessboard . ownCheckMate }
30
30
31
- public ownThreats ( row : number , col : number ) : number {
32
- return this . chessboard . ownThreats ( row , col ) ;
33
- }
34
-
35
- public opponentThreats ( row : number , col : number ) : number {
36
- return this . chessboard . opponentThreats ( row , col ) ;
37
- }
31
+ public ownThreats ( row : number , col : number ) : number {
32
+ return this . chessboard . ownThreats ( row , col ) ;
33
+ }
38
34
39
- public suggestMove ( ) : Move {
40
- return new Suggestor ( this . chessboard ) . suggestMove ( this . lookahead , this . breadth ) ;
41
- }
35
+ public opponentThreats ( row : number , col : number ) : number {
36
+ return this . chessboard . opponentThreats ( row , col ) ;
37
+ }
42
38
39
+ public suggestMove ( ) : Move {
40
+ return new Suggestor ( this . chessboard ) . suggestMove ( this . lookahead , this . breadth ) ;
41
+ }
43
42
44
- get moveHistory ( ) : Array < Move > { return this . chessboard . moveHistory }
45
-
46
- onclick ( row : number , col : number ) : void {
47
- if ( ! this . isPieceSelected )
48
- this . setSelectedPiece ( row , col ) ;
49
- else {
50
- this . isPieceSelected = false ;
51
- if ( this . chessboard . isLegalMove ( this . selectedPieceRow , this . selectedPieceCol , row , col ) ) {
52
- this . chessboard . move ( this . selectedPieceRow , this . selectedPieceCol , row , col , this . isWhitePlaying ? 5 : - 5 ) ;
53
- var answer = new Suggestor ( this . chessboard ) . suggestMove ( this . lookahead , this . breadth )
54
- if ( null != answer )
55
- this . move ( answer )
56
- }
57
- }
58
- }
59
43
60
- public move ( mv : Move ) {
61
- this . chessboard . move ( mv . fromRow , mv . fromCol , mv . toRow , mv . toCol , mv . promotion )
44
+ get moveHistory ( ) : Array < Move > { return this . chessboard . moveHistory }
45
+
46
+ onclick ( row : number , col : number ) : void {
47
+ if ( ! this . isPieceSelected )
48
+ this . setSelectedPiece ( row , col ) ;
49
+ else {
50
+ this . isPieceSelected = false ;
51
+ if ( this . chessboard . isLegalMove ( this . selectedPieceRow , this . selectedPieceCol , row , col ) ) {
52
+ this . chessboard . move ( this . selectedPieceRow , this . selectedPieceCol , row , col , this . isWhitePlaying ? 5 : - 5 ) ;
53
+ setTimeout ( ( ) => {
54
+ var answer = new Suggestor ( this . chessboard ) . suggestMove ( this . lookahead , this . breadth )
55
+ if ( null != answer )
56
+ this . move ( answer )
57
+ } , 10 )
62
58
}
59
+ }
60
+ }
63
61
64
- setSelectedPiece ( row : number , col : number ) : void {
65
- var piece = this . chessboard . fields [ row ] [ col ] ;
66
- if ( this . isWhitePlaying ) {
67
- if ( piece <= 0 ) return ;
68
- } else {
69
- if ( piece >= 0 ) return ;
70
- }
71
-
72
- this . isPieceSelected = true
73
- this . selectedPieceRow = row
74
- this . selectedPieceCol = col
75
- }
62
+ public move ( mv : Move ) {
63
+ this . chessboard . move ( mv . fromRow , mv . fromCol , mv . toRow , mv . toCol , mv . promotion )
64
+ }
76
65
66
+ setSelectedPiece ( row : number , col : number ) : void {
67
+ var piece = this . chessboard . fields [ row ] [ col ] ;
68
+ if ( this . isWhitePlaying ) {
69
+ if ( piece <= 0 ) return ;
70
+ } else {
71
+ if ( piece >= 0 ) return ;
72
+ }
73
+
74
+ this . isPieceSelected = true
75
+ this . selectedPieceRow = row
76
+ this . selectedPieceCol = col
77
+ }
77
78
78
- isLegalMove2 ( toRow : number , toCol : number ) : boolean {
79
- if ( ! this . isPieceSelected )
80
- return false ;
81
- return this . chessboard . isLegalMove ( this . selectedPieceRow , this . selectedPieceCol , toRow , toCol )
82
- }
83
79
84
- public revertLastMove ( ) : void {
85
- this . chessboard . revertLastMove ( ) ;
86
- }
80
+ isLegalMove2 ( toRow : number , toCol : number ) : boolean {
81
+ if ( ! this . isPieceSelected )
82
+ return false ;
83
+ return this . chessboard . isLegalMove ( this . selectedPieceRow , this . selectedPieceCol , toRow , toCol )
84
+ }
87
85
86
+ public revertLastMove ( ) : void {
87
+ this . chessboard . revertLastMove ( ) ;
88
88
}
89
+
90
+ }
89
91
}
0 commit comments