From 2d3494f1c6b87db6362dd13b0e8d991eb0804e01 Mon Sep 17 00:00:00 2001 From: Renata Estes Date: Tue, 29 Aug 2017 18:11:48 -0500 Subject: [PATCH] towers --- 07week/towersOfHanoi/script.js | 43 ++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/07week/towersOfHanoi/script.js b/07week/towersOfHanoi/script.js index 9b156e880..3974d68d5 100644 --- a/07week/towersOfHanoi/script.js +++ b/07week/towersOfHanoi/script.js @@ -3,24 +3,53 @@ class TowersOfHanoi extends React.Component { constructor(props) { super(props); + this.state = { + block: null + }; } +clickStack=(stackid)=>{ + const state = {...this.state}; + //state['block'] = this.state.block; + //const attribute = element.getAttribute('data-block').value; + //const children = this.state.['block']; + const currentStack = document.getElementById(stackid); + const children = currentStack.children; + const lastChild = children.item(children.length-1); + + if (state['block'] == null) { + state['block'] = currentStack.removeChild(lastChild); + } else { + if ( (lastChild != null) && (parseInt(lastChild.dataset['block']) < parseInt(state['block'].dataset['block']))) { + alert("Invalid Move"); + } else { + currentStack.appendChild(state['block']); + state['block'] = null; + } + } + + if (document.getElementById('stack2').children.length >= 4 || document.getElementById('stack2').children.length >= 4 ){ + state.win = (`You win!`); + } + +this.setState(state) +}; + render() { return (
-
+
this.clickStack('stack1')}>{this.state.stack1}
-
-
-
-
-
+
this.clickStack('stack2')}>{this.state.stack2}
+
this.clickStack('stack3')}>{this.state.stack3}
+
{this.state.win}
+
); } } - +//
this.handleClick('cell0')}>{this.state.cell0}
ReactDOM.render(, document.getElementById('towers-of-hanoi'));