From b07e7747014071177fda222fb1b22175712aaa01 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 21 Feb 2018 16:48:54 -0600 Subject: [PATCH 1/4] initial commit --- 05week/checkers.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/05week/checkers.js b/05week/checkers.js index 8f33a089c..a87abb634 100644 --- a/05week/checkers.js +++ b/05week/checkers.js @@ -10,9 +10,11 @@ const rl = readline.createInterface({ function Checker() { // Your code here + this.symbol = 'B'; } function Board() { + this.grid = []; // creates an 8x8 array, filled with null values this.createGrid = function() { @@ -21,7 +23,8 @@ function Board() { this.grid[row] = []; // push in 8 columns of nulls for (let column = 0; column < 8; column++) { - this.grid[row].push(null); + const checker = new Checker(); + this.grid[row].push(checker); } } }; From 3af199717fb251e4ca597abe3555d3912162054e Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 28 Feb 2018 15:41:19 -0600 Subject: [PATCH 2/4] working move and jump function, successfully printing board --- 05week/checkers.js | 162 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 144 insertions(+), 18 deletions(-) diff --git a/05week/checkers.js b/05week/checkers.js index a87abb634..e8e2a3aae 100644 --- a/05week/checkers.js +++ b/05week/checkers.js @@ -8,29 +8,42 @@ const rl = readline.createInterface({ }); -function Checker() { +class Checker { // Your code here - this.symbol = 'B'; + constructor() { + this.symbol1 = 'B'; + this.symbol2 = 'A'; + } + } -function Board() { +class Board { + constructor() { + + this.grid = []; + this.checkers = 24; + // creates an 8x8 array, filled with null values - this.grid = []; - // creates an 8x8 array, filled with null values - this.createGrid = function() { + } + createGrid() { // 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++) { const checker = new Checker(); - this.grid[row].push(checker); + + + this.grid[row].push(null); + + } + } - }; + } // prints out the board - this.viewGrid = function() { + viewGrid() { // add our column numbers let string = " 0 1 2 3 4 5 6 7\n"; for (let row = 0; row < 8; row++) { @@ -41,7 +54,7 @@ function Board() { // 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); + rowOfCheckers.push(this.grid[row][column]); } else { // just push in a blank space rowOfCheckers.push(' '); @@ -53,18 +66,131 @@ function Board() { string += "\n"; } console.log(string); - }; + } + + playerStartingPositions() { + const checker = new Checker(); + const p1 = checker.symbol2; + const p2 = checker.symbol1; + this.grid.forEach((row, index) => { + if (index % 2 === 0 && index < 3) { + this.grid[index] = [null, p1, null, p1, null, p1, null, p1]; + } else if (index > 2 && index < 5) { + this.grid[index] = [null, null, null, null, null, null, null, null]; + } else if (index % 2 === 0 && index > 4) { + this.grid[index] = [null, p2, null, p2, null, p2, null, p2]; + } else if (index % 2 !== 0 && index < 3) { + this.grid[index] = [p1, null, p1, null, p1, null, p1, null]; + } else if (index % 2 !== 0 && index > 4) { + this.grid[index] = [p2, null, p2, null, p2, null, p2, null]; + } + }) + console.log(this.grid); + } - // Your code here } -function Game() { - this.board = new Board(); +class Game { + constructor() { - this.start = function() { + this.board = new Board(); + this.playerTurn = 'A'; + this.player1ClaimedPieces = 0; + this.player2ClaimedPieces = 0; + } + start() { this.board.createGrid(); + // Your code here - }; + this.board.playerStartingPositions(); + + } + stringToNumber(str) { + str = str.split(''); + str.forEach((item) => { + parseInt(item); + }) + return str; + } + moveChecker(whichPiece, toWhere) { + whichPiece = whichPiece.split('').map(Number); + toWhere = toWhere.split('').map(Number); + if (this.isLegalMove(whichPiece, toWhere) && this.jumpedPiece(whichPiece, toWhere)) { + if (this.playerTurn === 'A') { + if (whichPiece[1] < toWhere[1]) { + this.board.grid[toWhere[0]-1][toWhere[1]-1] = null; + } else if (whichPiece[1] > toWhere[1]) { + this.board.grid[toWhere[0]-1][toWhere[1]+1] = null; + } + this.board.grid[whichPiece[0]][whichPiece[1]] = null; + this.board.grid[toWhere[0]][toWhere[1]] = this.playerTurn; + this.player1ClaimedPieces ++; + this.playerTurn = 'B' + console.log(this.board.grid); + // console.log(game.board.checkers.length); + console.log(this.player1ClaimedPieces); + + } else { + if (whichPiece[1] < toWhere[1]) { + this.board.grid[toWhere[0]+1][toWhere[1]-1] = null; + } else if (whichPiece[1] > toWhere[1]) { + this.board.grid[toWhere[0]+1][toWhere[1]+1] = null; + } + this.board.grid[whichPiece[0]][whichPiece[1]] = null; + this.board.grid[toWhere[0]][toWhere[1]] = this.playerTurn; + this.player2ClaimedPieces ++; + this.playerTurn = 'A' + console.log(this.board.grid); + console.log(this.player2ClaimedPieces); + + + } + } else if (this.isLegalMove(whichPiece, toWhere)) { + if (this.playerTurn === 'A') { + this.board.grid[whichPiece[0]][whichPiece[1]] = null; + this.board.grid[toWhere[0]][toWhere[1]] = this.playerTurn; + this.playerTurn = 'B' + console.log(this.board.grid); + console.log(game.board.checkers.length); + + } else { + this.board.grid[whichPiece[0]][whichPiece[1]] = null; + this.board.grid[toWhere[0]][toWhere[1]] = this.playerTurn; + this.playerTurn = 'A' + console.log(this.board.grid); + + } + } + + } + jumpedPiece(whichPiece, toWhere) { + if(this.playerTurn === 'A' && + toWhere[0] - whichPiece[0] === 2 && + !this.board.grid[toWhere[0]][toWhere[1]]) { + return true; + } else if (this.playerTurn === 'B' && + whichPiece[0] - toWhere[0] === 2 && + !this.board.grid[toWhere[0]][toWhere[1]]) { + return true; + } else { + return false; + } + } + isLegalMove(whichPiece, toWhere) { + if (this.playerTurn === 'A' && + whichPiece[0] < toWhere[0] && + !this.board.grid[toWhere[0]][toWhere[1]]) { + return true; + } else if (this.playerTurn === 'B' && + whichPiece[0] > toWhere[0] && + !this.board.grid[toWhere[0]][toWhere[1]]) { + return true; + } else { + console.log('Unable to move.', whichPiece[0], whichPiece); + + return false; + } + } } function getPrompt() { @@ -93,8 +219,8 @@ if (typeof describe === 'function') { }); }); - describe('Game.moveChecker()', function () { - it('should move a checker', function () { + 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]); From b0083ee25c19a82f2a41e738277b9dd4f95f5d35 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 28 Feb 2018 20:00:05 -0600 Subject: [PATCH 3/4] FINALLY passing all tests, just needs checkForWin function --- 05week/checkers.js | 122 ++++++++++++++++++++++++++++++--------------- 1 file changed, 82 insertions(+), 40 deletions(-) diff --git a/05week/checkers.js b/05week/checkers.js index e8e2a3aae..2c0e2071c 100644 --- a/05week/checkers.js +++ b/05week/checkers.js @@ -21,7 +21,8 @@ class Board { constructor() { this.grid = []; - this.checkers = 24; + this.checkers = []; + // creates an 8x8 array, filled with null values } @@ -46,6 +47,7 @@ class Board { viewGrid() { // 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]; @@ -54,47 +56,86 @@ class Board { // 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]); + // checkers.push(this.grid[row][column]); + + // } else if (this.grid.indexOf(this.grid[row]) > 4 && this.grid[row][column]) { + // rowOfCheckers.push(this.grid[row][column].symbol2); + // checkers.push(this.grid[row][column]); + + } 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); + console.log(this.checkers.length) } + playerStartingPositions() { const checker = new Checker(); - const p1 = checker.symbol2; - const p2 = checker.symbol1; - this.grid.forEach((row, index) => { - if (index % 2 === 0 && index < 3) { - this.grid[index] = [null, p1, null, p1, null, p1, null, p1]; - } else if (index > 2 && index < 5) { - this.grid[index] = [null, null, null, null, null, null, null, null]; - } else if (index % 2 === 0 && index > 4) { - this.grid[index] = [null, p2, null, p2, null, p2, null, p2]; - } else if (index % 2 !== 0 && index < 3) { - this.grid[index] = [p1, null, p1, null, p1, null, p1, null]; - } else if (index % 2 !== 0 && index > 4) { - this.grid[index] = [p2, null, p2, null, p2, null, p2, null]; - } + // const p1 = checker; + // const p2 = checker.symbol1; + // this.grid.forEach((row, index) => { + // if (index % 2 === 0 && index < 3) { + // this.grid[index] = [null, checker, null, checker, null, checker, null, checker]; + // } else if (index % 2 !== 0 && index < 3) { + // this.grid[index] = [checker, null, checker, null, checker, null, checker, null]; + // } else if (index > 2 && index < 5) { + // this.grid[index] = [null, null, null, null, null, null, null, null]; + // } else if (index % 2 === 0 && index > 4) { + // this.grid[index] = [null, checker, null, checker, null, checker, null, checker]; + // } else if (index % 2 !== 0 && index > 4) { + // this.grid[index] = [checker, null, checker, null, checker, null, checker, null]; + // } + // }) + + this.grid.forEach((row, index1) => { + row.forEach((column, index2) => { + if (index1 % 2 === 0 && index1 < 3) { + if (index2 % 2 !== 0) { + this.grid[index1][index2] = checker.symbol1; + this.checkers.push(this.grid[index1][index2]); + } + } else if (index1 % 2 !== 0 && index1 < 3) { + if (index2 % 2 === 0) { + this.grid[index1][index2] = checker.symbol1; + this.checkers.push(this.grid[index1][index2]) + } + } else if (index1 % 2 === 0 && index1 > 4) { + if (index2 % 2 !== 0) { + this.grid[index1][index2] = checker.symbol2; + this.checkers.push(this.grid[index1][index2]) + } + } else if (index1 % 2 !== 0 && index1 > 4) { + if (index2 % 2 === 0) { + this.grid[index1][index2] = checker.symbol2; + this.checkers.push(this.grid[index1][index2]) + } + } + }) + + // console.log(this.grid); + // console.log(this.checkers); }) - console.log(this.grid); - } + } } class Game { constructor() { this.board = new Board(); - this.playerTurn = 'A'; + this.playerTurn = 'B'; this.player1ClaimedPieces = 0; this.player2ClaimedPieces = 0; } @@ -105,76 +146,77 @@ class Game { this.board.playerStartingPositions(); } - stringToNumber(str) { - str = str.split(''); - str.forEach((item) => { - parseInt(item); - }) - return str; - } + moveChecker(whichPiece, toWhere) { whichPiece = whichPiece.split('').map(Number); toWhere = toWhere.split('').map(Number); if (this.isLegalMove(whichPiece, toWhere) && this.jumpedPiece(whichPiece, toWhere)) { if (this.playerTurn === 'A') { if (whichPiece[1] < toWhere[1]) { - this.board.grid[toWhere[0]-1][toWhere[1]-1] = null; + this.board.grid[toWhere[0] - 1][toWhere[1] - 1] = null; } else if (whichPiece[1] > toWhere[1]) { - this.board.grid[toWhere[0]-1][toWhere[1]+1] = null; + this.board.grid[toWhere[0] - 1][toWhere[1] + 1] = null; } + this.board.grid[toWhere[0]][toWhere[1]] = this.board.grid[whichPiece[0]][whichPiece[1]]; this.board.grid[whichPiece[0]][whichPiece[1]] = null; - this.board.grid[toWhere[0]][toWhere[1]] = this.playerTurn; - this.player1ClaimedPieces ++; + this.player1ClaimedPieces++; + this.board.checkers.shift(); this.playerTurn = 'B' console.log(this.board.grid); // console.log(game.board.checkers.length); console.log(this.player1ClaimedPieces); + console.log("checkers :", this.board.checkers.length); } else { if (whichPiece[1] < toWhere[1]) { - this.board.grid[toWhere[0]+1][toWhere[1]-1] = null; + this.board.grid[toWhere[0] + 1][toWhere[1] - 1] = null; } else if (whichPiece[1] > toWhere[1]) { - this.board.grid[toWhere[0]+1][toWhere[1]+1] = null; + this.board.grid[toWhere[0] + 1][toWhere[1] + 1] = null; } + this.board.grid[toWhere[0]][toWhere[1]] = this.board.grid[whichPiece[0]][whichPiece[1]]; this.board.grid[whichPiece[0]][whichPiece[1]] = null; - this.board.grid[toWhere[0]][toWhere[1]] = this.playerTurn; - this.player2ClaimedPieces ++; + this.player2ClaimedPieces++; + this.board.checkers.shift(); this.playerTurn = 'A' console.log(this.board.grid); console.log(this.player2ClaimedPieces); + console.log('checkers:', this.board.checkers.length); + + } } else if (this.isLegalMove(whichPiece, toWhere)) { if (this.playerTurn === 'A') { + this.board.grid[toWhere[0]][toWhere[1]] = this.board.grid[whichPiece[0]][whichPiece[1]]; this.board.grid[whichPiece[0]][whichPiece[1]] = null; - this.board.grid[toWhere[0]][toWhere[1]] = this.playerTurn; this.playerTurn = 'B' console.log(this.board.grid); console.log(game.board.checkers.length); } else { + this.board.grid[toWhere[0]][toWhere[1]] = this.board.grid[whichPiece[0]][whichPiece[1]]; this.board.grid[whichPiece[0]][whichPiece[1]] = null; - this.board.grid[toWhere[0]][toWhere[1]] = this.playerTurn; this.playerTurn = 'A' console.log(this.board.grid); + } } } jumpedPiece(whichPiece, toWhere) { - if(this.playerTurn === 'A' && + if (this.playerTurn === 'A' && toWhere[0] - whichPiece[0] === 2 && !this.board.grid[toWhere[0]][toWhere[1]]) { return true; } else if (this.playerTurn === 'B' && whichPiece[0] - toWhere[0] === 2 && !this.board.grid[toWhere[0]][toWhere[1]]) { - return true; - } else { - return false; - } + return true; + } else { + return false; + } } isLegalMove(whichPiece, toWhere) { if (this.playerTurn === 'A' && From 2e8fea8564e037ea35d121095624bd355d7d1797 Mon Sep 17 00:00:00 2001 From: Cory Date: Thu, 1 Mar 2018 00:42:42 -0600 Subject: [PATCH 4/4] it's working. checkForWin function and all. I must sleep --- 05week/checkers.js | 107 ++++++++++++++++++++++++++------------------- 1 file changed, 61 insertions(+), 46 deletions(-) diff --git a/05week/checkers.js b/05week/checkers.js index 2c0e2071c..2a696d0b6 100644 --- a/05week/checkers.js +++ b/05week/checkers.js @@ -58,11 +58,6 @@ class Board { // push the symbol of the check in that location into the array rowOfCheckers.push(this.grid[row][column]); - // checkers.push(this.grid[row][column]); - - // } else if (this.grid.indexOf(this.grid[row]) > 4 && this.grid[row][column]) { - // rowOfCheckers.push(this.grid[row][column].symbol2); - // checkers.push(this.grid[row][column]); } else { @@ -77,27 +72,12 @@ class Board { string += "\n"; } console.log(string); - console.log(this.checkers.length) } - + //Everything works, but I am aware it is in need of some serious refactoring. + //which I will do in the future. I am very tired. playerStartingPositions() { const checker = new Checker(); - // const p1 = checker; - // const p2 = checker.symbol1; - // this.grid.forEach((row, index) => { - // if (index % 2 === 0 && index < 3) { - // this.grid[index] = [null, checker, null, checker, null, checker, null, checker]; - // } else if (index % 2 !== 0 && index < 3) { - // this.grid[index] = [checker, null, checker, null, checker, null, checker, null]; - // } else if (index > 2 && index < 5) { - // this.grid[index] = [null, null, null, null, null, null, null, null]; - // } else if (index % 2 === 0 && index > 4) { - // this.grid[index] = [null, checker, null, checker, null, checker, null, checker]; - // } else if (index % 2 !== 0 && index > 4) { - // this.grid[index] = [checker, null, checker, null, checker, null, checker, null]; - // } - // }) this.grid.forEach((row, index1) => { row.forEach((column, index2) => { @@ -124,8 +104,6 @@ class Board { } }) - // console.log(this.grid); - // console.log(this.checkers); }) } @@ -136,8 +114,12 @@ class Game { this.board = new Board(); this.playerTurn = 'B'; + /*be aware (and I'm not sure how this happened) player 'B' is player 1 and is represented + on the board as 'A', and vice versa.*/ this.player1ClaimedPieces = 0; this.player2ClaimedPieces = 0; + this.player1IllegalMoves = 0; + this.player2IllegalMoves = 0; } start() { this.board.createGrid(); @@ -151,38 +133,32 @@ class Game { whichPiece = whichPiece.split('').map(Number); toWhere = toWhere.split('').map(Number); if (this.isLegalMove(whichPiece, toWhere) && this.jumpedPiece(whichPiece, toWhere)) { - if (this.playerTurn === 'A') { - if (whichPiece[1] < toWhere[1]) { + if (this.playerTurn === 'A') { //if player 2 jumps. . . + if (whichPiece[1] < toWhere[1]) { //. . .down and to the right this.board.grid[toWhere[0] - 1][toWhere[1] - 1] = null; - } else if (whichPiece[1] > toWhere[1]) { + } else if (whichPiece[1] > toWhere[1]) { //. . .or down and to the left this.board.grid[toWhere[0] - 1][toWhere[1] + 1] = null; } this.board.grid[toWhere[0]][toWhere[1]] = this.board.grid[whichPiece[0]][whichPiece[1]]; this.board.grid[whichPiece[0]][whichPiece[1]] = null; - this.player1ClaimedPieces++; + this.player2ClaimedPieces++; this.board.checkers.shift(); this.playerTurn = 'B' - console.log(this.board.grid); - // console.log(game.board.checkers.length); - console.log(this.player1ClaimedPieces); - console.log("checkers :", this.board.checkers.length); + this.player2IllegalMoves = 0; - } else { - if (whichPiece[1] < toWhere[1]) { + + } else { // player 1 + if (whichPiece[1] < toWhere[1]) { //. . .up and to the right this.board.grid[toWhere[0] + 1][toWhere[1] - 1] = null; - } else if (whichPiece[1] > toWhere[1]) { + } else if (whichPiece[1] > toWhere[1]) { //. . .up and to the left this.board.grid[toWhere[0] + 1][toWhere[1] + 1] = null; } this.board.grid[toWhere[0]][toWhere[1]] = this.board.grid[whichPiece[0]][whichPiece[1]]; this.board.grid[whichPiece[0]][whichPiece[1]] = null; - this.player2ClaimedPieces++; + this.player1ClaimedPieces++; this.board.checkers.shift(); this.playerTurn = 'A' - console.log(this.board.grid); - console.log(this.player2ClaimedPieces); - console.log('checkers:', this.board.checkers.length); - - + this.player1IllegalMoves = 0; } @@ -191,17 +167,21 @@ class Game { this.board.grid[toWhere[0]][toWhere[1]] = this.board.grid[whichPiece[0]][whichPiece[1]]; this.board.grid[whichPiece[0]][whichPiece[1]] = null; this.playerTurn = 'B' - console.log(this.board.grid); - console.log(game.board.checkers.length); + this.player2IllegalMoves = 0; } else { this.board.grid[toWhere[0]][toWhere[1]] = this.board.grid[whichPiece[0]][whichPiece[1]]; this.board.grid[whichPiece[0]][whichPiece[1]] = null; this.playerTurn = 'A' - console.log(this.board.grid); - + this.player1IllegalMoves = 0; } + } else { + console.log('Unable to move.'); + + this.playerIllegalMoveCounter(whichPiece, toWhere); + this.checkForWin(); + } } @@ -228,11 +208,46 @@ class Game { !this.board.grid[toWhere[0]][toWhere[1]]) { return true; } else { - console.log('Unable to move.', whichPiece[0], whichPiece); return false; } } + playerIllegalMoveCounter(whichPiece, toWhere) { + const legalMove = this.isLegalMove(whichPiece, toWhere); + if (this.playerTurn === 'A' && !legalMove) { + this.player2IllegalMoves++; + console.log(this.player2IllegalMoves, 'p2 illegal moves'); + } else if (this.playerTurn === 'B' && !legalMove) { + this.player1IllegalMoves++; + console.log(this.player1IllegalMoves, 'p1 illegal moves'); + + } + } + checkForWin() { + if (this.playerTurn === 'A' && this.player2ClaimedPieces === 12) { + console.log('Player 2 wins!!'); + this.resetGame(); + } else if (this.playerTurn === 'A' && this.player2IllegalMoves === 12 - this.player1ClaimedPieces) { + console.log('Player 1 wins!!'); + this.resetGame(); + } else if (this.playerTurn === 'B' && this.player1ClaimedPieces === 12) { + console.log('Player 1 wins!!'); + this.resetGame(); + } else if (this.playerTurn === 'B' && this.player1IllegalMoves === 12 - this.player2ClaimedPieces) { + console.log('Player 2 wins!!'); + this.resetGame(); + } + } + resetGame() { + this.playerTurn = 'B'; + this.player1ClaimedPieces = 0; + this.player2ClaimedPieces = 0; + this.player1IllegalMoves = 0; + this.player2IllegalMoves = 0; + this.board.createGrid(); + this.board.playerStartingPositions(); + + } } function getPrompt() {