Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How would I get this text based game to print Congratulations, You have collected all the items and defeated The Overseer! at the end of

How would I get this text based game to print "Congratulations, You have collected all the items and defeated The Overseer!" at the end of the game? Everytime, whether I collect all items or not I always get the output "You are in the Artifact Vault
Battling The Overseer..................................................
GAME OVER!
Thanks for playing!" How would i get the correct winning output? Here is my code.
# Sample function showing the goal of the game and move commands
def show_Instructions():
# Print main menu and commands
print("Cosmic Explorer Adventure")
print("Collect 7 items to win the game, or be destroyed by The Overseer.")
print("Move commands: go South, go North, go East, go West")
print("Add to Inventory: get 'item name'")
print("-----------------------------------")
show_Instructions()
def show_state(inventory, rooms, state):
# Display player's inventory and current room's item
print('Inventory: ', inventory)
print('You see a', rooms[state][0])
print("-")
move_input = input('Enter your move: ')
move_parts = move_input.split(maxsplit=1)
cmd = move_parts[0].lower()
direction = move_parts[1] if len(move_parts)>1 else "return"
return cmd, direction
# Corrected room definitions
rooms ={
'Launch Pad': ['Holo-Map', {'East': 'Control Room'}],
'Control Room': ['Quantum Key', {'South': 'Engineering Bay'}],
'Engineering Bay': ['Nano Drone', {'East': 'Research Lab'}],
'Research Lab': ['Anti-Gravity Boots', {'South': 'Zero-Gravity Chamber'}],
'Zero-Gravity Chamber': ['Plasma Blaster', {'South': 'Teleportation Hub'}],
'Teleportation Hub': ['Energy Shield', {'South': 'Storage Bay'}],
'Storage Bay': ['Exo-Armor', {'West': 'Artifact Vault'}],
'Artifact Vault': ['The Overseer']
}
state = 'Launch Pad'
inventory =[]
def get_new_state(state, direction):
# Function to get the new state based on the direction
new_state = state
for i in rooms:
if i == state and direction in rooms[i][1]:
new_state = rooms[i][1][direction]
break
return new_state
while True:
print('You are in the', state)
if state == 'Artifact Vault':
print('Battling The Overseer', end="")
for i in range(50):
for j in range(1000000):
pass
print(".", end=", flush=True")
print()
if len(inventory)==7:
print("Congratulations! You have collected all items and defeated The Overseer!")
else:
print('GAME OVER!')
print("Thanks for playing!")
break
cmd, direction = show_state(inventory, rooms, state)
if cmd == 'get':
# Check if the item is in the current room
if direction.lower()== rooms[state][0].lower():
if rooms[state][0] not in inventory:
inventory.append(rooms[state][0])
print(f'You obtained {rooms[state][0]}!')
else:
print(f'{rooms[state][0]} has already been collected!')
else:
print('No such item. Try again!')
elif cmd =='go':
new_state = get_new_state(state, direction)
if new_state == state:
print('Cannot go that way. Choose another direction.')
else:
state = new_state
elif cmd == 'exit':
print('Thanks for playing!')
break
else:
print('Invalid command! Please enter a valid command.')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions