Skip to content

Commit 4b1ba00

Browse files
author
christianwheeler09
committed
Enable Pawn promotion
1 parent e9d5b98 commit 4b1ba00

21 files changed

+136
-99
lines changed

classes/Pawn.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ export class Pawn extends ChessPiece {
55
/**
66
* Pawn constructor
77
* @param {string} pieceID
8-
* @param {string} team
98
* @param {string} location
109
* @param {string} image
1110
*/
12-
constructor(pieceID, team, location, image) {
13-
super(pieceID, team, location, image);
11+
constructor(pieceID, location, image) {
12+
super(pieceID, location, image);
1413
}
1514

1615
/**

classes/bishop.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ export class Bishop extends ChessPiece {
55
/**
66
* Pawn constructor
77
* @param {string} pieceID
8-
* @param {string} team
98
* @param {string} location
109
* @param {string} image
1110
*/
12-
constructor(pieceID, team, location, image) {
13-
super(pieceID, team, location, image);
11+
constructor(pieceID, location, image) {
12+
super(pieceID, location, image);
1413
}
1514

1615
/**

classes/chess_piece.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,17 @@ export class ChessPiece {
44
/**
55
* ChessPiece constructor
66
* @param {string} pieceID, example 'LP1'
7-
* @param {string} team
87
* @param {string} location, example 'A2'
9-
* @param {string} image
108
*/
11-
constructor(pieceID, team, location, image) {
9+
constructor(pieceID, location) {
1210
this.pieceID = pieceID;
13-
this.team = team;
1411
this.location = location;
15-
this.image = image;
1612
this.message = '';
1713
}
1814

1915
//getters
2016
getPieceID() { return this.pieceID; }
2117
getLocation() { return this.location; }
22-
getImage() { return this.image; }
2318

2419
// get team
2520
getTeam() {
@@ -52,6 +47,12 @@ export class ChessPiece {
5247
}
5348
}
5449

50+
//get image --- returns the file location of the picture for this piece
51+
//example: 'images/dark-rook.png'
52+
getImage() {
53+
return `images/${this.getTeam()}${this.getType()}.png`;
54+
}
55+
5556
// get rank AKA row
5657
getRank() {
5758
return Number(this.location[1]);

classes/king.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ export class King extends ChessPiece {
55
/**
66
* Pawn constructor
77
* @param {string} pieceID
8-
* @param {string} team
98
* @param {string} location
109
* @param {string} image
1110
*/
12-
constructor(pieceID, team, location, image) {
13-
super(pieceID, team, location, image);
11+
constructor(pieceID, location, image) {
12+
super(pieceID, location, image);
1413
}
1514

1615
/**

classes/knight.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ export class Knight extends ChessPiece {
55
/**
66
* Pawn constructor
77
* @param {string} pieceID
8-
* @param {string} team
98
* @param {string} location
109
* @param {string} image
1110
*/
12-
constructor(pieceID, team, location, image) {
13-
super(pieceID, team, location, image);
11+
constructor(pieceID, location, image) {
12+
super(pieceID, location, image);
1413
}
1514

1615
/**

classes/queen.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ export class Queen extends ChessPiece {
55
/**
66
* Pawn constructor
77
* @param {string} pieceID
8-
* @param {string} team
98
* @param {string} location
109
* @param {string} image
1110
*/
12-
constructor(pieceID, team, location, image) {
13-
super(pieceID, team, location, image);
11+
constructor(pieceID, location, image) {
12+
super(pieceID, location, image);
1413
}
1514

1615
/**
@@ -22,7 +21,7 @@ export class Queen extends ChessPiece {
2221
*/
2322
canMove(target, occSet) {
2423
if (super.canMove(target, occSet)) { //ask the parent class
25-
const CM2 = this.canMove2(target);//ask the queen's specific rules for movement
24+
const CM2 = this.canMove2(target, occSet);//ask the queen's specific rules for movement
2625
return this.canMove3(CM2);//publish the results
2726
}
2827
//else super says no, you cannot move there

classes/rook.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ export class Rook extends ChessPiece {
55
/**
66
* Pawn constructor
77
* @param {string} pieceID
8-
* @param {string} team
98
* @param {string} location
109
* @param {string} image
1110
*/
12-
constructor(pieceID, team, location, image) {
13-
super(pieceID, team, location, image);
11+
constructor(pieceID, location, image) {
12+
super(pieceID, location, image);
1413
}
1514

1615
/**

game_master.js

Lines changed: 102 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class GameMaster {
4949
gameSetup() {
5050
this.#createPieces();
5151
this.#aelSquares();
52-
this.#aelPawnPromotions();
52+
this.#aelPawnPromotion();
5353
this.#aelButton();
5454
}
5555

@@ -58,22 +58,22 @@ export class GameMaster {
5858
*/
5959
#createPieces() {
6060
//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'));
6969

7070
//Create the dark pawns
7171
//loops through 8 times -- one for each pawn
7272
for (let col = 0; col < this.#NUM_COLS; col++) {
7373
//creates dark pawn 8, ... dark pawn 1, and places it in the data structure
7474
const ID = `DP${this.#NUM_COLS - col}`;
7575
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));
7777
}
7878

7979
//Create the light pawns
@@ -82,32 +82,40 @@ export class GameMaster {
8282
//creates light pawn 8, ... light pawn 1, and places it in the data structure
8383
const ID = `LP${col + 1}`;
8484
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));
8686
}
8787

8888
//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'));
9797

9898
//Once I create all the ChessPiece objects, place them on the board
9999
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);
108101
});
109102
}
110103

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+
111119
/**Add Event Listener to the button
112120
* this function adds an event listener to the button that moves the pieces
113121
*/
@@ -126,10 +134,11 @@ export class GameMaster {
126134
// check for pawn promotion
127135
if (this.#checkPawnPromotion(chessPiece)) {
128136
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
130141
}
131-
132-
this.#startNextTurn(); //start the next turn
133142
}
134143
});
135144
}
@@ -138,7 +147,7 @@ export class GameMaster {
138147
* Makes the board ready for the next turn
139148
*/
140149
#startNextTurn() {
141-
console.log('entered startNextTurn() function');
150+
//console.log('entered startNextTurn() function');
142151
this.#whoseTurn = this.#whoseTurn === 'Light' ? 'Dark' : 'Light'; // if it was light's turn, it is now dark's turn, and vice versa
143152
document.querySelector('#whoseTurn').innerText = this.#whoseTurn;
144153

@@ -154,31 +163,31 @@ export class GameMaster {
154163
this.#squareIDs.forEach(squareID => {
155164
document.querySelector(`#${squareID}`).addEventListener('click', e => {
156165
if (squareID === this.#chosenSQ1) {
157-
console.log('first if');
166+
//console.log('first if');
158167
this.#unclickSQ1(squareID);
159168
//return;
160169
}
161170
else if (squareID === this.#chosenSQ2) {
162-
console.log('second if');
171+
//console.log('second if');
163172
this.#unclickSQ2(squareID);
164173
//return;
165174
}
166175
else if (this.#occupiedSquares.has(squareID)) { // Is this square occupied?
167-
console.log('third if');
176+
//console.log('third if');
168177
// Does this square contain an ally?
169178
if (document.querySelector(`#${squareID}`).innerText[0].toLowerCase() === this.#whoseTurn[0].toLowerCase()) {
170-
console.log('fourth if');
179+
//console.log('fourth if');
171180
this.#clickSQ1(squareID);
172181
//return;
173182
}
174183
else { // else this square contains an enemy
175-
console.log('fifth if');
184+
//console.log('fifth if');
176185
this.#clickSQ2(squareID);
177186
//return;
178187
}
179188
}
180189
else {
181-
console.log('last else');
190+
//console.log('last else');
182191
this.#clickSQ2(squareID);
183192
//return;
184193
}
@@ -227,23 +236,67 @@ export class GameMaster {
227236
else return false; // not a pawn
228237
}
229238

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');
239247
}
240248

241249
/**
242250
* adds event listeners for promoting pawns buttons
243251
*/
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();
247256

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+
});
248301
}
249302
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)