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 (