diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 1abf5b900..805fdcdc3 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -13,6 +13,7 @@ let board = [ ]; let playerTurn = 'X'; +let counter = 0; function printBoard() { console.log(' 0 1 2'); @@ -23,24 +24,42 @@ function printBoard() { console.log('2 ' + board[2].join(' | ')); } + +// X is the first mark on the board followed by O until there is a winner or the board is filled (9 spaces on the board) for a tie game. the tictactoe function takes two arguments the first is row and second is column. board.splice[][] can change the board deleting the blank space and entering new value for the multiarray. for horizontal vertical and diagonal win functions enter the array index information for three connecting boxes with && for the boxes and || for the different winning combinations. +function checkMark(player) { + return (player === 'X' || player === 'O') +} + + function horizontalWin() { - // Your code here + return ((board[0].every(checkMark)) || + (board[1].every(checkMark)) || + (board[2].every(checkMark))) } function verticalWin() { - // Your code here -} -function diagonalWin() { - // Your code here } function checkForWin() { - // Your code here + if (horizontalWin() ) { + + console.log(playerTurn + " Wins"); + } } function ticTacToe(row, column) { - // Your code here + + counter += 1 + + if (counter % 2 === 0) { + playerTurn = 'X', board[row][column] = 'O' + } else { + playerTurn = 'O', board[row][column] = 'X' + } + + + } function getPrompt() { @@ -49,6 +68,7 @@ function getPrompt() { rl.question('row: ', (row) => { rl.question('column: ', (column) => { ticTacToe(row, column); + checkForWin(); getPrompt(); }); }); @@ -64,22 +84,42 @@ if (typeof describe === 'function') { describe('#ticTacToe()', () => { it('should place mark on the board', () => { ticTacToe(1, 1); - assert.deepEqual(board, [ [' ', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]); + assert.deepEqual(board, [ + [' ', ' ', ' '], + [' ', 'X', ' '], + [' ', ' ', ' '] + ]); }); it('should alternate between players', () => { ticTacToe(0, 0); - assert.deepEqual(board, [ ['O', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]); + assert.deepEqual(board, [ + ['O', ' ', ' '], + [' ', 'X', ' '], + [' ', ' ', ' '] + ]); }); it('should check for vertical wins', () => { - board = [ [' ', 'X', ' '], [' ', 'X', ' '], [' ', 'X', ' '] ]; + board = [ + [' ', 'X', ' '], + [' ', 'X', ' '], + [' ', 'X', ' '] + ]; assert.equal(verticalWin(), true); }); it('should check for horizontal wins', () => { - board = [ ['X', 'X', 'X'], [' ', ' ', ' '], [' ', ' ', ' '] ]; + board = [ + ['X', 'X', 'X'], + [' ', ' ', ' '], + [' ', ' ', ' '] + ]; assert.equal(horizontalWin(), true); }); it('should check for diagonal wins', () => { - board = [ ['X', ' ', ' '], [' ', 'X', ' '], [' ', ' ', 'X'] ]; + board = [ + ['X', ' ', ' '], + [' ', 'X', ' '], + [' ', ' ', 'X'] + ]; assert.equal(diagonalWin(), true); }); it('should detect a win', () => {