Skip to content

Commit 47e0a65

Browse files
author
christianwheeler09
committed
Make it possible to move a chess piece.
1 parent 3771c74 commit 47e0a65

File tree

5 files changed

+125
-72
lines changed

5 files changed

+125
-72
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"liveServer.settings.port": 5504
3+
}

classes/ChessPiece.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export class ChessPiece {
88
this.team = team;
99
this.location = location;
1010
this.image = image;
11-
//console.log(this);
1211
}
1312

1413
//getters
@@ -23,9 +22,16 @@ export class ChessPiece {
2322
* @param {string} newLocation the new location that this chess piece will be moved to
2423
*/
2524
move(newLocation) {
26-
const node = document.querySelector(`#${location}`).removeChild(`#${pieceID}`);//go to the square where this chess piece is located, and remove its child, which will be an image, and store the image as a node
27-
document.querySelector(`#${newLocation}`).appendChild(node);//go to the square which is to be the new location, and place the image there
28-
location = newLocation;//update the location stored in this object
25+
//put pieceID in new square
26+
document.querySelector(`#${newLocation}`).textContent = this.pieceID;
27+
//remove chess piece from old location
28+
const node = document.querySelector(`#${this.location}`).removeChild(document.querySelector(`#${this.pieceID}`));
29+
//place the chess piece in new square
30+
document.querySelector(`#${newLocation}`).appendChild(node);
31+
//remove pieceID from old square
32+
document.querySelector(`#${this.location}`).textContent = '';
33+
//update the location stored in this object
34+
this.location = newLocation;
2935
}
3036

3137
/**
@@ -35,5 +41,9 @@ export class ChessPiece {
3541
*/
3642
canMove(newLocation) {
3743
//TODO: this function
44+
if(newLocation === this.location){
45+
return false;//it is invalid to move a piece zero spaces -- that doesn't make any sense
46+
}
47+
return true;
3848
}
3949
}

index.html

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,29 @@
99
</head>
1010

1111
<body>
12+
<form>
13+
<label for="movePiece">Piece to move:</label><br>
14+
<input type="text" id="movePiece" name="movePiece" value="LR1"><br>
15+
<label for="newLocation">Destination:</label><br>
16+
<input type="text" id="newLocation" name="newLocation" value="A6"><br><br>
17+
<input type="submit" value="Submit" id="btnMove">
18+
</form>
1219
<table>
1320
<tr id="row9">
1421
<td class="corner-header"></td>
15-
<td class="bottom-top-header"></td>
16-
<td class="bottom-top-header"></td>
17-
<td class="bottom-top-header"></td>
18-
<td class="bottom-top-header"></td>
19-
<td class="bottom-top-header"></td>
20-
<td class="bottom-top-header"></td>
21-
<td class="bottom-top-header"></td>
22-
<td class="bottom-top-header"></td>
22+
<td class="bottom-top-header">A</td>
23+
<td class="bottom-top-header">B</td>
24+
<td class="bottom-top-header">C</td>
25+
<td class="bottom-top-header">D</td>
26+
<td class="bottom-top-header">E</td>
27+
<td class="bottom-top-header">F</td>
28+
<td class="bottom-top-header">G</td>
29+
<td class="bottom-top-header">H</td>
2330
<td class="corner-header"></td>
2431
</tr>
2532
<tr id="row8">
2633
<td>
27-
<div class="left-right-header">8</div>
34+
<div class="left-right-header"><br><br>8</div>
2835
</td>
2936
<td>
3037
<div class="square light" id="A8"></div>
@@ -51,12 +58,12 @@
5158
<div class="square dark" id="H8"></div>
5259
</td>
5360
<td>
54-
<div class="left-right-header"></div>
61+
<div class="left-right-header"><br><br>8</div>
5562
</td>
5663
</tr>
5764
<tr id="row7">
5865
<td>
59-
<div class="left-right-header">7</div>
66+
<div class="left-right-header"><br><br>7</div>
6067
</td>
6168
<td>
6269
<div class="square dark" id="A7"></div>
@@ -83,12 +90,12 @@
8390
<div class="square light" id="H7"></div>
8491
</td>
8592
<td>
86-
<div class="left-right-header"></div>
93+
<div class="left-right-header"><br><br>7</div>
8794
</td>
8895
</tr>
8996
<tr id="row6">
9097
<td>
91-
<div class="left-right-header">6</div>
98+
<div class="left-right-header"><br><br>6</div>
9299
</td>
93100
<td>
94101
<div class="square light" id="A6"></div>
@@ -115,12 +122,12 @@
115122
<div class="square dark" id="H6"></div>
116123
</td>
117124
<td>
118-
<div class="left-right-header"></div>
125+
<div class="left-right-header"><br><br>6</div>
119126
</td>
120127
</tr>
121128
<tr id="row5">
122129
<td>
123-
<div class="left-right-header">5</div>
130+
<div class="left-right-header"><br><br>5</div>
124131
</td>
125132
<td>
126133
<div class="square dark" id="A5"></div>
@@ -147,12 +154,12 @@
147154
<div class="square light" id="H5"></div>
148155
</td>
149156
<td>
150-
<div class="left-right-header"></div>
157+
<div class="left-right-header"><br><br>5</div>
151158
</td>
152159
</tr>
153160
<tr id="row4">
154161
<td>
155-
<div class="left-right-header">4</div>
162+
<div class="left-right-header"><br><br>4</div>
156163
</td>
157164
<td>
158165
<div class="square light" id="A4"></div>
@@ -179,12 +186,12 @@
179186
<div class="square dark" id="H4"></div>
180187
</td>
181188
<td>
182-
<div class="left-right-header"></div>
189+
<div class="left-right-header"><br><br>4</div>
183190
</td>
184191
</tr>
185192
<tr id="row3">
186193
<td>
187-
<div class="left-right-header">3</div>
194+
<div class="left-right-header"><br><br>3</div>
188195
</td>
189196
<td>
190197
<div class="square dark" id="A3"></div>
@@ -211,12 +218,12 @@
211218
<div class="square light" id="H3"></div>
212219
</td>
213220
<td>
214-
<div class="left-right-header"></div>
221+
<div class="left-right-header"><br><br>3</div>
215222
</td>
216223
</tr>
217224
<tr id="row2">
218225
<td>
219-
<div class="left-right-header">2</div>
226+
<div class="left-right-header"><br><br>2</div>
220227
</td>
221228
<td>
222229
<div class="square light" id="A2"></div>
@@ -243,12 +250,12 @@
243250
<div class="square dark" id="H2"></div>
244251
</td>
245252
<td>
246-
<div class="left-right-header"></div>
253+
<div class="left-right-header"><br><br>2</div>
247254
</td>
248255
</tr>
249256
<tr id="row1">
250257
<td>
251-
<div class="left-right-header">1</div>
258+
<div class="left-right-header"><br><br>1</div>
252259
</td>
253260
<td>
254261
<div class="square dark" id="A1"></div>
@@ -275,7 +282,7 @@
275282
<div class="square light" id="H1"></div>
276283
</td>
277284
<td>
278-
<div class="left-right-header"></div>
285+
<div class="left-right-header"><br><br>1</div>
279286
</td>
280287
</tr>
281288
<tr id="row0">

main.js

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,80 +9,95 @@ import { ChessPiece } from './classes/ChessPiece.js';
99
//to convert letters to numbers -- Z is a dummy value -- so the indeces of the letters line up with the numbering on the board
1010
// 0 1 2 3 4 5 6 7 8
1111
const alpha2num = ['Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
12-
const EIGHT = 8;//since the length of alpha2num is obfuscated by the Z, use this instead of alpha2num.length-1, because that is confusing
12+
const pieceID2Index = ['DR2', 'DK2', 'DB2', 'DQ1', 'DQ0', 'DB1', 'DK1', 'DR1',
13+
'DP8', 'DP7', 'DP6', 'DP5', 'DP4', 'DP3', 'DP2', 'DP1',
14+
'LP1', 'LP2', 'LP3', 'LP4', 'LP5', 'LP6', 'LP7', 'LP8',
15+
'LR1', 'LK1', 'LB1', 'LQ1', 'LQ0', 'LB2', 'LK2', 'LR2'];
16+
const NUM_COLS = 8;//the chess board has 8 columns
17+
const NUM_ROWS = 8;//and 8 rows
18+
const btnMove = document.querySelector('#btnMove');
1319

1420
//Create the ChessPiece objects, and store them in array vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
1521
let chessPieces = new Array(32);
1622
chessPieces[0] = new ChessPiece('DR2', 'dark', 'A8', 'images/dark-rook.png');//dark rook 2
1723
chessPieces[1] = new ChessPiece('DK2', 'dark', 'B8', 'images/dark-knight.png');//dark knight 2
1824
chessPieces[2] = new ChessPiece('DB2', 'dark', 'C8', 'images/dark-bishop.png');//dark bishop 2
1925
chessPieces[3] = new ChessPiece('DQ1', 'dark', 'D8', 'images/dark-queen.png');//dark queen
20-
chessPieces[4] = new ChessPiece('DQ0', 'dark', 'E8', 'images/dark-king.png');//dark king -- can't have pieceID 'DK1' because that will be used by dark knight 1 -- so I gave it pieceID of 'DQ0' so he can be associated with his wife, the queen
26+
chessPieces[4] = new ChessPiece('DQ0', 'dark', 'E8', 'images/dark-king.png');//dark king
2127
chessPieces[5] = new ChessPiece('DB1', 'dark', 'F8', 'images/dark-bishop.png');//dark bishop 1
2228
chessPieces[6] = new ChessPiece('DK1', 'dark', 'G8', 'images/dark-knight.png');//dark knight 1
2329
chessPieces[7] = new ChessPiece('DR1', 'dark', 'H8', 'images/dark-rook.png');//dark rook 1
24-
25-
//instead of this duplicate code, I did a for loop to do the same thing
26-
//chessPieces[8] = new ChessPiece('DP8', 'dark', 'A7', 'images/dark-pawn.png');//dark pawn 8
27-
//...
28-
// chessPieces[15] = new ChessPiece('DP1', 'dark', 'H7', 'images/dark-pawn.png');//dark pawn 1
29-
for (let col = 0; col < EIGHT; col++) {//loops through 8 times -- one for each pawn
30-
chessPieces[col + EIGHT] = new ChessPiece(`DP${EIGHT - col}`, 'dark', `${alpha2num[col + 1]}7`, 'images/dark-pawn.png');//creates dark pawn 8, ... dark pawn 1
30+
for (let col = 0; col < NUM_COLS; col++) {//loops through 8 times -- one for each pawn
31+
chessPieces[col + NUM_COLS] = new ChessPiece(`DP${NUM_COLS - col}`, 'dark', `${alpha2num[col + 1]}7`, 'images/dark-pawn.png');//creates dark pawn 8, ... dark pawn 1
3132
}
32-
33-
//instead of this duplicate code, I did a for loop to do the same thing
34-
//chessPieces[16] = new ChessPiece('LP1', 'light', 'A2', 'images/light-pawn.png');//light pawn 1
35-
//...
36-
//chessPieces[23] = new ChessPiece('LP8', 'light', 'H2', 'images/light-pawn.png');//light pawn 8
37-
for (let col = 0; col < EIGHT; col++) {//loops through 8 times -- one for each pawn
38-
chessPieces[col + 16] = new ChessPiece(`LP${col + 1}`, 'light', `${alpha2num[col + 1]}2`, 'images/light-pawn.png');//creates light pawn 8, ... light pawn 1
33+
for (let col = 0; col < NUM_COLS; col++) {//loops through 8 times -- one for each pawn
34+
chessPieces[col+16] = new ChessPiece(`LP${col + 1}`, 'light', `${alpha2num[col + 1]}2`, 'images/light-pawn.png');//creates light pawn 8, ... light pawn 1
3935
}
40-
4136
chessPieces[24] = new ChessPiece('LR1', 'light', 'A1', 'images/light-rook.png');//light rook 1
4237
chessPieces[25] = new ChessPiece('LK1', 'light', 'B1', 'images/light-knight.png');//light knight 1
4338
chessPieces[26] = new ChessPiece('LB1', 'light', 'C1', 'images/light-bishop.png');//light bishop 1
4439
chessPieces[27] = new ChessPiece('LQ1', 'light', 'D1', 'images/light-queen.png');//light queen
45-
chessPieces[28] = new ChessPiece('LQ0', 'light', 'E1', 'images/light-king.png');//light king -- can't have pieceID 'LK1' because that will be used by light knight 1 -- so I gave it pieceID of 'LQ0' so he can be associated with his wife, the queen
40+
chessPieces[28] = new ChessPiece('LQ0', 'light', 'E1', 'images/light-king.png');//light king
4641
chessPieces[29] = new ChessPiece('LB2', 'light', 'F1', 'images/light-bishop.png');//light bishop 2
4742
chessPieces[30] = new ChessPiece('LK2', 'light', 'G1', 'images/light-knight.png');//light knight 2
4843
chessPieces[31] = new ChessPiece('LR2', 'light', 'H1', 'images/light-rook.png');//light rook 2
49-
//Create the ChessPiece objects, and store them in array ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
//Create the light ChessPiece objects, and store them in array ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5045

5146
//Once I create all the ChessPiece objects, place them on the board vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
5247
for (let i = 0; i < chessPieces.length; i++) {
5348
const element = document.createElement('img');
5449
element.id = chessPieces[i].getPieceID();
5550
element.src = chessPieces[i].getImage();
5651
element.alt = chessPieces[i].getPieceID();
57-
document.querySelector(`#${chessPieces[i].getLocation()}`).appendChild(element);
52+
let currentLocation = `#${chessPieces[i].getLocation()}`;
53+
document.querySelector(currentLocation).textContent = chessPieces[i].getPieceID();
54+
document.querySelector(currentLocation).appendChild(element);
55+
//document.querySelector(currentLocation).classList.add('occupied');
5856
}
5957
//Once I create all the ChessPiece objects, place them on the board ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6058

61-
//garbage probably
62-
const lightPawn1 = document.querySelector('#LP1');
63-
console.log(lightPawn1);
64-
lightPawn1.addEventListener('click', e => {
65-
lightPawn1.classList.toggle('clicked');
59+
//this section adds event listeners to help move pieces vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
60+
btnMove.addEventListener('click', e => {
61+
e.preventDefault();
62+
let pieceID = document.querySelector('#movePiece').value;//get the piece ID from the input
63+
let chessPiece = chessPieces[pieceID2Index.indexOf(pieceID)];//get the chessPiece object to be moved
64+
let newLocation = document.querySelector('#newLocation').value.trim().toUpperCase();//get the new location from the input, trimmed and upper case
65+
if(chessPiece.canMove(newLocation)){
66+
chessPiece.move(newLocation);
67+
}
6668
});
69+
//this section adds event listeners to help move pieces ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
70+
6771

72+
73+
74+
/*
75+
//garbage vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
6876
//this section adds event listeners for when the squares are clicked vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
6977
//creates a 2d array to hold all the squares
70-
let squares = new Array(EIGHT);
71-
for (let i = 0; i < EIGHT; i++) {
72-
squares[i] = new Array(EIGHT);
78+
let squares = new Array(NUM_COLS);
79+
for (let i = 0; i < NUM_COLS; i++) {
80+
squares[i] = new Array(NUM_ROWS);
7381
}
82+
83+
let originClicked = null;
84+
let destinationClicked = null;
85+
7486
//in a double for loop, adds all the squares to a 2D array
75-
for (let col = 0; col < EIGHT; col++) {
76-
for (let row = 0; row < EIGHT; row++) {
87+
for (let col = 0; col < NUM_COLS; col++) {
88+
for (let row = 0; row < NUM_ROWS; row++) {
7789
squares[col][row] = document.querySelector(`#${alpha2num[col + 1]}${row + 1}`);
7890
}
7991
}
80-
//adds event listeners to all the squares so they toggle an orange color when they are clicked
81-
for (let col = 0; col < EIGHT; col++) {
82-
for (let row = 0; row < EIGHT; row++) {
83-
squares[col][row].addEventListener('click', e => {
84-
squares[col][row].classList.toggle('clicked');
92+
//adds event listeners to all the squares so they toggle color when they are clicked
93+
for (let col = 0; col < NUM_COLS; col++) {
94+
for (let row = 0; row < NUM_ROWS; row++) {
95+
const square = squares[col][row];
96+
square.addEventListener('click', e => {
97+
square.classList.toggle('clicked');
98+
console.log(square);
8599
});
86100
}
87101
}
88102
//this section adds event listeners for when the squares are clicked ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
103+
*/

0 commit comments

Comments
 (0)