Created
November 9, 2016 07:46
-
-
Save RangerDane/b48dd892c485d62b989ac1b2375b4dbe to your computer and use it in GitHub Desktop.
connect four - built in 1 hour for an interview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var GameView = require('./console_view.js'); | |
function ConnectFour() { | |
this.buildBoard(); | |
this.player = 1; | |
this.playerWon = false; | |
} | |
ConnectFour.prototype.buildBoard = function() { | |
var board = [], row; | |
for( var i = 0; i < 6; i++ ) { | |
row = []; | |
for (var j = 0; j < 7; j++) { | |
row.push(0); | |
} | |
board.push( row ); | |
} | |
this.board = board; | |
} | |
ConnectFour.prototype.placePiece = function( input ) { | |
var i = 0; | |
while ( i < 6 && this.board[i][parseInt(input)] === 0 ) { | |
i++; | |
} | |
this.board[i-1][parseInt(input)] = this.player; | |
this.switchPlayer(); | |
} | |
ConnectFour.prototype.isWon = function() { | |
var consecutive, currentChip, x, y; | |
for (var i = 0; i < 6; i++) { | |
for (var j = 0; j < 7; j++) { | |
currentChip = this.board[i][j]; | |
if ( currentChip ) { | |
// console.log("Found chip at " + j + ", " + i ); | |
[[1,0],[1,1],[0,1],[-1,1]].forEach( function( dir ) { | |
consecutive = 0; | |
y = i; | |
x = j; | |
// console.log( x + " " + y); | |
while ( (( x < 7 && x >= 0 ) && ( y < 6 && y >= 0 )) && this.board[y][x] === currentChip ) { | |
y += dir[0]; | |
x += dir[1]; | |
consecutive++; | |
// console.log("==== testing " + x + ", " + y + " consecutive: " + consecutive); | |
if ( consecutive >= 4 ) { | |
this.playerWon = true; | |
} | |
} | |
}.bind(this)); | |
if ( this.playerWon ) { | |
return true; | |
} | |
} | |
} | |
} | |
return false; | |
} | |
ConnectFour.prototype.switchPlayer = function () { | |
if ( this.player == 1 ) { | |
this.player = 2; | |
} else { | |
this.player = 1; | |
} | |
}; | |
var game = new ConnectFour(); | |
var gameView = new GameView( game ); | |
gameView.run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var readline = require('readline'); | |
var interface = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
function ConsoleView( game ) { | |
this.game = game; | |
} | |
ConsoleView.prototype.run = function() { | |
this.draw(); | |
this.prompt(); | |
} | |
ConsoleView.prototype.prompt = function() { | |
interface.question( "Player " + this.game.player + " , where? ", function( input ) { | |
this.placePiece(input); | |
if ( this.game.isWon() ) { | |
console.log("Player " + this.game.player + " has won!"); | |
process.exit(); | |
} else { | |
this.prompt(); | |
} | |
}.bind(this)); | |
} | |
ConsoleView.prototype.placePiece = function( input ) { | |
this.game.placePiece( input ); | |
this.draw(); | |
} | |
ConsoleView.prototype.draw = function() { | |
var printedLine; | |
this.game.board.forEach( function( row ) { | |
printedLine = ""; | |
row.forEach( function( square ) { | |
if ( square === 0 ) { | |
printedLine += "[ ]"; | |
} else if ( square === 1 ) { | |
printedLine += "[X]"; | |
} else { | |
printedLine += "[O]"; | |
} | |
}); | |
console.log(printedLine); | |
}); | |
} | |
module.exports = ConsoleView; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment