diff --git a/lib/Board.js b/lib/Board.js index 192a505..8f02ddd 100644 --- a/lib/Board.js +++ b/lib/Board.js @@ -1,6 +1,6 @@ -const Game = require('./Game.js') -const Red = require('./Red.js') -const Blue = require('./Blue.js') +const Game = require('./Game.js'); +const Red = require('./Red.js'); +const Blue = require('./Blue.js'); class Board { constructor () { @@ -12,32 +12,61 @@ class Board { [null, null, null, null, null, null, null], [null, null, null, null, null, null, null], [null, null, null, null, null, null, null] - ] + ]; + this.win = false; } updateBoard (col, color) { - let column = this.updateCol(col) - let row = this.updateRow(column, color) + let column = this.updateCol(col); + let row = this.updateRow(column, color); + + this.checkIfWin(column, row, color); return row; } + checkIfWin (column, row, color) { + this.checkHorizontalWin(column, row, color); + } + + checkHorizontalWin (column, row, color) { + // checkRight + // if slot to right is same color + // increase checkersInRow ++ + // else + // return + + for (let i = 1; i < 7; i++) { + if (this.status[i]) { + let checkersInRow = 0; + for (let j = 0; j <= 7; j++) { + if (this.status[j] === this.status[j + 1]) { + checkersInRow++; + } else { + checkersInRow = 0; + } + this.win = true; + } + } + } + } + updateCol (col) { // switch statement?? if (col === 50) { - return 0 + return 0; } else if (col === 150) { - return 1 + return 1; } else if (col === 250) { - return 2 + return 2; } else if (col === 350) { - return 3 + return 3; } else if (col === 450) { - return 4 + return 4; } else if (col === 550) { - return 5 + return 5; } else { - return 6 + return 6; } } @@ -45,11 +74,11 @@ class Board { updateRow (column, color) { for (let i = 6; i >= 1; i--) { if (this.status[i][column] === null) { - this.status[i][column] = color + this.status[i][column] = color; return i; } } } } -module.exports = Board \ No newline at end of file +module.exports = Board; \ No newline at end of file diff --git a/lib/Game.js b/lib/Game.js index ebd1263..90351a7 100644 --- a/lib/Game.js +++ b/lib/Game.js @@ -1,44 +1,45 @@ -const Board = require('./Board.js') -const Red = require('./Red.js') -const Blue = require('./Blue.js') +const Board = require('./Board.js'); +const Red = require('./Red.js'); +const Blue = require('./Blue.js'); class Game { constructor (context, canvasHeight, canvasWidth) { - this.canvasHeight = canvasHeight - this.canvasWidth = canvasWidth - this.context = context - this.gameLoop = this.gameLoop.bind(this) - this.red = new Red(context) - this.blue = new Blue(context) - this.gameBoard = new Board() - this.playerRed = true - this.checkersOnBoard = [] + this.canvasHeight = canvasHeight; + this.canvasWidth = canvasWidth; + this.context = context; + this.gameLoop = this.gameLoop.bind(this); + this.red = new Red(context); + this.blue = new Blue(context); + this.gameBoard = new Board(); + this.playerRed = true; + this.checkersOnBoard = []; + this.gameBoard.win = false; } static drawBoard (context) { for (let i = 0; i <= 600; i = i + 100) { - context.strokeStyle = 'black' - context.lineWidth = 2 - context.moveTo(i, 100) - context.lineTo(i, 700) - context.stroke() + context.strokeStyle = 'black'; + context.lineWidth = 2; + context.moveTo(i, 100); + context.lineTo(i, 700); + context.stroke(); for (let j = 0; j <= 700; j = j + 100) { - context.moveTo(0, j) - context.lineTo(700, j) - context.strokeStyle = 'black' - context.stroke() + context.moveTo(0, j); + context.lineTo(700, j); + context.strokeStyle = 'black'; + context.stroke(); } } } drawPieces () { this.checkersOnBoard.forEach(function (item) { - this.context.beginPath() - this.context.arc(item[0], item[1], 40, 0, Math.PI * 2) - this.context.fillStyle = item[2] - this.context.fill() - }.bind(this)) + this.context.beginPath(); + this.context.arc(item[0], item[1], 40, 0, Math.PI * 2); + this.context.fillStyle = item[2]; + this.context.fill(); + }.bind(this)); } checkMaxCheckers () { @@ -51,13 +52,38 @@ class Game { moveChecker (key) { if (this.playerRed) { - this.red.selectCol(key, this) + this.selectCol(key, 'red'); } else { - this.blue.selectCol(key, this) + this.selectCol(key, 'blue'); } } + // selectCol (key, color) { + // if (key === 37) { + // this[color].x -= 100; + // } else if (key === 39) { + // this[color].x += 100; + // } else if (key === 13) { + // let row = this.gameBoard.updateBoard(this[color].x, color); + // let tempArr = []; + // + // this.y = (row * 100) + 50; + // tempArr.push(row, this.y, color); + // this.checkersOnBoard.push(tempArr); + // console.log(this.checkersOnBoard); + // if (this.playerRed) { + // this.playerRed = false; + // this.switchPlayer('blue'); + // } else if (!this.playerRed) { + // this.playerRed = true; + // this.switchPlayer('red'); + // } + // } + // } + switchPlayer (player) { + console.log('switch player'); + console.log(player); if (player === 'red') { this.red = new Red(this.context); this.red.draw(); @@ -65,8 +91,6 @@ class Game { this.blue = new Blue(this.context); this.blue.draw(); } - this.checkMaxCheckers(); - } gameLoop () { @@ -79,8 +103,15 @@ class Game { } else { this.blue.draw(); } - window.requestAnimationFrame(this.gameLoop) + + window.requestAnimationFrame(this.gameLoop); + + // if (!this.gameBoard.win) { + // window.requestAnimationFrame(this.gameLoop); + // } else { + // // show win condition. even an alert + // } } } -module.exports = Game \ No newline at end of file +module.exports = Game; \ No newline at end of file diff --git a/lib/Red.js b/lib/Red.js index da7555d..99308c3 100644 --- a/lib/Red.js +++ b/lib/Red.js @@ -1,9 +1,9 @@ class Red { constructor (context, x = 350, y = 50) { - this.context = context - this.x = x - this.y = y - this.color = 'red' + this.context = context; + this.x = x; + this.y = y; + this.color = 'red'; } draw (x = 350, y = 50) { @@ -13,11 +13,14 @@ class Red { this.context.fill(); } + // selectCol should be in Game bc red selectCol (key, game) { + // selectCol (key, color) { if (key === 37) { - game.red.x -= 100 + game.red.x -= 100; + // this[color].x -= 100; } else if (key === 39) { - game.red.x += 100 + game.red.x += 100; } else if (key === 13) { let row = game.gameBoard.updateBoard(this.x, 'red'); let tempArr = []; @@ -31,4 +34,4 @@ class Red { } } -module.exports = Red \ No newline at end of file +module.exports = Red; \ No newline at end of file diff --git a/test/game-test.js b/test/game-test.js index c2a38e7..916bf99 100644 --- a/test/game-test.js +++ b/test/game-test.js @@ -1,56 +1,41 @@ -var chai = require('chai') -var assert = chai.assert +var chai = require('chai'); +var assert = chai.assert; // const Board = require('../lib/Board.js') -var Game = require('../lib/Game.js') +var Game = require('../lib/Game.js'); describe('Game', function () { + // 7 arrays with 7 rows + // defualt params + // default methods + // + it('Only have 42 Pieces on board', function () { - let game = new Game() - // let tempGameBoard = [ - // [null, null, null, null, null, 'red', null], - // ['red', 'red', 'red', 'red', 'red', 'red', 'red'], - // ['red', 'red', 'red', 'red', 'red', 'red', 'red'], - // ['red', 'red', 'red', 'red', 'red', 'red', 'red'], - // ['red', 'red', 'red', 'red', 'red', 'red', 'red'], - // ['red', 'red', 'red', 'red', 'red', 'red', 'red'], - // ['red', 'red', 'red', 'red', 'red', 'red', 'red'] - // ] - // let flattened = tempGameBoard.reduce( - // function (accumulator, currentValue) { - // return accumulator.concat(currentValue) - // }, - // [] - // ) - // - // function isNotNull (value) { - // return value !== null - // } - // - // let filtered = flattened.filter(isNotNull) + let game = new Game(); + for (let i = 0; i <= 41; i++) { - game.checkersOnBoard.push(i); + game.red.selectCol(0) } - assert.equal(game.checkersOnBoard.length, 42) + assert.equal(game.checkersOnBoard.length, 42); - }) + }); - it.skip('Only have 21 red Pieces on board', function () { + it.skip('Only have 21 red pieces on board', function () { - }) + }); - it.skip('Only have 21 blue Pieces on board', function () { + it.skip('Only have 21 blue pieces on board', function () { - }) + }); it.skip('Only 6 checkers in a column', function () { - }) + }); it.skip('Only 7 checkers in a row', function () { - }) -}) + }); +}); // Test if there is a winner // Only have 42 pieces on the board