Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created December 5, 2020 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/f649e0224a847ae98297f289d8c873cb to your computer and use it in GitHub Desktop.
Save codecademydev/f649e0224a847ae98297f289d8c873cb to your computer and use it in GitHub Desktop.
Codecademy export
#imports all basic stack helper functions to make this a seemless process
from stack import Stack
print("\nLet's play Towers of Hanoi!!")
#Create the Stacks
stacks = []
#Each stack is defined as a stack instance, and is then appended to the empty stacks list
left_stack = Stack("Left")
middle_stack = Stack("Middle")
right_stack = Stack("Right")
stacks.append(left_stack)
stacks.append(middle_stack)
stacks.append(right_stack)
#could also do this:
#stacks += [left_stack, middle_stack, right_stack]
#Set up the Game: int input takes the user input as an integer instead of a string. Disks must be greater than 3 or a message will appear. Disks are iterated backwards by 1 until 0, and added to the left stack. The number of optimal moves is printed
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 disk in range(num_disks, 0, -1):
left_stack.push(disk)
num_optimal_moves = 2 ** num_disks - 1
print("\nThe fastest you can solve this game is in {nom} moves".format(nom=num_optimal_moves))
#Get User Input: choices is defined using a list comp which calls get name for stack at index 0 in a stack in the stacks list, which is the first initial of the list, ex: "Left" stack will be "L". Name is assigned to the name of each stack in the length of the stack, and letter is assigned to the initial letter of that stack. A string statement prints the instructions, takes in a user input, and if it is valid, returns it.
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 {l} for {n}".format(l=letter, n=name))
user_input = input("")
if user_input in choices:
for i in range(len(stacks)):
return stacks[i]
#Play the Game: While the game isnt over (all disks on right stack=game over), the current stacks will be printed by iterating through stacks, asking for a user input for the "move from" stack, followed by the "move to" stack. If the user enters a valid move, their move counter is incremented and the game goes on.
num_user_moves = 0
while right_stack.get_size() != num_disks:
print("\n\n\n...Current Stacks...")
for stack in stacks:
stack.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()
#test code
#print(to_stack.is_empty())
#print(from_stack.is_empty())
if from_stack.is_empty():
print("\n\nInvalid yyyy 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
break
else:
print("\n\nInvalid Move. Try Again")
print("\n\nYou completed the game in {num} moves, and the optimal number of moves is {nom}".format(num=num_user_moves, nom=num_optimal_moves))
#code is functioning but bugged. Always returns "else: invalid move", something is up in get_input bc it's saying to_stack.is_empty is false. From_stack.is_empty=false is correct, so it skips the "if" but also skips "elif" since it thinks to_stack is NOT empty
#imports the basic Node linking structure helper functions to allow stacks to function seemlessly
from node import Node
#stack is limited to 1000 linked Nodes
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment