forked from quasoft/backgammonjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RuleBgTapa.js
322 lines (287 loc) · 11.1 KB
/
RuleBgTapa.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
var model = require('../model.js');
var Rule = require('./rule.js');
/**
* Bulgarian variant of Tapa (тапа).
* @constructor
* @extends Rule
*/
function RuleBgTapa() {
Rule.call(this);
/**
* Rule name, matching the class name (eg. 'RuleBgTapa')
* @type {string}
*/
this.name = 'RuleBgTapa';
/**
* Short title describing rule specifics
* @type {string}
*/
this.title = 'Tapa';
/**
* Full description of rule
* @type {string}
*/
this.description = 'Bulgarian variant of Tapa (тапа).';
/**
* Full name of country where this rule (variant) is played.
* To list multiple countries use a pipe ('|') character as separator.
* @type {string}
*/
this.country = 'Bulgaria';
/**
* Two character ISO code of country where this rule (variant) is played.
* To list multiple codes use a pipe ('|') character as separator.
* List codes in same order as countries in the field above.
* @type {string}
*/
this.countryCode = 'bg';
/**
* Descendents should list all action types that are allowed in this rule.
* @type {MoveActionType[]}
*/
this.allowedActions = [
model.MoveActionType.MOVE,
model.MoveActionType.BEAR
];
}
RuleBgTapa.prototype = Object.create(Rule.prototype);
RuleBgTapa.prototype.constructor = RuleBgTapa;
/**
* Reset state to initial position of pieces according to current rule.
* @memberOf RuleBgTapa
* @param {State} state - Board state
*/
RuleBgTapa.prototype.resetState = function(state) {
/**
* Move pieces to correct initial positions for both players.
* Values in state.points are zero based and denote the .
* the number of pieces on each position.
* Index 0 of array is position 1 and increments to the number of maximum
* points.
*
* Position: |12 13 14 15 16 17| |18 19 20 21 22 23| White
* | | | 15w| <-
* | | | |
* | | | |
* | | | |
* | | | 15b| <-
* Position: |11 10 09 08 07 06| |05 04 03 02 01 00| Black
*
*/
model.State.clear(state);
this.place(state, 15, model.PieceType.WHITE, 23);
this.place(state, 15, model.PieceType.BLACK, 0);
};
/**
* Increment position by specified number of steps and return an incremented position
* @memberOf RuleBgTapa
* @param {number} position - Denormalized position
* @param {PieceType} type - Type of piece
* @param {number} steps - Number of steps to increment towards first home position
* @returns {number} - Incremented position (denormalized)
*/
RuleBgTapa.prototype.incPos = function(position, type, steps) {
var newPosition;
if (type === model.PieceType.WHITE) {
newPosition = position - steps;
}
else {
newPosition = position + steps;
}
return newPosition;
};
/**
* Normalize position - Normalized positions start from 0 to 23 for both players,
* where 0 is the first position in the home part of the board, 6 is the last
* position in the home part and 23 is the furthest position - in the opponent's
* home.
* @memberOf RuleBgTapa
* @param {number} position - Denormalized position (0 to 23 for white and 23 to 0 for black)
* @param {PieceType} type - Type of piece (white/black)
* @returns {number} - Normalized position (0 to 23 for both players)
*/
RuleBgTapa.prototype.normPos = function(position, type) {
var normPosition = position;
if (type === model.PieceType.BLACK) {
normPosition = 23 - position;
}
return normPosition;
};
/**
* Get denormalized position - start from 0 to 23 for white player and from
* 23 to 0 for black player.
* @memberOf RuleBgTapa
* @param {number} position - Normalized position (0 to 23 for both players)
* @param {PieceType} type - Type of piece (white/black)
* @return {number} - Denormalized position (0 to 23 for white and 23 to 0 for black)
*/
RuleBgTapa.prototype.denormPos = function(position, type) {
var denormPosition = position;
if (type === model.PieceType.BLACK) {
denormPosition = 23 - position;
}
return denormPosition;
};
/**
* Call this method after a request for moving a piece has been made.
* Determines if the move is allowed and what actions will have to be made as
* a result. Actions can be `move` or `bear`.
*
* Multiple actions can be returned, if required.
*
* The list of actions returned would usually be appllied to game state and then
* sent to client. The client's UI would play the actions (eg. with movement animation)
* in the same order.
*
* @memberOf RuleBgTapa
* @param {State} state - State
* @param {Piece} piece - Piece to move
* @param {PieceType} type - Type of piece
* @param {number} steps - Number of steps to increment towards first home position
* @returns {MoveAction[]} - List of actions if move is allowed, empty list otherwise.
*/
RuleBgTapa.prototype.getMoveActions = function(state, piece, steps) {
var actionList = [];
// Next, check conditions specific to this game rule and build the list of
// actions that has to be made.
/**
* Create a new move action and add it to actionList. Used internally.
*
* @alias RuleBgTapa.getMoveActions.addAction
* @memberof RuleBgTapa.getMoveActions
* @method RuleBgTapa.getMoveActions.addAction
* @param {MoveActionType} moveActionType - Type of move action (eg. move, bear)
* @param {Piece} piece - Piece to move
* @param {number} from - Denormalized source position. If action uses only one position parameter, this one is used.
* @param {number} to - Denormalized destination position.
* @returns {MoveAction}
* @see {@link getMoveActions} for more information on purpose of move actions.
*/
function addAction(moveActionType, piece, from, to) {
var action = new model.MoveAction();
action.type = moveActionType;
action.piece = piece;
action.position = from;
action.from = from;
if (typeof to !== "undefined") {
action.to = to;
}
actionList.push(action);
return action;
}
// TODO: Catch exceptions due to disallowed move requests and pass them as error message to the client.
try {
var position = model.State.getPiecePos(state, piece);
// TODO: Consider using state machine? Is it worth, can it be useful in other methods too?
if (this.allPiecesAreHome(state, piece.type)) {
/*
If all pieces are in home field, the player can bear pieces
Cases:
- Normalized position >= 0 --> Just move the piece
- Normalized position === -1 --> Bear piece
- Normalized position < -1 --> Bear piece, only if there are no player pieces at higher positions
+12-13-14-15-16-17------18-19-20-21-22-23-+
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | O O |
| | | O O O |
+11-10--9--8--7--6-------5--4--3--2--1--0-+ -1
*/
var destination = this.incPos(position, piece.type, steps);
var normDestination = this.normPos(destination, piece.type);
// Move the piece, unless point is blocked by opponent
if (normDestination >= 0) {
var destTopPiece = model.State.getTopPiece(state, destination);
var destTopPieceType = (destTopPiece) ? destTopPiece.type : null;
// There are no pieces at this point or the top piece is owned by player,
// so just move piece to that position
if ((destTopPieceType === null) || (destTopPieceType === piece.type)) {
addAction(
model.MoveActionType.MOVE, piece, position, destination
);
}
// The top piece is opponent's and is only one (i.e. the point is not blocked),
// so move ours on top of opponent's piece
else if (model.State.countAllAtPos(state, destination) === 1) {
addAction(
model.MoveActionType.MOVE, piece, position, destination
);
}
}
// If steps are just enought to reach position -1, bear piece
else if (normDestination === -1) {
addAction(
model.MoveActionType.BEAR, piece, position
);
}
// If steps move the piece beyond -1 position, the player can bear the piece,
// only if there are no other pieces at higher positions
else {
var normSource = this.normPos(position, piece.type);
if (this.countAtHigherPos(state, normSource + 1, piece.type) <= 0) {
addAction(
model.MoveActionType.BEAR, piece, position
);
}
}
}
else {
/*
If there is at least one piece outside home, just move the piece.
Input data: position=13, steps=3
Cases:
- Opponent has no pieces there --> move the checker at position 10
- Opponent has exactly one piece --> move out piece over opponent's one
- Opponent has two or more pieces --> point is blocked, cannot move piece there
!
+12-13-14-15-16-17------18-19-20-21-22-23-+
| O | | X |
| O | | X |
| | | X |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | O |
| | | O O O |
+11-10--9--8--7--6-------5--4--3--2--1--0-+ -1
!
*/
var destination = this.incPos(position, piece.type, steps);
// Make sure that destination is within board
if ((destination >= 0) && (destination <= 23)) {
var normDest = this.normPos(destination, piece.type);
// TODO: Make sure position is not outside board
var destTopPiece = model.State.getTopPiece(state, destination);
var destTopPieceType = (destTopPiece) ? destTopPiece.type : null;
// There are no pieces at this point or the top piece is owned by player
if ((destTopPieceType === null) || (destTopPieceType === piece.type)) {
addAction(
model.MoveActionType.MOVE, piece, position, destination
);
}
// The top piece is opponent's and is a blot (i.e. the point is not blocked)
else if (model.State.countAllAtPos(state, destination) === 1) {
addAction(
model.MoveActionType.MOVE, piece, position, destination
);
}
}
}
}
catch (e) {
actionList = [];
return actionList;
}
return actionList;
};
module.exports = new RuleBgTapa();