// ===================== Init: =====================
/* Config input schema: {
"rows": 6,
"cols": 7,
"moves": [0,0,1,1,2,2,3]
}*/
var ConnectFour = function(config, placeholderId){
this.rows = config.rows;
this.cols = config.cols;
this.winner = false;
var gameBoard = document.createElement('div');
gameBoard.class = 'gameBoard-render';
this.content = gameBoard;
this.virtualBoard = [];
this.placeHolder = document.getElementById(placeholderId);
this.buildGameBoard();
this.movesLog = [];
this.moveQueue = config.moves
this.doMoves();
}
// ===================== Actions: =====================
ConnectFour.prototype.doMoves = function(){
for(var i in this.moveQueue){
var currentMove = this.moveQueue[i];
this.doMove(currentMove, i);
}
}
//Didn't plan out virtual board correctly, have to use moveCount to determine player
ConnectFour.prototype.doMove = function(col, moveCount){
var row = this.rows
var player = moveCount%2 ? 2 : 1;
//Count backwards down row count to find the next possible move.
for(var i = this.rows-1; i > 1; i--){
if(this.virtualBoard[i][col] === false){
//this is bad, need player classes
this.virtualBoard[i][col] = player;
//Can get away with even vs odd movecount to determine player for now
//Player class should be added ASAP.
this.renderMove({row: i, col: col, player : player});
//break out of the loop, move was found
break;
} else {
//this square is occupied, move up another row
continue;
}
}
//check winCondition (Note, win is only possible after 7 moves)
if(moveCount >= 6){
this.checkWinCondition(player);
}
if(this.winner){
moveCount++
console.log('player '+player+' won! '+moveCount+' pieces played.');
}
//return this for logging - it is not used by the main program
return this.virtualBoard;
}
ConnectFour.prototype.checkWinCondition = function(player){
//check horizontal
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.