From 17711ab8d615b69bdb88edd662f137dff6f00eb2 Mon Sep 17 00:00:00 2001 From: tonezone108 Date: Tue, 24 Sep 2019 21:51:08 -0500 Subject: [PATCH 1/3] tic tac toe --- 02week/ticTacToe.js | 214 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 214 insertions(+) create mode 100644 02week/ticTacToe.js diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js new file mode 100644 index 000000000..d3cd7aaec --- /dev/null +++ b/02week/ticTacToe.js @@ -0,0 +1,214 @@ +"use strict"; + +const assert = require("assert"); +const readline = require("readline"); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); +let board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]; + +let playerTurn = "X"; + +function printBoard() { + console.log(" 0 1 2"); + console.log("0 " + board[0].join(" | ")); + console.log(" ---------"); + console.log("1 " + board[1].join(" | ")); + console.log(" ---------"); + console.log("2 " + board[2].join(" | ")); +} + +function horizontalWin() { + // Your code here + if ( + (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) + ) { + printBoard(); + console.log("Player " + playerTurn + " wins!"); + playerTurn = "X"; + board[0][0] = " "; + board[0][1] = " "; + board[0][2] = " "; + board[1][0] = " "; + board[1][1] = " "; + board[1][2] = " "; + board[2][0] = " "; + board[2][1] = " "; + board[2][2] = " "; + return true; + } +} + +function verticalWin() { + // Your code here + if ( + (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) + ) { + printBoard(); + console.log("Player " + playerTurn + " wins!"); + playerTurn = "X"; + board[0][0] = " "; + board[0][1] = " "; + board[0][2] = " "; + board[1][0] = " "; + board[1][1] = " "; + board[1][2] = " "; + board[2][0] = " "; + board[2][1] = " "; + board[2][2] = " "; + return true; + } +} + +function diagonalWin() { + // Your code here + if ( + (board[0][0] == playerTurn && + board[1][1] == playerTurn && + board[2][2] == playerTurn) || + (board[2][0] == playerTurn && + board[1][1] == playerTurn && + board[0][2] == playerTurn) + ) { + printBoard(); + console.log("Player " + playerTurn + " wins!"); + playerTurn = "X"; + board[0][0] = " "; + board[0][1] = " "; + board[0][2] = " "; + board[1][0] = " "; + board[1][1] = " "; + board[1][2] = " "; + board[2][0] = " "; + board[2][1] = " "; + board[2][2] = " "; + return true; + } +} + +function checkForWin() { + // Your code here + var horizontalWin = horizontalWin(); + var verticalWin = verticalWin(); + var diagonalWin = diagonalWin(); + + if (horizontalWin || verticalWin || diagonalWin) { + return true; + } + + if ( + board[0][0] !== " " && + board[0][1] !== " " && + board[0][2] !== " " && + board[1][0] !== " " && + board[1][1] !== " " && + board[1][2] !== " " && + board[2][0] !== " " && + board[2][1] !== " " && + board[2][2] !== " " + ) { + printBoard(); + console.log("Its draw!"); + playerTurn = "X"; + board[0][0] = " "; + board[0][1] = " "; + board[0][2] = " "; + board[1][0] = " "; + board[1][1] = " "; + board[1][2] = " "; + board[2][0] = " "; + board[2][1] = " "; + board[2][2] = " "; + } +} + +function ticTacToe(row, column) { + // Your code here + if (row > 2 || column > 2) { + console.log("Cordiantes must be between 0 and 2. Try again."); + } else if (board[row][column] == " ") { + board[row][column] = playerTurn; + var checkForWin = checkForWin(); + if (playerTurn == "X" && checkForWin !== true) { + playerTurn = "O"; + } else { + playerTurn = "X"; + } + } else { + console.log("That spot is taken pick another one"); + } +} + +function getPrompt() { + printBoard(); + console.log("It's Player " + playerTurn + "'s turn."); + rl.question("row: ", row => { + rl.question("column: ", column => { + ticTacToe(row, column); + + getPrompt(); + }); + }); +} + +// Tests + +if (typeof describe === "function") { + describe("#ticTacToe()", () => { + 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", " "], + [" ", " ", " "] + ]); + }); + it("should check for vertical wins", () => { + board = [[" ", "X", " "], [" ", "X", " "], [" ", "X", " "]]; + assert.equal(verticalWin(), true); + }); + it("should check for horizontal wins", () => { + board = [["X", "X", "X"], [" ", " ", " "], [" ", " ", " "]]; + assert.equal(horizontalWin(), true); + }); + it("should check for diagonal wins", () => { + board = [["X", " ", " "], [" ", "X", " "], [" ", " ", "X"]]; + assert.equal(diagonalWin(), true); + }); + it("should detect a win", () => { + assert.equal(checkForWin(), true); + }); + }); +} else { + getPrompt(); +} + +//trim down methods +//make new ones as necessary +//have methods return true and store them in variables to test if statements. +//run it all in tic-tac-toe, have a checkforwin and checkfordraw variable / method to know when to clear the board. From b633d3c08b3923ffadc96622919587b55daac687 Mon Sep 17 00:00:00 2001 From: tonezone108 Date: Wed, 25 Sep 2019 13:41:12 -0500 Subject: [PATCH 2/3] tic tac toe done --- 02week/ticTacToe.js | 82 +++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 48 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index d3cd7aaec..29ea839d9 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -34,16 +34,6 @@ function horizontalWin() { ) { printBoard(); console.log("Player " + playerTurn + " wins!"); - playerTurn = "X"; - board[0][0] = " "; - board[0][1] = " "; - board[0][2] = " "; - board[1][0] = " "; - board[1][1] = " "; - board[1][2] = " "; - board[2][0] = " "; - board[2][1] = " "; - board[2][2] = " "; return true; } } @@ -63,16 +53,6 @@ function verticalWin() { ) { printBoard(); console.log("Player " + playerTurn + " wins!"); - playerTurn = "X"; - board[0][0] = " "; - board[0][1] = " "; - board[0][2] = " "; - board[1][0] = " "; - board[1][1] = " "; - board[1][2] = " "; - board[2][0] = " "; - board[2][1] = " "; - board[2][2] = " "; return true; } } @@ -89,30 +69,24 @@ function diagonalWin() { ) { printBoard(); console.log("Player " + playerTurn + " wins!"); - playerTurn = "X"; - board[0][0] = " "; - board[0][1] = " "; - board[0][2] = " "; - board[1][0] = " "; - board[1][1] = " "; - board[1][2] = " "; - board[2][0] = " "; - board[2][1] = " "; - board[2][2] = " "; return true; } } function checkForWin() { // Your code here - var horizontalWin = horizontalWin(); - var verticalWin = verticalWin(); - var diagonalWin = diagonalWin(); + // var horizontalWin = horizontalWin(); + // var verticalWin = verticalWin(); + // var diagonalWin = diagonalWin(); - if (horizontalWin || verticalWin || diagonalWin) { + if (horizontalWin() || verticalWin() || diagonalWin()) { return true; + } else { + return false; } +} +function checkForDraw() { if ( board[0][0] !== " " && board[0][1] !== " " && @@ -125,17 +99,10 @@ function checkForWin() { board[2][2] !== " " ) { printBoard(); - console.log("Its draw!"); - playerTurn = "X"; - board[0][0] = " "; - board[0][1] = " "; - board[0][2] = " "; - board[1][0] = " "; - board[1][1] = " "; - board[1][2] = " "; - board[2][0] = " "; - board[2][1] = " "; - board[2][2] = " "; + console.log("Its a draw!"); + return true; + } else { + return false; } } @@ -143,12 +110,31 @@ function ticTacToe(row, column) { // Your code here if (row > 2 || column > 2) { console.log("Cordiantes must be between 0 and 2. Try again."); + } else if (row.length > 1 || column.length > 1) { + console.log("Single digits please"); } else if (board[row][column] == " ") { board[row][column] = playerTurn; - var checkForWin = checkForWin(); - if (playerTurn == "X" && checkForWin !== true) { + // var checkForWin = checkForWin(); + if (checkForWin() || checkForDraw()) { + board[0][0] = " "; + board[0][1] = " "; + board[0][2] = " "; + board[1][0] = " "; + board[1][1] = " "; + board[1][2] = " "; + board[2][0] = " "; + board[2][1] = " "; + board[2][2] = " "; + playerTurn = "X"; + } else if ( + playerTurn == "X" && + (checkForWin() == false || checkForDraw() == false) + ) { playerTurn = "O"; - } else { + } else if ( + playerTurn == "O" && + (checkForWin() == false || checkForDraw() == true) + ) { playerTurn = "X"; } } else { From 0ff1ca1870c8573e389e9481c187ee762c4a476d Mon Sep 17 00:00:00 2001 From: tonezone108 Date: Wed, 25 Sep 2019 14:09:57 -0500 Subject: [PATCH 3/3] tic tac toe done --- 02week/ticTacToe.js | 49 ++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index 29ea839d9..48ed2064d 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -6,11 +6,12 @@ const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); -let board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]; +let board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]; //board array is defined and empty in order to receive values from the user let playerTurn = "X"; function printBoard() { + // function 'prints' out board but is really just priting out the values of the board array and a few '-''s and '|' to build a board console.log(" 0 1 2"); console.log("0 " + board[0].join(" | ")); console.log(" ---------"); @@ -20,7 +21,7 @@ function printBoard() { } function horizontalWin() { - // Your code here + //checks to see if board array values satisfy a horizonal win, print out board, announce winning player, and return true if ( (board[0][0] == playerTurn && board[0][1] == playerTurn && @@ -39,7 +40,7 @@ function horizontalWin() { } function verticalWin() { - // Your code here + //checks to see if board array values satisfy a vertical win, print out board, announce winning player, and return true if ( (board[0][0] == playerTurn && board[1][0] == playerTurn && @@ -58,7 +59,8 @@ function verticalWin() { } function diagonalWin() { - // Your code here + //checks to see if board array values satisfy a diagnonal win, print out board, announce winning player, and return true + if ( (board[0][0] == playerTurn && board[1][1] == playerTurn && @@ -74,10 +76,7 @@ function diagonalWin() { } function checkForWin() { - // Your code here - // var horizontalWin = horizontalWin(); - // var verticalWin = verticalWin(); - // var diagonalWin = diagonalWin(); + //checkForWin merely checks to see if horizonalWin or verticalWin or diagonal win return true, and if so checkForWin returns true or false if (horizontalWin() || verticalWin() || diagonalWin()) { return true; @@ -87,6 +86,10 @@ function checkForWin() { } function checkForDraw() { + /**checkForDraw checks to see if the board is filled up with no clear winner, + * its important that its at the bottom so that the file can check for a win first before checking for a draw. + * If a draw is detected it will print the board announce the draw and return true or false + **/ if ( board[0][0] !== " " && board[0][1] !== " " && @@ -107,15 +110,19 @@ function checkForDraw() { } function ticTacToe(row, column) { - // Your code here + //This is where most of the magic happens. There is some quality control going on here. if (row > 2 || column > 2) { + //checks to see if row and column values from the user are under 2 and anounces a message. console.log("Cordiantes must be between 0 and 2. Try again."); } else if (row.length > 1 || column.length > 1) { + //checkes to see if row and column value length are over 1 character and announces a message. console.log("Single digits please"); - } else if (board[row][column] == " ") { - board[row][column] = playerTurn; - // var checkForWin = checkForWin(); + } //most of the work gets done here + else if (board[row][column] == " ") { + //checks to see if the board array cordinates are empty, everything within this else if statement will not run if that condition is not satisfied + board[row][column] = playerTurn; //assigns value if empty. if (checkForWin() || checkForDraw()) { + //after player 'move' has been made this checks to see if there is a win or a draw and restarts the game as needed. board[0][0] = " "; board[0][1] = " "; board[0][2] = " "; @@ -127,22 +134,23 @@ function ticTacToe(row, column) { board[2][2] = " "; playerTurn = "X"; } else if ( - playerTurn == "X" && - (checkForWin() == false || checkForDraw() == false) + //if there is no winner or draw it immediately goes to check weather to switch to player X or O + playerTurn == "X" ) { playerTurn = "O"; - } else if ( - playerTurn == "O" && - (checkForWin() == false || checkForDraw() == true) - ) { + } else if (playerTurn == "O") { playerTurn = "X"; } } else { console.log("That spot is taken pick another one"); + /** everything above will not run if the else if statement that tests to see if board spot is empty fails, + * the message will announce that it is full and request the player to make another move + * */ } } function getPrompt() { + //get prompt will run ticTacToe with input from the user and keep re-runing and also run the tests below printBoard(); console.log("It's Player " + playerTurn + "'s turn."); rl.question("row: ", row => { @@ -193,8 +201,3 @@ if (typeof describe === "function") { } else { getPrompt(); } - -//trim down methods -//make new ones as necessary -//have methods return true and store them in variables to test if statements. -//run it all in tic-tac-toe, have a checkforwin and checkfordraw variable / method to know when to clear the board.