1
- const IsValidSquare = require ( './square_methods' ) . isValidSquare ;
1
+ import * as SQUARE_METHODS from './square_methods.js' ;
2
2
3
3
export class ChessPiece {
4
4
/**
@@ -10,8 +10,11 @@ export class ChessPiece {
10
10
this . team = team ;
11
11
this . location = location ;
12
12
this . image = image ;
13
+ this . makeOccupied ( location ) ;
13
14
}
14
15
16
+ feedback = document . querySelector ( '#feedback' ) ;
17
+
15
18
//getters
16
19
getPieceID ( ) { return this . pieceID ; }
17
20
getTeam ( ) { return this . team ; }
@@ -21,9 +24,36 @@ export class ChessPiece {
21
24
//setters -- dummy functions -- delete later
22
25
setTeam ( newTeam ) { this . team = newTeam ; }
23
26
setLocation ( newLocation ) { this . location = newLocation ; }
27
+ setFeedback ( text ) { this . feedback . textContent = text ; }
24
28
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
+ }
25
37
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
+ /**---------------------------------------------------------------------------------------------------------------------------------------
27
57
* Moves this ChessPiece object from the current location to the new location, by updating the location field
28
58
*
29
59
* @param {string } newSquare the new square that this chess piece will be moved to
@@ -42,42 +72,53 @@ export class ChessPiece {
42
72
document . querySelector ( `#${ oldSquare } ` ) . textContent = '' ;
43
73
44
74
//declare that the OLD square is no longer occupied
45
- document . querySelector ( `# ${ oldSquare } ` ) . classList . remove ( 'occupied' ) ;
75
+ this . makeEmpty ( oldSquare ) ;
46
76
//declare that the NEW square is now occupied
47
- document . querySelector ( `# ${ newSquare } ` ) . classList . add ( 'occupied' ) ;
77
+ this . makeOccupied ( newSquare ) ;
48
78
49
79
//update the location stored in this object
50
80
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' ) ;
51
84
}
52
85
53
- /**
86
+ /**---------------------------------------------------------------------------------------------------------------------------------------
54
87
* Checks the requested newSquare, to see if this ChessPiece can actually move there
55
88
*
56
89
* @param {string } newSquare the location which we want to check that this piece is allowed to move to
57
90
* @return boolean true if the ChessPiece can move there, false otherwise
58
91
*/
59
92
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` ) ;
61
97
return false ; //you cannot move to an invalid square
62
98
}
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' ) ;
66
104
return false ; //it is invalid to move a piece zero spaces -- that doesn't make any sense
67
105
}
68
106
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 ) ) {
71
109
return true ;
72
110
}
73
111
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
+
81
118
}
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 ;
82
123
}
83
124
}
0 commit comments