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
150 changes: 150 additions & 0 deletions 04week/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
'use strict';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really a code plan, lol..


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

let stacks = {
a: [4, 3, 2, 1],
b: [],
c: [],
};

function printStacks() {
console.log("a: " + stacks.a);
console.log("b: " + stacks.b);
console.log("c: " + stacks.c);
}

// Take the last element of the starting array and add it to the last part of the ending array
function movePiece(startStack, endStack) {
stacks[endStack].push(stacks[startStack].pop());
}


// Compare the size of the last element of the starting and ending array
function isLegal(startStack, endStack) {
// Allow a move if the array is empty as a special condition
if (stacks[endStack].slice(-1)==""){
return true;
// Allow a move if the number of the ending array is smaller than the size of the starting array
} else if (stacks[endStack].slice(-1) > stacks[startStack].slice(-1)) {
moves = moves + 1;
return true;
} else {
return false;
}
};

// Check to see if the b or c array has the winning arrangement. Array a does not count towards a win.
//loop thru object array
let winStack = [4, 3, 2, 1];
let gameOver = false;
//check to see if the object property values equal a winning array of [4,3,2,1]
function arraysEqual(arr1, arr2) {
if(arr1.length !== arr2.length)
return false;
for(var i = arr1.length; i--;) {
if(arr1[i] !== arr2[i])
return false;
}
return true;
}

function checkForWin() {
//if the function that determined whether the array contents are equal is true, comparing it to winStack, proceed
if (arraysEqual(stacks.b, winStack) || arraysEqual(stacks.c, winStack)) {
console.log("you're a winner!");
gameOver = true;
return true;
} else {
return false;
}
}


function towersOfHanoi(startStack, endStack) {
//first check for win
checkForWin();
//if check for win is false, continue with moves
if (!gameOver) {
//make sure it's a legal move
if (isLegal(startStack, endStack) == true) {
movePiece(startStack, endStack);
}
} else {
//if you won, don't allow a move and instead log a winning sentence
console.log("You win!");
}
}

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 in stack b', () => {
stacks = { a: [], b: [4, 3, 2, 1], c: [] };
assert.equal(checkForWin(), true);
stacks = { a: [1], b: [4, 3, 2], c: [] };
assert.equal(checkForWin(), false);
});
//sepcifically check if stack c is a winner
it('should detect a win in stack c', () => {
stacks = { a: [], b: [], c: [4, 3, 2, 1] };
assert.equal(checkForWin(), true);
stacks = { a: [1], b: [4, 3, 2], c: [] };
assert.equal(checkForWin(), false);
});
//specifically check to see that stack a cannot trigger a win
it('should not detect a win in stack a', () => {
stacks = { a: [4, 3, 2, 1], b: [], c: [] };
assert.equal(checkForWin(), false);
stacks = { a: [], b: [4, 3, 2, 1], c: [] };
assert.equal(checkForWin(), true);
});
//make sure the game is over once a winner is chosen
it('should end game once won, ()', () => {
stacks = { a: [], b: [4, 3, 2, 1], c: [] };
assert.equal(gameOver, true);
});
});
} else {
getPrompt();
}
141 changes: 141 additions & 0 deletions 04week/towersOfHanoi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
**
'use strict';

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

let stacks = {
a: [4, 3, 2, 1],
b: [],
c: [],
};

function printStacks() {
console.log("a: " + stacks.a);
console.log("b: " + stacks.b);
console.log("c: " + stacks.c);
}

// Take the last element of the starting array and append it to the last of the ending array
function movePiece(startStack, endStack) {
stacks[endStack].push(stacks[startStack].pop());
}


// Compare the size of the last element of the starting and ending array
function isLegal(startStack, endStack) {
// Allow a move if the array is empty as a special condition
if (stacks[endStack].slice(-1)==""){
return true;
// Allow a move if the number of the ending array is smaller than the size of the starting array
} else if (stacks[endStack].slice(-1) > stacks[startStack].slice(-1)) {
return true;
} else {
return false;
}
};

// Check to see if the b or c array has the winning arrangement. Array a does not count towards a win.
//loop thru object array
let winStack = [4, 3, 2, 1];
let gameOver = false;
function arraysEqual(arr1, arr2) {
if(arr1.length !== arr2.length)
return false;
for(var i = arr1.length; i--;) {
if(arr1[i] !== arr2[i])
return false;
}
return true;
}

function checkForWin() {
if (arraysEqual(stacks.b, winStack) || arraysEqual(stacks.c, winStack)) {
console.log("you're a winner!");
gameOver = true;
return true;
} else {
return false;
}
}


function towersOfHanoi(startStack, endStack) {
checkForWin();
if (!gameOver) {
if (isLegal(startStack, endStack) == true) {
movePiece(startStack, endStack);
}
} else {
console.log("You win!");
}
}

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 in stack b', () => {
stacks = { a: [], b: [4, 3, 2, 1], c: [] };
assert.equal(checkForWin(), true);
stacks = { a: [1], b: [4, 3, 2], c: [] };
assert.equal(checkForWin(), false);
});
it('should detect a win in stack c', () => {
stacks = { a: [], b: [], c: [4, 3, 2, 1] };
assert.equal(checkForWin(), true);
stacks = { a: [1], b: [4, 3, 2], c: [] };
assert.equal(checkForWin(), false);
});
it('should not detect a win in stack a', () => {
stacks = { a: [4, 3, 2, 1], b: [], c: [] };
assert.equal(checkForWin(), false);
stacks = { a: [], b: [4, 3, 2, 1], c: [] };
assert.equal(checkForWin(), true);
});
it('should end game once won, ()', () => {
stacks = { a: [], b: [4, 3, 2, 1], c: [] };
assert.equal(gameOver, true);
});
});
} else {
getPrompt();
}