-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
124 lines (105 loc) · 3.69 KB
/
Copy pathgame.js
File metadata and controls
124 lines (105 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const { Chess } = require('chess.js')
// Neccessary code for each single instance of a game between two players.
var game = function() {
// Private fields
this.turn = "white";
this.conversion = ["a", "b", "c", "d", "e", "f", "g", "h"];
this.lToN = function(l){
return this.conversion.lastIndexOf(l);
}
this.nToL = function(n){
return this.conversion[n];
}
this.SANtoN = function(pos){
var promotion = false;
pos=pos.replace("+", "");
pos=pos.replace("#", "");
if(pos.includes("=")){
pos = pos.replace(/=.*/, "");
promotion = true;
}
return {x:(this.lToN(pos.charAt(pos.length-2))), y:(8-Number(pos.charAt(pos.length-1))), promotion: promotion};
}
this.NtoSAN = function(x, y){
return this.nToL(x)+""+((7-y)+1);
}
this.pToPiece = function(p){
switch(p.toLowerCase()) {
case "p": return "pawn";
break;
case "r": return "rook";
break;
case "k": return "king";
break;
case "q": return "queen";
break;
case "b": return "bishop";
break;
case "n": return "knight";
break;
default:
return null;
break;
}
}
this.chess = new Chess()
//console.log(this.chess.ascii())
}
game.prototype.bestmoveToCoord = function(bestmove){
bestmove = bestmove.slice(9, -12);
return {from: this.SANtoN(bestmove.substring(0,2)), to: this.SANtoN(bestmove.substring(2,4))}
}
game.prototype.getAvailableMoves = function(fromX, fromY){
var moves = this.chess.moves({square: this.nToL(fromX)+""+((7-fromY)+1)});
//console.log(this.chess.moves({square: this.nToL(fromX)+""+((7-fromY)+1), verbose: true}));
var cMoves = [];
moves.forEach(move => {
if(!(move===null)){
if(move === "O-O") move = (this.chess.turn() === "w") ? "g1" : "g8"; //castling
if(move === "O-O-O") move = (this.chess.turn() === "w") ? "c1" : "c8";
cMoves.push(this.SANtoN(move));
}
});
return cMoves;
}
game.prototype.checkMove = function(fromX, toX, fromY, toY){
var move = this.chess.move({from: this.NtoSAN(fromX, fromY), to: this.NtoSAN(toX, toY), promotion:'q'});
if(move===null){
return null;
}else{
var additionalMove = false;
var additionalCoord = {from: null, to: null};
var additionalPiece = null;
console.log(this.chess.ascii());
console.log(move);
if(move.san === "O-O-O"){
additionalMove = true;
additionalCoord.from = this.SANtoN((this.chess.turn() === "b") ? "a1" : "a8")
additionalCoord.to = this.SANtoN((this.chess.turn() === "b") ? "d1" : "d8")
additionalPiece = "rook";
}else if(move.san === "O-O"){
additionalMove = true;
additionalCoord.from = this.SANtoN((this.chess.turn() === "b") ? "h1" : "h8")
additionalCoord.to = this.SANtoN((this.chess.turn() === "b") ? "f1" : "f8")
additionalPiece = "rook";
}else if(move.promotion === 'q' || move.promotion === 'r' ||move.promotion === 'b' || move.promotion === 'n'){
additionalMove = true;
additionalPiece = this.pToPiece(move.promotion);
additionalCoord.from = {x: toX, y:toY};
additionalCoord.to = {x: toX, y:toY};
}
return {fromX: fromX, fromY: fromY, toX: toX, toY:toY, player: (this.chess.turn()==="b") ? "black" : "white", piece: this.pToPiece(move.piece), piece_color: (move.color==="b") ? "black" : "white", additionalMove: additionalMove, additionalCoord: additionalCoord, additionalPiece: additionalPiece, san: move.san}
}
}
game.prototype.checkGameOver = function(){
var checkMate = this.chess.in_checkmate();
if(checkMate){
return {game_over: true, type: "checkmate", winner:(this.chess.turn()==="w") ? "black" : "white"};
}else{
return {game_over: false};
}
}
game.prototype.getFen = function(){
return this.chess.fen();
}
module.exports = game;