Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## JavaScript 211 Project: Towers of Hanoi

## Overview


* [Towers of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi) is a simple logic game involving three stacks. The first stack has four (or more) blocks, each one bigger than the next, stacked like a pyramid. The point of the game is to move the blocks from one stack and arrange them in the same order into another stack, but never placing a larger block onto a smaller block. You can play the game [here](http://vornlocher.de/tower.html) to get an idea.

Expand All @@ -17,7 +17,34 @@ Your checkpoint is really a terminal app; which is what you'll be graded on. How
<!-- This is for their personal navigation through the project. They can go through and make sure they get each thing and can comb over it later. -->

1. 20pts - **Code Plan** - Include this in a `README.md` file in your folder with comments in your code
<!--create array for each 'stack'-->
<!--call 'stacks' [a, b, c]-->

<!--create stone objects-->
<!--call 'stones' (1, 2, 3, 4)-->

<!--establish which 'stack' the stones start in-->
<!--start in stack a-->

<!-- sort stones largest to smallest with smallest being last on stack-->
<!--put in reverse order(4,3,2,1)-->

<!--move last stones in 'stack' to a new stack-->
<!--lastChild = stack.lastElementChild()-->
<!--stack.appendChild(stone)-->

<!--not allow larger stones on top of smaller stones-->
<!--stone 4 cannot go on stone 3-->

<!--establish which 'stack' the 'blocks' end-->
<!--stack b or c-->

<!--when all stones have been moved to a new stack that wasn't the starting stack-->
<!--check for win-->
<!--when stack B or C = [4, 3, 2, 1]-->

1. 10pts - **Move Blocks** - User can move "blocks" from column to column

1. 20pts - **Illegal Moves** - Prevents larger blocks from stacking on smaller blocks
1. 20pts - **Notifies winner** - When all the blocks are stacked into column 2 or 1 the user is alerted they won!
1. 20pts - **Minimum 3 Unit Tests** - Should be attached to your file the same way Tic, Tac, Toe, PigLatin or Rock Paper Scissors is done.
Expand Down Expand Up @@ -74,6 +101,8 @@ Dissect the following game to get an insight on how to build Towers of Hanoi wit

### Hints

hellow how are you

1. Run your unit tests first!!
1. Use [repl.it](https://www.repl.it) to write the solution code first. (its a faster environment vs using the `node main.js` command over and over again.)
1. Read the comments in `main.js`
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<head>
<meta charset="utf-8">
<title></title>
<script src="index.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
Expand All @@ -18,6 +17,7 @@ <h1>Towers Of Hanoi</h1>
<div id="1" data-size="1" data-color="blue" class="stone" ></div>
</div>
<hr/>
<script src="index.js"></script>
</body>
</html>

Expand Down
53 changes: 50 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@

const stone = null

// this function is called when a row is clicked.
// this function is called when a row is clicked.
// Open your inspector tool to see what is being captured and can be used.
const selectRow = (row) => {
const currentRow = row.getAttribute("data-row")

console.log("Yay, we clicked an item", row)
console.log("Here is the stone's id: ", row.id)
console.log("Here is the stone's data-size: ", currentRow)

pickUpStone(row.id)
}
}

// this function can be called to get the last stone in the stack
// but there might be something wrong with it...
Expand All @@ -38,5 +37,53 @@ const dropStone = (rowID, stone) => {
stone = null
}

// Next, what do you think this function should do?
const movePiece = (startStack, endStack) => {
stacks[endStack].push(stacks[startStack].pop())
}

// Before you move, should you check if the move it actually allowed? Should 3 be able to be stacked on 2
const isLegal = (start, end) => {
if (stacks[end].length === 0 || stacks[start][stacks[start].length - 1] < stacks[end][stacks[end].length -1]) {
return true;
} else {
console.log("straight to jail");
return false
}
}

// What is a win in Towers of Hanoi? When should this function run?
const checkForWin = () => {
if (stacks['b'].length === 4) {
console.log('winner winner chicken dinner!')
return true
} else {
return false
}
}

const towersOfHanoi = (start, end) => {
// isLegal
if (isLegal(start, end)){
// movePiece
movePiece(start, end)
//checkForWin
checkForWin(start, end)
} else {
towersOfHanoi()
}
// else towersOfHanoi
}

const getPrompt = () => {
printStacks();
rl.question('start stack: ', (startStack) => {
rl.question('end stack: ', (endStack) => {
towersOfHanoi(startStack, endStack);
getPrompt();
});
});
}

// * Remember you can use your logic from 'main.js' to maintain the rules of the game. But how? Follow the flow of data just like falling dominoes.

91 changes: 73 additions & 18 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

const assert = require('assert');
const readline = require('readline');
const { start } = require('repl');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

// An object that represents the three stacks of Towers of Hanoi;
// * each key is an array of Numbers:
// * A is the far-left,
// * B is the middle,
// An object that represents the three stacks of Towers of Hanoi;
// * each key is an array of Numbers:
// * A is the far-left,
// * B is the middle,
// * C is the far-right stack
// * Each number represents the largest to smallest tokens:
// * 4 is the largest,
// * Each number represents the largest to smallest tokens:
// * 4 is the largest,
// * 1 is the smallest

let stacks = {
Expand All @@ -29,28 +30,55 @@ const printStacks = () => {
console.log("c: " + stacks.c);
}

// Next, what do you think this function should do?
const movePiece = () => {
// Your code here

}
// Next, what do you think this function should do?
const movePiece = (startStack, endStack) => {
stacks[endStack].push(stacks[startStack].pop())
}
//popping from startStack and pushing to endStack
//move last stones in 'stack' to a new stack

// Before you move, should you check if the move it actually allowed? Should 3 be able to be stacked on 2
const isLegal = () => {
// Your code here

const isLegal = (start, end) => {
if (stacks[end].length === 0 || stacks[start][stacks[start].length - 1] < stacks[end][stacks[end].length -1]) {
return true;
} else {
console.log("straight to jail");
return false
}
}
// if thing is in hand, and endStack is empty or thing is in hand and is smaller than last thing in endStack place thing from hand to endStack
// not allow larger stones on top of smaller stones
// stone 4 cannot go on stone 3


// What is a win in Towers of Hanoi? When should this function run?
const checkForWin = () => {
// Your code here
if (stacks['b'].length === 4) { //should I also add OR stack c = 4?
console.log('winner winner chicken dinner!')
return true
} else {
return false
}
}
// at any point if stacks length is equal to 4 things it's a win, else keep playing
// when all stones have been moved to a new stack that wasn't the starting stack
// when stack B or C = [4, 3, 2, 1]

}

// When is this function called? What should it do with its argument?
const towersOfHanoi = (startStack, endStack) => {
// Your code here

const towersOfHanoi = (start, end) => {

// isLegal
if (isLegal(start, end)){
// movePiece
movePiece(start, end)
//checkForWin
checkForWin(start, end)
} else {
towersOfHanoi()
}
// else towersOfHanoi
}

const getPrompt = () => {
Expand All @@ -67,6 +95,26 @@ const getPrompt = () => {

if (typeof describe === 'function') {

//my test for movePiece
describe('#movePiece', () => {
it('stack.a should start with all 4 pieces', () => {
stacks = {
a: [4, 3, 2, 1],
b: [],
c: []
};
});
});

// my test for printStacks
describe('#printStacks()', () => {
it('should print out all stacks', () => {
console.log("a: " + stacks.a);
console.log("b: " + stacks.b);
console.log("c: " + stacks.c);
});
});

describe('#towersOfHanoi()', () => {
it('should be able to move a block', () => {
towersOfHanoi('a', 'b');
Expand Down Expand Up @@ -100,6 +148,13 @@ if (typeof describe === 'function') {
assert.equal(checkForWin(), false);
});
});
//my test for obvious part of check for win...
it('stack.a should equal 0, stack b can win', () => {
stacks = { a: [], b: [4, 3, 2, 1], c: [] };
assert.equal(checkForWin(), true);
stacks = { a: [1], b: [], c: [4, 3, 2] };
assert.equal(checkForWin(), false);
});

} else {

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"http-server": "^0.11.1",
"javascripting": "^2.6.1",
"jsdom": "^11.6.2",
"main.js": "0.0.1",
"mocha": "^5.0.0",
"postcss-html": "^0.34.0",
"stylelint": "^7.13.0"
Expand Down