-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamePieces.js
executable file
·164 lines (117 loc) · 5.96 KB
/
gamePieces.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//Toggles board, and highlights the squares that player 1 can move to on this turn.
gameBoard.player1Highlight = function (gameBoard , rowNum , columnNum) {
//define clicked as the square that was clicked.
var clicked = gameBoard[rowNum][columnNum];
//if it's player ones turn
if(gameBoard.turnCount % 2 === 0) {
if (!gameBoard.isClicked) {
//if it isnt clicked
//toggle set to true
gameBoard.isClicked = true;
//If the piece is a super piece, it should be able to move in any direction
if(clicked.gamePiece.typeOfPiece === "super"){
//if there is a square there.
if (rowNum !== 0 && gameBoard[rowNum - 1][columnNum - 1]) {
//color the square grey and set the piece object as a property on it.
gameBoard[rowNum - 1][columnNum - 1].color = 'grey';
gameBoard[rowNum - 1][columnNum - 1].pieceToMove = clicked.gamePiece;
}
//repeat for each direction
if (rowNum !== 0 && gameBoard[rowNum - 1][columnNum + 1]) {
gameBoard[rowNum - 1][columnNum + 1].color = 'grey';
gameBoard[rowNum - 1][columnNum + 1].pieceToMove = clicked.gamePiece;
}
if (rowNum !== 8 && gameBoard[rowNum + 1][columnNum - 1]) {
gameBoard[rowNum + 1][columnNum - 1].color = 'grey';
gameBoard[rowNum + 1][columnNum - 1].pieceToMove = clicked.gamePiece;
}
if (rowNum !== 8 && gameBoard[rowNum + 1][columnNum + 1]) {
gameBoard[rowNum + 1][columnNum + 1].color = 'grey';
gameBoard[rowNum + 1][columnNum + 1].pieceToMove = clicked.gamePiece;
}
//Else if the piece is not a super piece, it can only move forward
} else {
if (rowNum !== 0 && gameBoard[rowNum - 1][columnNum - 1]) {
gameBoard[rowNum - 1][columnNum - 1].color = 'grey';
gameBoard[rowNum - 1][columnNum - 1].pieceToMove = clicked.gamePiece;
}
if (rowNum !== 0 && gameBoard[rowNum - 1][columnNum + 1]) {
gameBoard[rowNum - 1][columnNum + 1].color = 'grey';
gameBoard[rowNum - 1][columnNum + 1].pieceToMove = clicked.gamePiece;
}
}
} else {
// If any piece/board is clicked on turn it off and reset the board
gameBoard.isClicked = false;
gameBoard.rebuildBoard();
console.log("toggle off")
}
}
renderGameBoard(gameBoard);
}
//Highlights the squares player2 can move to (similar to player 1's highlight feature!)
gameBoard.player2Highlight = function (gameBoard , rowNum , columnNum) {
console.log('player 2 clicked')
var clicked = gameBoard[rowNum][columnNum];
//If it is player2's turn
if(gameBoard.turnCount % 2 !== 0) {
if (!gameBoard.isClicked) {
//if it isnt clicked
//toggle set to on/true
gameBoard.isClicked = true;
if(clicked.gamePiece.typeOfPiece === "super"){
if (rowNum !== 0 && gameBoard[rowNum - 1][columnNum - 1]) {
gameBoard[rowNum - 1][columnNum - 1].color = 'grey';
gameBoard[rowNum - 1][columnNum - 1].pieceToMove = clicked.gamePiece;
}
if (rowNum !== 0 && gameBoard[rowNum - 1][columnNum + 1]) {
gameBoard[rowNum - 1][columnNum + 1].color = 'grey';
gameBoard[rowNum - 1][columnNum + 1].pieceToMove = clicked.gamePiece;
}
if (rowNum !== 8 && gameBoard[rowNum + 1][columnNum - 1]) {
gameBoard[rowNum + 1][columnNum - 1].color = 'grey';
gameBoard[rowNum + 1][columnNum - 1].pieceToMove = clicked.gamePiece;
}
if (rowNum !== 8 && gameBoard[rowNum + 1][columnNum + 1]) {
gameBoard[rowNum + 1][columnNum + 1].color = 'grey';
gameBoard[rowNum + 1][columnNum + 1].pieceToMove = clicked.gamePiece;
}
}
//Else if it is not a super piece, piece can only move one space forward (which is one row down in the index)
else {
if (rowNum !== 8 && gameBoard[rowNum + 1][columnNum - 1]) {
gameBoard[rowNum + 1][columnNum - 1].color = 'grey';
gameBoard[rowNum + 1][columnNum - 1].pieceToMove = clicked.gamePiece;
}
if (rowNum !== 8 && gameBoard[rowNum + 1][columnNum + 1]) {
gameBoard[rowNum + 1][columnNum + 1].color = 'grey';
gameBoard[rowNum + 1][columnNum + 1].pieceToMove = clicked.gamePiece;
}
}
} else {
// If any piece/board is clicked on turn it off and reset the board
gameBoard.isClicked = false;
gameBoard.rebuildBoard();
console.log("toggle off")
}
}
renderGameBoard(gameBoard);
}
//Set up the initial position of the pieces
var defaultPieces = function (img1, img2) {
// iterate through each row and each square on that row
myEach(gameBoard, function (row,rowNum){
myEach(row, function (square,sqrNum) {
// If on the last 3 rows assign the pieces to player one, attach the highlight method to the piece here.
if (rowNum >= 6) {
// only assign every other square a piece
if ( (rowNum + sqrNum) % 2 === 0 ) makePiece(gameBoard, [rowNum,sqrNum], 'normal' , 'player1' , gameBoard.player1Highlight, img1);
//if on the first 3 rows, assign pieces for player 2
} else if (rowNum <= 2) {
//only on every other square
if ( (rowNum + sqrNum) % 2 === 0 ) makePiece(gameBoard, [rowNum,sqrNum], 'normal' , 'player2' , gameBoard.player2Highlight, img2); }
})
})
}
//This initializes the board and sets the pieces with default clip art photos for each team!
defaultPieces("http://www.cliparthut.com/clip-arts/529/penguin-clip-art-529942.png", "http://cronaldofan.mobi/wp-content/uploads/baby-lion-face-clip-art-hd-images-4.png" );