Skip to content

Commit 1663243

Browse files
committed
execute the chess engine asynchronously (i.e. after rendering the player's move)
1 parent 1b3acd0 commit 1663243

File tree

6 files changed

+81
-73
lines changed

6 files changed

+81
-73
lines changed

Chess/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "ng2chess",
33
"version": "0.0.0",
4-
"license": "MIT",
4+
"license": "Apache License V2",
55
"angular-cli": {},
66
"scripts": {
7-
"start": "ng serve",
7+
"start": "ng serve --browser",
88
"lint": "tslint \"src/**/*.ts\"",
99
"test": "ng test",
1010
"pree2e": "webdriver-manager update",

Chess/src/engine/chessboardUI.ts

Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,85 +5,87 @@ import {Chessboard} from './chessboard';
55
import {Suggestor} from './suggestor';
66

77
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;
1414

15-
private chessboard: Chessboard = new Chessboard(new Array<Move>());
15+
private chessboard: Chessboard = new Chessboard(new Array<Move>());
1616

17-
get fields(): number[][] {
18-
return this.chessboard.fields;
19-
}
17+
get fields(): number[][] {
18+
return this.chessboard.fields;
19+
}
2020

21-
get isWhitePlaying(): boolean { return this.chessboard.isWhitePlaying }
21+
get isWhitePlaying(): boolean { return this.chessboard.isWhitePlaying }
2222

23-
get capturedPieces(): Array<number> { return this.chessboard.capturedPieces }
23+
get capturedPieces(): Array<number> { return this.chessboard.capturedPieces }
2424

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 }
3030

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+
}
3834

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+
}
4238

39+
public suggestMove(): Move {
40+
return new Suggestor(this.chessboard).suggestMove(this.lookahead, this.breadth);
41+
}
4342

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-
}
5943

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)
6258
}
59+
}
60+
}
6361

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+
}
7665

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+
}
7778

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-
}
8379

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+
}
8785

86+
public revertLastMove(): void {
87+
this.chessboard.revertLastMove();
8888
}
89+
90+
}
8991
}

ElectronPlaysChess/electron/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function createWindow () {
2424
// in an array if your app supports multi windows, this is the time
2525
// when you should delete the corresponding element.
2626
mainWindow = null
27+
app.quit()
2728
})
2829
}
2930

ElectronPlaysChess/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
2-
"name": "angular2-electron-starter",
3-
"version": "0.3.0",
4-
"license": "MIT",
2+
"name": "electron-plays-chess",
3+
"version": "0.1.0",
4+
"license": "Apache License V2",
55
"angular-cli": {},
66
"scripts": {
7+
"dev": "open http://localhost:4200 & ng serve -port 4200",
78
"start": "ng serve",
89
"lint": "tslint \"src/**/*.ts\"",
10+
"dev": "light-server -s dist",
911
"test": "ng test",
1012
"pree2e": "webdriver-manager update",
1113
"e2e": "protractor",
@@ -43,6 +45,7 @@
4345
"karma-cli": "^1.0.1",
4446
"karma-jasmine": "^1.0.2",
4547
"karma-remap-istanbul": "^0.2.1",
48+
"light-server": "^1.0.0",
4649
"protractor": "4.0.9",
4750
"ts-node": "1.2.1",
4851
"tslint": "3.13.0",

ElectronPlaysChess/src/app/chess-app/chess.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="container">
22
<div class="row">
33
<div class="col-md-12">
4-
<h1>AngularJS 2.0 plays Chess!</h1>
4+
<h1>AngularJS 2.1 plays Chess!</h1>
55
</div>
66
</div>
77
<div class="row">

ElectronPlaysChess/src/app/engine/chessboardUI.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ export module ChessEngineAPI {
5050
this.isPieceSelected = false;
5151
if (this.chessboard.isLegalMove(this.selectedPieceRow, this.selectedPieceCol, row, col)) {
5252
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)
53+
setTimeout(() => {
54+
var answer = new Suggestor(this.chessboard).suggestMove(this.lookahead, this.breadth)
55+
if (null != answer)
56+
this.move(answer)
57+
}, 10)
5658
}
5759
}
5860
}

0 commit comments

Comments
 (0)