Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions lib/Board.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -12,44 +12,73 @@ 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;
}
}

// FIX THIS RED BLUE
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
module.exports = Board;
95 changes: 63 additions & 32 deletions lib/Game.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -51,22 +52,45 @@ 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();
} else {
this.blue = new Blue(this.context);
this.blue.draw();
}
this.checkMaxCheckers();

}

gameLoop () {
Expand All @@ -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
module.exports = Game;
17 changes: 10 additions & 7 deletions lib/Red.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 = [];
Expand All @@ -31,4 +34,4 @@ class Red {
}
}

module.exports = Red
module.exports = Red;
55 changes: 20 additions & 35 deletions test/game-test.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down