Skip to content

Instantly share code, notes, and snippets.

@brendo
Last active January 4, 2018 00:48
Show Gist options
  • Save brendo/b91a16d4a8df12233ff6933655f2efce to your computer and use it in GitHub Desktop.
Save brendo/b91a16d4a8df12233ff6933655f2efce to your computer and use it in GitHub Desktop.
// Connect 4
// 7 across, 6 down
//
// | | | | | | | |
// | | | | | | |
//
const board = [
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]
];
// width = 7
// height = 6
//
// [
// [0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0]
// ]
//
// [
// [0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 0, 0, 0],
// [1, 0, 0, 0, 0, 0, 0],
// [0, 2, 1, 0, 0, 0, 0],
// [0, 1, 2, 0, 0, 0, 0]
// ]
//
// routine is a player has a turn. In a turn a player selects a column
// routine will check each row until it hits a value other than 0
// if 0, place chip on row prior to this row
// end turn
// winner check.
// 4 consecutive numbers that are the same
// That can be a row, or it can be a column, or the diagonal.
// If 4, player who took last turn wins,
// else next player
//
// TODO: no more possible moves.
//
//
//
class Player {
constructor(id, name) {
this.id = id;
this.name = name;
}
}
const players = [
new Player(1, 'brendan'),
new Player(2, 'luke')
];
class Connect4 {
constructor(players, board) {
this.players = players;
this.board = board;
this.turns = [];
}
// Make this automated.
takeTurn(player, column) {
const { id, name } = player;
// Check that the column is a valid one.
if (column > this.board[0].length) {
console.error(`Invalid turn with ${column}. Max columns is ${this.board.length}`);
}
// Loop through board, row by row to find a place
for (let i = this.board.length - 1; i >= 0; i--) {
// No chip is placed
if (this.board[i][column] === 0) {
this.board[i][column] = id;
this.turns.push(`${name} placed chip at ${i}, ${column}`);
break;
}
}
if (this.hasWon()) {
console.log(`${name} is the winner!`);
console.log(this.turns);
} else {
console.log('next players turn');
}
console.log(this.board);
}
hasWon() {
const columns = 7;
const columnsToSkip = [];
// Get the bottom row and work up.
for (let i = this.board.length - 1; i >= 0; i--) {
// There was a zero in this column, so we don't need to check it
if (columnsToSkip.includes(i)) {
continue;
}
// start from the left column and work across.
for (let j = 0; j < 7; j++) {
// 0 here, no need to check any higher
if (this.board[i][j] === 0) {
columnsToSkip.push(i);
continue;
}
const currentPosition = this.board[i][j];
// Only check across if we are less than half the way across the board
if (j < 4) {
// across [i, j], [i, j+1], [i, j+2], [i, j+3]
if (
currentPosition === this.board[i][j+1]
&& currentPosition === this.board[i][j+2]
&& currentPosition === this.board[i][j+3]
) {
return true;
}
}
// Only check up if we're past the top of the board (or where the 4 streak would be)
if (i > 3) {
// up [i, j], [i+1, j], [i+2, j], [i+3, j]
if (
currentPosition === this.board[i-1][j]
&& currentPosition === this.board[i-2][j]
&& currentPosition === this.board[i-3][j]
) {
return true;
}
}
// diagonal [i, j], [i+1, j+1], [i+2, j+2], [i+3, j+3]
if (j < 4 && i >= 3) {
if (
(currentPosition === this.board[i-1][j+1]
&& currentPosition === this.board[i-2][j+2]
&& currentPosition === this.board[i-3][j+3])
) {
return true;
}
}
// diagonal [i, j], [i-1, j-1], [i-2, j-2], [i-3, j-3]
if (j >= 3 && i >= 3) {
if (
(currentPosition === this.board[i-1][j-1]
&& currentPosition === this.board[i-2][j-2]
&& currentPosition === this.board[i-3][j-3])
) {
return true;
}
}
}
}
return false;
}
}
const game = new Connect4(players, board);
const [ brendan, luke ] = players;
game.takeTurn(luke, 3);
game.takeTurn(brendan, 3);
game.takeTurn(brendan, 2);
game.takeTurn(luke, 2);
game.takeTurn(brendan, 1);
game.takeTurn(brendan, 1);
game.takeTurn(luke, 1);
game.takeTurn(brendan, 0);
game.takeTurn(brendan, 0);
game.takeTurn(brendan, 0);
game.takeTurn(luke, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment