From 98636a6142fcb2417339c438b09990ba5c70f1b4 Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Tue, 25 Jul 2017 07:00:43 -0500 Subject: [PATCH 1/4] adding my tic tac toe game --- 02week/ticTacToe.js | 73 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index 1abf5b900..a9fdfb18c 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -24,23 +24,75 @@ function printBoard() { } function horizontalWin() { - // Your code here + return board[0].every(square => square === playerTurn) || board[1].every(square => square === playerTurn) || board[2].every(square => square === playerTurn); } function verticalWin() { - // Your code here + return [board[0][0], board[1][0], board[2][0]].every(square => square === playerTurn) || [board[0][1], board[1][1], board[2][1]].every(square => square === playerTurn) || [board[0][2], board[1][2], board[2][2]].every(square => square === playerTurn); } function diagonalWin() { - // Your code here + return [board[0][0], board[1][1], board[2][2]].every(square => square === playerTurn) || [board[0][2], board[1][1], board[2][0]].every(square => square === playerTurn); } function checkForWin() { - // Your code here + if (horizontalWin()) { + printBoard(); + console.log(`Congratulations player ${playerTurn}. You notched a horizontal win`); + return true; + } else if (verticalWin()) { + printBoard(); + console.log(`Congratulations player ${playerTurn}. You notched a vertical win`); + return true; + } else if (diagonalWin()) { + printBoard(); + console.log(`Congratulations player ${playerTurn}. You notched a diagonal win`); + return true; + } + return false; +} + +function checkForTie() { + return board.every(square => square.trim() !== ''); } function ticTacToe(row, column) { - // Your code here + // Program will control player turns using X's and O's + // Based on row and column, populate array position in board + // Test for 0, 1, 2 indexes. User cannot enter anything else. + // Make sure square is available. + // Now check for a win. Can be horizontal, vertical or diagonal. + // If no one wins, switch to the next player and repeat. + + const validValue = (myIndex) => { + const valuesArr = [0,1,2]; + return valuesArr.some(validIndex => myIndex == validIndex); + } + + if (validValue(row) && validValue(column)) { // This test makes sure values entered are 0, 1, 2 and nothing else. + if (!board[row][column].trim() ) { // This test makes sure the square is empty. + board[row][column] = playerTurn; // set the square equal to the current player. + + if (!checkForWin()) { // checkForWin returns TRUE if someone won. It returns FALSE if no one has won yet. + + // Ok no one has won yet. + // this logic controls who's turn it is. + if (playerTurn === 'X') { + playerTurn = 'O'; + } else { + playerTurn = 'X'; + } + } else { + console.log(`Yes we have a winner folks... player ${playerTurn}. Start a new game`); + return true; // returning true ends the game. We have a winner. + } + } else { + console.log('Hey, that square is already filled in. Select another'); + } + } else { + console.log('Please enter a valid index. Valid values are 0, 1, 2'); + } + return false; // returning false keeps the game going. } function getPrompt() { @@ -48,15 +100,18 @@ function getPrompt() { console.log("It's Player " + playerTurn + "'s turn."); rl.question('row: ', (row) => { rl.question('column: ', (column) => { - ticTacToe(row, column); - getPrompt(); + // I wrapped ticTacToe around a condition so I can "end" the game. ticTacToe returns TRUE if someone won the game and + // therefore the game is over. It returns FALSE if the game is still going on. + if (!ticTacToe(row, column)) { + getPrompt(); + } else { + process.exit(0); // this command exits the Program + } }); }); } - - // Tests if (typeof describe === 'function') { From 05eab53d0729d5bc24240b38207713f0dddb49ee Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Tue, 25 Jul 2017 07:23:02 -0500 Subject: [PATCH 2/4] added a tie check. More comments --- 02week/ticTacToe.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index a9fdfb18c..ff2ae2f8c 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -37,15 +37,15 @@ function diagonalWin() { function checkForWin() { if (horizontalWin()) { - printBoard(); + printBoard(); // refreshes the screen and shows the last play prior to the message below. console.log(`Congratulations player ${playerTurn}. You notched a horizontal win`); return true; } else if (verticalWin()) { - printBoard(); + printBoard(); // refreshes the screen and shows the last play prior to the message below. console.log(`Congratulations player ${playerTurn}. You notched a vertical win`); return true; } else if (diagonalWin()) { - printBoard(); + printBoard(); // refreshes the screen and shows the last play prior to the message below. console.log(`Congratulations player ${playerTurn}. You notched a diagonal win`); return true; } @@ -53,7 +53,7 @@ function checkForWin() { } function checkForTie() { - return board.every(square => square.trim() !== ''); + return board[0].every(square => square.trim() !== '') && board[1].every(square => square.trim() !== '') && board[2].every(square => square.trim() !== ''); } function ticTacToe(row, column) { @@ -73,8 +73,14 @@ function ticTacToe(row, column) { if (!board[row][column].trim() ) { // This test makes sure the square is empty. board[row][column] = playerTurn; // set the square equal to the current player. - if (!checkForWin()) { // checkForWin returns TRUE if someone won. It returns FALSE if no one has won yet. - + if (checkForWin()) { // checkForWin returns TRUE if someone won. It returns FALSE if no one has won yet. + console.log(`Yes we have a winner folks... player ${playerTurn}. Start a new game`); + return true; // returning true ends the game. We have a winner. + } else if (checkForTie()) { + printBoard(); // refreshes the screen and shows the last play prior to the message below. + console.log(`Yes we have a tie folks. No one won. Start a new game`); + return true; // returning true ends the game. We have a winner. + } else { // Ok no one has won yet. // this logic controls who's turn it is. if (playerTurn === 'X') { @@ -82,9 +88,6 @@ function ticTacToe(row, column) { } else { playerTurn = 'X'; } - } else { - console.log(`Yes we have a winner folks... player ${playerTurn}. Start a new game`); - return true; // returning true ends the game. We have a winner. } } else { console.log('Hey, that square is already filled in. Select another'); @@ -100,8 +103,8 @@ function getPrompt() { console.log("It's Player " + playerTurn + "'s turn."); rl.question('row: ', (row) => { rl.question('column: ', (column) => { - // I wrapped ticTacToe around a condition so I can "end" the game. ticTacToe returns TRUE if someone won the game and - // therefore the game is over. It returns FALSE if the game is still going on. + // I wrapped ticTacToe function around a condition so I can "end" the game. ticTacToe returns TRUE if someone won the game + // or there is a tie. The game is over. It returns FALSE if the game is still going on. if (!ticTacToe(row, column)) { getPrompt(); } else { From 4d1c007769fcf9518a200751a8062b1e7f2e870f Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Thu, 27 Jul 2017 05:27:19 -0500 Subject: [PATCH 3/4] Adding project 1 tests. TTT added in previous commit --- 02week/project1.js | 108 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 02week/project1.js diff --git a/02week/project1.js b/02week/project1.js new file mode 100644 index 000000000..fd45da6e4 --- /dev/null +++ b/02week/project1.js @@ -0,0 +1,108 @@ +'use strict'; + +console.log('Project 1'); +console.log(''); +console.log(''); + +// Test 1 - Print out array length +console.log('Test 1 - array length'); +let cars = ['Ford','Chevy','Chrysler','Cadillac']; +console.log(cars.length); +console.log(''); + +// Test 2 - Use concat method to combine 2 arrays. +console.log('Test 2 - using concat'); +let moreCars = ['BMW','Mercedes','Toyota','Honda']; +let totalCars = cars.concat(moreCars); +console.log(totalCars); +console.log(''); + +// Test 3 - Using indexOf and lastIndexOf +console.log('Test 3 - using indexOf and lastIndexOf'); +console.log(`Index for Honda is ${totalCars.indexOf('Honda')}`); +console.log(`Index for Ford is ${totalCars.lastIndexOf('Ford')}`); +console.log(''); + +// Test 4 - Using join +console.log('Test 4 - using join'); +const stringOfCars = totalCars.join(', '); +console.log(stringOfCars); +console.log(''); + +// Test 5 - Using split +console.log('Test 5 - using split'); +totalCars = stringOfCars.split(', '); +console.log(totalCars); +console.log(''); + +// Test 6 - Using reverse +console.log('Test 6 - using reverse'); +const carsInReverse = totalCars.reverse(); +console.log(carsInReverse); +console.log(''); + + +// Test 7 - Using sort +console.log('Test 7 - using sort'); +console.log(carsInReverse.sort()); +// alert(`Does BMW have first position in array? BMW index = ${carsInReverse.indexOf('BMW')}`); +console.log(`Does BMW have first position in array? BMW index = ${carsInReverse.indexOf('BMW')}`); +console.log(''); + + +// Test 8 - Using slice +console.log('Test 8 - Using slice'); +const removedCars = carsInReverse.slice(4,-2); +console.log(carsInReverse); // still has the values +console.log(removedCars); +console.log(''); + + +// Test 9 - Using splice +console.log('Test 9 - using splice'); +console.log(carsInReverse.splice(1, 2, 'Ford', 'Honda')); +console.log(carsInReverse); +console.log(''); + + +// Test 10 - Using push +console.log('Test 10 - using push'); +carsInReverse.push('Cadillac', 'Chevy'); +console.log(carsInReverse); +console.log(''); + + +// Test 11 - Using pop +console.log('Test 11 - using pop'); +console.log(`The last entry removed: ${carsInReverse.pop()}`); +console.log(`The last car in the array now: ${carsInReverse[carsInReverse.length-1]}`); +console.log(''); + + +// Test 12 - Using shift +console.log('Test 12 - using shift'); +console.log(`The first entry removed: ${carsInReverse.shift()}`); +console.log(`The first car in the array now: ${carsInReverse[0]}`); +console.log(''); + + +// Test 13 - Using unshift +console.log('Test 13 - using unshift'); +carsInReverse.unshift('Buick'); +console.log(carsInReverse); +console.log(''); + + +// Test 14 - Use For Each to add 2 to each element in array. +console.log('Test 14 - using forEach'); +function addTwo() { + + let numbers = [23, 45, 0, 2]; + console.log(numbers); + numbers.forEach((theNum, index) => { + numbers[index] = theNum + 2}); + console.log(' adding 2...'); + console.log(numbers); + +} +addTwo(); From 74e22ca622dac135b13766ec2e1754c44d2eaff3 Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Tue, 15 Aug 2017 06:31:02 -0500 Subject: [PATCH 4/4] Made improvements... Used ternary operator for turn control. Better way to check for tie. --- 02week/ticTacToe.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index ff2ae2f8c..32a64d982 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -13,6 +13,7 @@ let board = [ ]; let playerTurn = 'X'; +let turnCount = 0; function printBoard() { console.log(' 0 1 2'); @@ -52,10 +53,6 @@ function checkForWin() { return false; } -function checkForTie() { - return board[0].every(square => square.trim() !== '') && board[1].every(square => square.trim() !== '') && board[2].every(square => square.trim() !== ''); -} - function ticTacToe(row, column) { // Program will control player turns using X's and O's // Based on row and column, populate array position in board @@ -72,22 +69,18 @@ function ticTacToe(row, column) { if (validValue(row) && validValue(column)) { // This test makes sure values entered are 0, 1, 2 and nothing else. if (!board[row][column].trim() ) { // This test makes sure the square is empty. board[row][column] = playerTurn; // set the square equal to the current player. - + turnCount++; if (checkForWin()) { // checkForWin returns TRUE if someone won. It returns FALSE if no one has won yet. console.log(`Yes we have a winner folks... player ${playerTurn}. Start a new game`); return true; // returning true ends the game. We have a winner. - } else if (checkForTie()) { + } else if (turnCount === 9) { printBoard(); // refreshes the screen and shows the last play prior to the message below. console.log(`Yes we have a tie folks. No one won. Start a new game`); return true; // returning true ends the game. We have a winner. } else { // Ok no one has won yet. // this logic controls who's turn it is. - if (playerTurn === 'X') { - playerTurn = 'O'; - } else { - playerTurn = 'X'; - } + playerTurn === 'X'? playerTurn = 'O' : playerTurn = 'X'; } } else { console.log('Hey, that square is already filled in. Select another');