diff --git a/05week/checkers.js b/05week/checkers.js index 8f33a089c..2a696d0b6 100644 --- a/05week/checkers.js +++ b/05week/checkers.js @@ -8,28 +8,46 @@ const rl = readline.createInterface({ }); -function Checker() { +class Checker { // Your code here + constructor() { + this.symbol1 = 'B'; + this.symbol2 = 'A'; + } + } -function Board() { - this.grid = []; - // creates an 8x8 array, filled with null values - this.createGrid = function() { +class Board { + constructor() { + + this.grid = []; + this.checkers = []; + + // creates an 8x8 array, filled with null values + + } + 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(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++) { // we start with our row number in our array const rowOfCheckers = [row]; @@ -38,30 +56,198 @@ 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(' '); } } + // 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 + //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(); + + 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]) + } + } + }) + + }) + + } } -function Game() { - this.board = new Board(); +class Game { + constructor() { - this.start = function() { + 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(); + // Your code here - }; + this.board.playerStartingPositions(); + + } + + 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 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]) { //. . .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.player2ClaimedPieces++; + this.board.checkers.shift(); + this.playerTurn = 'B' + this.player2IllegalMoves = 0; + + + } 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]) { //. . .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.player1ClaimedPieces++; + this.board.checkers.shift(); + this.playerTurn = 'A' + this.player1IllegalMoves = 0; + + + } + } 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.playerTurn = 'B' + 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' + this.player1IllegalMoves = 0; + + } + } else { + console.log('Unable to move.'); + + this.playerIllegalMoveCounter(whichPiece, toWhere); + this.checkForWin(); + + } + + } + 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 { + + 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() { @@ -90,8 +276,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]);