One of our first javascript programs was tho play around with a tic tac toe array. We needed to find if the array of a game was a win. The first function was pretty easy to think about checking the rows. The columns were going to be a little more trouble, but then I thought about not wanting to duplicate code to do the checking. I came up with the idea to transform the columns to rows and reuse the row checking function.
Below is the code that I came up with.
var all_games =
[ [ [ 'X', 'X', 'X' ],
[ 'X', 'O', 'O' ],
[ 'X', 'O', 'O' ] ],
[ [ 'X', 'X', 'X' ],
[ 'X', 'O', 'O' ],
[ 'O', 'X', 'O' ] ],
[ [ 'X', 'X', 'X' ],
[ 'X', 'O', 'O' ],
[ 'O', 'O', 'X' ] ],
[ [ 'X', 'X', 'X' ],
[ 'X', 'O', 'O' ],
[ 'O', ' ', ' ' ] ],
[ [ 'X', 'X', 'X' ],
[ 'X', 'O', 'O' ],
[ ' ', 'O', ' ' ] ],
[ [ 'X', 'X', 'X' ],
[ 'X', 'O', 'O' ],
[ ' ', ' ', 'O' ] ],
[ [ ' ', 'X', 'O' ],
[ 'X', 'O', 'O' ],
[ 'O', 'X', 'X' ] ],
[ [ 'O', 'O', 'X' ],
[ 'X', 'X', 'O' ],
[ 'O', 'X', 'X' ] ] ]
function checkRow (board){
var win = false;
board.forEach(function(row) {
if ((row[0] === row[1]) && (row[2] === row[1])) {
win = true;
}
});return win;
}
function checkCol (board) {
var col1 = [],
col2 = [],
col3 = [],
columns = [[]];
board.forEach(function(row){
col1.push (row[0]);
col2.push (row[1]);
col3.push (row[2]);
})
columns = [col1, col2, col3];
return checkRow(columns);
}
function checkDiagonal (board) {
var win = false;
if ((board[0][0] === board[1][1]) && (board[2][2] === board[1][1])) {win = true;};
if ((board[0][2] === board[1][1]) && (board[0][2] === board[1][1])) {win = true;};
return win;
}
function gameWin (board) {
if (checkCol(board)) {winStats.col++; return true;}
if (checkRow(board)) {winStats.row++; return true;}
if (checkDiagonal(board)) {winStats.diag++; return true;}
}
var y= 0,
winStats = {
row: 0,
col: 0,
diag: 0
}
;
all_games.forEach(function(board, i){
if (gameWin (board)){
y++;
console.log("WIN " + i +"----"+ y)};
})
console.log(winStats);
console.log("Total : " + (winStats.row+winStats.col+winStats.diag));