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
83 changes: 66 additions & 17 deletions 03week/towersOfHanoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,91 @@ let stacks = {
c: []
};

/*
*This function displays the "board" to the user
*/
function printStacks() {
console.log("a: " + stacks.a);
console.log("b: " + stacks.b);
console.log("c: " + stacks.c);
}

function movePiece() {
// Your code here

/*
*This function removes a block from a stack and adds that block to a different stack
Copy link

Choose a reason for hiding this comment

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

I really like the documentation, and it was obvious you used log statements to debug as you were going along. Nice!

*
* @param startStack the stack we are removing the element from
* @param endStack the stack we are adding the element to
*/
function movePiece(startStack, endStack) {
let blockMove = stacks[startStack].pop(); //remove the "top block" from startStack
stacks[endStack].push(blockMove); //add the removed block to endStack
}

function isLegal() {
// Your code here

/*
*This function checks to see if the proposed move is legal
*
* @param startStack the stack we are removing the element from
* @param endStack the stack we are adding the element to
* @return true if the move is legal
*/
function isLegal(startStack, endStack) {
if (((startStack==='a')||(startStack==='b')||(startStack==='c'))&&
((endStack==='a')||(endStack==='b')||(endStack==='c'))) { //checking to make sure input is valid
let begin = stacks[startStack];
let final = stacks[endStack];
//console.log("Length of startStack: " + begin.length);
//console.log("Value of block being moved: " + begin[begin.length - 1]);
//console.log("Value of block being topped off: " + final[final.length - 1]);
if (begin[begin.length - 1] > final[final.length - 1]) { //making sure the "block" is being move onto a larger "block"
Copy link

Choose a reason for hiding this comment

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

this should return a false if it is not a legal move.
Also checking for a legal move is a better approach. IE start out assuming the move is not legal unless you prove otherwise.

This implementation allows you to move piece back to its own stack. Not wrong just weird that it would be allowed.

Copy link

Choose a reason for hiding this comment

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

You also allow me to move from an empty stack to another empty stack, or from an empty stack to a non-empty stack

console.log("***ILLEGAL MOVE***");
} else{
return true;
}
} else{
console.log("***Invalid input.***")
}
}

/*
*This function checks for a win
*/
function checkForWin() {
// Your code here

if (stacks.c[3] === 1){ //checks for the "1" block on top of a four-stack in stack "c."
Copy link

Choose a reason for hiding this comment

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

i like that you reset the board.
Checking to see if the last piece is 1 assumes that the reset of your code will not let that happen.
While this is logically correct, a better approach might have been to verify that the a and b stacks are empty and the c stack - 4,3,2,1

console.log("**************");
console.log("***YOU WIN!***");
console.log("**************");
stacks = {
a: [4, 3, 2, 1],
b: [],
c: []
};
}
}

/*
*This function runs the game
*
* @param startStack the stack we are removing the element from
* @param endStack the stack we are adding the element to
*/
function towersOfHanoi(startStack, endStack) {
// Your code here

if (isLegal(startStack, endStack)) { //if move is legal, move piece then check for win
movePiece(startStack, endStack);
checkForWin();
}
}

/*
*This function prompts the user for inputs then runs towersOfHanoi
*/
function getPrompt() {
printStacks();
rl.question('start stack: ', (startStack) => {
rl.question('end stack: ', (endStack) => {
towersOfHanoi(startStack, endStack);
getPrompt();
rl.question('start stack: ', (startStack) => { //get user input to define what stack the "block" is being taken from
rl.question('end stack: ', (endStack) => { //get user to define what stack the "block" is being placed on
towersOfHanoi(startStack, endStack); //run game logic
getPrompt(); //recursion to prompt next move
});
});
}

getPrompt();


getPrompt(); //start game on run
88 changes: 82 additions & 6 deletions 04week/mastermind.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const assert = require('assert');
const readline = require('readline');
const colors = require('colors');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
Expand All @@ -11,33 +12,106 @@ let board = [];
let solution = '';
let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];

/*
*This function prints the board: the hints and their corresponding guesses
*/
function printBoard() {
for (let i = 0; i < board.length; i++) {
console.log(board[i]);
}
}

/*
*This function generates a random 4-letter solution using the first 8 letters of the alphabet (duplicates allowed)
*/
function generateSolution() {
for (let i = 0; i < 4; i++) {
const randomIndex = getRandomInt(0, letters.length);
solution += letters[randomIndex];
// console.log('Solution is: ' + solution);
}
}

/*
*This function generates a random integer from which to correspond to a random letter in generateSolution
*
* @param min minimum value allowed
* @param max maximum value allowed
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}

function generateHint() {
// your code here
/*
*This function gives a hint based on the inputted guess; for each letter that exactly matches between the guess and the solution, redPeg will increment by one. For each letter that matches, except for location, whitePeg will increment by one.
*
* @param guess The guess entered by the user
*/
function generateHint(guess) {
let redPeg = 0;
let whitePeg = 0;
let guessArray = guess.split("");
let solutionArray = solution.split("");
for (let i=0; i < 4; i++) {
let x = guessArray[i];
let y = solutionArray[i];
if (guessArray[i]===solutionArray[i]){
//for each guess letter that matches each solution letter, tick up one red, then set index of array to null to avoid false positives when tallying whitePeg
redPeg++;
solutionArray[i] = null;
}
}
for (let i=0; i < 4; i++) {
//for index of each guess letter that matches a letter (but not idex of) solution letter, tick up one white
if ((guessArray[i]!==solutionArray[i])&&(solutionArray.indexOf(guessArray[i])!==(-1))) {
whitePeg++;
solutionArray[solutionArray.indexOf(guessArray[i])] = null;
}
}
let redPegStr = redPeg.toString();
let whitePegStr = whitePeg.toString();
let hint = ((redPegStr.red)+"-"+(whitePegStr.white));
//console.log("Hint: "+hint);
board.push(hint+" --- "+guess);
return hint;
}

/*
*This function checks the guess against the solution
*
* @param guess The user inputted guess
*/
function checkForWin(guess) {
let win = "You guessed it!";
let reguess = "Guess again.";
let lose = "You ran out of turns! The solution was "+solution;
if (solution===guess){
console.log(win);
return win;
}else if ((solution!=guess)&&(board.length < 10)){
console.log(reguess);
return reguess;
}else if ((solution!=guess)&&(board.length = 10)) {
console.log(lose);
return lose;
}
}

/*
*This function runs the mastermind game
*
* @param guess The user inputted guess
*/
function mastermind(guess) {
solution = 'abcd'; // Comment this out to generate a random solution
// your code here
generateHint(guess);
//console.log(solution);
checkForWin(guess);
}


/*
*This function prompts the user for an inputted guess
*/
function getPrompt() {
rl.question('guess: ', (guess) => {
mastermind(guess);
Expand All @@ -62,10 +136,12 @@ if (typeof describe === 'function') {

describe('#generateHint()', () => {
it('should generate hints', () => {
assert.equal(generateHint('abdc'), '2-2');
let expected = ('2'.red)+"-"+('2'.white);
assert.equal(generateHint('abdc'), expected);
});
it('should generate hints if solution has duplicates', () => {
assert.equal(generateHint('aabb'), '1-1');
let expected = ('1'.red)+"-"+('1'.white);
assert.equal(generateHint('aabb'), expected);
});

});
Expand Down