From 06dcc908dfd711957751fec82dfcae5b8b4ea4b8 Mon Sep 17 00:00:00 2001 From: walzer85 Date: Thu, 19 Oct 2017 20:25:42 -0500 Subject: [PATCH 01/12] wrote plan for horizontal, vertical, and diagonal wins --- 03week/ticTacToe.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 1abf5b900..2c5a9ca04 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -24,19 +24,19 @@ function printBoard() { } function horizontalWin() { - // Your code here + //horizontalWin(), checks if there are three X's or O's in [0][0], [0][1], [0][2], indexOf method } function verticalWin() { - // Your code here + //verticalWin(), checks if there are three X's or O'ss in [0][0], [1][0], [2][0]; [0][1], [1][1], [2][1]; [0][2], [1][2], [2][2]; indexOf method } function diagonalWin() { - // Your code here + //diagonalWin(), checks if there are three X's or O's in [0][0], [1][1], [2][2]; [0][2], [1][1], [2][0]; indexOf method } function checkForWin() { - // Your code here + //checkForWin(), check if horizontalWin, verticalWin, diagonalWin have three of the same values, if else statements } function ticTacToe(row, column) { From 307bae67470f735c7bac2576a8a39e5d24af50fb Mon Sep 17 00:00:00 2001 From: walzer85 Date: Thu, 19 Oct 2017 20:40:40 -0500 Subject: [PATCH 02/12] added most of the plan for ticTaccToe function --- 03week/ticTacToe.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 2c5a9ca04..83a3a9d26 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -40,7 +40,12 @@ function checkForWin() { } function ticTacToe(row, column) { - // Your code here + //ticTacToe(), begin the game with player X + //playerXTurn(), place an X in the column/row that player X chooses, splice method on var board + //playerOTurn(), switch to player O, place an O in the column/row that player O chooses, splice method on var board + //switch back to player X, run the same code again + //Between every turn, run checkForWin + } function getPrompt() { From 4ba3d60ac1f0f85e127a5915b90cbb355da61c96 Mon Sep 17 00:00:00 2001 From: walzer85 Date: Sat, 21 Oct 2017 09:12:14 -0500 Subject: [PATCH 03/12] added if else for indexOf to check directional wins --- 03week/ticTacToe.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 83a3a9d26..e3d8915bf 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -24,15 +24,30 @@ function printBoard() { } function horizontalWin() { - //horizontalWin(), checks if there are three X's or O's in [0][0], [0][1], [0][2], indexOf method + //horizontalWin(), checks if there are three X's or O's in [0][0], [0][1], [0][2], etc, indexOf method + if (printBoard.indexOf(printBoard) === ((([0][0]) && [0][1] && [0][2]) || ([1][0] && [1][1] && [1][2]) || ([2][0] && [2][1] && [2][2])) { + return 'You Win!' + } else { + const getPrompt; + } } function verticalWin() { - //verticalWin(), checks if there are three X's or O'ss in [0][0], [1][0], [2][0]; [0][1], [1][1], [2][1]; [0][2], [1][2], [2][2]; indexOf method + //verticalWin(), checks if there are th ree X's or O'ss in [0][0], [1][0], [2][0]; [0][1], [1][1], [2][1]; [0][2], [1][2], [2][2]; indexOf method + if (printBoard.indexOf(printBoard) === ((([0][0]) && [1][0] && [2][0]) || ([0][1] && [1][1] && [2][1]) || ([0][2] && [1][2] && [2][2])) { + return 'You Win!' + } else { + const getPrompt; + } } function diagonalWin() { //diagonalWin(), checks if there are three X's or O's in [0][0], [1][1], [2][2]; [0][2], [1][1], [2][0]; indexOf method + if (printBoard.indexOf(printBoard) === ((([0][0]) && [1][1] && [2][2]) || ([0][2] && [1][1] && [2][0])) { + return 'You Win!' + } else { + const getPrompt; + } } function checkForWin() { @@ -41,8 +56,8 @@ function checkForWin() { function ticTacToe(row, column) { //ticTacToe(), begin the game with player X - //playerXTurn(), place an X in the column/row that player X chooses, splice method on var board - //playerOTurn(), switch to player O, place an O in the column/row that player O chooses, splice method on var board + //playerXTurn(), set playerTurn to 'X' - place an X in the column/row that player X chooses, splice method on var board + //playerOTurn(), change playerTurn to 'O' - switch to player O, place an O in the column/row that player O chooses, splice method on var board //switch back to player X, run the same code again //Between every turn, run checkForWin From 755a919208abe8b370405c32bc5747b4c6516e2f Mon Sep 17 00:00:00 2001 From: walzer85 Date: Sat, 21 Oct 2017 13:31:38 -0500 Subject: [PATCH 04/12] Read more about indexOf, coming back to this later --- 03week/ticTacToe.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index e3d8915bf..a8304c2b2 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -26,7 +26,7 @@ function printBoard() { function horizontalWin() { //horizontalWin(), checks if there are three X's or O's in [0][0], [0][1], [0][2], etc, indexOf method if (printBoard.indexOf(printBoard) === ((([0][0]) && [0][1] && [0][2]) || ([1][0] && [1][1] && [1][2]) || ([2][0] && [2][1] && [2][2])) { - return 'You Win!' + return 'You Win!'; } else { const getPrompt; } @@ -35,7 +35,7 @@ function horizontalWin() { function verticalWin() { //verticalWin(), checks if there are th ree X's or O'ss in [0][0], [1][0], [2][0]; [0][1], [1][1], [2][1]; [0][2], [1][2], [2][2]; indexOf method if (printBoard.indexOf(printBoard) === ((([0][0]) && [1][0] && [2][0]) || ([0][1] && [1][1] && [2][1]) || ([0][2] && [1][2] && [2][2])) { - return 'You Win!' + return 'You Win!'; } else { const getPrompt; } @@ -44,7 +44,7 @@ function verticalWin() { function diagonalWin() { //diagonalWin(), checks if there are three X's or O's in [0][0], [1][1], [2][2]; [0][2], [1][1], [2][0]; indexOf method if (printBoard.indexOf(printBoard) === ((([0][0]) && [1][1] && [2][2]) || ([0][2] && [1][1] && [2][0])) { - return 'You Win!' + return 'You Win!'; } else { const getPrompt; } @@ -52,6 +52,9 @@ function diagonalWin() { function checkForWin() { //checkForWin(), check if horizontalWin, verticalWin, diagonalWin have three of the same values, if else statements + if (diagonalWin || verticalWin || horizontalWin) { + return 'Play again?'; + } } function ticTacToe(row, column) { @@ -60,7 +63,7 @@ function ticTacToe(row, column) { //playerOTurn(), change playerTurn to 'O' - switch to player O, place an O in the column/row that player O chooses, splice method on var board //switch back to player X, run the same code again //Between every turn, run checkForWin - + if(playerTurn = 'X') } function getPrompt() { From c9850ff8a45db21f3edbc9f88cf529876300b042 Mon Sep 17 00:00:00 2001 From: walzer85 Date: Sun, 29 Oct 2017 08:10:15 -0500 Subject: [PATCH 05/12] figureing out changing turns and check for wins in tictactoe --- 03week/ticTacToe.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index a8304c2b2..61cab6890 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -59,11 +59,13 @@ function checkForWin() { function ticTacToe(row, column) { //ticTacToe(), begin the game with player X + print(row, column) //playerXTurn(), set playerTurn to 'X' - place an X in the column/row that player X chooses, splice method on var board + let playerTurn = 'O'; //playerOTurn(), change playerTurn to 'O' - switch to player O, place an O in the column/row that player O chooses, splice method on var board //switch back to player X, run the same code again + let playerTurn = 'X'; //Between every turn, run checkForWin - if(playerTurn = 'X') } function getPrompt() { From e5cde3fa1763640b8aa73a318ee8529553f8c81e Mon Sep 17 00:00:00 2001 From: walzer85 Date: Sun, 29 Oct 2017 08:12:33 -0500 Subject: [PATCH 06/12] figureing out changing turns and check for wins in tictactoe --- 03week/ticTacToe.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 61cab6890..6a8cd42a6 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -61,11 +61,14 @@ function ticTacToe(row, column) { //ticTacToe(), begin the game with player X print(row, column) //playerXTurn(), set playerTurn to 'X' - place an X in the column/row that player X chooses, splice method on var board + checkForWin let playerTurn = 'O'; //playerOTurn(), change playerTurn to 'O' - switch to player O, place an O in the column/row that player O chooses, splice method on var board //switch back to player X, run the same code again + checkForWin let playerTurn = 'X'; //Between every turn, run checkForWin + checkForWin; } function getPrompt() { From 75c9f6a0be492a43195d4c74ce716419f6521275 Mon Sep 17 00:00:00 2001 From: walzer85 Date: Sat, 4 Nov 2017 15:29:21 -0500 Subject: [PATCH 07/12] correcting several errors from submission --- 03week/ticTacToe.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 6a8cd42a6..340e37aab 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -59,14 +59,15 @@ function checkForWin() { function ticTacToe(row, column) { //ticTacToe(), begin the game with player X - print(row, column) + const + console.log;(row, column) //playerXTurn(), set playerTurn to 'X' - place an X in the column/row that player X chooses, splice method on var board - checkForWin - let playerTurn = 'O'; + checkForWin(); + playerTurn = 'O'; //playerOTurn(), change playerTurn to 'O' - switch to player O, place an O in the column/row that player O chooses, splice method on var board //switch back to player X, run the same code again - checkForWin - let playerTurn = 'X'; + checkForWin(); + playerTurn = 'X'; //Between every turn, run checkForWin checkForWin; } From e6afa7b1045f0aa0dcd6f373f098020db5db4e6c Mon Sep 17 00:00:00 2001 From: walzer85 Date: Sun, 5 Nov 2017 12:33:18 -0600 Subject: [PATCH 08/12] understanding the logic behind calling the correct elements --- 03week/ticTacToe.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 340e37aab..4186dc4de 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -25,7 +25,7 @@ function printBoard() { function horizontalWin() { //horizontalWin(), checks if there are three X's or O's in [0][0], [0][1], [0][2], etc, indexOf method - if (printBoard.indexOf(printBoard) === ((([0][0]) && [0][1] && [0][2]) || ([1][0] && [1][1] && [1][2]) || ([2][0] && [2][1] && [2][2])) { + if ((board[0][0].trim() && board[0][0] === board[0][1] && board[0][0] === board[0][2]) || (board[1][0].trim() && board[1][0] === board[1][1] && board[1][0] === board[1][2]) || (board[2][0].trim() && board[2][0] === board[2][1] && board[2][0] === board[2][2])) { return 'You Win!'; } else { const getPrompt; @@ -34,7 +34,7 @@ function horizontalWin() { function verticalWin() { //verticalWin(), checks if there are th ree X's or O'ss in [0][0], [1][0], [2][0]; [0][1], [1][1], [2][1]; [0][2], [1][2], [2][2]; indexOf method - if (printBoard.indexOf(printBoard) === ((([0][0]) && [1][0] && [2][0]) || ([0][1] && [1][1] && [2][1]) || ([0][2] && [1][2] && [2][2])) { + if (board[0][0].trim() && board[0][0] === board[1][0] && board[0][0] === board[2][0]) || (board[0][1].trim() && board[1][1] === board[1][1] && board[2][1] === board[0][2]) || (board[0][2].trim() && board[2][0] === board[2][1] && board[2][0] === board[2][2])) { return 'You Win!'; } else { const getPrompt; @@ -44,9 +44,7 @@ function verticalWin() { function diagonalWin() { //diagonalWin(), checks if there are three X's or O's in [0][0], [1][1], [2][2]; [0][2], [1][1], [2][0]; indexOf method if (printBoard.indexOf(printBoard) === ((([0][0]) && [1][1] && [2][2]) || ([0][2] && [1][1] && [2][0])) { - return 'You Win!'; - } else { - const getPrompt; + return true; } } @@ -59,7 +57,7 @@ function checkForWin() { function ticTacToe(row, column) { //ticTacToe(), begin the game with player X - const + const console.log;(row, column) //playerXTurn(), set playerTurn to 'X' - place an X in the column/row that player X chooses, splice method on var board checkForWin(); From 394f7605476b7c3d4b8030fe28820894712c27c9 Mon Sep 17 00:00:00 2001 From: walzer85 Date: Mon, 13 Nov 2017 19:26:08 -0600 Subject: [PATCH 09/12] revamping tictactoe now that I understand a lot more about calling attributes in a function --- 03week/ticTacToe.js | 9 ++- 04week/functional-javascript/helloWorld.js | 71 +++++++++++++++++++++- 2 files changed, 73 insertions(+), 7 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 4186dc4de..4d2233838 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -24,11 +24,10 @@ function printBoard() { } function horizontalWin() { - //horizontalWin(), checks if there are three X's or O's in [0][0], [0][1], [0][2], etc, indexOf method - if ((board[0][0].trim() && board[0][0] === board[0][1] && board[0][0] === board[0][2]) || (board[1][0].trim() && board[1][0] === board[1][1] && board[1][0] === board[1][2]) || (board[2][0].trim() && board[2][0] === board[2][1] && board[2][0] === board[2][2])) { - return 'You Win!'; - } else { - const getPrompt; + //Checks for horizontalWin, joins each array into a string + //check each array in board by forEaching through them + board.foreach.join('') === 'XXX' || 'OOO' { + console.log(`player`) } } diff --git a/04week/functional-javascript/helloWorld.js b/04week/functional-javascript/helloWorld.js index adb078759..38ddc4bc9 100644 --- a/04week/functional-javascript/helloWorld.js +++ b/04week/functional-javascript/helloWorld.js @@ -1,5 +1,72 @@ +'use strict' + +//# 1 function upperCaser(input) { - return input.toUpperCase(); -} + return input.toUpperCase(); +}; module.exports = upperCaser + +//# 2 +//Looked up how to make a recusive function. Plugged the argument names into function +function repeat(operation, num) { + if (num === 0) { + return operation +} +return repeat(num — 1, num * operation) +} + } + +// Do not remove the line below +module.exports = repeat + +//# 3 +var numbers = [1, 5, 10, 15]; +var doubles = numbers.map(function(x) { + return x * 2; +}; + +//# 4 +function getShortMessages(messages) { + // SOLUTION GOES HERE + } + + module.exports = getShortMessages + +//# 5 + +//goodUsers, a list of valid users +var goodUsers = [ + { id: 1 }, + { id: 2 }, + { id: 3 } +] + +// `checkUsersValid` is the function you'll define +var testAllValid = checkUsersValid(goodUsers) + +const checkUsersValid = (goodUsers) => { + if (!testAllValid.same(goodusers)) { + console.log('None correct.'); + } else if { + (testAllValid.same(goodusers) === true { + testAllValid.every(goodusers) { + if true { + console.log ('All correct!'); + } else { + console.log('At least one user wrong.'); + } + } + } + } +}; + +//Given an Array of strings, use Array#reduce to create an object that contains the number of times each string occured in the array. Return the object directly (no need to console.log). + +function countWords(inputWords) { + var result = numbers.reduce(function(accumulator, currentValue) { + return accumulator + currentValue; + } +} + +// From b723904304ba13d27b192aedd53b67fee77dc7be Mon Sep 17 00:00:00 2001 From: walzer85 Date: Tue, 14 Nov 2017 18:57:50 -0600 Subject: [PATCH 10/12] changing my win functions to actually make sense --- 03week/ticTacToe.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 4d2233838..019ac3e71 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -26,8 +26,9 @@ function printBoard() { function horizontalWin() { //Checks for horizontalWin, joins each array into a string //check each array in board by forEaching through them + //if the array equals a string of three Xs or Os board.foreach.join('') === 'XXX' || 'OOO' { - console.log(`player`) + console.log(`You win!`) } } From 05b9ba50f060a5ec895450caebd8c1f90f329cf2 Mon Sep 17 00:00:00 2001 From: walzer85 Date: Mon, 20 Nov 2017 06:40:14 -0600 Subject: [PATCH 11/12] haven't been able to fix git issues --- 03week/ticTacToe.js | 77 ++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 019ac3e71..1bc25953a 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -15,7 +15,7 @@ let board = [ let playerTurn = 'X'; function printBoard() { - console.log(' 0 1 2'); + console.log('\n 0 1 2'); console.log('0 ' + board[0].join(' | ')); console.log(' ---------'); console.log('1 ' + board[1].join(' | ')); @@ -23,56 +23,75 @@ function printBoard() { console.log('2 ' + board[2].join(' | ')); } + +//Check for horizontal win function horizontalWin() { - //Checks for horizontalWin, joins each array into a string - //check each array in board by forEaching through them - //if the array equals a string of three Xs or Os - board.foreach.join('') === 'XXX' || 'OOO' { - console.log(`You win!`) + for (var i = 0; i < 3; i++) { + if (board[i].join('') === 'XXX' || board[i].join('') === 'OOO') { + console.log(`Player ${board[i][0]} wins!`); + return true; + } } } +//Check for vertical win function verticalWin() { - //verticalWin(), checks if there are th ree X's or O'ss in [0][0], [1][0], [2][0]; [0][1], [1][1], [2][1]; [0][2], [1][2], [2][2]; indexOf method - if (board[0][0].trim() && board[0][0] === board[1][0] && board[0][0] === board[2][0]) || (board[0][1].trim() && board[1][1] === board[1][1] && board[2][1] === board[0][2]) || (board[0][2].trim() && board[2][0] === board[2][1] && board[2][0] === board[2][2])) { - return 'You Win!'; - } else { - const getPrompt; + let vertCheck; + for (var i = 0; i < 3; i++) { + //Reset vertCheck to blank + vertCheck = ''; + for (var x = 0; x < 3; x++) { + vertCheck = vertCheck + board[x][i]; + } + if (vertCheck === 'XXX' || vertCheck === 'OOO') { + console.log(`\nPlayer ${vertCheck.charAt(0)} wins!`); + return true; + } } } +//Check for diagonal win function diagonalWin() { - //diagonalWin(), checks if there are three X's or O's in [0][0], [1][1], [2][2]; [0][2], [1][1], [2][0]; indexOf method - if (printBoard.indexOf(printBoard) === ((([0][0]) && [1][1] && [2][2]) || ([0][2] && [1][1] && [2][0])) { + //Set upLeft = to string of upper left to lower right + let upLeft = board[0][0] + board[1][1] + board[2][2]; + //Set upRight = to string of upper right to lower left + let upRight = board[0][2] + board[1][1] + board[2][0]; + //Check if upLeft or upRight are xxx or OOO + if ( + upLeft === 'XXX' || upLeft === 'OOO' || + upRight === 'XXX' || upRight === 'OOO' + ) { + //Log player win + console.log(`\nPlayer ${board[1][1]} wins!`); return true; } } +//Runs all directional win checks function checkForWin() { - //checkForWin(), check if horizontalWin, verticalWin, diagonalWin have three of the same values, if else statements - if (diagonalWin || verticalWin || horizontalWin) { - return 'Play again?'; + if ( + horizontalWin() === true || + verticalWin() === true || + diagonalWin() === true + ) { + return true; } } +//Main TTT function function ticTacToe(row, column) { - //ticTacToe(), begin the game with player X - const - console.log;(row, column) - //playerXTurn(), set playerTurn to 'X' - place an X in the column/row that player X chooses, splice method on var board - checkForWin(); - playerTurn = 'O'; - //playerOTurn(), change playerTurn to 'O' - switch to player O, place an O in the column/row that player O chooses, splice method on var board - //switch back to player X, run the same code again - checkForWin(); - playerTurn = 'X'; - //Between every turn, run checkForWin - checkForWin; + if (board[row][column] === ' ') { + board[row][column] = playerTurn; + playerTurn = (playerTurn==='X'?'O':'X'); + checkForWin(); + } else { + console.log('\nInvalid location!'); + } } function getPrompt() { printBoard(); - console.log("It's Player " + playerTurn + "'s turn."); + console.log("\nIt's player " + playerTurn + "'s turn."); rl.question('row: ', (row) => { rl.question('column: ', (column) => { ticTacToe(row, column); From 20f0b2baddb80c813a01994f16ccaf3ba4844fd6 Mon Sep 17 00:00:00 2001 From: walzer85 Date: Wed, 22 Nov 2017 23:25:59 -0600 Subject: [PATCH 12/12] trying to add an islegal function, meeting an error where it fails every time --- 03week/ticTacToe.js | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 1bc25953a..950c9874a 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -26,6 +26,9 @@ function printBoard() { //Check for horizontal win function horizontalWin() { + //loop through each row of the board and check if they have three Xs or Os. + //wanted to use forEach but couldn't figure out how to console log the correct winner + //console.log which player won for (var i = 0; i < 3; i++) { if (board[i].join('') === 'XXX' || board[i].join('') === 'OOO') { console.log(`Player ${board[i][0]} wins!`); @@ -36,6 +39,8 @@ function horizontalWin() { //Check for vertical win function verticalWin() { + //create a variable vertCheck + // let vertCheck; for (var i = 0; i < 3; i++) { //Reset vertCheck to blank @@ -44,7 +49,7 @@ function verticalWin() { vertCheck = vertCheck + board[x][i]; } if (vertCheck === 'XXX' || vertCheck === 'OOO') { - console.log(`\nPlayer ${vertCheck.charAt(0)} wins!`); + console.log(`Player ${vertCheck.charAt(0)} wins!`); return true; } } @@ -52,23 +57,22 @@ function verticalWin() { //Check for diagonal win function diagonalWin() { + // //Set upLeft = to string of upper left to lower right let upLeft = board[0][0] + board[1][1] + board[2][2]; //Set upRight = to string of upper right to lower left let upRight = board[0][2] + board[1][1] + board[2][0]; //Check if upLeft or upRight are xxx or OOO - if ( - upLeft === 'XXX' || upLeft === 'OOO' || - upRight === 'XXX' || upRight === 'OOO' - ) { - //Log player win - console.log(`\nPlayer ${board[1][1]} wins!`); - return true; + if (upLeft === 'XXX' || upLeft === 'OOO' ||upRight === 'XXX' || upRight === 'OOO') { + console.log(`\nPlayer ${board[1][1]} wins!`); + return true; } } -//Runs all directional win checks + function checkForWin() { + //checkForWin checks if any of the other win states are true + //if any of them are true, returns true if ( horizontalWin() === true || verticalWin() === true || @@ -78,6 +82,18 @@ function checkForWin() { } } +function isLegal(row, column) { + //isLegal checks if row and column equal X or O + //if row or column !=== X or 0, rerun getPrompt + //if row or column does ==== X or O, return true + if (((row === 0) || (row === 1) || (row === 2)) && ((column === 0) || (column === 1) || (column === 2))) { + true; + } else if (((row !== 0) || (row !== 1) || (row !== 2)) && ((column !== 0) || (column !== 1) || (column !== 2))) { + console.log('Please chose either 0, 1, or 2.') + return getPrompt(); + } +}; + //Main TTT function function ticTacToe(row, column) { if (board[row][column] === ' ') { @@ -94,8 +110,10 @@ function getPrompt() { console.log("\nIt's player " + playerTurn + "'s turn."); rl.question('row: ', (row) => { rl.question('column: ', (column) => { + if (isLegal(row, column) === true) { ticTacToe(row, column); getPrompt(); + } }); });