From 64c0b18626be442253472e780a4f1a24e71ae6ef Mon Sep 17 00:00:00 2001 From: nae deshazer Date: Thu, 11 Jul 2019 20:53:49 -0500 Subject: [PATCH 1/2] checkpoint --- 03week/towersOfHanoi.js | 189 +++++++++++++++++++++++----------------- 1 file changed, 110 insertions(+), 79 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 3cf6df049..af583e30e 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -1,94 +1,125 @@ -'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 + input: process.stdin, + output: process.stdout }); let stacks = { - a: [4, 3, 2, 1], - b: [], - c: [] + a: [4, 3, 2, 1], + b: [], + c: [] }; function printStacks() { - console.log("a: " + stacks.a); - console.log("b: " + stacks.b); - console.log("c: " + stacks.c); + console.log("a: " + stacks.a); + console.log("b: " + stacks.b); + console.log("c: " + stacks.c); } -function movePiece() { - // Your code here - +function move(n, a, b, c) { + if (n > 0) { + move(n - 1, a, c, b); + console.log("Move disk from " + a + " to " + c); + move(n - 1, b, a, c); + } } +move(4, "A", "B", "C"); function isLegal() { - // Your code here - -} - -function checkForWin() { - // Your code here - + function move(n, a, b, c) { + if (n > 0) { + move(n - 1, a, c, b); + console.log("Move disk from " + a + " to " + c); + move(n - 1, b, a, c); + } + } + move(4, "A", "B", "C"); + + function checkForWin() { + stacks = { a: [], b: [4, 3, 2, 1], c: [] }; + stacks = { a: [1], b: [4, 3, 2], c: [] }; + } + + function towersOfHanoi(startStack, endStack) { + (() => { + "use strict"; + + // hanoi :: Int -> String -> String -> String -> [[String, String]] + const hanoi = (n, a, b, c) => + n + ? hanoi(n - 1, a, c, b) + .concat([[a, b]]) + .concat(hanoi(n - 1, c, b, a)) + : []; + + // show :: a -> String + const show = x => JSON.stringify(x, null, 2); + + return show( + hanoi(3, "left", "right", "mid").map(d => d[0] + " -> " + d[1]) + ); + })(); + function getPrompt() { + printStacks(); + rl.question("start stack: ", startStack => { + rl.question("end stack: ", endStack => { + towersOfHanoi(startStack, endStack); + getPrompt(); + }); + }); + } + + // Tests + + 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", () => { + stacks = { + a: [4, 3, 2], + b: [1], + c: [] + }; + assert.equal(isLegal("a", "b"), false); + }); + it("should allow a legal move", () => { + stacks = { + a: [4, 3, 2, 1], + b: [], + c: [] + }; + assert.equal(isLegal("a", "c"), true); + }); + }); + 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(); + } + } } -function towersOfHanoi(startStack, endStack) { - // Your code here - -} - -function getPrompt() { - printStacks(); - rl.question('start stack: ', (startStack) => { - rl.question('end stack: ', (endStack) => { - towersOfHanoi(startStack, endStack); - getPrompt(); - }); - }); -} - -// Tests - -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', () => { - stacks = { - a: [4, 3, 2], - b: [1], - c: [] - }; - assert.equal(isLegal('a', 'b'), false); - }); - it('should allow a legal move', () => { - stacks = { - a: [4, 3, 2, 1], - b: [], - c: [] - }; - assert.equal(isLegal('a', 'c'), true); - }); - }); - 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(); - -} +// 1. build a board. +// 3 verticle lines for the blocks to go through. +// 2. build the blocks. +// only 3 blocks +// each block should be slightly bigger than the last block +// for terminal app no colors. +// +// 3. set the rules for the game. +// 4. set a check for winner. From 4cfbdabdee86ab7096c0ea3566c99893053c2137 Mon Sep 17 00:00:00 2001 From: nae deshazer Date: Thu, 11 Jul 2019 20:59:14 -0500 Subject: [PATCH 2/2] Now the brown dog growls and hisses it may be a snake --- 03week/towersOfHanoi.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index af583e30e..80dcf95bf 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -122,4 +122,11 @@ function isLegal() { // for terminal app no colors. // // 3. set the rules for the game. +// no big blocks on small blcoks. +// all blocks must be in order from largest to biggest +// also must be bigger on bottom and smaller on top +// all blocks must be on the last peg // 4. set a check for winner. +// check for winner by checking where blocks are on the pegs +// also check to see if blocks are in correct order +//if so then there is a winner