Skip to content

Commit 5b6bb71

Browse files
committed
Added a new game : Towers of Hanoi
1 parent 8a733eb commit 5b6bb71

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

Diff for: Towers of Hanoi/Rules.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Python-Projects 🐍
2+
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)
3+
4+

Diff for: Towers of Hanoi/Towers of Hanoi.py

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
class Node:
2+
def __init__(self, value, link_node=None):
3+
self.value = value
4+
self.link_node = link_node
5+
6+
def set_next_node(self, link_node):
7+
self.link_node = link_node
8+
9+
def get_next_node(self):
10+
return self.link_node
11+
12+
def get_value(self):
13+
return self.value
14+
15+
class Stack:
16+
def __init__(self, name):
17+
self.size = 0
18+
self.top_item = None
19+
self.limit = 1000
20+
self.name = name
21+
22+
def push(self, value):
23+
if self.has_space():
24+
item = Node(value)
25+
item.set_next_node(self.top_item)
26+
self.top_item = item
27+
self.size += 1
28+
else:
29+
print("No more room!")
30+
31+
def pop(self):
32+
if self.size > 0:
33+
item_to_remove = self.top_item
34+
self.top_item = item_to_remove.get_next_node()
35+
self.size -= 1
36+
return item_to_remove.get_value()
37+
print("This stack is totally empty.")
38+
39+
def peek(self):
40+
if self.size > 0:
41+
return self.top_item.get_value()
42+
print("Nothing to see here!")
43+
44+
def has_space(self):
45+
return self.limit > self.size
46+
47+
def is_empty(self):
48+
return self.size == 0
49+
50+
def get_size(self):
51+
return self.size
52+
53+
def get_name(self):
54+
return self.name
55+
56+
def print_items(self):
57+
pointer = self.top_item
58+
print_list = []
59+
while(pointer):
60+
print_list.append(pointer.get_value())
61+
pointer = pointer.get_next_node()
62+
print_list.reverse()
63+
print("{0} Stack: {1}".format(self.get_name(), print_list))
64+
65+
66+
print("\nLet's play Towers of Hanoi!!")
67+
68+
#Create the Stacks
69+
stacks = []
70+
left_stack = Stack("Left")
71+
middle_stack =Stack("Middle")
72+
right_stack = Stack("Right")
73+
stacks.extend([left_stack, middle_stack, right_stack])
74+
#Set up the Game
75+
num_disks = int(input("\nHow many disks do you want to play with?\n"))
76+
while num_disks < 3:
77+
num_disks = int(input("Enter a number greater than or equal to 3 \n"))
78+
for i in range(num_disks, 0, -1):
79+
left_stack.push(i)
80+
81+
num_optimal_moves =(2 ** num_disks) - 1
82+
print("\n The fastest you can solve this game is in {} moves".format(num_optimal_moves))
83+
#Get User Input
84+
def get_input():
85+
choices = [(stack.get_name()[0]) for stack in stacks]
86+
while True:
87+
for i in range(len(stacks)):
88+
name =stacks[i]. get_name()
89+
letter = choices[i]
90+
print("Enter {let} for {nam}".format(let=letter, nam=name))
91+
user_input = input("")
92+
if user_input in choices:
93+
for i in range(len(stacks)):
94+
if user_input == choices[i]:
95+
return stacks[i]
96+
#Play the Game
97+
num_user_moves = 0
98+
while right_stack.get_size() != num_disks:
99+
print("\n\n\n...Current Stacks...")
100+
for i in stacks:
101+
i.print_items()
102+
while True:
103+
print("\nWhich stack do you want to move from?\n")
104+
from_stack = get_input()
105+
print("\nWhich stack do you want to move to?\n")
106+
to_stack = get_input()
107+
if from_stack.is_empty():
108+
print("\n\nInvalid Move. Try Again")
109+
elif (to_stack.is_empty()) or (from_stack.peek() < to_stack.peek()):
110+
disk = from_stack.pop()
111+
to_stack.push(disk)
112+
num_user_moves += 1
113+
else:
114+
print("\n\nInvalid Move. Try Again")
115+
break
116+
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))
117+

0 commit comments

Comments
 (0)