Demo entry 6352172

Python

   

Submitted by anonymous on Mar 23, 2017 at 13:50
Language: Python 3. Code size: 10.4 kB.

import sys
import random

#This comes up whenever the user inputs something invalid. Acts as a reminder.
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
    global numtreasure
    global numbandit
    global gameWon
    global sea
    global banditcount
    playAgain="y"
        
    while playAgain=="y":

        print("\nWelcome to Option 1: Treasure Hunt Game!\n")

        #This asks the user their number of treasure chests and bandits. Essential for my program.
        numtreasure=int(input("Number of treasure chests:"))
        numbandit=int(input("Number of bandits:"))
        
        sea=setupGame() #Call the subroutine to set up the game

        #The following section is used to set up values for variables that are useful for the game
        coins=0
        treasurecount=0
        falserate=0
        timescount=0
        totalmoves=0
        save=0
        ir=0
        banditcount=0
        gameWon=False

        #These two statements act as a tip for my program. Improves impression from the users
        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)

                   
        treasureloop()

        #This section determines whether the game is finished or not
        if coins>=100:
            print("Congratulations! You have won the game by collecting 100 gold coins!\n")
            menu()
        if numtreasure==0:
            print("Unfortunately you have lost the game.")
            print()
            playAgain=input("Do you want to play again? (y/n)")
            playAgain=playAgain.lower()

def treasureloop():
    global sea
    global treasurecount
    global coins
    global Player1
    global Player2
    global falserate
    global timescount
    global totalmoves
    global save
    global ir
    global numtreasure
    global Bandit1
    global Bandit2
    global numbandit
    global gameWon
    global banditcount
    
    while not gameWon and coins<100:
        print()
        drawGrid(sea)
        #Call the subroutine to draw the current state of the game

        #This section tells the users the details of the grid
        print()
        print("Your number of moves:",totalmoves)
        print("You now have",coins,"coins.")
        print("\nNumber of treasure chests:",numtreasure)
        print("\nNumber of bandits:",numbandit)
        print("\nYour current coordinates are (",Player1,",",Player2+1,")")

        #This list saves the original coordinates of the Position Figure
        useful=[]
        useful.append(Player1)
        useful.append(Player2+1)
        
##        sea[Player1][Player2]=".  "

        #This helps python to decide what the previous spot on the previous grid will turn into
        if sea[Player1][Player2]=="p  " or sea[Player1][Player2]==".  ":
            sea[Player1][Player2]=".  "
        if sea[Player1][Player2]=="t  ":
            sea[Player1][Player2]=".  "
        if sea[Player1][Player2]=="t  ":
            sea[Player1][Player2]="t  "

        #This is where the users will input their next move, and the program will then move it
        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?"))
                Player1=Player1-tryX
                Player2=Player2-tryY
                if Player1>7 or Player1<0:
                    wrong()
                    Player1=useful[0]
                    Player2=useful[1]-1
                    sea[Player1][Player2]="p  "
                    treasureloop()
                elif Player2>8 or Player2<0:
                    wrong()
                    Player1=useful[0]
                    Player2=useful[1]-1
                    sea[Player1][Player2]="p  "
                    treasureloop()
                sea[tryX][tryY]=sea[Player1][Player2]
                
            elif leftright=="RIGHT":
                tryY=int(input("By how many squares?"))
                Player1=Player1-tryX
                Player2=Player2+tryY
                if Player1>7 or Player1<0:
                    wrong()
                    treasureloop()
                elif Player2>8 or Player2<0:
                    wrong()
                    treasureloop()
                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?"))
                Player1=Player1+tryX
                Player2=Player2-tryY
                if Player1>7 or Player1<0:
                    wrong()
                    treasureloop()
                    
                elif Player2>8 or Player2<0:
                    wrong()
                    treasureloop()
                sea[tryX][tryY]=sea[Player1][Player2]
            elif leftright=="RIGHT":
                tryY=int(input("By how many squares?"))
                Player1=Player1+tryX
                Player2=Player2+tryY
                if Player1>7 or Player1<0:
                    wrong()
                    treasureloop()
                elif Player2>8 or Player2<0:
                    wrong()
                    treasureloop()
                sea[tryX][tryY]=sea[Player1][Player2]
            else:
                wrong()
                continue
        else:
            wrong()
            continue

        #This helps python to calculate the number of moves the user has had in my game
        totalmoves=totalmoves+1

        #This is where python determines what the coordinates that the user inputted will
        #become a bandit, treasure chest, or stays the same etc.
        if sea[Player1][Player2]=="B  ":
            print("You have stepped into a bandit, and you have lost all your coins.\n")
            numtreasure=numtreasure-1
            coins=0

        if sea[Player1][Player2]=="2  ":
            sea[Player1][Player2]="B  "
            print("\nIt has turned into a bandit")
            coins=coins+10

        if sea[Player1][Player2]=="1  ":
            sea[Player1][Player2]="2  "
            print("This is your second time stepping into it.")
            coins=coins+10
            

        if sea[Player1][Player2]=="t  ":
            sea[Player1][Player2]="1  "
            print("\nYou have found a treasure chest.")
            coins=coins+10

        if numtreasure==0:
            print("Unfortunately you have lost the game.")
            print()
            menu()

def drawGrid(sea):
    #This is where my program draws the grid, and python will print out the grid based on this
    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(".  ", end="")

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

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

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

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

        print()
   
def setupGame():
    #This is where my treasure hunt game is first set up. This helps generate random locations for each of the treasure chest and bandit.
    
    global Treasure1
    global Treasure2
    global Bandit1
    global Bandit2
    global numtreasure
    global numbandit
    global numrows
    global numcolumns
    
    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 i in range(numtreasure):
        Treasure1=random.randrange(8)
        Treasure2=random.randrange(8)
        while sea[Treasure1][Treasure2]=="p  " or sea[Treasure1][Treasure2]=="b  " or sea[Treasure1][Treasure2]=="t  ":
            Treasure1=random.randrange(8)
            Treasure2=random.randrange(8)
            
        sea[Treasure1][Treasure2]="t  "

    for b in range(numbandit):
        Bandit1=random.randrange(8)
        Bandit2=random.randrange(8)
        while sea[Bandit1][Bandit2]=="p  " or sea[Bandit1][Bandit2]=="b  " or sea[Bandit1][Bandit2]=="t  ":
            Bandit1=random.randrange(8)
            Bandit2=random.randrange(8)
            
        sea[Bandit1][Bandit2]="B  "
     
    return sea

def menu():
    #This is my menu function. When the user starts the whole program, this is what they will see first.
    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 quit the program.")
    while True:
        try:
            response=input("")
            if response=="1":
                main()
            elif response=="2":
                print("You can now quit the program.")
                sys.exit()
                break
            else:
                wrong()
                print()
                menu()
        except ValueError:
            wrong()

menu()

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).