Created
June 24, 2019 16:37
-
-
Save Dangeranger/1492c25e11655ece351f30d0fac1d0af to your computer and use it in GitHub Desktop.
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 gridOne = { | |
cells: [ | |
["x", "o", "o"], | |
["o", "x", "o"], | |
["x", "x", "o"], | |
], | |
column: function(number) { | |
number = number - 1; | |
return [ | |
this.cells[0][number], | |
this.cells[1][number], | |
this.cells[2][number], | |
]; | |
}, | |
row: function(number) { | |
number = number - 1; | |
return this.cells[number]; | |
}, | |
diagonal: function(leftOrRight) { | |
if (leftOrRight === "left") { | |
return [ | |
this.cells[0][0], | |
this.cells[1][1], | |
this.cells[2][2], | |
]; | |
} else { | |
return [ | |
this.cells[0][2], | |
this.cells[1][1], | |
this.cells[2][0], | |
]; | |
} | |
}, | |
}; | |
console.log({winner: winCheck(gridOne)}); | |
// a win is when all three consecutive cells have | |
// the same value, a 'run' is a collection of | |
// three cells in either a row or a column or a diagonal | |
function winCheck(board) { | |
const result = runWin(board, 'column') || runWin(board, 'row'); | |
return result; | |
} | |
function runWin(board, run) { | |
console.log('rows or columns'); | |
const allCells = [ | |
board[run](1), board[run](2), board[run](3), | |
]; | |
let winOrLoss = false; | |
for (let index = 0; index < allCells.length; index++) { | |
const theRun = allCells[index]; | |
const mark = theRun[0]; | |
winOrLoss = theRun.every(function(move) { | |
return move === mark; | |
}); | |
if (winOrLoss) { return winOrLoss; } | |
} | |
return winOrLoss; | |
} | |
function rowWin(board) { | |
const allRows = [ | |
board.row(1), board.row(2), board.row(3), | |
]; | |
let winOrLoss = false; | |
for (let index = 0; index < allRows.length; index++) { | |
const theRow = allRows[index]; | |
const mark = theRow[0]; | |
winOrLoss = theRow.every(function(move) { | |
return move === mark; | |
}); | |
if (winOrLoss) { return winOrLoss; } | |
} | |
return winOrLoss; | |
} | |
function columnWin(board) { | |
const allColumns = [ | |
board.column(1), board.column(2), board.column(3), | |
]; | |
let winOrLoss = false; | |
for (let index = 0; index < allColumns.length; index++) { | |
const theColumn = allColumns[index]; | |
const mark = theColumn[0]; | |
winOrLoss = theColumn.every(function(move) { | |
return move === mark; | |
}); | |
if (winOrLoss) { return winOrLoss; } | |
} | |
return winOrLoss; | |
} | |
function arrayCompare(first, second) { | |
if (first.length !== second.length) { return false; } | |
for (let index = 0; index < first.length; index++) { | |
const firstValue = first[index]; | |
const secondValue = second[index]; | |
if (firstValue !== secondValue) { return false; } | |
} | |
return true; | |
} | |
function assertTrue(message, value) { | |
if (typeof value === 'function') { | |
value = value(); | |
} | |
if (value) { | |
console.log(`Success: ${message}`); | |
return true; | |
} else { | |
console.log(`Failure: ${message}`); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment