From 5b9ccb0271936fc157c21a326c03d07d33c3a8c3 Mon Sep 17 00:00:00 2001 From: uhuru3 Date: Sun, 28 Oct 2018 13:38:18 -0700 Subject: [PATCH 1/5] some changes --- 03week/towersOfHanoi.js | 50 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 3cf6df049..103f740a5 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -13,14 +13,62 @@ let stacks = { c: [] }; +//Rules +// Only one disk can be moved at a time +// A disk can only be moved if it is the uppermost disk on a stack +// No disk may be placed on top of a smaller disk + + + + +// One Solution +const run = function(){ + solveTowerOfHanoi(4); +} + +function solveTowerOfHanoi(pieces){ + solveTower(pieces, 1, 3); +} + +function solveTower(pieces, startStack, endStack){ + if (pieces == 0) return; + + const middleStact = 6 -startStack -endStack; + solveTower(pieces -1, startStack, middleStact); + + const text = "Move {0} from {1} to {2}" + .fomat(pieces, stacks[startStack -1], stacks[endStack -1]); + return(text); + + solveTower(pieces -1, middleStack, endStack); +} + +// Second Solution +const disc = prompt("How many discs"); +const result = hanoi(disc, 'A','B','C'); +document.getElementById(result); + +function hanoi (n, a, b, c){ + if (n==1){ + document.write('Move disc '+n+' from '+a+' to '+b+ "
"); + }else{ + hanoi (n-1,a,c,b); + hanoi (1,a,b,c); + hanoi (n-1,c,b,a); + } +} + + + function printStacks() { console.log("a: " + stacks.a); console.log("b: " + stacks.b); console.log("c: " + stacks.c); } -function movePiece() { +function movePiece(a, b) { // Your code here + b.push(a.pop().content); } From 6523bdffa013d429675611e17dd3af8e7fb9eea0 Mon Sep 17 00:00:00 2001 From: uhuru3 Date: Mon, 29 Oct 2018 16:09:13 -0700 Subject: [PATCH 2/5] some changes --- 03week/towersOfHanoi.js | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 103f740a5..4020934d9 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -14,9 +14,10 @@ let stacks = { }; //Rules -// Only one disk can be moved at a time -// A disk can only be moved if it is the uppermost disk on a stack -// No disk may be placed on top of a smaller disk +// Only one disk can be moved at a time, movePiece() +// A disk can only be moved if it is the uppermost disk on a stack, isLegal() +// No disk may be placed on top of a smaller disk, isLegal() +// You have to move all disk from on side to the other, checkForWin() @@ -43,18 +44,14 @@ function solveTower(pieces, startStack, endStack){ solveTower(pieces -1, middleStack, endStack); } -// Second Solution -const disc = prompt("How many discs"); -const result = hanoi(disc, 'A','B','C'); -document.getElementById(result); -function hanoi (n, a, b, c){ +function stacks (n, a, b, c){ if (n==1){ - document.write('Move disc '+n+' from '+a+' to '+b+ "
"); + console.log('Move disc '+n+' from '+a+' to '+b+') }else{ - hanoi (n-1,a,c,b); - hanoi (1,a,b,c); - hanoi (n-1,c,b,a); + stacks (n-1,a,c,b); + stacks (1,a,b,c); + stacks (n-1,c,b,a); } } @@ -66,9 +63,9 @@ function printStacks() { console.log("c: " + stacks.c); } -function movePiece(a, b) { +function movePiece() { // Your code here - b.push(a.pop().content); + } @@ -83,7 +80,20 @@ function checkForWin() { } function towersOfHanoi(startStack, endStack) { - // Your code here +// Your code here +//Rules +// Only one disk can be moved at a time, movePiece() +// A disk can only be moved if it is the uppermost disk on a stack, isLegal() +// No disk may be placed on top of a smaller disk, isLegal() +// You have to move all disk from on side to the other, checkForWin() + + + // if (stacks(row, column)){ + // if (isEmpty(row, column)){ + // board[row][column] = playerTurn; + // if (checkForWin()){ + // console.log(playerTurn + ' Wins!'); + // resetGame(); } From 228b9d12d6e61aaa091d482d2e10011437457da5 Mon Sep 17 00:00:00 2001 From: uhuru3 Date: Tue, 30 Oct 2018 13:48:07 -0700 Subject: [PATCH 3/5] some edits --- 03week/towersOfHanoi.js | 166 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 150 insertions(+), 16 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 4020934d9..d85153b48 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -13,12 +13,12 @@ let stacks = { c: [] }; -//Rules -// Only one disk can be moved at a time, movePiece() -// A disk can only be moved if it is the uppermost disk on a stack, isLegal() -// No disk may be placed on top of a smaller disk, isLegal() -// You have to move all disk from on side to the other, checkForWin() - +const disk1 = 1; +const disk2 =2; +const disk3 =3; +const disk4 =4; +const endStackValidWin = [4, 3, 2, 1]; +const firstPick = stacks.a[3]; @@ -65,19 +65,50 @@ function printStacks() { function movePiece() { // Your code here + if (stacks.a ==1){ + return true; + } + if (stacks.b ==1){ + return true; + } + if (stacks.c == 1){ + return true; + } } -function isLegal() { +let stacks = { + a: [4, 3, 2, 1], + b: [], + c: [] +}; + +function isLegal(startStack, endStack) { // Your code here + for(let i = 0; i < stacks.length; i++){ + if (stacks[i].includes(' ')){ + return false; + } + } + return true; +}; + -} function checkForWin() { // Your code here + return endStackValidWin(); + + +const resetGame=()=>{ + stacks.forEach(element => { + element.fill(stacks); +}); + + + -} function towersOfHanoi(startStack, endStack) { // Your code here @@ -86,16 +117,119 @@ function towersOfHanoi(startStack, endStack) { // A disk can only be moved if it is the uppermost disk on a stack, isLegal() // No disk may be placed on top of a smaller disk, isLegal() // You have to move all disk from on side to the other, checkForWin() +if (isLegal(startStack, endStack)){ + if (movePiece(startStack, endStack)){ + stacks[startStack][endStack] = playerTurn; + if (checkForWin()){ + console.log('You Win!'); + resetGame(); + }else{ + console.log('Pick A Valid Play') + } +} - // if (stacks(row, column)){ - // if (isEmpty(row, column)){ - // board[row][column] = playerTurn; - // if (checkForWin()){ - // console.log(playerTurn + ' Wins!'); - // resetGame(); +//code to look at from TicTacToe -} + 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 checkForWin() { + // Your code here + return horizontalWin() || verticalWin() || diagonalWin(); + } + + const resetGame=()=>{ + board.forEach(element => { + element.fill(" "); + }); + playerTurn = 'X'; + } + + const isValid =(row, column)=>{ + if(row == '0' || row == '1' || row == '2'){ + if(column == '0' || column == '1' || column == '2'){ + return true; + } + } + }; + + const isEmpty =(row, column)=>{ + if(board[row][column] == ' '){ + return true; + } + }; + + const draw =()=>{ + for(let i = 0; i < board.length; i++){ + if (board[i].includes(' ')){ + return false; + } + } + return true; + }; + + function ticTacToe(row, column) { + // Your code here + // user selelects position on the board to place their mark + // make sure it is a valid pick, isValid() T/F + // make sure the place is empty, isEmpty() T/F + // put the mark + // check win, checkForWin() + // swich user, swithUser() + // check for draw, checkForDraw() + if (isValid(row, column)){ + if (isEmpty(row, column)){ + board[row][column] = playerTurn; + if (checkForWin()){ + console.log(playerTurn + ' Wins!'); + resetGame(); + }else{ + if (draw()){ + console.log('Draw! No One Wins, Restart Game'); + resetGame(); + }else{ + switchPlayer() + } + } + }else{ + console.log('Pick An Empty Space') + } + }else{ + console.log('Enter Valid Entry!') + } + } + + const switchPlayer =()=>{ + if(playerTurn == 'X'){ + playerTurn = 'O'; + }else{ + playerTurn = 'X'; + } + } + + function getPrompt() { + printBoard(); + console.log("It's Player " + playerTurn + "'s turn."); + rl.question('row: ', (row) => { + rl.question('column: ', (column) => { + ticTacToe(row, column); + getPrompt(); + }); + }); + + } function getPrompt() { printStacks(); From eb4570d1984c4fa5cdd92242cf9722b893706baa Mon Sep 17 00:00:00 2001 From: uhuru3 Date: Tue, 30 Oct 2018 18:22:00 -0700 Subject: [PATCH 4/5] some changes, not completed --- 03week/towersOfHanoi.js | 197 +++++----------------------------------- 1 file changed, 21 insertions(+), 176 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index d85153b48..581d1bf13 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -13,49 +13,6 @@ let stacks = { c: [] }; -const disk1 = 1; -const disk2 =2; -const disk3 =3; -const disk4 =4; -const endStackValidWin = [4, 3, 2, 1]; -const firstPick = stacks.a[3]; - - - -// One Solution -const run = function(){ - solveTowerOfHanoi(4); -} - -function solveTowerOfHanoi(pieces){ - solveTower(pieces, 1, 3); -} - -function solveTower(pieces, startStack, endStack){ - if (pieces == 0) return; - - const middleStact = 6 -startStack -endStack; - solveTower(pieces -1, startStack, middleStact); - - const text = "Move {0} from {1} to {2}" - .fomat(pieces, stacks[startStack -1], stacks[endStack -1]); - return(text); - - solveTower(pieces -1, middleStack, endStack); -} - - -function stacks (n, a, b, c){ - if (n==1){ - console.log('Move disc '+n+' from '+a+' to '+b+') - }else{ - stacks (n-1,a,c,b); - stacks (1,a,b,c); - stacks (n-1,c,b,a); - } -} - - function printStacks() { console.log("a: " + stacks.a); @@ -63,50 +20,39 @@ function printStacks() { console.log("c: " + stacks.c); } -function movePiece() { - // Your code here - if (stacks.a ==1){ - return true; - } - if (stacks.b ==1){ - return true; - } - if (stacks.c == 1){ - return true; - } - +function movePiece(startStack, endStack) { + // Your code here + // Pop is taking 1 and storing it in the varible called take + const take = stacks[startStack].pop() + const put = stacks[endStack].push(take) } -let stacks = { - a: [4, 3, 2, 1], - b: [], - c: [] -}; + function isLegal(startStack, endStack) { // Your code here - for(let i = 0; i < stacks.length; i++){ - if (stacks[i].includes(' ')){ - return false; - } + // Checking to see if one block is larger than the other for valid move + const grabLast = stacks[startStack].slice(-1) + const compareWhereGoing = stacks[endStack].slice(-1) + if(grabLast < compareWhereGoing || compareWhereGoing === undefined){ + return true; } - return true; }; function checkForWin() { // Your code here - return endStackValidWin(); - - -const resetGame=()=>{ - stacks.forEach(element => { - element.fill(stacks); -}); + // C in quotes to access the array and 4 is the lenght of the pieces needed to Win + return (stacks['c'].length == 4); +} +// Function To Reset Game +const resetGame = { + +} @@ -119,117 +65,16 @@ function towersOfHanoi(startStack, endStack) { // You have to move all disk from on side to the other, checkForWin() if (isLegal(startStack, endStack)){ if (movePiece(startStack, endStack)){ - stacks[startStack][endStack] = playerTurn; + stacks[startStack][endStack]; if (checkForWin()){ console.log('You Win!'); resetGame(); }else{ - console.log('Pick A Valid Play') + console.log('Invalid') } } - - -//code to look at from TicTacToe - - 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 checkForWin() { - // Your code here - return horizontalWin() || verticalWin() || diagonalWin(); - } - - const resetGame=()=>{ - board.forEach(element => { - element.fill(" "); - }); - playerTurn = 'X'; - } - - const isValid =(row, column)=>{ - if(row == '0' || row == '1' || row == '2'){ - if(column == '0' || column == '1' || column == '2'){ - return true; - } - } - }; - - const isEmpty =(row, column)=>{ - if(board[row][column] == ' '){ - return true; - } - }; - - const draw =()=>{ - for(let i = 0; i < board.length; i++){ - if (board[i].includes(' ')){ - return false; - } - } - return true; - }; - - function ticTacToe(row, column) { - // Your code here - // user selelects position on the board to place their mark - // make sure it is a valid pick, isValid() T/F - // make sure the place is empty, isEmpty() T/F - // put the mark - // check win, checkForWin() - // swich user, swithUser() - // check for draw, checkForDraw() - if (isValid(row, column)){ - if (isEmpty(row, column)){ - board[row][column] = playerTurn; - if (checkForWin()){ - console.log(playerTurn + ' Wins!'); - resetGame(); - }else{ - if (draw()){ - console.log('Draw! No One Wins, Restart Game'); - resetGame(); - }else{ - switchPlayer() - } - } - }else{ - console.log('Pick An Empty Space') - } - }else{ - console.log('Enter Valid Entry!') - } - } - - const switchPlayer =()=>{ - if(playerTurn == 'X'){ - playerTurn = 'O'; - }else{ - playerTurn = 'X'; - } - } - - function getPrompt() { - printBoard(); - console.log("It's Player " + playerTurn + "'s turn."); - rl.question('row: ', (row) => { - rl.question('column: ', (column) => { - ticTacToe(row, column); - getPrompt(); - }); - }); - - } + function getPrompt() { printStacks(); From 474664484c19908d1d9cc795e8acd47f0c84031c Mon Sep 17 00:00:00 2001 From: uhuru3 Date: Tue, 30 Oct 2018 18:30:07 -0700 Subject: [PATCH 5/5] more changes --- 03week/towersOfHanoi.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 581d1bf13..91e8edaa8 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -50,8 +50,8 @@ function checkForWin() { // Function To Reset Game -const resetGame = { - +const resetGame=()=>{ + return stacks; }