From dea1d524d741297cb058488c4a89c07d6a3703cf Mon Sep 17 00:00:00 2001 From: Dane Middleton Date: Thu, 3 May 2018 17:11:18 -0500 Subject: [PATCH 1/3] whiteboarding details and completed mark on the board and alternate between players. stuck on win functions --- 03week/ticTacToe.js | 68 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 10 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 1abf5b900..9f8cb5c9e 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,51 @@ 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 horizontalWin() { - // Your code here + if ((board[0][0] === playerTurn && board[0][1] === playerTurn && board[0][2] === playerTurn) || + (board[1][0] === playerTurn && board[1][1] === playerTurn && board[1][2] === playerTurn) || + (board[2][0] === playerTurn && board[2][1] === playerTurn && board[2][2] === playerTurn)) { + return true; + } } function verticalWin() { - // Your code here + if ((board[0][0] === playerTurn && board[1][0] === playerTurn && board[2][0] === PlayerTurn) || + (board[0][1] === playerTurn && board[1][1] === playerTurn && board[2][1] === playerTurn) || + (board[0][2] === playerTurn && board[1][2] === playerTurn && board[2][2] === playerTurn)) { + return true; + } } function diagonalWin() { - // Your code here + if ((board[0][0] === playerTurn && board[1][1] === playerTurn && board[2][2] === PlayerTurn) || + (board[0][2] === playerTurn && board[1][1] === playerTurn && board[2][0] === playerTurn)) { + return true; + } } + function checkForWin() { - // Your code here + if (horizontalWin() || verticalWin() || diagonalWin()) { + return true; + 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() { @@ -64,22 +92,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', () => { From c40f66ba550436cdd34ba23a4d9057debe07ce57 Mon Sep 17 00:00:00 2001 From: Dane Middleton Date: Thu, 3 May 2018 18:52:07 -0500 Subject: [PATCH 2/3] update --- 03week/ticTacToe.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 9f8cb5c9e..2b5e0ef8a 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -13,7 +13,7 @@ let board = [ ]; let playerTurn = 'X'; -let counter = 0 +let counter = 0; function printBoard() { console.log(' 0 1 2'); @@ -69,6 +69,8 @@ function ticTacToe(row, column) { playerTurn = 'O', board[row][column] = 'X' } + + } function getPrompt() { From 2f1e2653ecf490b90ffb0c6615f8db22b0bcf8b6 Mon Sep 17 00:00:00 2001 From: Dane Middleton Date: Tue, 15 May 2018 17:43:54 -0500 Subject: [PATCH 3/3] refactored horizontal win --- 03week/ticTacToe.js | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 2b5e0ef8a..805fdcdc3 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -26,35 +26,24 @@ function printBoard() { // 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() { - if ((board[0][0] === playerTurn && board[0][1] === playerTurn && board[0][2] === playerTurn) || - (board[1][0] === playerTurn && board[1][1] === playerTurn && board[1][2] === playerTurn) || - (board[2][0] === playerTurn && board[2][1] === playerTurn && board[2][2] === playerTurn)) { - return true; - } + return ((board[0].every(checkMark)) || + (board[1].every(checkMark)) || + (board[2].every(checkMark))) } function verticalWin() { - if ((board[0][0] === playerTurn && board[1][0] === playerTurn && board[2][0] === PlayerTurn) || - (board[0][1] === playerTurn && board[1][1] === playerTurn && board[2][1] === playerTurn) || - (board[0][2] === playerTurn && board[1][2] === playerTurn && board[2][2] === playerTurn)) { - return true; - } -} -function diagonalWin() { - if ((board[0][0] === playerTurn && board[1][1] === playerTurn && board[2][2] === PlayerTurn) || - (board[0][2] === playerTurn && board[1][1] === playerTurn && board[2][0] === playerTurn)) { - return true; - } } - function checkForWin() { - if (horizontalWin() || verticalWin() || diagonalWin()) { - return true; + if (horizontalWin() ) { + console.log(playerTurn + " Wins"); } } @@ -69,7 +58,7 @@ function ticTacToe(row, column) { playerTurn = 'O', board[row][column] = 'X' } - + } @@ -79,6 +68,7 @@ function getPrompt() { rl.question('row: ', (row) => { rl.question('column: ', (column) => { ticTacToe(row, column); + checkForWin(); getPrompt(); }); });