From b6710161cd23bdde20554332c8b921695a3bb939 Mon Sep 17 00:00:00 2001 From: deytonk Date: Thu, 19 Oct 2017 20:03:03 -0500 Subject: [PATCH 01/13] project 1, in class progress --- 02week/exercises.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 02week/exercises.js diff --git a/02week/exercises.js b/02week/exercises.js new file mode 100644 index 000000000..16bcd1f78 --- /dev/null +++ b/02week/exercises.js @@ -0,0 +1,44 @@ +"use strict"; +const cars = ['ford', 'chevy', 'mazda', 'subaru']; +console.log(cars.length); + +const moreCars = ['jeep', 'bmw', 'smart', 'honda']; + +let totalCars = cars.concat(moreCars); + +console.log(moreCars.indexOf('honda')); + +const stringOfCars = totalCars.join(', '); +console.log(stringOfCars); + +totalCars = stringOfCars.split(', '); + +let carsInReverse = totalCars.reverse(); +console.log(carsInReverse); + +carsInReverse.sort(); +console.log(carsInReverse); + +alert(carsInReverse.indexOf('benz')); + +const removedCars = carsInReverse.slice(3, 5); + +carsInReverse.splice(1, 2, 'ford', 'honda'); +console.log(carsInReverse); + +carsInReverse.push('bmw', 'chevy'); + +const deletedItem = carsInReverse.pop(); +console.log(deletedItem); + +const shiftedCar = carsInReverse.shift(); +console.log(shiftedCar); + +carsInReverse.unshift('tesla'); +console.log(carsInReverse); + +const numbers = [23, 45, 0, 2]; +numbers.forEach((item, index) => { + number[index] item + 2; +}); +console.log(numbers); From cbe971bd8ec804ab4ac7615429c0c49df9a97395 Mon Sep 17 00:00:00 2001 From: deytonk Date: Thu, 19 Oct 2017 20:11:08 -0500 Subject: [PATCH 02/13] checking new branch --- 02week/ticTacToe.js | 1 + 1 file changed, 1 insertion(+) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index 1abf5b900..272c54b56 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -41,6 +41,7 @@ function checkForWin() { function ticTacToe(row, column) { // Your code here + console.log(row, column) } function getPrompt() { From 021618f1973fa1bf0fa9ade0cbc5e742029e0020 Mon Sep 17 00:00:00 2001 From: deytonk Date: Thu, 19 Oct 2017 20:26:13 -0500 Subject: [PATCH 03/13] completed whiteboarding :sunglasses: --- 02week/ticTacToe.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index 272c54b56..df00bd3cd 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -25,23 +25,36 @@ function printBoard() { function horizontalWin() { // Your code here + // in row, column format these combinations of matching x's or o's will return a win + // 00 01 02 + // 10 11 12 + // 20 21 22 } function verticalWin() { // Your code here + // in row, column format these combinations of matching x's or o's will return a win + // 00 10 20 + // 01 11 21 + // 02 12 22 } function diagonalWin() { // Your code here + // in row, column format these combinations of matching x's or o's will return a win + // 00 11 22 + // 02 11 20 } function checkForWin() { // Your code here + // if in any of the above combinations, there are either all x's or all o's then the respective player wins } function ticTacToe(row, column) { // Your code here - console.log(row, column) + // the playerturn will start at x but will alternate between xs and os onclicks, and for each click it will checkForWin. + // if win, game winner will be announced and board will reset. } function getPrompt() { From ae5be1ed7146da7ea89b7347d32a65d63474a5a7 Mon Sep 17 00:00:00 2001 From: deytonk Date: Thu, 19 Oct 2017 21:15:22 -0500 Subject: [PATCH 04/13] changes? --- 02week/ticTacToe.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index df00bd3cd..b03d33394 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -29,6 +29,9 @@ function horizontalWin() { // 00 01 02 // 10 11 12 // 20 21 22 + if (([0,0] === [0,1] === [0,2]) || ([1,0] === [1,1] === [1,2]) || ([2,0] === [2,1] === [2,2])) { + return "Player ${} Wins!"; + } } function verticalWin() { @@ -37,6 +40,9 @@ function verticalWin() { // 00 10 20 // 01 11 21 // 02 12 22 + if (([0,0] === [1,0] === [2,0]) || ([0,1] === [1,1] === [2,1]) || ([0,2] === [1,2] === [2,2])) { + return "Player ${} Wins!"; + } } function diagonalWin() { @@ -44,11 +50,15 @@ function diagonalWin() { // in row, column format these combinations of matching x's or o's will return a win // 00 11 22 // 02 11 20 + if (([0,0] === [1,1] === [2,2]) || ([0,2] === [1,1] === [2,0])) { + return "Player ${} Wins!" + } } function checkForWin() { // Your code here // if in any of the above combinations, there are either all x's or all o's then the respective player wins + // on each click, if horizontalWin or verticalWin or diagonalWin - gameover and ${} player wins. If no win, then alternate player. } function ticTacToe(row, column) { From 291609930d226ed703c8fe346c2e3641bc7aeb99 Mon Sep 17 00:00:00 2001 From: deytonk Date: Sat, 21 Oct 2017 06:52:44 -0500 Subject: [PATCH 05/13] Combined all the winning combinations into 1 function --- 02week/ticTacToe.js | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index b03d33394..f8378fe44 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -13,6 +13,7 @@ let board = [ ]; let playerTurn = 'X'; +let win = false; function printBoard() { console.log(' 0 1 2'); @@ -23,39 +24,51 @@ function printBoard() { console.log('2 ' + board[2].join(' | ')); } -function horizontalWin() { +// function horizontalWin() { // Your code here // in row, column format these combinations of matching x's or o's will return a win // 00 01 02 // 10 11 12 // 20 21 22 - if (([0,0] === [0,1] === [0,2]) || ([1,0] === [1,1] === [1,2]) || ([2,0] === [2,1] === [2,2])) { - return "Player ${} Wins!"; - } -} + // if (([0,0] === [0,1] === [0,2]) || ([1,0] === [1,1] === [1,2]) || ([2,0] === [2,1] === [2,2])) { + // return "Player ${} Wins!"; + // } + +// } -function verticalWin() { +// function verticalWin() { // Your code here // in row, column format these combinations of matching x's or o's will return a win // 00 10 20 // 01 11 21 // 02 12 22 - if (([0,0] === [1,0] === [2,0]) || ([0,1] === [1,1] === [2,1]) || ([0,2] === [1,2] === [2,2])) { - return "Player ${} Wins!"; - } -} + // if (([0,0] === [1,0] === [2,0]) || ([0,1] === [1,1] === [2,1]) || ([0,2] === [1,2] === [2,2])) { + // return "Player ${} Wins!"; + // } +// } -function diagonalWin() { +// function diagonalWin() { // Your code here // in row, column format these combinations of matching x's or o's will return a win // 00 11 22 // 02 11 20 - if (([0,0] === [1,1] === [2,2]) || ([0,2] === [1,1] === [2,0])) { - return "Player ${} Wins!" - } + // if (([0,0] === [1,1] === [2,2]) || ([0,2] === [1,1] === [2,0])) { + // return "Player ${} Wins!" + // } +// } + +winningCombinations = () => { + [ + // horizontal wins + [0, 1, 2], [3, 4, 5], [6, 7, 8], + // verticals wins + [0, 3, 6], [1, 4, 7], [2, 5, 8], + // diagonal wins + [0, 4, 8], [2, 4, 6] + ]; } -function checkForWin() { +function checkForWin(playerTurn) { // Your code here // if in any of the above combinations, there are either all x's or all o's then the respective player wins // on each click, if horizontalWin or verticalWin or diagonalWin - gameover and ${} player wins. If no win, then alternate player. From 874621ab5e5eee10b534c014eb2f959bcf090f26 Mon Sep 17 00:00:00 2001 From: deytonk Date: Sat, 21 Oct 2017 10:40:07 -0500 Subject: [PATCH 06/13] just making a big mess of things :turtle: --- 02week/ticTacToe.js | 72 ++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index f8378fe44..2fdcdf41d 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -24,60 +24,78 @@ function printBoard() { console.log('2 ' + board[2].join(' | ')); } -// function horizontalWin() { +function horizontalWin() { // Your code here // in row, column format these combinations of matching x's or o's will return a win // 00 01 02 // 10 11 12 // 20 21 22 - // if (([0,0] === [0,1] === [0,2]) || ([1,0] === [1,1] === [1,2]) || ([2,0] === [2,1] === [2,2])) { - // return "Player ${} Wins!"; - // } - -// } + if (([0,0] === [0,1] === [0,2]) || ([1,0] === [1,1] === [1,2]) || ([2,0] === [2,1] === [2,2])) { + return `Player ${playerTurn} Wins!`; + } +} -// function verticalWin() { +function verticalWin() { // Your code here // in row, column format these combinations of matching x's or o's will return a win // 00 10 20 // 01 11 21 // 02 12 22 - // if (([0,0] === [1,0] === [2,0]) || ([0,1] === [1,1] === [2,1]) || ([0,2] === [1,2] === [2,2])) { - // return "Player ${} Wins!"; - // } -// } + if (([0,0] === [1,0] === [2,0]) || ([0,1] === [1,1] === [2,1]) || ([0,2] === [1,2] === [2,2])) { + return `Player ${playerTurn} Wins!`; + } +} -// function diagonalWin() { +function diagonalWin() { // Your code here // in row, column format these combinations of matching x's or o's will return a win // 00 11 22 // 02 11 20 - // if (([0,0] === [1,1] === [2,2]) || ([0,2] === [1,1] === [2,0])) { - // return "Player ${} Wins!" - // } -// } - -winningCombinations = () => { - [ - // horizontal wins - [0, 1, 2], [3, 4, 5], [6, 7, 8], - // verticals wins - [0, 3, 6], [1, 4, 7], [2, 5, 8], - // diagonal wins - [0, 4, 8], [2, 4, 6] - ]; + if (([0,0] === [1,1] === [2,2]) || ([0,2] === [1,1] === [2,0])) { + return `Player ${playerTurn} Wins!`; + } } +const winningCombinations = horizontalWin() || verticalWin() || diagonalWin(); + +// winningCombinations = (row, column) => { +// [ +// // horizontal wins +// [[0,0], [0,1], [0,2]], [[1,0], [1,1], [1,2]], [[2,0], [2,1], [2,2]], +// // verticals wins +// [[0,0], [1,0], [2,0]], [[0,1], [1,1], [2,1]], [[0,2], [1,2], [2,2]], +// // diagonal wins +// [[0,0], [1,1], [2,2]], [[0,2], [1,1], [2,0]]; +// ]; +// } + function checkForWin(playerTurn) { // Your code here // if in any of the above combinations, there are either all x's or all o's then the respective player wins // on each click, if horizontalWin or verticalWin or diagonalWin - gameover and ${} player wins. If no win, then alternate player. + for (var i = 0; i < winningCombinations.length; i++) { + let i, j + let count = 0; + for (let j = 0; j < winningCombinations[i].length; j++) { + if (board[winningCombinations[i][j]] === playerTurn) { + count++; + }if (count === 3) { + return true; + } + } + return false; } function ticTacToe(row, column) { // Your code here // the playerturn will start at x but will alternate between xs and os onclicks, and for each click it will checkForWin. - // if win, game winner will be announced and board will reset. + // if win, game winner will be announced and board will reset. + // if (playerTurn === 'X') { + // playTurn('O'); + // } else { + // playTurn('X'); + } + } function getPrompt() { From fb6b51e1004e85c56dfac09ad5e756c6b473cbf8 Mon Sep 17 00:00:00 2001 From: deytonk Date: Sat, 21 Oct 2017 16:34:02 -0500 Subject: [PATCH 07/13] simplified checking wins functions:coffee: --- 02week/ticTacToe.js | 46 +++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index 2fdcdf41d..c0b91d956 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -13,6 +13,7 @@ let board = [ ]; let playerTurn = 'X'; +if () let win = false; function printBoard() { @@ -30,8 +31,9 @@ function horizontalWin() { // 00 01 02 // 10 11 12 // 20 21 22 - if (([0,0] === [0,1] === [0,2]) || ([1,0] === [1,1] === [1,2]) || ([2,0] === [2,1] === [2,2])) { - return `Player ${playerTurn} Wins!`; + if ((board[0][0] === board[0][1] && board[0][1] === board[0][2]) || (board[1][0] === board[1][1] && board[1][1] === board[1][2]) || (board[2][0] === board[2][1] + && board[2][1] === board[2][2])) { + return true; } } @@ -41,8 +43,9 @@ function verticalWin() { // 00 10 20 // 01 11 21 // 02 12 22 - if (([0,0] === [1,0] === [2,0]) || ([0,1] === [1,1] === [2,1]) || ([0,2] === [1,2] === [2,2])) { - return `Player ${playerTurn} Wins!`; + if ((board[0][0] === board[1][0] && board[1][0] === board[2][0]) || (board[0][1] === board[1][1] && board[1][1] === board[2][1]) || (board[0][2] === board[1][2] + && board[1][2] === board[2][2])) { + return true; } } @@ -51,12 +54,12 @@ function diagonalWin() { // in row, column format these combinations of matching x's or o's will return a win // 00 11 22 // 02 11 20 - if (([0,0] === [1,1] === [2,2]) || ([0,2] === [1,1] === [2,0])) { - return `Player ${playerTurn} Wins!`; + if ((board[0][0] === board[1][1] && board[1][1] === board[2][2]) || (board[0][2] === board[1][1] && board[1][1] === board[2][0])) { + return true; } } -const winningCombinations = horizontalWin() || verticalWin() || diagonalWin(); +// const winningCombinations = horizontalWin() || verticalWin() || diagonalWin(); // winningCombinations = (row, column) => { // [ @@ -73,28 +76,35 @@ function checkForWin(playerTurn) { // Your code here // if in any of the above combinations, there are either all x's or all o's then the respective player wins // on each click, if horizontalWin or verticalWin or diagonalWin - gameover and ${} player wins. If no win, then alternate player. - for (var i = 0; i < winningCombinations.length; i++) { - let i, j - let count = 0; - for (let j = 0; j < winningCombinations[i].length; j++) { - if (board[winningCombinations[i][j]] === playerTurn) { - count++; - }if (count === 3) { - return true; - } + + // for (var i = 0; i < winningCombinations.length; i++) { + // let i, j + // let count = 0; + // for (let j = 0; j < winningCombinations[i].length; j++) { + // if (board[winningCombinations[i][j]] === playerTurn) { + // count++; + // }if (count === 3) { + // return true; + // } + // } + // return false; + // } + + if (horizontalWin() || verticalWin() || diagonalWin()) { + console.log(`Player ${playerTurn} Wins!`); } - return false; } function ticTacToe(row, column) { // Your code here // the playerturn will start at x but will alternate between xs and os onclicks, and for each click it will checkForWin. // if win, game winner will be announced and board will reset. + // call printBoard after every turn (push playerTurn to array) // if (playerTurn === 'X') { // playTurn('O'); // } else { // playTurn('X'); - } +// } } From 19c1491255abfd12fa22ed0a80fb59b7708afdad Mon Sep 17 00:00:00 2001 From: deytonk Date: Sat, 21 Oct 2017 17:50:19 -0500 Subject: [PATCH 08/13] first attempt at tictactoe function :fish: --- 02week/ticTacToe.js | 49 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index c0b91d956..5e27077f9 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -2,6 +2,7 @@ const assert = require('assert'); const readline = require('readline'); +// https://nodejs.org/api/readline.html const rl = readline.createInterface({ input: process.stdin, output: process.stdout @@ -13,8 +14,7 @@ let board = [ ]; let playerTurn = 'X'; -if () -let win = false; +let moveCount = 0; function printBoard() { console.log(' 0 1 2'); @@ -92,6 +92,10 @@ function checkForWin(playerTurn) { if (horizontalWin() || verticalWin() || diagonalWin()) { console.log(`Player ${playerTurn} Wins!`); + } else if (turnCount === 9) { + console.log("It's a tie!") + } else { + return false; } } @@ -106,6 +110,47 @@ function ticTacToe(row, column) { // playTurn('X'); // } + // board[row][column] = playerTurn; + // if (moveCount > 0){ + // checkForWin(); + // } + // moveCount ++; + // playerTurn = (playerTurn === "X" ? "O" : "X"); + // } + +const validMove = (row,column) => { + if (board[row][column] === ' ') { + board[row].splice(column, 1, playTurn); + } +} + + // const validValue = (myIndex) => { + // const valuesArr = [0,1,2]; + // return valuesArr.some(validIndex => myIndex == validIndex); + // } + // + // if (validValue(row) && validValue(column)) { + // if (!board[row][column].trim() ) { + // board[row][column] = playerTurn; + // + // if (!checkForWin()) { + // if (playerTurn === 'X') { + // playerTurn = 'O'; + // } else { + // playerTurn = 'X'; + // } + // return false; + // } else { + // console.log(`The winner is player ${playerTurn}. Start a new game`); + // return true; + // } + // } else { + // console.log('Please choose another square! That one is taken!'); + // } + // } else { + // console.log('Please enter a valid index. Valid values are 0, 1, 2'); + // } + } function getPrompt() { From 485ecd13afdc22ee6b3dfe5a6511b38b6eb8a8b2 Mon Sep 17 00:00:00 2001 From: deytonk Date: Sat, 21 Oct 2017 18:23:38 -0500 Subject: [PATCH 09/13] so close to working! Thank you Mark! :sunglasses: --- 02week/ticTacToe.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index 5e27077f9..df0ee832a 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -72,7 +72,7 @@ function diagonalWin() { // ]; // } -function checkForWin(playerTurn) { +function checkForWin() { // Your code here // if in any of the above combinations, there are either all x's or all o's then the respective player wins // on each click, if horizontalWin or verticalWin or diagonalWin - gameover and ${} player wins. If no win, then alternate player. @@ -104,11 +104,11 @@ function ticTacToe(row, column) { // the playerturn will start at x but will alternate between xs and os onclicks, and for each click it will checkForWin. // if win, game winner will be announced and board will reset. // call printBoard after every turn (push playerTurn to array) - // if (playerTurn === 'X') { - // playTurn('O'); - // } else { - // playTurn('X'); -// } + if (playerTurn === 'X') { + playerTurn = 'O'; + } else { + playerTurn = 'X'; + } // board[row][column] = playerTurn; // if (moveCount > 0){ @@ -118,11 +118,11 @@ function ticTacToe(row, column) { // playerTurn = (playerTurn === "X" ? "O" : "X"); // } -const validMove = (row,column) => { if (board[row][column] === ' ') { - board[row].splice(column, 1, playTurn); + board[row].splice(column, 1, playerTurn); } -} + + // const validValue = (myIndex) => { // const valuesArr = [0,1,2]; From e71a434bf7dfd35f2671e14dc560337f765f2bc6 Mon Sep 17 00:00:00 2001 From: deytonk Date: Tue, 24 Oct 2017 18:29:09 -0500 Subject: [PATCH 10/13] progress before class --- 02week/ticTacToe.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index df0ee832a..bc4fe228c 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -13,7 +13,7 @@ let board = [ [' ', ' ', ' '] ]; -let playerTurn = 'X'; +let playerTurn = 'O'; let moveCount = 0; function printBoard() { @@ -103,12 +103,16 @@ function ticTacToe(row, column) { // Your code here // the playerturn will start at x but will alternate between xs and os onclicks, and for each click it will checkForWin. // if win, game winner will be announced and board will reset. - // call printBoard after every turn (push playerTurn to array) - if (playerTurn === 'X') { - playerTurn = 'O'; - } else { - playerTurn = 'X'; - } + // call printBoard after every turn (push playerTurn to array + + + // if (checkForWin() = false) { + if (playerTurn === 'X') { + playerTurn = 'O'; + } else { + playerTurn = 'X'; + } + // } // board[row][column] = playerTurn; // if (moveCount > 0){ From 66a5fcd2782b2d6c1bfb5503faa966310cbdc100 Mon Sep 17 00:00:00 2001 From: deytonk Date: Wed, 1 Nov 2017 12:02:52 -0500 Subject: [PATCH 11/13] broke my code :( --- 02week/ticTacToe.js | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index bc4fe228c..ef494db04 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -72,6 +72,8 @@ function diagonalWin() { // ]; // } + + function checkForWin() { // Your code here // if in any of the above combinations, there are either all x's or all o's then the respective player wins @@ -90,10 +92,14 @@ function checkForWin() { // return false; // } - if (horizontalWin() || verticalWin() || diagonalWin()) { - console.log(`Player ${playerTurn} Wins!`); - } else if (turnCount === 9) { - console.log("It's a tie!") + if (moveCount === 9) { + console.log("It's a tie!"); + return true; + // reset(); + } else if (horizontalWin() || verticalWin() || diagonalWin()) { + console.log(`Player ${playerTurn} Wins!`) + return true; + // reset(); } else { return false; } @@ -103,15 +109,19 @@ function ticTacToe(row, column) { // Your code here // the playerturn will start at x but will alternate between xs and os onclicks, and for each click it will checkForWin. // if win, game winner will be announced and board will reset. - // call printBoard after every turn (push playerTurn to array - - - // if (checkForWin() = false) { - if (playerTurn === 'X') { - playerTurn = 'O'; - } else { - playerTurn = 'X'; - } + // call printBoard after every turn (push playerTurn to array) + + // if ((row === 0 || 1 || 2) && (column === 0 || 1 || 2)) { + checkForWin(); + // if (checkForWin() === false){ + if (playerTurn === 'X') { + playerTurn = 'O'; + } else { + playerTurn = 'X'; + } + moveCount++; + // } else { + // console.log("Invalid move, please try again!") // } // board[row][column] = playerTurn; From 4022ee709cdccd8ae5e2c7f81995c4fddf348f33 Mon Sep 17 00:00:00 2001 From: deytonk Date: Tue, 28 Nov 2017 08:00:24 -0600 Subject: [PATCH 12/13] Did not save from Sunday --- 02week/ticTacToe.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index ef494db04..447b36b4f 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -106,20 +106,12 @@ function checkForWin() { } function ticTacToe(row, column) { - // Your code here // the playerturn will start at x but will alternate between xs and os onclicks, and for each click it will checkForWin. // if win, game winner will be announced and board will reset. // call printBoard after every turn (push playerTurn to array) - - // if ((row === 0 || 1 || 2) && (column === 0 || 1 || 2)) { checkForWin(); - // if (checkForWin() === false){ - if (playerTurn === 'X') { - playerTurn = 'O'; - } else { - playerTurn = 'X'; - } - moveCount++; + // if ((row === 0 || 1 || 2) && (column === 0 || 1 || 2)) { + // } else { // console.log("Invalid move, please try again!") // } @@ -137,7 +129,6 @@ function ticTacToe(row, column) { } - // const validValue = (myIndex) => { // const valuesArr = [0,1,2]; // return valuesArr.some(validIndex => myIndex == validIndex); @@ -164,6 +155,12 @@ function ticTacToe(row, column) { // } else { // console.log('Please enter a valid index. Valid values are 0, 1, 2'); // } + if (playerTurn === 'X') { + playerTurn = 'O'; + } else { + playerTurn = 'X'; + } + moveCount++; } From b5b603bfd40d5df6634bb97ec938c8ede7b62f87 Mon Sep 17 00:00:00 2001 From: deytonk Date: Tue, 28 Nov 2017 11:30:39 -0600 Subject: [PATCH 13/13] Passing 5/6 tests, but forgot how to run in terminal --- 02week/ticTacToe.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index 447b36b4f..9b2d40447 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -13,7 +13,7 @@ let board = [ [' ', ' ', ' '] ]; -let playerTurn = 'O'; +let playerTurn = 'X'; let moveCount = 0; function printBoard() { @@ -74,7 +74,7 @@ function diagonalWin() { -function checkForWin() { +const checkForWin = () => { // Your code here // if in any of the above combinations, there are either all x's or all o's then the respective player wins // on each click, if horizontalWin or verticalWin or diagonalWin - gameover and ${} player wins. If no win, then alternate player. @@ -94,22 +94,23 @@ function checkForWin() { if (moveCount === 9) { console.log("It's a tie!"); - return true; + printBoard(); // reset(); } else if (horizontalWin() || verticalWin() || diagonalWin()) { console.log(`Player ${playerTurn} Wins!`) - return true; + printBoard(); // reset(); - } else { - return false; } } -function ticTacToe(row, column) { +const ticTacToe = (row, column) => { // the playerturn will start at x but will alternate between xs and os onclicks, and for each click it will checkForWin. // if win, game winner will be announced and board will reset. // call printBoard after every turn (push playerTurn to array) + checkForWin(); + + // if ((row === 0 || 1 || 2) && (column === 0 || 1 || 2)) { // } else { @@ -125,7 +126,8 @@ function ticTacToe(row, column) { // } if (board[row][column] === ' ') { - board[row].splice(column, 1, playerTurn); + board[row][column] = playerTurn; + // board[row].splice(column, 1, playerTurn); } @@ -155,13 +157,13 @@ function ticTacToe(row, column) { // } else { // console.log('Please enter a valid index. Valid values are 0, 1, 2'); // } + moveCount++; + if (playerTurn === 'X') { playerTurn = 'O'; } else { playerTurn = 'X'; } - moveCount++; - } function getPrompt() {