# Demo entry 6345796

Treasure Hunt

Submitted by anonymous on Feb 08, 2017 at 17:54
Language: Python 3. Code size: 8.3 kB.

```import sys
import random

def wrong():
print("\nOops, I am afraid you have inputted an invalid value.")

def main():
global Treasure1
global Treasure2
global coins
global treasurecount
global Player1
global Player2
global falserate
global timescount
global totalmoves
global save
global ir
playAgain="y"

while playAgain=="y":

sea=setupGame() #Call the subroutine to set up the game

coins=0
treasurecount=0
falserate=0
timescount=0
totalmoves=0
save=0
ir=0
gameWon=False
print("\nYou are starting from the bottom left corner of the grid.\n")
print("The player's position is represented by the letter P.")
Player1=random.randint(7,7)
Player2=random.randint(0,0)

def treasureloop():
global treasurecount
global coins
global Player1
global Player2
global falserate
global timescount
global totalmoves
global save
global ir
while not gameWon and coins<100:
print()
drawGrid(sea)
#Call the subroutine to draw the current state of the game
print()
print("You now have",coins,"coins.")
if falserate>=1:
Player1=random.randint(7,7)
Player2=random.randint(0,0)
print("\nYour current coordinates are (",Player1,",",Player2+1,")")
else:
print("\nYour current coordinates are (",Player1,",",Player2+1,")")

updown=input("Would you like to move up or down?")
updown=updown.upper()
if updown=="UP":
tryX=int(input("By how many squares?"))
leftright=input("Would you like to move left or right?")
leftright=leftright.upper()
if leftright=="LEFT":
tryY=int(input("By how many squares?"))
tryY=tryY
Player1=Player1-tryX
Player2=Player2-tryY
sea[tryX][tryY]==sea[Player1][Player2]

elif leftright=="RIGHT":
tryY=int(input("By how many squares?"))
tryY=tryY
Player1=Player1-tryX
Player2=Player2+tryY
sea[tryX][tryY]==sea[Player1][Player2]
else:
wrong()
continue

elif updown=="DOWN":
tryX=int(input("By how many squares?"))
leftright=input("Would you like to move left or right?")
leftright=leftright.upper()
if leftright=="LEFT":
tryY=int(input("By how many squares?"))
tryY=tryY
Player1=Player1+tryX
Player2=Player2-tryY
sea[tryX][tryY]==sea[Player1][Player2]
elif leftright=="RIGHT":
tryY=int(input("By how many squares?"))
tryY=tryY
Player1=Player1+tryX
Player2=Player2+tryY
sea[tryX][tryY]==sea[Player1][Player2]
else:
wrong()
continue

totalmoves=totalmoves+1
onelist=[]
onelist.append(Player1)
twolist=[]
twolist.append(Player2+1)

if sea[Player1][Player2]==sea[onelist][twolist-1]:
sea[Player1][Player2]="p  "
if sea[Player1][Player2]=="t  ":
print("\nYou have found a treasure hunt.\n")
coins=coins+10
treasurecount=+1
treasureloop()

if sea[Player1][Player2]=="B  ":
sea[Player1][Player2]=="p  "
coins=0
print("\nUnfortunately you have stepped into a bandit, and you have lost all your coins.")

elif sea[Player1][Player2]=="2":
sea[Player1][Player2]=="B  "
coins=coins+10
print("\nThe treasure hunt has become a Bandit.")
treasurecount=treasurecount+1
treasureloop()

elif treasurecount==1 and sea[Player1][Player2]=="t  ":
sea[Player1][Player2]=="2  "
print("\nJust a reminder, you have already visited this treasure hunt two times.")
coins=coins+10
treasurecount=treasurecount+1
treasureloop()

elif sea[Player1][Player2]==".  ":
sea[Player1][Player2]="p  "

treasureloop()

print()
drawGrid(sea)
print()

if gameWon==True:
print("Congratulations! You have managed to get over 100 gold coins!")
else:
print("Unfortunately you have lost the game.")
print()
playAgain=input("Do you want to play again? (y/n)")
playAgain=playAgain.lower()

def drawGrid(sea):
global tryX
global tryY
print("  1  2  3  4  5  6  7  8")
for x in range(8):
print(x, end=" ")
for y in range(8):
if sea[x][y]=="p  ":
print("P  ", end="")

if sea[x][y]=="B  ":
print("B  ", end="")

if sea[x][y]=="3  ":
print(".  ", end="")

if sea[x][y]=="2  ":
print(".  ", end="")

if sea[x][y]=="1  ":
print(".  ", end="")

if sea[x][y]=="t  ":
print("T  ", end="")

if sea[x][y]==".  ":
print(".  ", end="")

print()

def drawnewgrid(sea):
global coins
global save
print("\n")
for x in range(8):
for y in range(8):
if sea[x][y]=="p  ":
if totalmoves >=1:
sea[x][y]=save
elif save == "1  ":
sea[x][y]=="2  "
elif save == "2  ":
sea[x][y]=="B  "
tcount=tcount-1
else:
sea[x][y]=".  "

save=sea[Player1][Player2]
drawGrid(sea)

def setupGame():
global Treasure1
global Treasure2
sea=[[".  " for y in range(8)] for x in range(8)]

for p in range(1):
Player1=random.randint(7,7)
Player2=random.randint(0,0)

sea[Player1][Player2]="p  "

for b in range(5):
Bandit1=random.randrange(8)
Bandit2=random.randrange(8)
while sea[Bandit1][Bandit2]==sea[Player1][Player2] or sea[Bandit1][Bandit2]=="b  ":
Bandit1=random.randrange(8)
Bandit2=random.randrange(8)
sea[Bandit1][Bandit2]="B  "

for i in range(10):
Treasure1=random.randrange(8)
Treasure2=random.randrange(8)
while sea[Treasure1][Treasure2]==sea[Player1][Player2] or sea[Treasure1][Treasure2]=="b  " or sea[Treasure1][Treasure2]=="t  ":
Treasure1=random.randrange(8)
Treasure2=random.randrange(8)

sea[Treasure1][Treasure2]="t  "

return sea

def menu():
print("Welcome to the Treasure Hunt Game.")
print("==================================")
print("Would you like to play the game or quit the program?")
print("Press 1 if you would like to play the game.")
print("Press 2 if you would like to quite the program.")
while True:
response=input("")
if response=="1":
main()
elif response=="2":
print("You can now quit the program.")
sys.exit()
break
else:
wrong()
print()
menu()

menu()
```

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.