From e4acbed94fd062cabbd49aa43659bd4b8dbe8c3b Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Fri, 1 Feb 2019 04:32:51 -0600 Subject: [PATCH 1/5] looked at checkers.js.. didn't accomplish anything, played with the way the board looks --- 05week/checkers.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/05week/checkers.js b/05week/checkers.js index 15d9953d1..5cd4261cc 100644 --- a/05week/checkers.js +++ b/05week/checkers.js @@ -8,8 +8,9 @@ const rl = readline.createInterface({ }); -function Checker() { +class Checker { // Your code here + } class Board { @@ -29,7 +30,7 @@ class Board { } viewGrid() { // add our column numbers - let string = " 0 1 2 3 4 5 6 7\n"; + let string = " | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n";// + '-----------------------------------' + '\n'; for (let row = 0; row < 8; row++) { // we start with our row number in our array const rowOfCheckers = [row]; @@ -45,14 +46,15 @@ class Board { } } // join the rowOfCheckers array to a string, separated by a space - string += rowOfCheckers.join(' '); + string += rowOfCheckers.join(' | ') + ' |'; // add a 'new line' - string += "\n"; + string += "\n";// + '-----------------------------------' + '\n'; } console.log(string); } // Your code here + } class Game { @@ -62,6 +64,9 @@ class Game { start() { this.board.createGrid(); } + moveChecker( whichPiece, toWhere ) { + game.board.grid[toWhere[0]].splice( toWhere[1], 1, game.board.grid[whichPiece[0]][whichPiece[1]] ); + } } function getPrompt() { From 7bacd28bfc154d86f979a687a22d92c035f3fe8e Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Wed, 6 Feb 2019 19:30:15 -0600 Subject: [PATCH 2/5] reset to original state --- 05week/checkers.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/05week/checkers.js b/05week/checkers.js index 5cd4261cc..19fdb52b1 100644 --- a/05week/checkers.js +++ b/05week/checkers.js @@ -8,9 +8,9 @@ const rl = readline.createInterface({ }); -class Checker { +function Checker() { // Your code here - + } class Board { @@ -30,7 +30,7 @@ class Board { } viewGrid() { // add our column numbers - let string = " | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n";// + '-----------------------------------' + '\n'; + 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]; @@ -46,15 +46,14 @@ class Board { } } // join the rowOfCheckers array to a string, separated by a space - string += rowOfCheckers.join(' | ') + ' |'; + string += rowOfCheckers.join(' '); // add a 'new line' - string += "\n";// + '-----------------------------------' + '\n'; + string += "\n"; } console.log(string); } // Your code here - } class Game { @@ -64,9 +63,6 @@ class Game { start() { this.board.createGrid(); } - moveChecker( whichPiece, toWhere ) { - game.board.grid[toWhere[0]].splice( toWhere[1], 1, game.board.grid[whichPiece[0]][whichPiece[1]] ); - } } function getPrompt() { From 2c59b8661e8ca286d480563051b8898a0e2edeb4 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Tue, 12 Feb 2019 18:14:06 -0600 Subject: [PATCH 3/5] built checkers, all tests pass --- 05week/checkers.js | 72 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/05week/checkers.js b/05week/checkers.js index 19fdb52b1..05b65c9ed 100644 --- a/05week/checkers.js +++ b/05week/checkers.js @@ -8,14 +8,17 @@ const rl = readline.createInterface({ }); -function Checker() { +function Checker( color ) { // Your code here - + // color = 'white' ? this.symbol = String.fromCharCode(0x125CB) : this.symbol = String.fromCharCode(0x125CF); + if(color == 'white') this.symbol = String.fromCharCode(0x125CF) + else this.symbol = String.fromCharCode(0x125CB); } class Board { constructor() { this.grid = [] + this.checkers = [] } // method that creates an 8x8 array, filled with null values createGrid() { @@ -54,6 +57,36 @@ class Board { } // Your code here + createCheckers() { + // pieces are either black or white + let whitePosition = [ + [0, 1], [0, 3], [0, 5], [0, 7], + [1, 0], [1, 2], [1, 4], [1, 6], + [2, 1], [2, 3], [2, 5], [2, 7] + ]; + + let blackPosition = [ + [5, 0], [5, 2], [5, 4], [5, 6], + [6, 1], [6, 3], [6, 5], [6, 7], + [7, 0], [7, 2], [7, 4], [7, 6] + ]; + + for( let i = 0; i < 12; i++ ) { + let whiteChecker = new Checker( 'white' ); + let whiteRow = whitePosition[i][0]; + let whiteColumn = whitePosition[i][1]; + this.checkers.push(whiteChecker); + this.grid[whiteRow][whiteColumn] = whiteChecker; + } + + for( let i = 0; i < 12; i++ ) { + let blackChecker = new Checker( 'black' ); + let blackRow = blackPosition[i][0]; + let blackColumn = blackPosition[i][1]; + this.checkers.push(blackChecker); + this.grid[blackRow][blackColumn] = blackChecker; + } + } } class Game { @@ -62,7 +95,42 @@ class Game { } start() { this.board.createGrid(); + this.board.createCheckers(); } + + moveChecker( start, end ) { + let startRow = parseInt(start[0]); + let startColumn = parseInt(start[1]); + let endRow = parseInt(end[0]); + let endColumn = parseInt(end[1]); + + if( isValidInput(startRow, startColumn, endRow, endColumn) && isLegal(startRow, startColumn, endRow, endColumn) ) { + this.board.grid[endRow][endColumn] = this.board.grid[startRow][startColumn]; + this.board.grid[startRow][startColumn] = null; + + if( Math.abs(endRow - startRow) == 2 ) { + let killedRow = endRow - startRow > 0 ? startRow + 1 : endRow + 1; + let killedColumn = endColumn - startColumn > 0 ? startColumn + 1 : endColumn + 1; + + this.board.grid[killedRow][killedColumn] = null; + this.board.checkers.pop(); + } + } + else console.log('Try again with a legal move.'); + } +} + +let isValidInput = function( startRow, startColumn, endRow, endColumn ) { + let validStart = ( startRow >= 0 && startRow <=8 ) && ( startColumn >= 0 && startColumn <= 8 ); + let validEnd = ( endRow >= 0 && endRow <= 8 ) && ( endColumn >= 0 && endColumn <= 8 ); + return ( validStart && validEnd ); +} + +let isLegal = function( startRow, startColumn, endRow, endColumn ) { + let rowDifference = Math.abs( endRow - startRow ); + let columnDifference = Math.abs( endColumn - startColumn ); + if( (rowDifference == 1 && columnDifference == 1) || (rowDifference == 2 && columnDifference == 2) ) return true; + else return false; } function getPrompt() { From ac7b8eef5e244565bb83e3a4605f70b46aec6b22 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Tue, 12 Feb 2019 20:08:43 -0600 Subject: [PATCH 4/5] did some stuff --- 05week/checkers.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/05week/checkers.js b/05week/checkers.js index 05b65c9ed..ead3afdfc 100644 --- a/05week/checkers.js +++ b/05week/checkers.js @@ -11,7 +11,7 @@ const rl = readline.createInterface({ function Checker( color ) { // Your code here // color = 'white' ? this.symbol = String.fromCharCode(0x125CB) : this.symbol = String.fromCharCode(0x125CF); - if(color == 'white') this.symbol = String.fromCharCode(0x125CF) + if( color == 'white' ) this.symbol = String.fromCharCode(0x125CF) else this.symbol = String.fromCharCode(0x125CB); } @@ -114,6 +114,8 @@ class Game { this.board.grid[killedRow][killedColumn] = null; this.board.checkers.pop(); + // let removeChecker = this.board.checkers.indexOf(this.grid[killedRow][killedColumn]); + // this.board.checkers.splice(removeChecker, 1); } } else console.log('Try again with a legal move.'); From b58d29e9343a4dc72b8d8d26dcc7f16410e69cbf Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Tue, 12 Feb 2019 20:42:08 -0600 Subject: [PATCH 5/5] minor change splicing specifically killed checker out of checkers array, instead of randomly popping off the last item --- 05week/checkers.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/05week/checkers.js b/05week/checkers.js index ead3afdfc..789d563e6 100644 --- a/05week/checkers.js +++ b/05week/checkers.js @@ -113,12 +113,11 @@ class Game { let killedColumn = endColumn - startColumn > 0 ? startColumn + 1 : endColumn + 1; this.board.grid[killedRow][killedColumn] = null; - this.board.checkers.pop(); - // let removeChecker = this.board.checkers.indexOf(this.grid[killedRow][killedColumn]); - // this.board.checkers.splice(removeChecker, 1); + let removeChecker = this.board.checkers.indexOf( this.board.grid[killedRow][killedColumn] ); + this.board.checkers.splice( removeChecker, 1 ); } } - else console.log('Try again with a legal move.'); + else console.log( 'Try again with a legal move.' ); } }