Skip to content

Added a new game : Towers of Hanoi #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2022
Merged
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
4 changes: 4 additions & 0 deletions Towers of Hanoi/Rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Python-Projects 🐍
The idea is to shift the numbers in descending order from the left stack to the right while making sure we don't place a bigger number after a smaller one (prompt will be given for invalid move)


117 changes: 117 additions & 0 deletions Towers of Hanoi/Towers of Hanoi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
class Node:
def __init__(self, value, link_node=None):
self.value = value
self.link_node = link_node

def set_next_node(self, link_node):
self.link_node = link_node

def get_next_node(self):
return self.link_node

def get_value(self):
return self.value

class Stack:
def __init__(self, name):
self.size = 0
self.top_item = None
self.limit = 1000
self.name = name

def push(self, value):
if self.has_space():
item = Node(value)
item.set_next_node(self.top_item)
self.top_item = item
self.size += 1
else:
print("No more room!")

def pop(self):
if self.size > 0:
item_to_remove = self.top_item
self.top_item = item_to_remove.get_next_node()
self.size -= 1
return item_to_remove.get_value()
print("This stack is totally empty.")

def peek(self):
if self.size > 0:
return self.top_item.get_value()
print("Nothing to see here!")

def has_space(self):
return self.limit > self.size

def is_empty(self):
return self.size == 0

def get_size(self):
return self.size

def get_name(self):
return self.name

def print_items(self):
pointer = self.top_item
print_list = []
while(pointer):
print_list.append(pointer.get_value())
pointer = pointer.get_next_node()
print_list.reverse()
print("{0} Stack: {1}".format(self.get_name(), print_list))


print("\nLet's play Towers of Hanoi!!")

#Create the Stacks
stacks = []
left_stack = Stack("Left")
middle_stack =Stack("Middle")
right_stack = Stack("Right")
stacks.extend([left_stack, middle_stack, right_stack])
#Set up the Game
num_disks = int(input("\nHow many disks do you want to play with?\n"))
while num_disks < 3:
num_disks = int(input("Enter a number greater than or equal to 3 \n"))
for i in range(num_disks, 0, -1):
left_stack.push(i)

num_optimal_moves =(2 ** num_disks) - 1
print("\n The fastest you can solve this game is in {} moves".format(num_optimal_moves))
#Get User Input
def get_input():
choices = [(stack.get_name()[0]) for stack in stacks]
while True:
for i in range(len(stacks)):
name =stacks[i]. get_name()
letter = choices[i]
print("Enter {let} for {nam}".format(let=letter, nam=name))
user_input = input("")
if user_input in choices:
for i in range(len(stacks)):
if user_input == choices[i]:
return stacks[i]
#Play the Game
num_user_moves = 0
while right_stack.get_size() != num_disks:
print("\n\n\n...Current Stacks...")
for i in stacks:
i.print_items()
while True:
print("\nWhich stack do you want to move from?\n")
from_stack = get_input()
print("\nWhich stack do you want to move to?\n")
to_stack = get_input()
if from_stack.is_empty():
print("\n\nInvalid Move. Try Again")
elif (to_stack.is_empty()) or (from_stack.peek() < to_stack.peek()):
disk = from_stack.pop()
to_stack.push(disk)
num_user_moves += 1
else:
print("\n\nInvalid Move. Try Again")
break
print("\n\nYou completed the game in {use} moves, and the optimal number of moves is {opt}".format(use=num_user_moves, opt=num_optimal_moves))