forked from AustinCodingAcademy/javascript-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckers.js
More file actions
223 lines (203 loc) · 6.97 KB
/
checkers.js
File metadata and controls
223 lines (203 loc) · 6.97 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
'use strict';
const assert = require('assert');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let playerTurn = 'O';
/*function Checker() {
this.king = no;
this.symbol =
//define Checker attributes
//checker.create per checker
}*/
class Checker {
constructor(symbol, king){
this.symbol = symbol;
this.king = king;
//this.row = game.board.grid[row];
//this.column = game.board.grid[column];
//this.where = this.row.tostring() + this.column.tostring();
}
moveChecker(whichPiece, toWhere){
}
}
function Board() {
this.grid = [];
// creates an 8x8 array, filled with null values
this.createGrid = function() {
// loop to create the 8 rows
for (let row = 0; row < 8; row++) {
this.grid[row] = [];
// push in 8 columns of nulls
for (let column = 0; column < 8; column++) {
if (((row%2==0)&&(column%2!=0))||((row%2!=0)&&(column%2==0))) {
if (row<3){
this.grid[row].push(new Checker('X',false));
}
if (row>4){
this.grid[row].push(new Checker('O',false));
}
}else{
this.grid[row].push(null);
}
}
}
};
// prints out the board
this.viewGrid = function() {
// add our column numbers
let string = " 0 1 2 3 4 5 6 7\n";
for (let row = 0; row < 8; row++) {
// we start with our row number in our array
const rowOfCheckers = [row];
// a loop within a loop
for (let column = 0; column < 8; column++) {
// if the location is "truthy" (contains a checker piece, in this case)
if (this.grid[row][column]) {
// push the symbol of the check in that location into the array
rowOfCheckers.push(this.grid[row][column].symbol);
} else {
// just push in a blank space
rowOfCheckers.push(' ');
}
}
// join the rowOfCheckers array to a string, separated by a space
string += rowOfCheckers.join(' ');
// add a 'new line'
string += "\n";
}
console.log(string);
};
// Your code here
}
function Game() {
this.board = new Board();
this.moveChecker = function(whichPiece, toWhere){
let x1 = Number(whichPiece.charAt(0));
let y1 = Number(whichPiece.charAt(1));
let x2 = Number(toWhere.charAt(0));
let y2 = Number(toWhere.charAt(1));
//If the player enters an empty square as whichPiece
if (game.board.grid[y1][x1]===null){
console.log("The square you specified in response to 'Which piece?' has no piece in it. Please try again.")
return;
}
//If the 'O' player enters a square with the wrong piece as whichPiece
if (playerTurn==='O'){
if(game.board.grid[y1][x1].symbol==='X'){
console.log("The square you specified in response to 'Which piece?' has an 'X' piece in it. Please try again.")
return;
}else{
if(game.board.grid[y2][x2]!=null){
console.log("The square you are trying to move to is already occupied. Please try again.")
return;
}else{
// if (((y2!=y1+1)&&(Math.abs(x2-x1)!=1))||
// (((y2!=y1+2)&&(Math.abs(x2-x1)!=2))&&
// (((x2>x1)&&(game.board.grid[y1+1][x1+1].symbol!='X'))||
// ((x1>x2)&&(game.board.grid[y1+1][x1-1].symbol!='X'))))){
// console.log("The square you want to move to is not diagonal from the square with the piece in it. Please try again.");
// return;
// }else{
let currentChecker = game.board.grid[y1][x1];
game.board.grid[y2].splice(x2,1,currentChecker);
game.board.grid[y1].splice(x1,1,null);
if (Math.abs(x2-x1)==2){
let deadx = (x1+x2)/2;
let deady = (y1+y2)/2;
game.board.grid[deady].splice(deadx,1,null);
}
// }
}
//if toWhere is diagonal and empty, move piece
//if toWhere is diagonal and occupied by same piece, console.log error
//if toWhere is diagonal and occupied by opposite piece and the space diagonal from THAT is empty, capture piece
//if toWhere row is 0, king
//if king, allow backwards moves
//repetitive captures (if possible)
}
playerTurn='X';
return;
}
//If the 'X' player enters a square with the wrong piece as whichPiece
if (playerTurn==='X'){
if(game.board.grid[y1][x1].symbol==='O'){
console.log("The square you specified in response to 'Which piece?' has an 'X' piece in it. Please try again.")
return;
}else{
if(game.board.grid[y2][x2]!=null){
console.log("The square you are trying to move to is already occupied. Please try again.")
return;
}else{
let currentChecker = game.board.grid[y1][x1];
game.board.grid[y2].splice(x2,1,currentChecker);
game.board.grid[y1].splice(x1,1,null);
if (Math.abs(x2-x1)==2){
let deadx = (x1+x2)/2;
let deady = (y1+y2)/2;
game.board.grid[deady].splice(deadx,1,null);
}
}
//if toWhere is diagonal and empty, move piece
//if toWhere is diagonal and occupied by same piece, console.log error
//if toWhere is diagonal and occupied by opposite piece and the space diagonal from THAT is empty, capture piece
//if toWhere row is 0, king
//if king, allow backwards moves
//repetitive captures (if possible)
}
playerTurn='O';
return;
}
}
//define moveChecker function
//flip between players
this.start = function() {
this.board.createGrid();
console.log('Refer to each piece/space by the column number then row number such as "10" or "67"')
// Your code here
};
}
function getPrompt() {
game.board.viewGrid();
console.log("It's Player " + playerTurn + "'s turn.");
rl.question('which piece?: ', (whichPiece) => {
rl.question('to where?: ', (toWhere) => {
game.moveChecker(whichPiece, toWhere);
getPrompt();
});
});
}
const game = new Game();
game.start();
// Tests
if (typeof describe === 'function') {
describe('Game', () => {
it('should have a board', () => {
assert.equal(game.board.constructor.name, 'Board');
});
it('board should have 24 checkers', () => {
assert.equal(game.board.checkers.length, 24);
});
});
describe('Game.moveChecker()', function () {
it('should move a checker', function () {
assert(!game.board.grid[4][1]);
game.moveChecker('50', '41');
assert(game.board.grid[4][1]);
game.moveChecker('21', '30');
assert(game.board.grid[3][0]);
game.moveChecker('52', '43');
assert(game.board.grid[4][3]);
});
it('should be able to jump over and kill another checker', () => {
game.moveChecker('30', '52');
assert(game.board.grid[5][2]);
assert(!game.board.grid[4][1]);
assert.equal(game.board.checkers.length, 23);
});
});
} else {
getPrompt();
}