Skip to content

Commit c493820

Browse files
author
christianwheeler09
committed
Update Pawn class
1 parent 32c65a8 commit c493820

File tree

11 files changed

+253
-155
lines changed

11 files changed

+253
-155
lines changed

TestCases/Main.xlsx

8.88 KB
Binary file not shown.

TestCases/Square.xlsx

-2.32 KB
Binary file not shown.

classes/Pawn.js

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,123 @@
11
import { ChessPiece } from "./chess_piece.js";
2-
const PAWN_METHODS = require('./pawn_methods');
2+
//import * as PAWN_METHODS from './pawn_methods.js';
3+
import * as SQUARE_METHODS from './square_methods.js';
34

45
export class Pawn extends ChessPiece {
56
//default constructor
67
constructor(pieceID, team, location, image) {
78
super(pieceID, team, location, image);
8-
this.maxSpeed = 2;
9+
this.isFirstMove = true;
910
}
1011

1112
/**
12-
* Determines whether this is a valid Pawn attack move -- by calling pawn_methods.isAttack()
13+
*
14+
* @param {string} dest the square we want to move the Pawn to
15+
* @returns true if that is an attack move; false otherwise
1316
*/
14-
isAttack(target) {
15-
return PAWN_METHODS.isAttack(this.getTeam(), this.getLocation(), target);
17+
isAttack(dest) {
18+
if (SQUARE_METHODS.DeltaY2(this.getTeam(), this.getLocation(), dest) === 1) { //if the pawn is moving forward 1
19+
return SQUARE_METHODS.DeltaDiag(this.getLocation(), dest) === 1; //if delta diag is 1, then this is attack move
20+
}
21+
else return false; //if not moving forward one, not an attack move
1622
}
1723

1824
/**
1925
* Determines whether this is a Passive1 move -- by calling pawn_methods.isPassive1()
2026
*/
21-
isPassive1(destination) {
22-
return PAWN_METHODS.isPassive1(this.getTeam(), this.getLocation(), destination);
27+
isPassive1(dest) {
28+
//return PAWN_METHODS.isPassive1(this.getTeam(), this.getLocation(), dest);
29+
if (SQUARE_METHODS.DeltaY2(this.getTeam(), this.getLocation(), dest) === 1) { //if the pawn is moving forward 1
30+
return SQUARE_METHODS.DeltaX(this.getLocation(), dest) === 0; //if delta x is 0, then this is passive move
31+
}
32+
else return false; //if not moving forward o1ne, not a Passive1 move
2333
}
2434

2535
/**
2636
* Determines whether this is a Passive2 move -- by calling pawn_methods.isPassive2()
2737
*/
28-
isPassive2(destination) {
29-
return PAWN_METHODS.isPassive2(this.getTeam(), this.getLocation(), destination);
38+
isPassive2(dest) {
39+
//return PAWN_METHODS.isPassive2(this.getTeam(), this.getLocation(), dest);
40+
if (SQUARE_METHODS.DeltaY2(this.getTeam(), this.getLocation(), dest) === 2) { //if the pawn is moving forward 2
41+
return SQUARE_METHODS.DeltaX(this.getLocation(), dest) === 0; //if delta x is 0, then this is passive move
42+
}
43+
else return false; //if not moving forward 2, not a Passive2 move
3044
}
3145

3246
/**
33-
* Checks the requested newSquare, to see if this Pawn can actually move there
47+
* forward1
48+
* @returns the new location if the chess piece moved forward 1 space
3449
*/
35-
canMove(destination) {
36-
return PAWN_METHODS.canMove(this.getTeam(), this.getLocation(), destination);
50+
forward1(team, square) {
51+
const col = square[0];
52+
let row = Number(square[1]);
53+
if (team === 'light') {
54+
row++;
55+
}
56+
else if (team === 'dark') {
57+
row--;
58+
}
59+
return `${col}${row}`;
60+
}
61+
62+
/**
63+
* canMove
64+
* @param {string} dest the new square that we want to move to
65+
* @returns true if the Pawn is allowed to move there, false otherwise
66+
*/
67+
canMove(dest) {
68+
console.log('Pawn.canMove()');
69+
if (!super.canMove(dest)) {
70+
//if super says false, then false
71+
return false;
72+
}
73+
if (super.isOccupied(dest)) { //is the dest occupied?
74+
if (this.isAttack(dest)) { //is it an attack move?
75+
76+
this.setFeedback(`Attacking square ${dest}`);
77+
return true;
78+
}
79+
//else not an attack move
80+
this.setFeedback("Error: a pawn's attack must be a forward diagonal move of distance 1");
81+
return false;
82+
}
83+
if (this.isPassive1(dest)) { //is it a passive1 move?
84+
this.setFeedback(`Peacefully moving to square ${dest}`); //passive moves are allowed to empty squares
85+
return true;
86+
}
87+
if (this.isPassive2(dest)) { //is it a passive2 move?
88+
if (this.isFirstMove) { //Passive2 moves are only allowed on the first move for that pawn
89+
const path = this.forward1(super.getTeam(), super.getLocation());
90+
if (super.isOccupied(path)) { //is the path obstructed?
91+
this.setFeedback(`The path is obstructed because square ${path} is occupied`);
92+
return false;
93+
}
94+
else { //else the path is clear
95+
this.setFeedback(`The path is clear because ${path} is empty`);
96+
return true;
97+
}
98+
}
99+
else { //not first move, passive2 is not allowed
100+
this.setFeedback('Error: pawn can move two spaces only on its first move');
101+
return false;
102+
}
103+
}
104+
else {
105+
this.setFeedback(`Error: you cannot move Pawn ${this.getPieceID()} to ${dest}`);
106+
return false;
107+
}
108+
109+
110+
111+
}
112+
113+
/**
114+
*
115+
* @param {string} newSquare
116+
* calls the base function to move the pawn to the new square,
117+
* then updates the Pawn's "isFirstMove" boolean variable to false
118+
*/
119+
move(newSquare) {
120+
super.move(newSquare);
121+
this.isFirstMove = false;
37122
}
38123
}

classes/chess_piece.js

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const IsValidSquare = require('./square_methods').isValidSquare;
1+
import * as SQUARE_METHODS from './square_methods.js';
22

33
export class ChessPiece {
44
/**
@@ -10,8 +10,11 @@ export class ChessPiece {
1010
this.team = team;
1111
this.location = location;
1212
this.image = image;
13+
this.makeOccupied(location);
1314
}
1415

16+
feedback = document.querySelector('#feedback');
17+
1518
//getters
1619
getPieceID() { return this.pieceID; }
1720
getTeam() { return this.team; }
@@ -21,9 +24,36 @@ export class ChessPiece {
2124
//setters -- dummy functions -- delete later
2225
setTeam(newTeam) { this.team = newTeam; }
2326
setLocation(newLocation) { this.location = newLocation; }
27+
setFeedback(text) {this.feedback.textContent = text; }
2428

29+
/**---------------------------------------------------------------------------------------------------------------------------------------
30+
*
31+
* @param {string} square
32+
* @returns true if the square is occupied, false otherwise
33+
*/
34+
isOccupied(square) {
35+
return document.querySelector(`#${square}`).classList.contains('occupied');
36+
}
2537

26-
/**
38+
/**---------------------------------------------------------------------------------------------------------------------------------------
39+
*
40+
* @param {string} square
41+
* @returns true if the square is occupied, false otherwise
42+
*/
43+
makeOccupied(square) {
44+
document.querySelector(`#${square}`).classList.add('occupied');
45+
}
46+
47+
/**---------------------------------------------------------------------------------------------------------------------------------------
48+
*
49+
* @param {string} square
50+
* @returns true if the square is occupied, false otherwise
51+
*/
52+
makeEmpty(square) {
53+
document.querySelector(`#${square}`).classList.remove('occupied');
54+
}
55+
56+
/**---------------------------------------------------------------------------------------------------------------------------------------
2757
* Moves this ChessPiece object from the current location to the new location, by updating the location field
2858
*
2959
* @param {string} newSquare the new square that this chess piece will be moved to
@@ -42,42 +72,53 @@ export class ChessPiece {
4272
document.querySelector(`#${oldSquare}`).textContent = '';
4373

4474
//declare that the OLD square is no longer occupied
45-
document.querySelector(`#${oldSquare}`).classList.remove('occupied');
75+
this.makeEmpty(oldSquare);
4676
//declare that the NEW square is now occupied
47-
document.querySelector(`#${newSquare}`).classList.add('occupied');
77+
this.makeOccupied(newSquare);
4878

4979
//update the location stored in this object
5080
this.location = newSquare;
81+
82+
feedback.textContent = 'Successfully moved a chess piece to new location';
83+
console.log('Successfully moved a chess piece to new location');
5184
}
5285

53-
/**
86+
/**---------------------------------------------------------------------------------------------------------------------------------------
5487
* Checks the requested newSquare, to see if this ChessPiece can actually move there
5588
*
5689
* @param {string} newSquare the location which we want to check that this piece is allowed to move to
5790
* @return boolean true if the ChessPiece can move there, false otherwise
5891
*/
5992
canMove(newSquare) {
60-
if(!isValidSquare(newSquare)){
93+
//if new square is invalid
94+
if (!SQUARE_METHODS.IsValidSquare(newSquare)) {
95+
feedback.textContent = `Error: the square ${newSquare} does not exist`;
96+
console.log(`Error: the square ${newSquare} does not exist`);
6197
return false; //you cannot move to an invalid square
6298
}
63-
64-
if (newSquare === this.location) {//if the user tries to move 0 spaces
65-
console.log('You cannot move 0 spaces!');
99+
100+
//if the user tries to move 0 spaces
101+
if (newSquare === this.location) {
102+
feedback.textContent = 'Error: you cannot move 0 spaces';
103+
console.log('Error: you cannot move 0 spaces');
66104
return false;//it is invalid to move a piece zero spaces -- that doesn't make any sense
67105
}
68106

69-
if (!document.querySelector(`#${newSquare}`).classList.contains('occupied')) {//if newSquare is empty -- new square is NOT occupied
70-
console.log('You successfully moved to an empty square');
107+
//if new square is empty
108+
if (!this.isOccupied(newSquare)) {
71109
return true;
72110
}
73111

74-
if (this.pieceID[0] != document.querySelector(`#${newSquare}`).textContent[0]) {//if new square has an enemy on it -- the teams are opposite
75-
console.log('Enemy captured!');
76-
return true;
77-
}
78-
else { //the destination square has a player of our team on it
79-
console.log('You cannot capture your own pieces!');
80-
return false;//can't capture our own players!
112+
//if new square has an ally on it -- the teams are same
113+
if (this.pieceID[0] === document.querySelector(`#${newSquare}`).textContent[0]) {
114+
feedback.textContent = 'Error: you cannot capture your own pieces';
115+
console.log('Error: you cannot capture your own pieces');
116+
return false; //can't capture our own players!
117+
81118
}
119+
// else the destination square has an enemy on it
120+
feedback.textContent = 'Attempting to capture an enemy';
121+
console.log('Attempting to capture an enemy');
122+
return true;
82123
}
83124
}

classes/pawn_methods.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,21 @@ function isAttack(team, src, dest) {
1010
if (sq.DeltaY2(team, src, dest) === 1) { //if the pawn is moving forward 1
1111
return sq.DeltaDiag(src, dest) === 1; //if delta diag is 1, then this is attack move
1212
}
13-
else return false; //if not moving forward one, not an attack move
13+
else return false; //if not moving forward 1, not an attack move
1414
}
1515

1616
function isPassive1(team, src, dest) {
1717
if (sq.DeltaY2(team, src, dest) === 1) { //if the pawn is moving forward 1
1818
return sq.DeltaX(src, dest) === 0; //if delta x is 0, then this is passive move
1919
}
20-
else return false; //if not moving forward one, not an attack move
20+
else return false; //if not moving forward o1ne, not a Passive1 move
2121
}
2222

2323
function isPassive2(team, src, dest) {
2424
if (sq.DeltaY2(team, src, dest) === 2) { //if the pawn is moving forward 2
2525
return sq.DeltaX(src, dest) === 0; //if delta x is 0, then this is passive move
2626
}
27-
else return false; //if not moving forward one, not an attack move
28-
}
29-
30-
function canMove(team, src, dest){
31-
return true;
27+
else return false; //if not moving forward 2, not a Passive2 move
3228
}
3329

3430
module.exports.isAttack = isAttack;

classes/square_methods.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
//this javascript code is used to calculate stuff with squares, as in distance between squares and such
22

3-
//number to letter 1 2 3 4 5 6 7 8
4-
num2alpha = ['Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
3+
//number to letter 0 1 2 3 4 5 6 7 8
4+
const num2alpha = ['Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
55

66
/**
77
* Determines whether the given string is a valid square on the chess board
88
* @param square a string representing the square
99
* @returns true if valid square, false if invalid
1010
*/
11-
const IsValidSquare = square => {
11+
export function IsValidSquare(square) {
1212
//is the length of the string valid?
13-
if(square.length != 2){
13+
if (square.length != 2) {
1414
return false;
1515
}
1616
//is the column valid?
1717
let column = square[0];
18-
if(column < 'A' || column > 'H'){
18+
if (column < 'A' || column > 'H') {
1919
return false;
2020
}
2121
//is the row valid?
2222
let row = Number(square[1]);
23-
if(Object.is(row, NaN) || row < 1 || row > 8){
23+
if (Object.is(row, NaN) || row < 1 || row > 8) {
2424
return false;
2525
}
2626
//it passed all my checks, so it is a valid square
@@ -33,7 +33,7 @@ const IsValidSquare = square => {
3333
* @param square2 a string representing the destination square
3434
* @returns the distance between the two COLUMNS
3535
*/
36-
const DeltaX = (square1, square2) => {
36+
export function DeltaX(square1, square2) {
3737
//let column1 = num2alpha.indexOf(square1[0]);//get the COLUMN of square1 and convert it to a number
3838
//let column2 = num2alpha.indexOf(square2[0]);//get the COLUMN of square2 and convert it to a number
3939
//return Math.abs(column2 - column1);
@@ -46,7 +46,7 @@ const DeltaX = (square1, square2) => {
4646
* @param square2 a string representing the destination square
4747
* @returns the distance between the two ROWS
4848
*/
49-
const DeltaY = (square1, square2) => {
49+
export function DeltaY(square1, square2) {
5050
//let row1 = square1[1];//get the ROW of square1
5151
//let row2 = square2[1];//get the ROW of square2
5252
//return Math.abs(row2 - row1);//return the distance between those two ROWS
@@ -65,8 +65,8 @@ const DeltaY = (square1, square2) => {
6565
* positive if moving DOWN (DARK team)
6666
* negative if moving UP (DARK team)
6767
*/
68-
const DeltaY2 = (team, square1, square2) => {
69-
if(team === 'light'){
68+
export function DeltaY2(team, square1, square2) {
69+
if (team === 'light') {
7070
return square2[1] - square1[1];//return the distance between those two ROWS
7171
}
7272
//else team is dark
@@ -79,7 +79,7 @@ const DeltaY2 = (team, square1, square2) => {
7979
* @param square2 a string representing the destination square
8080
* @returns the diagonal distance between the two squares
8181
*/
82-
const DeltaDiag = (square1, square2) => {
82+
export function DeltaDiag(square1, square2) {
8383
let dX = DeltaX(square1, square2);//find the x distance
8484
let dY = DeltaY(square1, square2);//find the y distance
8585
if (dX === dY) {//if DeltaX and DeltaY are the same, then it is a diagonal move
@@ -88,9 +88,5 @@ const DeltaDiag = (square1, square2) => {
8888
return -1;//else this is NOT a diagonal move
8989
}
9090

91-
//exports
92-
module.exports.IsValidSquare = IsValidSquare;
93-
module.exports.DeltaX = DeltaX;
94-
module.exports.DeltaY = DeltaY;
95-
module.exports.DeltaY2 = DeltaY2;
96-
module.exports.DeltaDiag = DeltaDiag;
91+
92+

0 commit comments

Comments
 (0)