Giter Site home page Giter Site logo

towers-of-hanoi's Introduction

Towers-of-Hanoi

Description

A Towers of Hanoi game to be played in the DOM coded in HTML, CSS, and JavaScript. I wanted to challenge myself with a game that required both logic and user interface elements like drag events. I wrote it initially using a point-and-click method to play the game. After I had proper game logic, I researched HTML drag events and rewrote my code using them.

After making the game playable using drag instead of click, I wanted another challenge, to write an algorithm that self-solves the towers.

Side note: dragging events do not work on mobile. Will have to learn about touch events and update.

Installation/Usage

Clone this repo and open the index.html file in your browser. Have fun playing!

Algorithm

In order to tackle writing an algorithm to solve the towers, I did some research online, but mostly studied how the game works and wrote down patterns that I noticed. When I came up with my own intuitive solution, I then checked with resources online

This was the resource I found most helpful.

My 3 rules for recursive solving the towers:

For reference:

  • First tower is A
  • Middle tower is B
  • Last tower is C
  1. There are 2 patterns that can determine a ring's movement behavior. I called these right and left.

    • If a ring has a right pattern, it repeatedly travels A - B - C - A
    • If a ring has a left pattern, it repeatedly travels A - C - B - A
  2. The largest ring in the stack always has a left pattern and rings alternate patterns.

    • For a 3 ring game:
      • Ring 1 = left
      • Ring 2 = right
      • Ring 3 = left
    • For a 4 ring game:
      • Ring 1 = right
      • Ring 2 = left
      • Ring 3 = right
      • Ring 4 = left
  3. Move the smallest ring possible that was not moved during the previous turn.

So rules 1 and 2 determine an individual ring's movement while rule 3 determines which ring to move on a turn.

Credits

MDN Drag and Drop: https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API CSS Scan Buttons: https://getcssscan.com/css-buttons-examples

Author: Mateo Lopez-Castillo

towers-of-hanoi's People

Contributors

mateolopcas avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.