From dcfa1ff069703e85fb3f78f58216b06df6991c35 Mon Sep 17 00:00:00 2001 From: tonezone108 Date: Fri, 27 Sep 2019 00:40:12 -0500 Subject: [PATCH 1/5] Tic Tac Toe Html --- 03week/ticTacToe.css | 149 +++++++++++++++++++++++++ 03week/ticTacToe.html | 32 ++++++ 03week/ticTacToe.js | 248 ++++++++++++++++++++++++++++++++---------- 3 files changed, 374 insertions(+), 55 deletions(-) create mode 100644 03week/ticTacToe.css create mode 100644 03week/ticTacToe.html diff --git a/03week/ticTacToe.css b/03week/ticTacToe.css new file mode 100644 index 000000000..dc8dacf79 --- /dev/null +++ b/03week/ticTacToe.css @@ -0,0 +1,149 @@ +div { + font-family: Impact, Charcoal, sans-serif; +} + +button { + align-self: center; + width: 100px; + height: 100px; + font-family: Impact, Charcoal, sans-serif; +} + +div.zero { + grid-area: zero; + border-right: 5px solid black; + border-bottom: 5px solid black; + width: 200px; + height: 200px; + display: flex; + align-items: center; + justify-content: center; + font-size: 200px; +} + +div.one { + grid-area: one; + border-bottom: 5px solid black; + width: 200px; + height: 200px; + margin-right: -5px; + display: flex; + align-items: center; + justify-content: center; + font-size: 200px; +} + +div.two { + grid-area: two; + border-left: 5px solid black; + border-bottom: 5px solid black; + width: 200px; + height: 200px; + display: flex; + align-items: center; + justify-content: center; + font-size: 200px; +} + +div.three { + grid-area: three; + border-right: 5px solid black; + border-bottom: 5px solid black; + width: 200px; + height: 200px; + display: flex; + align-items: center; + justify-content: center; + font-size: 200px; +} + +div.four { + grid-area: four; + border-bottom: 5px solid black; + width: 200px; + height: 200px; + margin-right: -5px; + display: flex; + align-items: center; + justify-content: center; + font-size: 200px; +} + +div.five { + grid-area: five; + border-left: 5px solid black; + border-bottom: 5px solid black; + width: 200px; + height: 200px; + display: flex; + align-items: center; + justify-content: center; + font-size: 200px; +} + +div.six { + grid-area: six; + border-right: 5px solid black; + width: 200px; + height: 200px; + display: flex; + align-items: center; + justify-content: center; + font-size: 200px; +} + +div.seven { + grid-area: seven; + width: 200px; + height: 200px; + margin-right: -5px; + display: flex; + align-items: center; + justify-content: center; + font-size: 200px; +} + +div.eight { + grid-area: eight; + border-left: 5px solid black; + width: 200px; + height: 200px; + display: flex; + align-items: center; + justify-content: center; + font-size: 200px; +} + +section.board { + width: 600px; + height: 600px; + margin-top: 30px; + display: grid; + grid-auto-columns: 1fr; + grid-template-areas: + "zero one two" + "three four five" + "six seven eight"; +} + +section.bottom { + width: 600px; + height: 100px; + display: flex; + justify-content: center; + flex-direction: column; +} + +div.logo { + text-align: center; + padding-top: 20px; + padding-bottom: 20px; +} + +main { + width: 750px; + margin: auto; + display: flex; + justify-content: center; + flex-direction: column; +} diff --git a/03week/ticTacToe.html b/03week/ticTacToe.html new file mode 100644 index 000000000..2b150b371 --- /dev/null +++ b/03week/ticTacToe.html @@ -0,0 +1,32 @@ + + + + + + + TicTacToe + + + + +
+ + +
+
+
+
+
+
+
+
+
+
+
+
+ + +
+
+ + diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 1abf5b900..539ce6035 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -1,93 +1,231 @@ -'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'; +"use strict"; + +// const assert = require("assert"); +// const readline = require("readline"); +// const rl = readline.createInterface({ +// input: process.stdin, +// output: process.stdout +// }); +let board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]; //board array is defined and empty in order to receive values from the user + +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 '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(" ---------"); + console.log("1 " + board[1].join(" | ")); + console.log(" ---------"); + console.log("2 " + board[2].join(" | ")); } 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 && + 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!"); + return true; + } } 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 && + 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!"); + return true; + } } 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 && + board[2][2] == playerTurn) || + (board[2][0] == playerTurn && + board[1][1] == playerTurn && + board[0][2] == playerTurn) + ) { + printBoard(); + console.log("Player " + playerTurn + " wins!"); + return true; + } } function checkForWin() { - // Your code here -} + //checkForWin merely checks to see if horizonalWin or verticalWin or diagonal win return true, and if so checkForWin returns true or false -function ticTacToe(row, column) { - // Your code here + if (horizontalWin() || verticalWin() || diagonalWin()) { + return true; + } else { + return false; + } } -function getPrompt() { - printBoard(); - console.log("It's Player " + playerTurn + "'s turn."); - rl.question('row: ', (row) => { - rl.question('column: ', (column) => { - ticTacToe(row, column); - getPrompt(); - }); - }); - +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] !== " " && + 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 a draw!"); + return true; + } else { + return false; + } } +function ticTacToe(row, column, cellId) { + //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"); + } //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. + let cell; + cell = document.getElementById(cellId); + console.log(cell); + cell.innerText = playerTurn; + console.log("I am trying to assign value to " + cellId); + + 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. + alert("Game Over"); + 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] = " "; + + document.getElementById("00").innerText = ""; + document.getElementById("01").innerText = ""; + document.getElementById("02").innerText = ""; + document.getElementById("10").innerText = ""; + document.getElementById("11").innerText = ""; + document.getElementById("12").innerText = ""; + document.getElementById("20").innerText = ""; + document.getElementById("21").innerText = ""; + document.getElementById("22").innerText = ""; + + playerTurn = "X"; + } else if (playerTurn == "X") { + playerTurn = "O"; + } else if (playerTurn == "O") { + playerTurn = "X"; + } + } +} +function handleInput(row, column) { + let rowString; + let columnString; + let cellId; + rowString = row.toString(); + columnString = column.toString(); + cellId = rowString + columnString; + ticTacToe(row, column, cellId); +} +function reset() { + 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] = " "; + + document.getElementById("00").innerText = ""; + document.getElementById("01").innerText = ""; + document.getElementById("02").innerText = ""; + document.getElementById("10").innerText = ""; + document.getElementById("11").innerText = ""; + document.getElementById("12").innerText = ""; + document.getElementById("20").innerText = ""; + document.getElementById("21").innerText = ""; + document.getElementById("22").innerText = ""; +} // Tests -if (typeof describe === 'function') { - - describe('#ticTacToe()', () => { - it('should place mark on the board', () => { +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', () => { + 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', ' '] ]; + it("should check for vertical wins", () => { + board = [[" ", "X", " "], [" ", "X", " "], [" ", "X", " "]]; assert.equal(verticalWin(), true); }); - it('should check for horizontal wins', () => { - board = [ ['X', 'X', 'X'], [' ', ' ', ' '], [' ', ' ', ' '] ]; + it("should check for horizontal wins", () => { + board = [["X", "X", "X"], [" ", " ", " "], [" ", " ", " "]]; assert.equal(horizontalWin(), true); }); - it('should check for diagonal wins', () => { - board = [ ['X', ' ', ' '], [' ', 'X', ' '], [' ', ' ', 'X'] ]; + it("should check for diagonal wins", () => { + board = [["X", " ", " "], [" ", "X", " "], [" ", " ", "X"]]; assert.equal(diagonalWin(), true); }); - it('should detect a win', () => { + it("should detect a win", () => { assert.equal(checkForWin(), true); }); }); } else { - - getPrompt(); - + // getPrompt(); } From 222ce7ffce7061892b5d6dd8f4ef1e03cc467210 Mon Sep 17 00:00:00 2001 From: tonezone108 Date: Mon, 30 Sep 2019 13:32:52 -0500 Subject: [PATCH 2/5] Pig Latin GUI --- 03week/PigLatin.css | 61 ++++++++++++++++++++++ 03week/PigLatin.html | 25 +++++++++ 03week/PigLatin.js | 119 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 03week/PigLatin.css create mode 100644 03week/PigLatin.html create mode 100644 03week/PigLatin.js diff --git a/03week/PigLatin.css b/03week/PigLatin.css new file mode 100644 index 000000000..263157bd8 --- /dev/null +++ b/03week/PigLatin.css @@ -0,0 +1,61 @@ +body { + background: sandybrown; +} + +div.title { + display: flex; + justify-content: center; + grid-area: title; + background: red; + width: auto; + height: auto; + margin: 10px; +} + +input.input { + padding: 10px; + font-size: 25px; +} + +p { + font-size: 25px; +} + +h1 { + font-family: Arial, Helvetica, sans-serif; +} + +div.input { + display: flex; + justify-content: center; + grid-area: input; +} + +div.button { + display: flex; + justify-content: center; + grid-area: button; +} +div.output { + display: flex; + justify-content: center; + grid-area: output; +} + +#pigLatinText { + color: red; + font-family: "Times New Roman", Times, serif; + padding: 10px; + font-size: 50px; +} + +main { + display: grid; + grid-row-gap: 35px; + grid-template-columns: 1 fr; + grid-template-areas: + "title" + "input" + "button" + "output"; +} diff --git a/03week/PigLatin.html b/03week/PigLatin.html new file mode 100644 index 000000000..bdcb4e9be --- /dev/null +++ b/03week/PigLatin.html @@ -0,0 +1,25 @@ + + + + + + + Document + + + + +
+

Pig Latin Translator

+

Enter english word:

+
+ +
+
+
+ +
+ +
+ + diff --git a/03week/PigLatin.js b/03week/PigLatin.js new file mode 100644 index 000000000..6c420ebda --- /dev/null +++ b/03week/PigLatin.js @@ -0,0 +1,119 @@ +"use strict"; + +// const assert = require("assert"); +// const readline = require("readline"); +// const rl = readline.createInterface({ +// input: process.stdin, +// output: process.stdout +// }); + +//RULES OF PIG LATIN: +//if starts with a vowel add "yay" to end +//if word does not have a vowel add "ay" to the end +//if word has vowel, split word before first vowel, switch halves, and add "ay" to the end + +function pigLatin(word) { + word = word.trim().toLowerCase(); //sets word to lowercase + let vowels = ["a", "e", "i", "o", "u"]; //array is created to compare with characters of word + let i; //variables are declared to run tests + let hasvowel = false; + let substring; + + if ( + //tests to see if first character of word is vowel + word[0] == vowels[0] || + word[0] == vowels[1] || + word[0] == vowels[2] || + word[0] == vowels[3] || + word[0] == vowels[4] + ) { + word = word.concat("yay"); + hasvowel = true; + } else { + //runs if first character is not a vowerl + for (i = 0; i < word.length && hasvowel == false; i++) { + if ( + //tests to see if any character in string is a vowel + word[i] == vowels[0] || + word[i] == vowels[1] || + word[i] == vowels[2] || + word[i] == vowels[3] || + word[i] == vowels[4] + ) { + //if test returns true substring gets valued assigned and is concated to word. Word is then sliced as needed and concated with 'ay' + substring = word.substring(0, i); + word = word.concat(substring); + word = word.slice(i, word.length); + word = word.concat("ay"); + hasvowel = true; + } + } + for (i = 0; i < word.length && hasvowel == false; i++) { + if ( + //tests to see if no characters are vowels, which should run if the first two test return false. Will concate 'ay' if test returns true + word[i] !== vowels[0] && + word[i] !== vowels[1] && + word[i] !== vowels[2] && + word[i] !== vowels[3] && + word[i] !== vowels[4] + ) { + word = word.concat("ay"); + hasvowel = true; + } + } + } + //modified word gets returned + return word; + + // Your code here +} + +function getPrompt() { + rl.question("word ", answer => { + console.log(pigLatin(answer)); + getPrompt(); + }); +} + +function handleTranslate() { + console.log("I am inside handleTranslate()"); + + //1. get the input value from the inputbox + var inputBox = document.getElementById("plainText"); + console.log(inputBox); + var englishWord = inputBox.value; + console.log("english word", englishWord); + + //2. call the pigLatin function with this value + var pigLatinWord = pigLatin(englishWord); + console.log("pig latin word ", pigLatinWord); + //3. write teh result that the piglatinfunction returns to the screen + var spanElement = document.getElementById("pigLatinText"); + console.log("span element", spanElement); + spanElement.innerText = pigLatinWord; +} + +// Tests + +if (typeof describe === "function") { + describe("#pigLatin()", () => { + it("should translate a simple word", () => { + assert.equal(pigLatin("car"), "arcay"); + assert.equal(pigLatin("dog"), "ogday"); + }); + it("should translate a complex word", () => { + assert.equal(pigLatin("create"), "eatecray"); + assert.equal(pigLatin("valley"), "alleyvay"); + }); + it('should attach "yay" if word begins with vowel', () => { + assert.equal(pigLatin("egg"), "eggyay"); + assert.equal(pigLatin("emission"), "emissionyay"); + }); + it("should lowercase and trim word before translation", () => { + assert.equal(pigLatin("HeLlO "), "ellohay"); + assert.equal(pigLatin(" RoCkEt"), "ocketray"); + }); + }); +} else { + getPrompt(); +} From 664030fd826eb799cefdfc26ec3a25cd2846f6c1 Mon Sep 17 00:00:00 2001 From: tonezone108 Date: Mon, 30 Sep 2019 14:01:08 -0500 Subject: [PATCH 3/5] Rock Paper Scissors Unit tests --- 03week/rockPaperScissors.js | 78 +++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 03week/rockPaperScissors.js diff --git a/03week/rockPaperScissors.js b/03week/rockPaperScissors.js new file mode 100644 index 000000000..b8bfbc927 --- /dev/null +++ b/03week/rockPaperScissors.js @@ -0,0 +1,78 @@ +"use strict"; + +const assert = require("assert"); +const readline = require("readline"); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +function rockPaperScissors(hand1, hand2) { + // Write code here + hand1 = hand1.trim().toLowerCase(); + hand2 = hand2.trim().toLowerCase(); + + if (hand1 === hand2) { + return "It's a tie!"; + } + + if (hand1 === "rock" && hand2 === "scissors") { + return "Hand one wins!"; + } + + if (hand1 === "rock" && hand2 === "paper") { + return "Hand two wins!"; + } + + if (hand1 === "paper" && hand2 === "rock") { + return "Hand one wins!"; + } + + if (hand1 === "paper" && hand2 === "scissors") { + return "Hand two wins!"; + } + + if (hand1 === "scissors" && hand2 === "paper") { + return "Hand one wins!"; + } + + if (hand1 === "scissors" && hand2 === "rock") { + return "Hand two wins!"; + } +} + +function getPrompt() { + rl.question("hand1: ", answer1 => { + rl.question("hand2: ", answer2 => { + console.log(rockPaperScissors(answer1, answer2)); + getPrompt(); + }); + }); +} + +// Tests + +if (typeof describe === "function") { + describe("#rockPaperScissors()", () => { + it("should detect a tie", () => { + assert.equal(rockPaperScissors("rock", "rock"), "It's a tie!"); + assert.equal(rockPaperScissors("paper", "paper"), "It's a tie!"); + assert.equal(rockPaperScissors("scissors", "scissors"), "It's a tie!"); + }); + it("should detect which hand won", () => { + assert.equal(rockPaperScissors("rock", "paper"), "Hand two wins!"); + assert.equal(rockPaperScissors("rock", "scissors"), "Hand one wins!"); + assert.equal(rockPaperScissors("paper", "scissors"), "Hand two wins!"); + assert.equal(rockPaperScissors("paper", "rock"), "Hand one wins!"); + assert.equal(rockPaperScissors("scissors", "rock"), "Hand two wins!"); + assert.equal(rockPaperScissors("scissors", "paper"), "Hand one wins!"); + }); + it('should scrub input to ensure lowercase with "trim"ed whitepace', () => { + assert.equal(rockPaperScissors("rOcK", " paper "), "Hand two wins!"); + assert.equal(rockPaperScissors("Paper", "SCISSORS"), "Hand two wins!"); + assert.equal(rockPaperScissors("rock ", "sCiSsOrs"), "Hand one wins!"); + }); + }); +} else { + getPrompt(); +} From d6c82bafcce1fe573b081811b569765173e3b5bd Mon Sep 17 00:00:00 2001 From: tonezone108 Date: Tue, 8 Oct 2019 14:43:39 -0500 Subject: [PATCH 4/5] towersOfHanoi --- 03week/towersOfHanoi.js | 118 +++++++++++++++++++++++++++++++--------- 1 file changed, 92 insertions(+), 26 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 3cf6df049..62d65c89b 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -1,7 +1,7 @@ -'use strict'; +"use strict"; -const assert = require('assert'); -const readline = require('readline'); +const assert = require("assert"); +const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout @@ -13,36 +13,106 @@ let stacks = { c: [] }; +//use .pop[] to remove and .push[] to add into array. Be sure to store the values of .pop[] and .push[] into variables. + function printStacks() { console.log("a: " + stacks.a); console.log("b: " + stacks.b); console.log("c: " + stacks.c); } -function movePiece() { +function movePiece(startStack, endStack) { + //accespts startStack & endStack as input and moves it // Your code here - + let start = ""; + let end = ""; + start = startStack; + end = endStack; + let startIndex; + let endIndex; + startIndex = stacks[start].length - 1; + endIndex = stacks[end].length - 1; + let startArray; + let endArray; + startArray = stacks[start]; + endArray = stacks[end]; + // console.log("start" + start); + // console.log("startIndex" + startIndex); + // console.log("startArray" + startArray); + var moved = startArray.pop(); + + stacks[end].push(moved); } -function isLegal() { +function isLegal(startStack, endStack) { + //accept startStack & endStack and compare them, if endstack is smaller than startstack return false, legal move is if end stack // JUST THINK THROUGH SCENARIOS // Your code here - + let start = ""; + let end = ""; + start = startStack; + end = endStack; + let startIndex; + let endIndex; + startIndex = stacks[start].length - 1; + endIndex = stacks[end].length - 1; + let startArray; + let endArray; + startArray = stacks[start][startIndex]; + endArray = stacks[end][endIndex]; + + // console.log("startArray " + startArray); + // console.log("endArray " + endArray); + if (startArray < endArray) { + return true; + } else if (stacks[end].length == 0) { + return true; + } else { + return false; + } } function checkForWin() { + // is c: [4, 3, 2, 1] // Your code here - + let winStack = [4, 3, 2, 1]; + if ( + (stacks.c[0] == winStack[0] && + stacks.c[1] == winStack[1] && + stacks.c[2] == winStack[2] && + stacks.c[3] == winStack[3]) || + (stacks.b[0] == winStack[0] && + stacks.b[1] == winStack[1] && + stacks.b[2] == winStack[2] && + stacks.b[3] == winStack[3]) + ) { + printStacks(); + console.log("You win!"); + stacks = { + a: [4, 3, 2, 1], + b: [], + c: [] + }; + return true; + } else { + return false; + } } function towersOfHanoi(startStack, endStack) { + //runs the previous three functions and sees if it wins and tests passs. // Your code here - + if (isLegal(startStack, endStack)) { + movePiece(startStack, endStack); + checkForWin(); + } else { + console.log("That move is illegal, try again"); + } } function getPrompt() { printStacks(); - rl.question('start stack: ', (startStack) => { - rl.question('end stack: ', (endStack) => { + rl.question("start stack: ", startStack => { + rl.question("end stack: ", endStack => { towersOfHanoi(startStack, endStack); getPrompt(); }); @@ -51,44 +121,40 @@ function getPrompt() { // Tests -if (typeof describe === 'function') { - - describe('#towersOfHanoi()', () => { - it('should be able to move a block', () => { - towersOfHanoi('a', 'b'); +if (typeof describe === "function") { + describe("#towersOfHanoi()", () => { + it("should be able to move a block", () => { + towersOfHanoi("a", "b"); assert.deepEqual(stacks, { a: [4, 3, 2], b: [1], c: [] }); }); }); - describe('#isLegal()', () => { - it('should not allow an illegal move', () => { + describe("#isLegal()", () => { + it("should not allow an illegal move", () => { stacks = { a: [4, 3, 2], b: [1], c: [] }; - assert.equal(isLegal('a', 'b'), false); + assert.equal(isLegal("a", "b"), false); }); - it('should allow a legal move', () => { + it("should allow a legal move", () => { stacks = { a: [4, 3, 2, 1], b: [], c: [] }; - assert.equal(isLegal('a', 'c'), true); + assert.equal(isLegal("a", "c"), true); }); }); - describe('#checkForWin()', () => { - it('should detect a win', () => { + describe("#checkForWin()", () => { + it("should detect a win", () => { stacks = { a: [], b: [4, 3, 2, 1], c: [] }; assert.equal(checkForWin(), true); stacks = { a: [1], b: [4, 3, 2], c: [] }; assert.equal(checkForWin(), false); }); }); - } else { - getPrompt(); - } From 8138f746bb59ffab60a983558a4f7692be4442e3 Mon Sep 17 00:00:00 2001 From: tonezone108 Date: Wed, 16 Oct 2019 18:21:16 -0500 Subject: [PATCH 5/5] md file --- 03week/towersOfHanoi.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 03week/towersOfHanoi.md diff --git a/03week/towersOfHanoi.md b/03week/towersOfHanoi.md new file mode 100644 index 000000000..a9a59aa28 --- /dev/null +++ b/03week/towersOfHanoi.md @@ -0,0 +1,37 @@ +Firstly the program runs the prompt function that prints out the stacks and inqures the user for an input. +The prompt asks request which stack to start and then which stack to move the furthest most number or 'ring' like in the actual game. +The stacks that are selected get stored into local variables called startStack and endStack. +These two variables are called into the constructor of the towersOfHanoi function. + +The towersOfHanoi function takes in the startStack and endStack variables and immediately runs a if statement that tests the two variables. +The if statement takes startStack and endStack and runs them through the constructor of the isLegal function. + +Weather the isLegal function runs true or false will determine weather or not the if statement runs. +When the if statement runs positive, it will run movePiece with startStack and endStack variables in its constructor. +Once movePiece runs then checkForWin will run to determine if the game is compelted. +If, the if statement in towersOfHanoi does not run, the else statement will run, and print out in the console that the move is illegal. +getPrompt will run again and the user will have to make another move that is legal and will run the functions in the towersOfHanoi if statement. + +The isLegal takes advantage of local variables. +isLegal uses local variables start and end to store the values of endStack and startStack from the constructor. +startIndex and endIndex values are defined by stacks[start].length - 1; & stacks[end].length - 1; respectively. +The local variables startArray and endArray are defined by stacks[start][startindex]; && stacks[end][endindex]; +Then an if statement is run which compares the values of of startArray and endArray. +Following the rules of the original Towers of Hanoi game if the value of the startArray is less than the value of endArray then the statement returns true indicating that the move is legal. +Otherwise a if else statement runs to test if endArray even has a value, to which if it doesn't and is effectively empty and can allow the player to make the move, then the if else statement returns true as well, allowing the player to move the piece. +If the if statement and the if else statement are unable to return true, then an else statement returns false in the isLegal function, indicating that the players move is illegal. + +If the move is legal, then the if statement inf towersOfHanoi will pass the startStack and endStack variable into the movePiece function. +MovePiece also takes advantage of local variables to accomplish its tasks, similarly to isLegal. +TtartStack and endStack get stored into the local variables start and end. +The array startArray is defined stacks[start]; +A local variable called move is defined by startArray.pop(); , taking the last value of startArray and storing it. +Lastly stacks[end].push(moved); is run to push into the array the value of move. +This effectively removes thast entry of the array selected from stacks and pushes into the next array of stacks, selected by the user. + +After the pieces are moved checkForWin is runned to see if the last move the user has made beaten the game. +checkForWin has a locally declared array called winStack, and compares it specifically to stacks.b and stacks.c. +If all the values of stacks.b or stacks.c match up to winStack then that indicates that the user has successfully moved all the values from stacks.a to either stacks.b or stacks.c +This sends a message to the console delcaring the victory for the user, +and then resets game by reassiging all the values of stacks, to their original value. +The game effectively starts over and the program can be repeated.