@@ -49,7 +49,7 @@ export class GameMaster {
49
49
gameSetup ( ) {
50
50
this . #createPieces( ) ;
51
51
this . #aelSquares( ) ;
52
- this . #aelPawnPromotions ( ) ;
52
+ this . #aelPawnPromotion ( ) ;
53
53
this . #aelButton( ) ;
54
54
}
55
55
@@ -58,22 +58,22 @@ export class GameMaster {
58
58
*/
59
59
#createPieces( ) {
60
60
//Create the dark chess pieces
61
- this . #chessPieces. set ( 'DR2' , new Rook ( 'DR2' , 'dark' , 'A8' , 'images/dark-rook.png ') ) ;
62
- this . #chessPieces. set ( 'DN2' , new Knight ( 'DN2' , 'dark' , 'B8' , 'images/dark-knight.png ') ) ;
63
- this . #chessPieces. set ( 'DB2' , new Bishop ( 'DB2' , 'dark' , 'C8' , 'images/dark-bishop.png ') ) ;
64
- this . #chessPieces. set ( 'DQ' , new Queen ( 'DQ' , 'dark' , 'D8' , 'images/dark-queen.png ') ) ;
65
- this . #chessPieces. set ( 'DK' , new King ( 'DK' , 'dark' , 'E8' , 'images/dark-king.png ') ) ;
66
- this . #chessPieces. set ( 'DB1' , new Bishop ( 'DB1' , 'dark' , 'F8' , 'images/dark-bishop.png ') ) ;
67
- this . #chessPieces. set ( 'DN1' , new Knight ( 'DN1' , 'dark' , 'G8' , 'images/dark-knight.png ') ) ;
68
- this . #chessPieces. set ( 'DR1' , new Rook ( 'DR1' , 'dark' , 'H8' , 'images/dark-rook.png ') ) ;
61
+ this . #chessPieces. set ( 'DR2' , new Rook ( 'DR2' , 'A8 ' ) ) ;
62
+ this . #chessPieces. set ( 'DN2' , new Knight ( 'DN2' , 'B8 ' ) ) ;
63
+ this . #chessPieces. set ( 'DB2' , new Bishop ( 'DB2' , 'C8 ' ) ) ;
64
+ this . #chessPieces. set ( 'DQ' , new Queen ( 'DQ' , 'D8 ' ) ) ;
65
+ this . #chessPieces. set ( 'DK' , new King ( 'DK' , 'E8 ' ) ) ;
66
+ this . #chessPieces. set ( 'DB1' , new Bishop ( 'DB1' , 'F8 ' ) ) ;
67
+ this . #chessPieces. set ( 'DN1' , new Knight ( 'DN1' , 'G8 ' ) ) ;
68
+ this . #chessPieces. set ( 'DR1' , new Rook ( 'DR1' , 'H8 ' ) ) ;
69
69
70
70
//Create the dark pawns
71
71
//loops through 8 times -- one for each pawn
72
72
for ( let col = 0 ; col < this . #NUM_COLS; col ++ ) {
73
73
//creates dark pawn 8, ... dark pawn 1, and places it in the data structure
74
74
const ID = `DP${ this . #NUM_COLS - col } ` ;
75
75
const LOCATION = `${ this . #num2alpha[ col + 1 ] } 7` ;
76
- this . #chessPieces. set ( ID , new Pawn ( ID , 'dark' , LOCATION , 'images/dark-pawn.png' ) ) ;
76
+ this . #chessPieces. set ( ID , new Pawn ( ID , LOCATION ) ) ;
77
77
}
78
78
79
79
//Create the light pawns
@@ -82,32 +82,40 @@ export class GameMaster {
82
82
//creates light pawn 8, ... light pawn 1, and places it in the data structure
83
83
const ID = `LP${ col + 1 } ` ;
84
84
const LOCATION = `${ this . #num2alpha[ col + 1 ] } 2` ;
85
- this . #chessPieces. set ( ID , new Pawn ( ID , 'light' , LOCATION , 'images/light-pawn.png' ) ) ;
85
+ this . #chessPieces. set ( ID , new Pawn ( ID , LOCATION ) ) ;
86
86
}
87
87
88
88
//Create the light chess pieces
89
- this . #chessPieces. set ( 'LR1' , new Rook ( 'LR1' , 'light' , 'A1' , 'images/light-rook.png ') ) ;
90
- this . #chessPieces. set ( 'LN1' , new Knight ( 'LN1' , 'light' , 'B1' , 'images/light-knight.png ') ) ;
91
- this . #chessPieces. set ( 'LB1' , new Bishop ( 'LB1' , 'light' , 'C1' , 'images/light-bishop.png ') ) ;
92
- this . #chessPieces. set ( 'LQ' , new Queen ( 'LQ' , 'light' , 'D1' , 'images/light-queen.png ') ) ;
93
- this . #chessPieces. set ( 'LK' , new King ( 'LK' , 'light' , 'E1' , 'images/light-king.png ') ) ;
94
- this . #chessPieces. set ( 'LB2' , new Bishop ( 'LB2' , 'light' , 'F1' , 'images/light-bishop.png ') ) ;
95
- this . #chessPieces. set ( 'LN2' , new Knight ( 'LN2' , 'light' , 'G1' , 'images/light-knight.png ') ) ;
96
- this . #chessPieces. set ( 'LR2' , new Rook ( 'LR2' , 'light' , 'H1' , 'images/light-rook.png ') ) ;
89
+ this . #chessPieces. set ( 'LR1' , new Rook ( 'LR1' , 'A1 ' ) ) ;
90
+ this . #chessPieces. set ( 'LN1' , new Knight ( 'LN1' , 'B1 ' ) ) ;
91
+ this . #chessPieces. set ( 'LB1' , new Bishop ( 'LB1' , 'C1 ' ) ) ;
92
+ this . #chessPieces. set ( 'LQ' , new Queen ( 'LQ' , 'D1 ' ) ) ;
93
+ this . #chessPieces. set ( 'LK' , new King ( 'LK' , 'E1 ' ) ) ;
94
+ this . #chessPieces. set ( 'LB2' , new Bishop ( 'LB2' , 'F1 ' ) ) ;
95
+ this . #chessPieces. set ( 'LN2' , new Knight ( 'LN2' , 'G1 ' ) ) ;
96
+ this . #chessPieces. set ( 'LR2' , new Rook ( 'LR2' , 'H1 ' ) ) ;
97
97
98
98
//Once I create all the ChessPiece objects, place them on the board
99
99
this . #chessPieces. forEach ( chessPiece => {
100
- const element = document . createElement ( 'img' ) ;
101
- element . id = chessPiece . getPieceID ( ) ;
102
- element . src = chessPiece . getImage ( ) ;
103
- element . alt = chessPiece . getPieceID ( ) ;
104
- let currentLocation = `#${ chessPiece . getLocation ( ) } ` ;
105
- document . querySelector ( currentLocation ) . textContent = chessPiece . getPieceID ( ) ;
106
- document . querySelector ( currentLocation ) . appendChild ( element ) ;
107
- this . #occupiedSquares. add ( chessPiece . getLocation ( ) ) ; //occupy the starting location
100
+ this . #emplaceChessPiece( chessPiece ) ;
108
101
} ) ;
109
102
}
110
103
104
+ /**
105
+ * there are two different places this needs to happen, so I put the code in a function
106
+ * @param {ChessPiece } chessPiece
107
+ */
108
+ #emplaceChessPiece( chessPiece ) {
109
+ const element = document . createElement ( 'img' ) ;
110
+ element . id = chessPiece . getPieceID ( ) ;
111
+ element . src = chessPiece . getImage ( ) ;
112
+ element . alt = chessPiece . getPieceID ( ) ;
113
+ let currentLocation = `#${ chessPiece . getLocation ( ) } ` ;
114
+ document . querySelector ( currentLocation ) . textContent = chessPiece . getPieceID ( ) ;
115
+ document . querySelector ( currentLocation ) . appendChild ( element ) ;
116
+ this . #occupiedSquares. add ( chessPiece . getLocation ( ) ) ; //occupy the starting location
117
+ }
118
+
111
119
/**Add Event Listener to the button
112
120
* this function adds an event listener to the button that moves the pieces
113
121
*/
@@ -126,10 +134,11 @@ export class GameMaster {
126
134
// check for pawn promotion
127
135
if ( this . #checkPawnPromotion( chessPiece ) ) {
128
136
console . log ( 'PROMOTING PAWN' ) ;
129
- this . #promotePawn( chessPiece ) ;
137
+ this . #promotePawn( chessPiece . getPieceID ( ) ) ;
138
+ }
139
+ else { //no pawn promotion available
140
+ this . #startNextTurn( ) ; //start the next turn
130
141
}
131
-
132
- this . #startNextTurn( ) ; //start the next turn
133
142
}
134
143
} ) ;
135
144
}
@@ -138,7 +147,7 @@ export class GameMaster {
138
147
* Makes the board ready for the next turn
139
148
*/
140
149
#startNextTurn( ) {
141
- console . log ( 'entered startNextTurn() function' ) ;
150
+ // console.log('entered startNextTurn() function');
142
151
this . #whoseTurn = this . #whoseTurn === 'Light' ? 'Dark' : 'Light' ; // if it was light's turn, it is now dark's turn, and vice versa
143
152
document . querySelector ( '#whoseTurn' ) . innerText = this . #whoseTurn;
144
153
@@ -154,31 +163,31 @@ export class GameMaster {
154
163
this . #squareIDs. forEach ( squareID => {
155
164
document . querySelector ( `#${ squareID } ` ) . addEventListener ( 'click' , e => {
156
165
if ( squareID === this . #chosenSQ1) {
157
- console . log ( 'first if' ) ;
166
+ // console.log('first if');
158
167
this . #unclickSQ1( squareID ) ;
159
168
//return;
160
169
}
161
170
else if ( squareID === this . #chosenSQ2) {
162
- console . log ( 'second if' ) ;
171
+ // console.log('second if');
163
172
this . #unclickSQ2( squareID ) ;
164
173
//return;
165
174
}
166
175
else if ( this . #occupiedSquares. has ( squareID ) ) { // Is this square occupied?
167
- console . log ( 'third if' ) ;
176
+ // console.log('third if');
168
177
// Does this square contain an ally?
169
178
if ( document . querySelector ( `#${ squareID } ` ) . innerText [ 0 ] . toLowerCase ( ) === this . #whoseTurn[ 0 ] . toLowerCase ( ) ) {
170
- console . log ( 'fourth if' ) ;
179
+ // console.log('fourth if');
171
180
this . #clickSQ1( squareID ) ;
172
181
//return;
173
182
}
174
183
else { // else this square contains an enemy
175
- console . log ( 'fifth if' ) ;
184
+ // console.log('fifth if');
176
185
this . #clickSQ2( squareID ) ;
177
186
//return;
178
187
}
179
188
}
180
189
else {
181
- console . log ( 'last else' ) ;
190
+ // console.log('last else');
182
191
this . #clickSQ2( squareID ) ;
183
192
//return;
184
193
}
@@ -227,23 +236,67 @@ export class GameMaster {
227
236
else return false ; // not a pawn
228
237
}
229
238
230
- #promotePawn( pawn ) {
231
- if ( pawn . getTeam ( ) === 'Light' ) {
232
- //remove the hidden attribute, so they can submit the form
233
- document . querySelector ( '#divLightPromo' ) . classList . remove ( 'hidden' ) ;
234
- }
235
- else { // team is dark
236
- //remove the hidden attribute, so they can submit the form
237
- document . querySelector ( '#divDarkPromo' ) . classList . remove ( 'hidden' ) ;
238
- }
239
+ #promotePawn( pawnID ) {
240
+ //disable the move button so they can't move until the promotion is finished
241
+ document . querySelector ( '#btnMove' ) . disabled = true ;
242
+
243
+ //show the promotion form, then wait for their answer
244
+ document . querySelector ( '#spnPromoPawn' ) . innerText = pawnID ;
245
+ document . querySelector ( '#spnPromoLoc' ) . innerText = this . #chessPieces. get ( pawnID ) . getLocation ( ) ;
246
+ document . querySelector ( '#divPromo' ) . classList . remove ( 'hidden' ) ;
239
247
}
240
248
241
249
/**
242
250
* adds event listeners for promoting pawns buttons
243
251
*/
244
- #aelPawnPromotions( ) {
245
- //TODO: PROMOTE LIGHT PAWN
246
- //TODO: PROMOTE DARK PAWN
252
+ #aelPawnPromotion( ) {
253
+ // Event listener for promoting a pawn:
254
+ document . querySelector ( '#btnPromo' ) . addEventListener ( 'click' , e => {
255
+ e . preventDefault ( ) ;
247
256
257
+ //get the team
258
+ const TEAM = document . querySelector ( '#spnPromoPawn' ) . innerText [ 0 ] ;
259
+ console . log ( TEAM ) ;
260
+
261
+ //get the type
262
+ const TYPE = document . querySelector ( '#selPromo' ) . value ;
263
+ console . log ( TYPE ) ;
264
+
265
+ // get the location
266
+ const LOCATION = document . querySelector ( '#spnPromoLoc' ) . innerText ;
267
+
268
+ let numId = 2 ; //start out with some number ID
269
+ while ( this . #chessPieces. has ( `${ TEAM } ${ TYPE } ${ numId } ` ) ) { //if this particular ID is taken
270
+ numId ++ ; //try a different number part of the ID
271
+ }
272
+ const pieceID = `${ TEAM } ${ TYPE } ${ numId } ` ;
273
+
274
+ switch ( TYPE ) {
275
+ case 'R' :
276
+ this . #chessPieces. set ( pieceID , new Rook ( pieceID , LOCATION ) ) ;
277
+ break ;
278
+ case 'N' :
279
+ this . #chessPieces. set ( pieceID , new Knight ( pieceID , LOCATION ) ) ;
280
+ break ;
281
+ case 'B' :
282
+ this . #chessPieces. set ( pieceID , new Bishop ( pieceID , LOCATION ) ) ;
283
+ break ;
284
+ case 'Q' :
285
+ this . #chessPieces. set ( pieceID , new Queen ( pieceID , LOCATION ) ) ;
286
+ break ;
287
+ default :
288
+ throw new Error ( 'Invalid promotion choice!' ) ;
289
+ }
290
+ //emplace the chess piece on the board
291
+ this . #emplaceChessPiece( this . #chessPieces. get ( pieceID ) ) ;
292
+
293
+ //hide the promo form, so they can't do anymore promos
294
+ document . querySelector ( '#divPromo' ) . classList . add ( 'hidden' ) ;
295
+
296
+ //enable the move button, so the game can proceed
297
+ document . querySelector ( '#btnMove' ) . disabled = false ;
298
+
299
+ this . #startNextTurn( ) ; //start the next turn
300
+ } ) ;
248
301
}
249
302
}
0 commit comments