From f051b15d06109e9785051e200c60907f88ce211a Mon Sep 17 00:00:00 2001 From: aguevara1 Date: Tue, 31 Jul 2018 03:07:08 -0500 Subject: [PATCH 1/5] started on program --- 03week/ticTacToe.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 1abf5b900..e0433ca14 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -13,7 +13,7 @@ let board = [ ]; let playerTurn = 'X'; - +//join() joins all elements of array into a string and returns string function printBoard() { console.log(' 0 1 2'); console.log('0 ' + board[0].join(' | ')); @@ -39,8 +39,28 @@ function checkForWin() { // Your code here } +// check if input is vali + function isValid(row1,column1){ + const possibleNumbers=[0,1,2]; + return possibleNumbers.indexOf(parseInt(row1,10)) !==-1 && possibleNumbers.indexOf(parseInt(column1,10))!== -1; + + +} + + + function ticTacToe(row, column) { - // Your code here + // check for valid input + + if(isValid(row,column)){ + board[row][column]='X'; + + + } else{ + console.log("Enter numbers 0-2 only!!!!!!!!"); + } + + } function getPrompt() { From fafe9666c8ef362449135f1c693758caf86a239a Mon Sep 17 00:00:00 2001 From: aguevara1 Date: Tue, 31 Jul 2018 17:15:26 -0500 Subject: [PATCH 2/5] kept editing --- 03week/ticTacToe.js | 63 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index e0433ca14..dc81b61d4 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -23,27 +23,53 @@ function printBoard() { console.log('2 ' + board[2].join(' | ')); } -function horizontalWin() { +function horizontalWin(playerTurn2) { // Your code here + if( board[0][0]===playerTurn2 && board[0][1]===playerTurn2 && board[0][2]===playerTurn2){ + return true; + }else if(board[1][0]===playerTurn2 && board[1][1]===playerTurn2 && board[1][2]===playerTurn2){ + return true; + }else if(board[2][0]===playerTurn2 && board[2][1]===playerTurn2 && board[2][2]===playerTurn2){ + return true; + } } -function verticalWin() { +function verticalWin(playerTurn2) { // Your code here + if( board[0][0]===playerTurn2 && board[1][0]===playerTurn2 && board[2][0]===playerTurn2){ + return true; + }else if(board[0][1]===playerTurn2 && board[1][1]===playerTurn2 && board[2][1]===playerTurn2){ + return true; + }else if(board[0][2]===playerTurn2 && board[1][2]===playerTurn2 && board[2][2]===playerTurn2){ + return true; + } } -function diagonalWin() { - // Your code here -} +function diagonalWin(playerTurn2) { + // Your code her + if( board[0][0]===playerTurn2 && board[1][1]===playerTurn2 && board[2][2]===playerTurn2){ + return true; + }else if(board[0][2]===playerTurn2 && board[1][1]===playerTurn2 && board[2][0]===playerTurn2){ + return true; + } + + } -function checkForWin() { +function checkForWin(playerTurn1) { // Your code here + if(verticalWin(playerTurn1)){ + return true; + }else if(horizontalWin(playerTurn1)){ + return true; + }else if(diagonalWin(playerTurn1)){ + return true; + } } -// check if input is vali - function isValid(row1,column1){ - const possibleNumbers=[0,1,2]; - return possibleNumbers.indexOf(parseInt(row1,10)) !==-1 && possibleNumbers.indexOf(parseInt(column1,10))!== -1; - +// check if input is valid +function isValid(row1,column1){ + const possibleNumbers=[0,1,2]; + return possibleNumbers.indexOf(parseInt(row1,10)) !==-1 && possibleNumbers.indexOf(parseInt(column1,10))!== -1; } @@ -53,7 +79,15 @@ function ticTacToe(row, column) { // check for valid input if(isValid(row,column)){ - board[row][column]='X'; + board[row][column]=playerTurn; + + if(checkForWin(playerTurn)){ + + console.log( playerTurn+' Won!!!!'); + return true; + }else{ + playerTurn= playerTurn==='X' ? playerTurn='O' : playerTurn='X'; + } } else{ @@ -61,6 +95,7 @@ function ticTacToe(row, column) { } + } function getPrompt() { @@ -75,8 +110,6 @@ function getPrompt() { } - - // Tests if (typeof describe === 'function') { @@ -92,7 +125,7 @@ if (typeof describe === 'function') { }); it('should check for vertical wins', () => { board = [ [' ', 'X', ' '], [' ', 'X', ' '], [' ', 'X', ' '] ]; - assert.equal(verticalWin(), true); + assert.equal(verticalWin(), 'true'); }); it('should check for horizontal wins', () => { board = [ ['X', 'X', 'X'], [' ', ' ', ' '], [' ', ' ', ' '] ]; From 8d220dbcd13db78dae9fee872d20d91b3f8720de Mon Sep 17 00:00:00 2001 From: aguevara1 Date: Wed, 1 Aug 2018 02:03:27 -0500 Subject: [PATCH 3/5] more and more edits --- 03week/ticTacToe.js | 142 +++++++++++++++++++++++++++++--------------- 1 file changed, 94 insertions(+), 48 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index dc81b61d4..7fe921000 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -23,74 +23,100 @@ function printBoard() { console.log('2 ' + board[2].join(' | ')); } -function horizontalWin(playerTurn2) { +function horizontalWin() { // Your code here - if( board[0][0]===playerTurn2 && board[0][1]===playerTurn2 && board[0][2]===playerTurn2){ - return true; - }else if(board[1][0]===playerTurn2 && board[1][1]===playerTurn2 && board[1][2]===playerTurn2){ - return true; - }else if(board[2][0]===playerTurn2 && board[2][1]===playerTurn2 && board[2][2]===playerTurn2){ - return true; - } +return 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; + + +/* if (board[0][0] === turn && board[0][1] === turn && board[0][2] === turn) { + return true; + } else if (board[1][0] === turn && board[1][1] === turn && board[1][2] === turn) { + return true; + } else if (board[2][0] === turn && board[2][1] === turn && board[2][2] === turn) { + return true; + } */ } -function verticalWin(playerTurn2) { +function verticalWin() { // Your code here - if( board[0][0]===playerTurn2 && board[1][0]===playerTurn2 && board[2][0]===playerTurn2){ - return true; - }else if(board[0][1]===playerTurn2 && board[1][1]===playerTurn2 && board[2][1]===playerTurn2){ - return true; - }else if(board[0][2]===playerTurn2 && board[1][2]===playerTurn2 && board[2][2]===playerTurn2){ - return true; - } + const verticalResult=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; + + console.log('Inside VerticalWin function'); + console.log(verticalResult); + return verticalResult; + // if (board[0][0] === turn && board[1][0] === turn && board[2][0] === turn) { + // return true; + // } else if (board[0][1] === turn && board[1][1] === turn && board[2][1] === turn) { + // return true; + // } else if (board[0][2] === turn && board[1][2] === turn && board[2][2] === turn) { + // return true; + // } } -function diagonalWin(playerTurn2) { +function diagonalWin() { // Your code her - if( board[0][0]===playerTurn2 && board[1][1]===playerTurn2 && board[2][2]===playerTurn2){ - return true; - }else if(board[0][2]===playerTurn2 && board[1][1]===playerTurn2 && board[2][0]===playerTurn2){ - return true; - } - - } - -function checkForWin(playerTurn1) { - // Your code here - if(verticalWin(playerTurn1)){ - return true; - }else if(horizontalWin(playerTurn1)){ + 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; + }else{ + return false; + } + /* + if (board[0][0] === turn && board[1][1] === turn && board[2][2] === turn) { return true; - }else if(diagonalWin(playerTurn1)){ + } else if (board[0][2] === turn && board[1][1] === turn && board[2][0] === turn) { return true; } +*/ +} + +function checkForWin() { + // Your code here + return verticalWin() || horizontalWin() || diagonalWin(); +/* + if (verticalWin(turn)) { + return true; + } else if (horizontalWin(turn)) { + return true; + } else if (diagonalWin(turn)) { + return true; + } +*/ } // check if input is valid -function isValid(row1,column1){ - const possibleNumbers=[0,1,2]; - return possibleNumbers.indexOf(parseInt(row1,10)) !==-1 && possibleNumbers.indexOf(parseInt(column1,10))!== -1; +function isValid(row1, column1) { + const possibleNumbers = [0, 1, 2]; + return possibleNumbers.indexOf(parseInt(row1, 10)) !== -1 && possibleNumbers.indexOf(parseInt(column1, 10)) !== -1; } +function reset(){ + +} + function ticTacToe(row, column) { // check for valid input - if(isValid(row,column)){ - board[row][column]=playerTurn; + if (isValid(row, column)) { + board[row][column] = playerTurn; - if(checkForWin(playerTurn)){ + if (checkForWin()) { - console.log( playerTurn+' Won!!!!'); - return true; - }else{ - playerTurn= playerTurn==='X' ? playerTurn='O' : playerTurn='X'; + console.log(' Won!!!!'); + return true; + } else { + playerTurn === 'X' ? playerTurn = 'O' : playerTurn = 'X'; } - } else{ + } else { console.log("Enter numbers 0-2 only!!!!!!!!"); } @@ -117,22 +143,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', ' '] ]; - assert.equal(verticalWin(), 'true'); + 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 fc4ad5f0fb2b9b67d634c586781584d82c7078e1 Mon Sep 17 00:00:00 2001 From: aguevara1 Date: Thu, 2 Aug 2018 14:38:37 -0500 Subject: [PATCH 4/5] Changes Changes --- 03week/ticTacToe.js | 91 +++++++++++++++------------------------------ 1 file changed, 30 insertions(+), 61 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 7fe921000..bac86b448 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -24,104 +24,73 @@ function printBoard() { } function horizontalWin() { - // Your code here -return 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; - - -/* if (board[0][0] === turn && board[0][1] === turn && board[0][2] === turn) { - return true; - } else if (board[1][0] === turn && board[1][1] === turn && board[1][2] === turn) { - return true; - } else if (board[2][0] === turn && board[2][1] === turn && board[2][2] === turn) { - return true; - } */ + // Your code + const horizontal = [0, 1, 2]; + let result = horizontal.some(indices => { + return board[indices][0] == playerTurn && board[indices][1] == playerTurn && board[indices][2] == playerTurn + }); + + return result; } function verticalWin() { - // Your code here - const verticalResult=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; - - console.log('Inside VerticalWin function'); - console.log(verticalResult); - return verticalResult; - // if (board[0][0] === turn && board[1][0] === turn && board[2][0] === turn) { - // return true; - // } else if (board[0][1] === turn && board[1][1] === turn && board[2][1] === turn) { - // return true; - // } else if (board[0][2] === turn && board[1][2] === turn && board[2][2] === turn) { - // return true; - // } + const vertical = [0, 1, 2]; + let result1 = vertical.some(indx => { + return board[0][indx] == playerTurn && board[1][indx] == playerTurn && board[2][indx] == playerTurn + }); + + return result1; + } function diagonalWin() { - // Your code her - 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; - }else{ - return false; - } - /* - if (board[0][0] === turn && board[1][1] === turn && board[2][2] === turn) { - return true; - } else if (board[0][2] === turn && board[1][1] === turn && board[2][0] === turn) { - return true; + /* if center space is equal to playerTurn then check the other 2. + will return true if either of the 2 sets will evaluate to true + */ + if (board[1][1] == playerTurn){ + return board[0][0] == playerTurn && board[2][2] == playerTurn || + board[0][2] == playerTurn && board[2][0] == playerTurn } -*/ + } function checkForWin() { - // Your code here + /* will call each of these functions and will return true if one + of these functions returns true + */ return verticalWin() || horizontalWin() || diagonalWin(); -/* - if (verticalWin(turn)) { - return true; - } else if (horizontalWin(turn)) { - return true; - } else if (diagonalWin(turn)) { - return true; - } -*/ + } -// check if input is valid +/* check if input is valid. Input must be 1 of the possibleNumbers, if not return + falsey +*/ function isValid(row1, column1) { const possibleNumbers = [0, 1, 2]; return possibleNumbers.indexOf(parseInt(row1, 10)) !== -1 && possibleNumbers.indexOf(parseInt(column1, 10)) !== -1; } -function reset(){ - - +function reset() { } function ticTacToe(row, column) { // check for valid input - if (isValid(row, column)) { board[row][column] = playerTurn; if (checkForWin()) { - - console.log(' Won!!!!'); + console.log(playerTurn + ' Has Won The Game!!!!'); return true; } else { playerTurn === 'X' ? playerTurn = 'O' : playerTurn = 'X'; } - } else { console.log("Enter numbers 0-2 only!!!!!!!!"); } - - } function getPrompt() { From 1d65a8c94df901a4e5b7f77bd08eb13979a8f2e6 Mon Sep 17 00:00:00 2001 From: aguevara1 Date: Fri, 3 Aug 2018 00:20:17 -0500 Subject: [PATCH 5/5] With Comments and Ready to be graded --- 03week/ticTacToe.js | 57 +++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index bac86b448..3040da9da 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -12,9 +12,11 @@ let board = [ [' ', ' ', ' '] ]; +//Global variable let playerTurn = 'X'; -//join() joins all elements of array into a string and returns string -function printBoard() { + +//join() joins all elements of array into a string and prints each element of the board +const printBoard= () => { console.log(' 0 1 2'); console.log('0 ' + board[0].join(' | ')); console.log(' ---------'); @@ -23,8 +25,9 @@ function printBoard() { console.log('2 ' + board[2].join(' | ')); } -function horizontalWin() { - // Your code +/* function checks for horizontal wins. Returns true if some() function gets at least 1 element of horizontal + array to equal true for the 3 cases. If doesn't get one to return true will return falsey*/ +const horizontalWin= () => { const horizontal = [0, 1, 2]; let result = horizontal.some(indices => { return board[indices][0] == playerTurn && board[indices][1] == playerTurn && board[indices][2] == playerTurn @@ -33,7 +36,10 @@ function horizontalWin() { return result; } -function verticalWin() { + +/* function checks for vertical wins. Returns true if some() function gets at least 1 element of vertical + array to equal true for the 3 cases. If doesn't get one to return true will return falsey*/ +const verticalWin= () => { const vertical = [0, 1, 2]; let result1 = vertical.some(indx => { return board[0][indx] == playerTurn && board[1][indx] == playerTurn && board[2][indx] == playerTurn @@ -43,43 +49,47 @@ function verticalWin() { } -function diagonalWin() { + +/* function checking for the 2 ways to win diagonally. */ +const diagonalWin= () => { /* if center space is equal to playerTurn then check the other 2. - will return true if either of the 2 sets will evaluate to true + Will return true if either of the 2 sets will evaluate to true, if not will + return falsey. */ if (board[1][1] == playerTurn){ return board[0][0] == playerTurn && board[2][2] == playerTurn || - board[0][2] == playerTurn && board[2][0] == playerTurn + board[0][2] == playerTurn && board[2][0] == playerTurn; } } -function checkForWin() { - /* will call each of these functions and will return true if one - of these functions returns true - */ - return verticalWin() || horizontalWin() || diagonalWin(); +/* will call each of these functions and will return true if one +of these functions returns true +*/ +const checkForWin= () => { + + return verticalWin() || horizontalWin() || diagonalWin(); } /* check if input is valid. Input must be 1 of the possibleNumbers, if not return falsey */ -function isValid(row1, column1) { +const isValid= (row1, column1) => { const possibleNumbers = [0, 1, 2]; return possibleNumbers.indexOf(parseInt(row1, 10)) !== -1 && possibleNumbers.indexOf(parseInt(column1, 10)) !== -1; } -function reset() { - -} - -function ticTacToe(row, column) { - // check for valid input +/* function takes in user input from 0-2. Will check if either of the players wins the +game. Will console.log which player wins the game. */ +const ticTacToe= (row, column) => { + // check for valid input. If invalid input will cosole.log message to user to enter numbers if (isValid(row, column)) { board[row][column] = playerTurn; + /* If true console.log who won the game. If falsey, will execute else statement and + switch users */ if (checkForWin()) { console.log(playerTurn + ' Has Won The Game!!!!'); return true; @@ -92,8 +102,9 @@ function ticTacToe(row, column) { } } - -function getPrompt() { +/* function prompts user to enter coordinates on board. Function will call ticTacToe function and then +call the prompt function(itself) again. */ +const getPrompt= () => { printBoard(); console.log("It's Player " + playerTurn + "'s turn."); rl.question('row: ', (row) => { @@ -105,7 +116,7 @@ function getPrompt() { } -// Tests +// Test cases if (typeof describe === 'function') {