From ff6c0b6b756cbd44430b4c02628bc86cca509fae Mon Sep 17 00:00:00 2001 From: TEldridge Date: Thu, 16 Jul 2020 11:57:42 -0500 Subject: [PATCH 1/3] made Start here --- main.js | 1 + 1 file changed, 1 insertion(+) diff --git a/main.js b/main.js index 2994e56..8822e1c 100644 --- a/main.js +++ b/main.js @@ -9,6 +9,7 @@ const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); +//start here// // creates and empty "board" for the user to see where marks can be placed. // using let because the variable is expected to change with more 'X's and 'O's to add From 6fdfbe5577dc08d528a194c1c5807728c11074a3 Mon Sep 17 00:00:00 2001 From: TEldridge Date: Thu, 16 Jul 2020 16:37:26 -0500 Subject: [PATCH 2/3] five passing, fixing detect win --- main.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index 8822e1c..d6f6a67 100644 --- a/main.js +++ b/main.js @@ -22,6 +22,16 @@ let board = [ // assigns the first mark as 'X' // using let because the variable is expected to change from 'X' to 'O' and back let playerTurn = 'X'; +let turn = 0; + + +const changeMarker = () => { + if(playerTurn === "X"){ + playerTurn = "O" + } else { + playerTurn = "X" + } +} // is a function that print the current status of the board using the variable - board const printBoard = () => { @@ -34,26 +44,104 @@ const printBoard = () => { } const horizontalWin = () => { + if((board[0][0] == "X" && board[0][1] == "X" && board[0][2] == "X") + || (board[0][0] == "O" && board[0][1] == "O" && board[0][2] == "O")){ + return true + } else if ((board[1][0] == "X" && board[1][1] == "X" && board[1][2] == "X") + || (board[1][0] == "O" && board[1][1] == "O" && boar0d[1][2] == "O")){ + return true + } else if((board[2][0] == "X" && board[2][1] == "X" && board[2][2] == "X") + || (board[2][0] == "O" && board[2][1] == "O" && board[2][2] == "O")){ + return true + } else { + return false + } + + // it('should check for horizontal wins', () => { + // board = [ ['X', 'X', 'X'], [' ', ' ', ' '], [' ', ' ', ' '] ]; + // assert.equal(horizontalWin(), true); + // }); // Your code here to check for horizontal wins } const verticalWin = () => { + if((board[0][0] == "X" && board[1][0] == "X" && board[2][0] == "X") + || (board[0][0] == "O" && board[1][0] == "O" && board[2][0] == "O")){ + return true + }else if((board[0][1] == "X" && board[1][1] == "X" && board[2][1] == "X") + || (board[0][1] == "O" && board[1][1] == "O" && board[2][1] == "O")){ + return true + } else if((board[0][2] == "X" && board[1][2] == "X" && board[2][2] == "X") + || (board[0][2] == "O" && board[1][2] == "O" && board[2][2] == "O")){ + return true +} else { + return false +} + + // it('should check for vertical wins', () => { + // board = [ [' ', 'X', ' '], [' ', 'X', ' '], [' ', 'X', ' '] ]; + // assert.equal(verticalWin(), true); + // }); // Your code here to check for vertical wins } const diagonalWin = () => { + if((board[0][0] == "X" && board[1][1] == "X" && board[2][2] == "X") + || (board[0][0] == "O" && board[1][1] == "O" && board[2][2] == "O")){ + return true + }else if((board[0][2] == "X" && board[1][1] == "X" && board[2][1] == "X") + || (board[0][1] == "O" && board[1][1] == "O" && board[2][1] == "O")){ + return true + } else { + return false + } + // it('should check for diagonal wins', () => { + // board = [ ['X', ' ', ' '], [' ', 'X', ' '], [' ', ' ', 'X'] ]; + // assert.equal(diagonalWin(), true); + // }); // Your code here to check for diagonal wins } -const checkForWin = () => { - // Your code here call each of the check for types of wins -} const ticTacToe = (row, column) => { // Your code here to place a marker on the board + board[row][column] = playerTurn + changeMarker() + turn += 1; + if (turn >= 5){ + checkForWin() + }else { + turn +=1 + } + + +} + // it('should place mark on the board', () => { + // ticTacToe(1, 1); + // assert.deepEqual(board, [ [' ', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]); + // }); + // it('should alternate between players', () => { + // ticTacToe(0, 0); + // assert.deepEqual(board, [ ['O', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]); + // }); + + + + + // then check for a win + + + + +const checkForWin = () => { + // it('should detect a win', () => { + // assert.equal(checkForWin(), true); + // }); + // Your code here call each of the check for types of wins } + const getPrompt = () => { printBoard(); console.log("It's Player " + playerTurn + "'s turn."); From c3cec1bb2023a3fc24b44d301c4ad1de4ffacfcd Mon Sep 17 00:00:00 2001 From: TEldridge Date: Fri, 17 Jul 2020 12:41:25 -0500 Subject: [PATCH 3/3] passing with winner --- main.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/main.js b/main.js index d6f6a67..b50e121 100644 --- a/main.js +++ b/main.js @@ -103,17 +103,17 @@ const diagonalWin = () => { } + const ticTacToe = (row, column) => { // Your code here to place a marker on the board board[row][column] = playerTurn + changeMarker() - turn += 1; - if (turn >= 5){ + if (turn >=4){ checkForWin() - }else { - turn +=1 + } else { + turn++; } - } // it('should place mark on the board', () => { @@ -125,7 +125,14 @@ const ticTacToe = (row, column) => { // assert.deepEqual(board, [ ['O', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]); // }); - + const checkForWin = () => { + if(horizontalWin() || verticalWin() || diagonalWin()){ + console.log(`Player ${playerTurn} won!`) + return true + } else { + ticTacToe() + } + @@ -134,7 +141,7 @@ const ticTacToe = (row, column) => { -const checkForWin = () => { + // it('should detect a win', () => { // assert.equal(checkForWin(), true); // }); @@ -181,6 +188,11 @@ if (typeof describe === 'function') { assert.equal(diagonalWin(), true); }); it('should detect a win', () => { + ticTacToe(0, 0) + ticTacToe(0, 1) + ticTacToe(1, 1) + ticTacToe(0, 2) + ticTacToe(2, 2) assert.equal(checkForWin(), true); }); });