Python Treasure hunt board game
up vote
1
down vote
favorite
The code below is for a board game program, which works by placing the user on a grid which then tasks the user with finding all the treasure chests. Each chest landed on gives 10 gold however landing on a bandit means all your gold is taken. The end score should be given once the user has found all the chests.
A few features including the end game, score board, timer and more are still required to be added. For now I just want feedback on how to improve the code at its current state (the code could very well be simplified).
import random
import sys
import time
boardeasy=
boardmed=
boardhard=
current=[0,0]
Treasure1_Row = random.randint(0,8)
Treasure1_Col = random.randint(0,8)
Treasure2_Row = random.randint(0,8)
Treasure2_Col = random.randint(0,8)
Treasure3_Row = random.randint(0,8)
Treasure3_Col = random.randint(0,8)
Treasure4_Row = random.randint(0,8)
Treasure4_Col = random.randint(0,8)
Treasure5_Row = random.randint(0,8)
Treasure5_Col = random.randint(0,8)
Treasure6_Row = random.randint(0,8)
Treasure6_Col = random.randint(0,8)
Treasure7_Row = random.randint(0,8)
Treasure7_Col = random.randint(0,8)
Treasure8_Row = random.randint(0,8)
Treasure8_Col = random.randint(0,8)
Treasure9_Row = random.randint(0,8)
Treasure9_Col = random.randint(0,8)
Treasure10_Row = random.randint(0,8)
Treasure10_Col = random.randint(0,8)
Treasure11_Row = random.randint(0,8)
Treasure11_Col = random.randint(0,8)
Treasure12_Row = random.randint(0,8)
Treasure12_Col = random.randint(0,8)
Bandit1_Row = random.randint(0,8)
Bandit1_Col = random.randint(0,8)
Bandit2_Row = random.randint(0,8)
Bandit2_Col = random.randint(0,8)
Bandit3_Row = random.randint(0,8)
Bandit3_Col = random.randint(0,8)
Bandit4_Row = random.randint(0,8)
Bandit4_Col = random.randint(0,8)
Bandit5_Row = random.randint(0,8)
Bandit5_Col = random.randint(0,8)
Bandit6_Row = random.randint(0,8)
Bandit6_Col = random.randint(0,8)
Bandit7_Row = random.randint(0,8)
Bandit7_Col = random.randint(0,8)
Bandit8_Row = random.randint(0,8)
Bandit8_Col = random.randint(0,8)
Bandit9_Row = random.randint(0,8)
Bandit9_Col = random.randint(0,8)
Bandit10_Row = random.randint(0,8)
Bandit10_Col = random.randint(0,8)
Bandit11_Row = random.randint(0,8)
Bandit11_Col = random.randint(0,8)
Bandit12_Row = random.randint(0,8)
Bandit12_Col = random.randint(0,8)
Coins = 0
class user():
def __init__(self, username, userscore, usertime):
self.username = username
self.userscore = userscore
self.usertime = usertime
#For loop prints a new 8*8 grid after every move
for i in range(8):
b=
for j in range(8):
b.append(' ')
boardeasy.append(b)
def table_game_easy():
print(" 1 2 3 4 5 6 7 8")
print("---------------------------------")
print ('| ' + boardeasy[0][0] + '| ' + boardeasy[0][1] + ' | ' + boardeasy[0][2] + ' | ' + boardeasy[0][3] + ' | ' + boardeasy[0][4] + ' | ' + boardeasy[0][5] + ' | ' + boardeasy[0][6] + ' | ' + boardeasy[0][7] + ' | ' + '1')
print("---------------------------------")
print ('| ' + boardeasy[1][0] + '| ' + boardeasy[1][1] + ' | ' + boardeasy[1][2] + ' | ' + boardeasy[1][3] + ' | ' + boardeasy[1][4] + ' | ' + boardeasy[1][5] + ' | ' + boardeasy[1][6] + ' | ' + boardeasy[1][7] + ' | ' + '2')
print("---------------------------------")
print ('| ' + boardeasy[2][0] + '| ' + boardeasy[2][1] + ' | ' + boardeasy[2][2] + ' | ' + boardeasy[2][3] + ' | ' + boardeasy[2][4] + ' | ' + boardeasy[2][5] + ' | ' + boardeasy[2][6] + ' | ' + boardeasy[2][7] + ' | ' + '3')
print("---------------------------------")
print ('| ' + boardeasy[3][0] + '| ' + boardeasy[3][1] + ' | ' + boardeasy[3][2] + ' | ' + boardeasy[3][3] + ' | ' + boardeasy[3][4] + ' | ' + boardeasy[3][5] + ' | ' + boardeasy[3][6] + ' | ' + boardeasy[3][7] + ' | ' + '4')
print("---------------------------------")
print ('| ' + boardeasy[4][0] + '| ' + boardeasy[4][1] + ' | ' + boardeasy[4][2] + ' | ' + boardeasy[4][3] + ' | ' + boardeasy[4][4] + ' | ' + boardeasy[4][5] + ' | ' + boardeasy[4][6] + ' | ' + boardeasy[4][7] + ' | ' + '5')
print("---------------------------------")
print ('| ' + boardeasy[5][0] + '| ' + boardeasy[5][1] + ' | ' + boardeasy[4][2] + ' | ' + boardeasy[5][3] + ' | ' + boardeasy[5][4] + ' | ' + boardeasy[5][5] + ' | ' + boardeasy[5][6] + ' | ' + boardeasy[5][7] + ' | ' + '6')
print("---------------------------------")
print ('| ' + boardeasy[6][0] + '| ' + boardeasy[6][1] + ' | ' + boardeasy[5][2] + ' | ' + boardeasy[6][3] + ' | ' + boardeasy[6][4] + ' | ' + boardeasy[6][5] + ' | ' + boardeasy[6][6] + ' | ' + boardeasy[6][7] + ' | ' + '7')
print("---------------------------------")
print ('| ' + boardeasy[7][0] + '| ' + boardeasy[7][1] + ' | ' + boardeasy[7][2] + ' | ' + boardeasy[7][3] + ' | ' + boardeasy[7][4] + ' | ' + boardeasy[7][5] + ' | ' + boardeasy[7][6] + ' | ' + boardeasy[7][7] + ' | ' + '8')
print("---------------------------------")
#Variables which will store a certain range
Treasure1_Row = random.randint(0,8)
Treasure1_Col = random.randint(0,8)
Treasure2_Row = random.randint(0,8)
Treasure2_Col = random.randint(0,8)
Treasure3_Row = random.randint(0,8)
Treasure3_Col = random.randint(0,8)
Treasure4_Row = random.randint(0,8)
Treasure4_Col = random.randint(0,8)
Treasure5_Row = random.randint(0,8)
Treasure5_Col = random.randint(0,8)
Treasure6_Row = random.randint(0,8)
Treasure6_Col = random.randint(0,8)
Treasure7_Row = random.randint(0,8)
Treasure7_Col = random.randint(0,8)
Treasure8_Row = random.randint(0,8)
Treasure8_Col = random.randint(0,8)
Treasure9_Row = random.randint(0,8)
Treasure9_Col = random.randint(0,8)
Treasure10_Row = random.randint(0,8)
Treasure10_Col = random.randint(0,8)
Bandit1_Row = random.randint(0,8)
Bandit1_Col = random.randint(0,8)
Bandit2_Row = random.randint(0,8)
Bandit2_Col = random.randint(0,8)
Bandit3_Row = random.randint(0,8)
Bandit3_Col = random.randint(0,8)
Bandit4_Row = random.randint(0,8)
Bandit4_Col = random.randint(0,8)
Bandit5_Row = random.randint(0,8)
Bandit5_Col = random.randint(0,8)
# For loop prints a new 10*10 grid after every move
for i in range(10):
b=
for j in range(10):
b.append(' ')
boardmed.append(b)
def table_game_meduim():
print(" 1 2 3 4 5 6 7 8 9 10")
print("-----------------------------------------")
print ('| ' + boardmed[0][0] + '| ' + boardmed[0][1] + ' | ' + boardmed[0][2] + ' | ' + boardmed[0][3] + ' | ' + boardmed[0][4] + ' | ' + boardmed[0][5] + ' | ' + boardmed[0][6] + ' | ' + boardmed[0][7] + ' | ' + boardmed[0][8] + ' | ' + boardmed[0][9] + ' | ' + '1')
print("-----------------------------------------")
print ('| ' + boardmed[1][0] + '| ' + boardmed[1][1] + ' | ' + boardmed[1][2] + ' | ' + boardmed[1][3] + ' | ' + boardmed[1][4] + ' | ' + boardmed[1][5] + ' | ' + boardmed[1][6] + ' | ' + boardmed[1][7] + ' | ' + boardmed[1][8] + ' | ' + boardmed[1][9] + ' | ' + '2')
print("-----------------------------------------")
print ('| ' + boardmed[2][0] + '| ' + boardmed[2][1] + ' | ' + boardmed[2][2] + ' | ' + boardmed[2][3] + ' | ' + boardmed[2][4] + ' | ' + boardmed[2][5] + ' | ' + boardmed[2][6] + ' | ' + boardmed[2][7] + ' | ' + boardmed[2][8] + ' | ' + boardmed[2][9] + ' | ' + '3')
print("-----------------------------------------")
print ('| ' + boardmed[3][0] + '| ' + boardmed[3][1] + ' | ' + boardmed[3][2] + ' | ' + boardmed[3][3] + ' | ' + boardmed[3][4] + ' | ' + boardmed[3][5] + ' | ' + boardmed[3][6] + ' | ' + boardmed[3][7] + ' | ' + boardmed[3][8] + ' | ' + boardmed[3][9] + ' | ' + '4')
print("-----------------------------------------")
print ('| ' + boardmed[4][0] + '| ' + boardmed[4][1] + ' | ' + boardmed[4][2] + ' | ' + boardmed[4][3] + ' | ' + boardmed[4][4] + ' | ' + boardmed[4][5] + ' | ' + boardmed[4][6] + ' | ' + boardmed[4][7] + ' | ' + boardmed[4][8] + ' | ' + boardmed[4][9] + ' | ' + '5')
print("-----------------------------------------")
print ('| ' + boardmed[5][0] + '| ' + boardmed[5][1] + ' | ' + boardmed[5][2] + ' | ' + boardmed[5][3] + ' | ' + boardmed[5][4] + ' | ' + boardmed[5][5] + ' | ' + boardmed[5][6] + ' | ' + boardmed[5][7] + ' | ' + boardmed[5][8] + ' | ' + boardmed[5][9] + ' | ' + '6')
print("-----------------------------------------")
print ('| ' + boardmed[6][0] + '| ' + boardmed[6][1] + ' | ' + boardmed[6][2] + ' | ' + boardmed[6][3] + ' | ' + boardmed[6][4] + ' | ' + boardmed[6][5] + ' | ' + boardmed[6][6] + ' | ' + boardmed[6][7] + ' | ' + boardmed[6][8] + ' | ' + boardmed[6][9] + ' | ' + '7')
print("-----------------------------------------")
print ('| ' + boardmed[7][0] + '| ' + boardmed[7][1] + ' | ' + boardmed[7][2] + ' | ' + boardmed[7][3] + ' | ' + boardmed[7][4] + ' | ' + boardmed[7][5] + ' | ' + boardmed[7][6] + ' | ' + boardmed[7][7] + ' | ' + boardmed[7][8] + ' | ' + boardmed[7][9] + ' | ' + '8')
print("-----------------------------------------")
print ('| ' + boardmed[8][0] + '| ' + boardmed[8][1] + ' | ' + boardmed[8][2] + ' | ' + boardmed[8][3] + ' | ' + boardmed[8][4] + ' | ' + boardmed[8][5] + ' | ' + boardmed[8][6] + ' | ' + boardmed[8][7] + ' | ' + boardmed[8][8] + ' | ' + boardmed[8][9] + ' | ' + '9')
print("-----------------------------------------")
print ('| ' + boardmed[9][0] + '| ' + boardmed[9][1] + ' | ' + boardmed[9][2] + ' | ' + boardmed[9][3] + ' | ' + boardmed[9][4] + ' | ' + boardmed[9][5] + ' | ' + boardmed[9][6] + ' | ' + boardmed[9][7] + ' | ' + boardmed[9][8] + ' | ' + boardmed[9][9] + ' | ' + '10')
print("-----------------------------------------")
Treasure1_Row = random.randint(0,10)
Treasure1_Col = random.randint(0,10)
Treasure2_Row = random.randint(0,10)
Treasure2_Col = random.randint(0,10)
Treasure3_Row = random.randint(0,10)
Treasure3_Col = random.randint(0,10)
Treasure4_Row = random.randint(0,10)
Treasure4_Col = random.randint(0,10)
Treasure5_Row = random.randint(0,10)
Treasure5_Col = random.randint(0,10)
Treasure6_Row = random.randint(0,10)
Treasure6_Col = random.randint(0,10)
Treasure7_Row = random.randint(0,10)
Treasure7_Col = random.randint(0,10)
Treasure8_Row = random.randint(0,10)
Treasure8_Col = random.randint(0,10)
Treasure9_Row = random.randint(0,10)
Treasure9_Col = random.randint(0,10)
Treasure10_Row = random.randint(0,10)
Treasure10_Col = random.randint(0,10)
Bandit1_Row = random.randint(0,10)
Bandit1_Col = random.randint(0,10)
Bandit2_Row = random.randint(0,10)
Bandit2_Col = random.randint(0,10)
Bandit3_Row = random.randint(0,10)
Bandit3_Col = random.randint(0,10)
Bandit4_Row = random.randint(0,10)
Bandit4_Col = random.randint(0,10)
Bandit5_Row = random.randint(0,10)
Bandit5_Col = random.randint(0,10)
Bandit6_Row = random.randint(0,10)
Bandit6_Col = random.randint(0,10)
Bandit7_Row = random.randint(0,10)
Bandit7_Col = random.randint(0,10)
# For loop prints a new 12*12 grid after every move
for i in range(12):
b=
for j in range(12):
b.append(' ')
boardhard.append(b)
def table_game_hard():
print(" 1 2 3 4 5 6 7 8 9 10 11 12")
print("-------------------------------------------------")
print ('| ' + boardhard[0][0] + '| ' + boardhard[0][1] + ' | ' + boardhard[0][2] + ' | ' + boardhard[0][3] + ' | ' + boardhard[0][4] + ' | ' + boardhard[0][5] + ' | ' + boardhard[0][6] + ' | ' + boardhard[0][7] + ' | ' + boardhard[0][8] + ' | ' + boardhard[0][9] + ' | '+ boardhard[0][10] + ' | ' + boardhard[0][11] + ' | ' + '1')
print("-------------------------------------------------")
print ('| ' + boardhard[1][0] + '| ' + boardhard[1][1] + ' | ' + boardhard[1][2] + ' | ' + boardhard[1][3] + ' | ' + boardhard[1][4] + ' | ' + boardhard[1][5] + ' | ' + boardhard[1][6] + ' | ' + boardhard[1][7] + ' | ' + boardhard[1][8] + ' | ' + boardhard[1][9] + ' | '+ boardhard[1][10] + ' | ' + boardhard[1][11] + ' | ' + '2')
print("-------------------------------------------------")
print ('| ' + boardhard[2][0] + '| ' + boardhard[2][1] + ' | ' + boardhard[2][2] + ' | ' + boardhard[2][3] + ' | ' + boardhard[2][4] + ' | ' + boardhard[2][5] + ' | ' + boardhard[2][6] + ' | ' + boardhard[2][7] + ' | ' + boardhard[2][8] + ' | ' + boardhard[2][9] + ' | '+ boardhard[2][10] + ' | ' + boardhard[2][11] + ' | ' + '3')
print("-------------------------------------------------")
print ('| ' + boardhard[3][0] + '| ' + boardhard[3][1] + ' | ' + boardhard[3][2] + ' | ' + boardhard[3][3] + ' | ' + boardhard[3][4] + ' | ' + boardhard[3][5] + ' | ' + boardhard[3][6] + ' | ' + boardhard[3][7] + ' | ' + boardhard[3][8] + ' | ' + boardhard[3][9] + ' | '+ boardhard[3][10] + ' | ' + boardhard[3][11] + ' | ' + '4')
print("-------------------------------------------------")
print ('| ' + boardhard[4][0] + '| ' + boardhard[4][1] + ' | ' + boardhard[4][2] + ' | ' + boardhard[4][3] + ' | ' + boardhard[4][4] + ' | ' + boardhard[4][5] + ' | ' + boardhard[4][6] + ' | ' + boardhard[4][7] + ' | ' + boardhard[4][8] + ' | ' + boardhard[4][9] + ' | '+ boardhard[4][10] + ' | ' + boardhard[4][11] + ' | ' + '5')
print("-------------------------------------------------")
print ('| ' + boardhard[5][0] + '| ' + boardhard[5][1] + ' | ' + boardhard[5][2] + ' | ' + boardhard[5][3] + ' | ' + boardhard[5][4] + ' | ' + boardhard[5][5] + ' | ' + boardhard[5][6] + ' | ' + boardhard[5][7] + ' | ' + boardhard[5][8] + ' | ' + boardhard[5][9] + ' | '+ boardhard[5][10] + ' | ' + boardhard[5][11] + ' | ' + '6')
print("-------------------------------------------------")
print ('| ' + boardhard[6][0] + '| ' + boardhard[6][1] + ' | ' + boardhard[6][2] + ' | ' + boardhard[6][3] + ' | ' + boardhard[6][4] + ' | ' + boardhard[6][5] + ' | ' + boardhard[6][6] + ' | ' + boardhard[6][7] + ' | ' + boardhard[6][8] + ' | ' + boardhard[6][9] + ' | '+ boardhard[6][10] + ' | ' + boardhard[6][11] + ' | ' + '7')
print("-------------------------------------------------")
print ('| ' + boardhard[7][0] + '| ' + boardhard[7][1] + ' | ' + boardhard[7][2] + ' | ' + boardhard[7][3] + ' | ' + boardhard[7][4] + ' | ' + boardhard[7][5] + ' | ' + boardhard[7][6] + ' | ' + boardhard[7][7] + ' | ' + boardhard[7][8] + ' | ' + boardhard[7][9] + ' | '+ boardhard[7][10] + ' | ' + boardhard[7][11] + ' | ' + '8')
print("-------------------------------------------------")
print ('| ' + boardhard[8][0] + '| ' + boardhard[8][1] + ' | ' + boardhard[8][2] + ' | ' + boardhard[8][3] + ' | ' + boardhard[8][4] + ' | ' + boardhard[8][5] + ' | ' + boardhard[8][6] + ' | ' + boardhard[8][7] + ' | ' + boardhard[8][8] + ' | ' + boardhard[8][9] + ' | '+ boardhard[8][10] + ' | ' + boardhard[8][11] + ' | ' + '9')
print("-------------------------------------------------")
print ('| ' + boardhard[9][0] + '| ' + boardhard[9][1] + ' | ' + boardhard[9][2] + ' | ' + boardhard[9][3] + ' | ' + boardhard[9][4] + ' | ' + boardhard[9][5] + ' | ' + boardhard[9][6] + ' | ' + boardhard[9][7] + ' | ' + boardhard[9][8] + ' | ' + boardhard[9][9] + ' | '+ boardhard[9][10] + ' | ' + boardhard[9][11] + ' | ' + '10')
print("-------------------------------------------------")
print ('| ' + boardhard[10][0] + '| ' + boardhard[10][1] + ' | ' + boardhard[10][2] + ' | ' + boardhard[10][3] + ' | ' + boardhard[10][4] + ' | ' + boardhard[10][5] + ' | ' + boardhard[10][6] + ' | ' + boardhard[10][7] + ' | ' + boardhard[10][8] + ' | ' + boardhard[10][9] + ' | ' + boardhard[10][10] + ' | ' + boardhard[10][11] + ' | ' + '11')
print("-------------------------------------------------")
print ('| ' + boardhard[11][0] + '| ' + boardhard[11][1] + ' | ' + boardhard[11][2] + ' | ' + boardhard[11][3] + ' | ' + boardhard[11][4] + ' | ' + boardhard[11][5] + ' | ' + boardhard[11][6] + ' | ' + boardhard[11][7] + ' | ' + boardhard[11][8] + ' | ' + boardhard[11][9] + ' | ' + boardhard[11][11] + ' | ' + boardhard[11][11] + ' | ' + '12')
print("-------------------------------------------------")
Treasure1_Row = random.randint(0,12)
Treasure1_Col = random.randint(0,12)
Treasure2_Row = random.randint(0,12)
Treasure2_Col = random.randint(0,12)
Treasure3_Row = random.randint(0,12)
Treasure3_Col = random.randint(0,12)
Treasure4_Row = random.randint(0,12)
Treasure4_Col = random.randint(0,12)
Treasure5_Row = random.randint(0,12)
Treasure5_Col = random.randint(0,12)
Treasure6_Row = random.randint(0,12)
Treasure6_Col = random.randint(0,12)
Treasure7_Row = random.randint(0,12)
Treasure7_Col = random.randint(0,12)
Treasure8_Row = random.randint(0,12)
Treasure8_Col = random.randint(0,12)
Treasure9_Row = random.randint(0,12)
Treasure9_Col = random.randint(0,12)
Treasure10_Row = random.randint(0,12)
Treasure10_Col = random.randint(0,12)
Bandit1_Row = random.randint(0,12)
Bandit1_Col = random.randint(0,12)
Bandit2_Row = random.randint(0,12)
Bandit2_Col = random.randint(0,12)
Bandit3_Row = random.randint(0,12)
Bandit3_Col = random.randint(0,12)
Bandit4_Row = random.randint(0,12)
Bandit4_Col = random.randint(0,12)
Bandit5_Row = random.randint(0,12)
Bandit5_Col = random.randint(0,12)
Bandit6_Row = random.randint(0,12)
Bandit6_Col = random.randint(0,12)
Bandit7_Row = random.randint(0,12)
Bandit7_Col = random.randint(0,12)
Bandit8_Row = random.randint(0,12)
Bandit8_Col = random.randint(0,12)
Bandit9_Row = random.randint(0,12)
Bandit9_Col = random.randint(0,12)
#this function is in charge of downwards movement
def down(num,lev):
num=(num+current[0])%lev#The % formats this equation
current[0]=num
#this function is in charge of downwards movement
def right(num,lev):
num=(num+current[1])%lev #The % formats this equation
current[1]=num
#this function is in charge of downwards movement
def left(num,lev):
if current[1]-num>=0:
current[1]=current[1]-num
else:
current[1]=current[1]-num+lev
#this function is in charge of downwards movement
def up(num,lev):
if current[0]-num>=0:
current[0]=current[0]-num
else:
current[0]=current[0]-num+lev
def easy_level(Coins):
#This function is for the movement of the game in easy difficulty
while True:
oldcurrent=current
boardeasy[oldcurrent[0]][oldcurrent[1]]='*'
table_game_easy()
boardeasy[oldcurrent[0]][oldcurrent[1]]=' '
n = input('Enter the direction followed by the number Ex:Up 5 , Number should be < 8 n')
n=n.split()
if n[0].lower() not in ['up','left','down','right']:#Validates input
print('Wrong command, please input again')
continue
elif n[0].lower()=='up':
up(int(n[1].lower()),8)#Boundary is set to 8 as the 'easy' grid is a 8^8
elif n[0].lower()=='down':
down(int(n[1].lower()),8)
elif n[0].lower()=='left':
left(int(n[1].lower()),8)
elif n[0].lower()=='right':
right(int(n[1].lower()),8)
print("8 chests left")
print("8 bandits left")
print("Coins:",Coins)#Acts as a counter, displays the number of coins that the player has
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
or current[0] == Treasure3_Row and current[1] == Treasure3_Col
or current[0] == Treasure4_Row and current[1] == Treasure4_Col
or current[0] == Treasure5_Row and current[1] == Treasure5_Col
or current[0] == Treasure6_Row and current[1] == Treasure6_Col
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col:
print("Hooray! You have found booty! +10 gold")
Coins = Coins+10 #Adds an additional 10 points
print("Coins:",Coins)
if current[0] == Bandit1_Row and current[1] == Bandit1_Col
or current[0] == Bandit2_Row and current[1] == Bandit2_Col
or current[0] == Bandit3_Row and current[1] == Bandit3_Col
or current[0] == Bandit4_Row and current[1] == Bandit4_Col
or current[0] == Bandit5_Row and current[1] == Bandit5_Col
or current[0] == Bandit6_Row and current[1] == Bandit6_Col
or current[0] == Bandit7_Row and current[1] == Bandit7_Col
or current[0] == Bandit8_Row and current[1] == Bandit8_Col:
print("Oh no! You have landed on a bandit...they steal all your coins!")
Coins = Coins-Coins #Removes all coins
print("Coins:",Coins)
boardeasy[current[0]][current[1]]='*'#sets value to players position
def med_level(Coins):
#This function is for the movement of the game in medium difficulty
while True:
oldcurrent=current
boardmed[oldcurrent[0]][oldcurrent[1]]='*'
table_game_meduim()
boardmed[oldcurrent[0]][oldcurrent[1]]=' '
n = input('Enter the direction followed by the number Ex:Up 5 , Number should be < 10 n')
n=n.split()
if n[0].lower() not in ['up','left','down','right']:
print('wrong command')
continue
elif n[0].lower()=='up':
up(int(n[1].lower()),10)#Boundary is set to 10 as the 'easy' grid is a 10^10
elif n[0].lower()=='down':
down(int(n[1].lower()),10)
elif n[0].lower()=='left':
left(int(n[1].lower()),10)
elif n[0].lower()=='right':
right(int(n[1].lower()),10)
print("10 chests left")
print("10 bandits left")
print("Coins:",Coins)#Acts as a counter, displays the number of coins that the player has
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
or current[0] == Treasure3_Row and current[1] == Treasure3_Col
or current[0] == Treasure4_Row and current[1] == Treasure4_Col
or current[0] == Treasure5_Row and current[1] == Treasure5_Col
or current[0] == Treasure6_Row and current[1] == Treasure6_Col
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col
or current[0] == Treasure9_Row and current[1] == Treasure9_Col
or current[0] == Treasure10_Row and current[1] == Treasure10_Col:
print("Hooray! You have found booty! +10 gold")
Coins = Coins+10 #Adds an additional 10 points
print("Coins:",Coins)
if current[0] == Bandit1_Row and current[1] == Bandit1_Col
or current[0] == Bandit2_Row and current[1] == Bandit2_Col
or current[0] == Bandit3_Row and current[1] == Bandit3_Col
or current[0] == Bandit4_Row and current[1] == Bandit4_Col
or current[0] == Bandit5_Row and current[1] == Bandit5_Col
or current[0] == Bandit6_Row and current[1] == Bandit6_Col
or current[0] == Bandit7_Row and current[1] == Bandit7_Col
or current[0] == Bandit8_Row and current[1] == Bandit8_Col
or current[0] == Bandit9_Row and current[1] == Bandit9_Col
or current[0] == Bandit10_Row and current[1] == Bandit10_Col:
print("Oh no! You have landed on a bandit...they steal all your coins!")
Coins = Coins-Coins #Removes all coins
print("Coins:",Coins)
boardmed[current[0]][current[1]]='*'
def hard_level(Coins):
#This function is for the movement of the game in hard difficulty
while True:
oldcurrent=current
boardhard[oldcurrent[0]][oldcurrent[1]]='*'
table_game_hard()
boardhard[oldcurrent[0]][oldcurrent[1]]=' '
n = input('Enter the direction followed by the number Ex:Up 5 , Number should be < 12 n')
n=n.split()
if n[0].lower() not in ['up','left','down','right']:
print('wrong command')
continue
elif n[0].lower()=='up':
up(int(n[1].lower()),12)#Boundary is set to 12 as the 'hard' grid is a 12^12
elif n[0].lower()=='down':
down(int(n[1].lower()),12)
elif n[0].lower()=='left':
left(int(n[1].lower()),12)
elif n[0].lower()=='right':
right(int(n[1].lower()),12)
print("12 chests left")
print("12 bandits left")
print("Coins:",Coins)#Acts as a counter, displays the number of coins that the player has
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
or current[0] == Treasure3_Row and current[1] == Treasure3_Col
or current[0] == Treasure4_Row and current[1] == Treasure4_Col
or current[0] == Treasure5_Row and current[1] == Treasure5_Col
or current[0] == Treasure6_Row and current[1] == Treasure6_Col
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col
or current[0] == Treasure9_Row and current[1] == Treasure9_Col
or current[0] == Treasure10_Row and current[1] == Treasure10_Col
or current[0] == Treasure11_Row and current[1] == Treasure11_Col
or current[0] == Treasure12_Row and current[1] == Treasure12_Col:
print("Hooray! You have found booty! +10 gold")
Coins = Coins+10 #Adds an additional 10 points
print("Coins:",Coins)
if current[0] == Bandit1_Row and current[1] == Bandit1_Col
or current[0] == Bandit2_Row and current[1] == Bandit2_Col
or current[0] == Bandit3_Row and current[1] == Bandit3_Col
or current[0] == Bandit4_Row and current[1] == Bandit4_Col
or current[0] == Bandit5_Row and current[1] == Bandit5_Col
or current[0] == Bandit6_Row and current[1] == Bandit6_Col
or current[0] == Bandit7_Row and current[1] == Bandit7_Col
or current[0] == Bandit8_Row and current[1] == Bandit8_Col
or current[0] == Bandit9_Row and current[1] == Bandit9_Col
or current[0] == Bandit10_Row and current[1] == Bandit10_Col
or current[0] == Bandit11_Row and current[1] == Bandit11_Col
or current[0] == Bandit12_Row and current[1] == Bandit12_Col:
print("Oh no! You have landed on a bandit...they steal all your coins!")
Coins = Coins-Coins #Removes all coins
print("Coins:",Coins)
boardhard[current[0]][current[1]]='*'
def instruct():
difficulty = input("""Before the game starts, please consider what difficulty
would you like to play in, easy, medium or (if you're brave) hard.
""")
if difficulty == "easy":
print("That's great! Lets continue.")
time.sleep(1)#Allows the user time to get ready
print("initiating game in...")
print()
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
for i in range(3):
print("")
easy_level(Coins)
elif difficulty == "medium":
print("That's great! Lets continue.")
time.sleep(1)#Allows the user time to get ready
print("initiating game in...")
print()
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
for i in range(3):
print("")
med_level(Coins)
elif difficulty == "hard":
print("That's great! Lets continue.")
time.sleep(1)#Allows the user time to get ready
print("initiating game in...")
print()
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
for i in range(3):
print("")
hard_level(Coins)
else:
print("Sorry, that is an invalid answer. Please restart the programme")
def menu():
#This function lets the user quit the application or progress to playing.
print("")
print ("Are you sure you wish to play this game? Please answer either yes or no.")
choice1 = input() # Sets variable to user input
if choice1.lower().startswith('y'):
print("Okay lets continue then!")
elif choice1.lower().startswith('n'):
print("Thank you, I hope you will play next time!")
print("")
quit("Thank you for playing!")#Terminates the programme
else:
print("Sorry, that is an invalid answer. Please restart the programme")
print("")
quit()
instruct()
def showInstructions():
time.sleep(1.0)
print("Instructions of the game:")
time.sleep(1.0)
print("""
You are a treasure hunter, your goal is to collect atleast 100 gold by the end
of the game from treasure chests randomly scattered across the grid. There are
10 chests within a grid (this can be changed based on difficulty) and each
treasure chest is worth 10 gold but can only be reclaimed 3 times before it is
replaced by a bandit. Landing on a bandit will cause you to lose all of your
gold and if all the chests have been replaced by bandits and you have less then
100 gold this means you lose!
Press enter to continue...""")
input()
print("""
At the start of the game, you always begin at the top right of the grid.
Below is a representation of the game:
* 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Press enter to continue...""")
input()
print("""
When deciding where to move, you should input the direct co-ordinates of your
desired location. For instance:
Enter the direction followed by the number Ex: Up 5 , Number should be < 8
right 3
0 0 0 * 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Unlucky move! You have found nothing!
If nothing on the grid changes , this means that your move was a bust! Landing
on nothing isn't displayed on the grid.
Press enter to continue...""")
input()
print("""
Enter the direction followed by the number Ex: Up 5 , Number should be < 8
down 4
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Hooray! You have found booty! +10 gold
Here you can see that the use has landed on a chest!
As you land on chest, they get replaced by bandits. Be sure to remember the
previous locations so you don't accidently land on a bandit and lose all
your gold!
Press enter to continue...""")
input()
#Introduces the user
print('Welcome to the Treasure hunt!')
time.sleep(0.3)
print()
time.sleep(0.3)
print('Would you like to see the instructions? (yes/no)')
if input().lower().startswith('y'):#If function checks for the first letter
showInstructions()
elif input == 'no' or 'No':
print("Lets continue then!")#Calls the function which displays instructions
else:
print("Please check your input and try again.")
menu()
python game
New contributor
add a comment |
up vote
1
down vote
favorite
The code below is for a board game program, which works by placing the user on a grid which then tasks the user with finding all the treasure chests. Each chest landed on gives 10 gold however landing on a bandit means all your gold is taken. The end score should be given once the user has found all the chests.
A few features including the end game, score board, timer and more are still required to be added. For now I just want feedback on how to improve the code at its current state (the code could very well be simplified).
import random
import sys
import time
boardeasy=
boardmed=
boardhard=
current=[0,0]
Treasure1_Row = random.randint(0,8)
Treasure1_Col = random.randint(0,8)
Treasure2_Row = random.randint(0,8)
Treasure2_Col = random.randint(0,8)
Treasure3_Row = random.randint(0,8)
Treasure3_Col = random.randint(0,8)
Treasure4_Row = random.randint(0,8)
Treasure4_Col = random.randint(0,8)
Treasure5_Row = random.randint(0,8)
Treasure5_Col = random.randint(0,8)
Treasure6_Row = random.randint(0,8)
Treasure6_Col = random.randint(0,8)
Treasure7_Row = random.randint(0,8)
Treasure7_Col = random.randint(0,8)
Treasure8_Row = random.randint(0,8)
Treasure8_Col = random.randint(0,8)
Treasure9_Row = random.randint(0,8)
Treasure9_Col = random.randint(0,8)
Treasure10_Row = random.randint(0,8)
Treasure10_Col = random.randint(0,8)
Treasure11_Row = random.randint(0,8)
Treasure11_Col = random.randint(0,8)
Treasure12_Row = random.randint(0,8)
Treasure12_Col = random.randint(0,8)
Bandit1_Row = random.randint(0,8)
Bandit1_Col = random.randint(0,8)
Bandit2_Row = random.randint(0,8)
Bandit2_Col = random.randint(0,8)
Bandit3_Row = random.randint(0,8)
Bandit3_Col = random.randint(0,8)
Bandit4_Row = random.randint(0,8)
Bandit4_Col = random.randint(0,8)
Bandit5_Row = random.randint(0,8)
Bandit5_Col = random.randint(0,8)
Bandit6_Row = random.randint(0,8)
Bandit6_Col = random.randint(0,8)
Bandit7_Row = random.randint(0,8)
Bandit7_Col = random.randint(0,8)
Bandit8_Row = random.randint(0,8)
Bandit8_Col = random.randint(0,8)
Bandit9_Row = random.randint(0,8)
Bandit9_Col = random.randint(0,8)
Bandit10_Row = random.randint(0,8)
Bandit10_Col = random.randint(0,8)
Bandit11_Row = random.randint(0,8)
Bandit11_Col = random.randint(0,8)
Bandit12_Row = random.randint(0,8)
Bandit12_Col = random.randint(0,8)
Coins = 0
class user():
def __init__(self, username, userscore, usertime):
self.username = username
self.userscore = userscore
self.usertime = usertime
#For loop prints a new 8*8 grid after every move
for i in range(8):
b=
for j in range(8):
b.append(' ')
boardeasy.append(b)
def table_game_easy():
print(" 1 2 3 4 5 6 7 8")
print("---------------------------------")
print ('| ' + boardeasy[0][0] + '| ' + boardeasy[0][1] + ' | ' + boardeasy[0][2] + ' | ' + boardeasy[0][3] + ' | ' + boardeasy[0][4] + ' | ' + boardeasy[0][5] + ' | ' + boardeasy[0][6] + ' | ' + boardeasy[0][7] + ' | ' + '1')
print("---------------------------------")
print ('| ' + boardeasy[1][0] + '| ' + boardeasy[1][1] + ' | ' + boardeasy[1][2] + ' | ' + boardeasy[1][3] + ' | ' + boardeasy[1][4] + ' | ' + boardeasy[1][5] + ' | ' + boardeasy[1][6] + ' | ' + boardeasy[1][7] + ' | ' + '2')
print("---------------------------------")
print ('| ' + boardeasy[2][0] + '| ' + boardeasy[2][1] + ' | ' + boardeasy[2][2] + ' | ' + boardeasy[2][3] + ' | ' + boardeasy[2][4] + ' | ' + boardeasy[2][5] + ' | ' + boardeasy[2][6] + ' | ' + boardeasy[2][7] + ' | ' + '3')
print("---------------------------------")
print ('| ' + boardeasy[3][0] + '| ' + boardeasy[3][1] + ' | ' + boardeasy[3][2] + ' | ' + boardeasy[3][3] + ' | ' + boardeasy[3][4] + ' | ' + boardeasy[3][5] + ' | ' + boardeasy[3][6] + ' | ' + boardeasy[3][7] + ' | ' + '4')
print("---------------------------------")
print ('| ' + boardeasy[4][0] + '| ' + boardeasy[4][1] + ' | ' + boardeasy[4][2] + ' | ' + boardeasy[4][3] + ' | ' + boardeasy[4][4] + ' | ' + boardeasy[4][5] + ' | ' + boardeasy[4][6] + ' | ' + boardeasy[4][7] + ' | ' + '5')
print("---------------------------------")
print ('| ' + boardeasy[5][0] + '| ' + boardeasy[5][1] + ' | ' + boardeasy[4][2] + ' | ' + boardeasy[5][3] + ' | ' + boardeasy[5][4] + ' | ' + boardeasy[5][5] + ' | ' + boardeasy[5][6] + ' | ' + boardeasy[5][7] + ' | ' + '6')
print("---------------------------------")
print ('| ' + boardeasy[6][0] + '| ' + boardeasy[6][1] + ' | ' + boardeasy[5][2] + ' | ' + boardeasy[6][3] + ' | ' + boardeasy[6][4] + ' | ' + boardeasy[6][5] + ' | ' + boardeasy[6][6] + ' | ' + boardeasy[6][7] + ' | ' + '7')
print("---------------------------------")
print ('| ' + boardeasy[7][0] + '| ' + boardeasy[7][1] + ' | ' + boardeasy[7][2] + ' | ' + boardeasy[7][3] + ' | ' + boardeasy[7][4] + ' | ' + boardeasy[7][5] + ' | ' + boardeasy[7][6] + ' | ' + boardeasy[7][7] + ' | ' + '8')
print("---------------------------------")
#Variables which will store a certain range
Treasure1_Row = random.randint(0,8)
Treasure1_Col = random.randint(0,8)
Treasure2_Row = random.randint(0,8)
Treasure2_Col = random.randint(0,8)
Treasure3_Row = random.randint(0,8)
Treasure3_Col = random.randint(0,8)
Treasure4_Row = random.randint(0,8)
Treasure4_Col = random.randint(0,8)
Treasure5_Row = random.randint(0,8)
Treasure5_Col = random.randint(0,8)
Treasure6_Row = random.randint(0,8)
Treasure6_Col = random.randint(0,8)
Treasure7_Row = random.randint(0,8)
Treasure7_Col = random.randint(0,8)
Treasure8_Row = random.randint(0,8)
Treasure8_Col = random.randint(0,8)
Treasure9_Row = random.randint(0,8)
Treasure9_Col = random.randint(0,8)
Treasure10_Row = random.randint(0,8)
Treasure10_Col = random.randint(0,8)
Bandit1_Row = random.randint(0,8)
Bandit1_Col = random.randint(0,8)
Bandit2_Row = random.randint(0,8)
Bandit2_Col = random.randint(0,8)
Bandit3_Row = random.randint(0,8)
Bandit3_Col = random.randint(0,8)
Bandit4_Row = random.randint(0,8)
Bandit4_Col = random.randint(0,8)
Bandit5_Row = random.randint(0,8)
Bandit5_Col = random.randint(0,8)
# For loop prints a new 10*10 grid after every move
for i in range(10):
b=
for j in range(10):
b.append(' ')
boardmed.append(b)
def table_game_meduim():
print(" 1 2 3 4 5 6 7 8 9 10")
print("-----------------------------------------")
print ('| ' + boardmed[0][0] + '| ' + boardmed[0][1] + ' | ' + boardmed[0][2] + ' | ' + boardmed[0][3] + ' | ' + boardmed[0][4] + ' | ' + boardmed[0][5] + ' | ' + boardmed[0][6] + ' | ' + boardmed[0][7] + ' | ' + boardmed[0][8] + ' | ' + boardmed[0][9] + ' | ' + '1')
print("-----------------------------------------")
print ('| ' + boardmed[1][0] + '| ' + boardmed[1][1] + ' | ' + boardmed[1][2] + ' | ' + boardmed[1][3] + ' | ' + boardmed[1][4] + ' | ' + boardmed[1][5] + ' | ' + boardmed[1][6] + ' | ' + boardmed[1][7] + ' | ' + boardmed[1][8] + ' | ' + boardmed[1][9] + ' | ' + '2')
print("-----------------------------------------")
print ('| ' + boardmed[2][0] + '| ' + boardmed[2][1] + ' | ' + boardmed[2][2] + ' | ' + boardmed[2][3] + ' | ' + boardmed[2][4] + ' | ' + boardmed[2][5] + ' | ' + boardmed[2][6] + ' | ' + boardmed[2][7] + ' | ' + boardmed[2][8] + ' | ' + boardmed[2][9] + ' | ' + '3')
print("-----------------------------------------")
print ('| ' + boardmed[3][0] + '| ' + boardmed[3][1] + ' | ' + boardmed[3][2] + ' | ' + boardmed[3][3] + ' | ' + boardmed[3][4] + ' | ' + boardmed[3][5] + ' | ' + boardmed[3][6] + ' | ' + boardmed[3][7] + ' | ' + boardmed[3][8] + ' | ' + boardmed[3][9] + ' | ' + '4')
print("-----------------------------------------")
print ('| ' + boardmed[4][0] + '| ' + boardmed[4][1] + ' | ' + boardmed[4][2] + ' | ' + boardmed[4][3] + ' | ' + boardmed[4][4] + ' | ' + boardmed[4][5] + ' | ' + boardmed[4][6] + ' | ' + boardmed[4][7] + ' | ' + boardmed[4][8] + ' | ' + boardmed[4][9] + ' | ' + '5')
print("-----------------------------------------")
print ('| ' + boardmed[5][0] + '| ' + boardmed[5][1] + ' | ' + boardmed[5][2] + ' | ' + boardmed[5][3] + ' | ' + boardmed[5][4] + ' | ' + boardmed[5][5] + ' | ' + boardmed[5][6] + ' | ' + boardmed[5][7] + ' | ' + boardmed[5][8] + ' | ' + boardmed[5][9] + ' | ' + '6')
print("-----------------------------------------")
print ('| ' + boardmed[6][0] + '| ' + boardmed[6][1] + ' | ' + boardmed[6][2] + ' | ' + boardmed[6][3] + ' | ' + boardmed[6][4] + ' | ' + boardmed[6][5] + ' | ' + boardmed[6][6] + ' | ' + boardmed[6][7] + ' | ' + boardmed[6][8] + ' | ' + boardmed[6][9] + ' | ' + '7')
print("-----------------------------------------")
print ('| ' + boardmed[7][0] + '| ' + boardmed[7][1] + ' | ' + boardmed[7][2] + ' | ' + boardmed[7][3] + ' | ' + boardmed[7][4] + ' | ' + boardmed[7][5] + ' | ' + boardmed[7][6] + ' | ' + boardmed[7][7] + ' | ' + boardmed[7][8] + ' | ' + boardmed[7][9] + ' | ' + '8')
print("-----------------------------------------")
print ('| ' + boardmed[8][0] + '| ' + boardmed[8][1] + ' | ' + boardmed[8][2] + ' | ' + boardmed[8][3] + ' | ' + boardmed[8][4] + ' | ' + boardmed[8][5] + ' | ' + boardmed[8][6] + ' | ' + boardmed[8][7] + ' | ' + boardmed[8][8] + ' | ' + boardmed[8][9] + ' | ' + '9')
print("-----------------------------------------")
print ('| ' + boardmed[9][0] + '| ' + boardmed[9][1] + ' | ' + boardmed[9][2] + ' | ' + boardmed[9][3] + ' | ' + boardmed[9][4] + ' | ' + boardmed[9][5] + ' | ' + boardmed[9][6] + ' | ' + boardmed[9][7] + ' | ' + boardmed[9][8] + ' | ' + boardmed[9][9] + ' | ' + '10')
print("-----------------------------------------")
Treasure1_Row = random.randint(0,10)
Treasure1_Col = random.randint(0,10)
Treasure2_Row = random.randint(0,10)
Treasure2_Col = random.randint(0,10)
Treasure3_Row = random.randint(0,10)
Treasure3_Col = random.randint(0,10)
Treasure4_Row = random.randint(0,10)
Treasure4_Col = random.randint(0,10)
Treasure5_Row = random.randint(0,10)
Treasure5_Col = random.randint(0,10)
Treasure6_Row = random.randint(0,10)
Treasure6_Col = random.randint(0,10)
Treasure7_Row = random.randint(0,10)
Treasure7_Col = random.randint(0,10)
Treasure8_Row = random.randint(0,10)
Treasure8_Col = random.randint(0,10)
Treasure9_Row = random.randint(0,10)
Treasure9_Col = random.randint(0,10)
Treasure10_Row = random.randint(0,10)
Treasure10_Col = random.randint(0,10)
Bandit1_Row = random.randint(0,10)
Bandit1_Col = random.randint(0,10)
Bandit2_Row = random.randint(0,10)
Bandit2_Col = random.randint(0,10)
Bandit3_Row = random.randint(0,10)
Bandit3_Col = random.randint(0,10)
Bandit4_Row = random.randint(0,10)
Bandit4_Col = random.randint(0,10)
Bandit5_Row = random.randint(0,10)
Bandit5_Col = random.randint(0,10)
Bandit6_Row = random.randint(0,10)
Bandit6_Col = random.randint(0,10)
Bandit7_Row = random.randint(0,10)
Bandit7_Col = random.randint(0,10)
# For loop prints a new 12*12 grid after every move
for i in range(12):
b=
for j in range(12):
b.append(' ')
boardhard.append(b)
def table_game_hard():
print(" 1 2 3 4 5 6 7 8 9 10 11 12")
print("-------------------------------------------------")
print ('| ' + boardhard[0][0] + '| ' + boardhard[0][1] + ' | ' + boardhard[0][2] + ' | ' + boardhard[0][3] + ' | ' + boardhard[0][4] + ' | ' + boardhard[0][5] + ' | ' + boardhard[0][6] + ' | ' + boardhard[0][7] + ' | ' + boardhard[0][8] + ' | ' + boardhard[0][9] + ' | '+ boardhard[0][10] + ' | ' + boardhard[0][11] + ' | ' + '1')
print("-------------------------------------------------")
print ('| ' + boardhard[1][0] + '| ' + boardhard[1][1] + ' | ' + boardhard[1][2] + ' | ' + boardhard[1][3] + ' | ' + boardhard[1][4] + ' | ' + boardhard[1][5] + ' | ' + boardhard[1][6] + ' | ' + boardhard[1][7] + ' | ' + boardhard[1][8] + ' | ' + boardhard[1][9] + ' | '+ boardhard[1][10] + ' | ' + boardhard[1][11] + ' | ' + '2')
print("-------------------------------------------------")
print ('| ' + boardhard[2][0] + '| ' + boardhard[2][1] + ' | ' + boardhard[2][2] + ' | ' + boardhard[2][3] + ' | ' + boardhard[2][4] + ' | ' + boardhard[2][5] + ' | ' + boardhard[2][6] + ' | ' + boardhard[2][7] + ' | ' + boardhard[2][8] + ' | ' + boardhard[2][9] + ' | '+ boardhard[2][10] + ' | ' + boardhard[2][11] + ' | ' + '3')
print("-------------------------------------------------")
print ('| ' + boardhard[3][0] + '| ' + boardhard[3][1] + ' | ' + boardhard[3][2] + ' | ' + boardhard[3][3] + ' | ' + boardhard[3][4] + ' | ' + boardhard[3][5] + ' | ' + boardhard[3][6] + ' | ' + boardhard[3][7] + ' | ' + boardhard[3][8] + ' | ' + boardhard[3][9] + ' | '+ boardhard[3][10] + ' | ' + boardhard[3][11] + ' | ' + '4')
print("-------------------------------------------------")
print ('| ' + boardhard[4][0] + '| ' + boardhard[4][1] + ' | ' + boardhard[4][2] + ' | ' + boardhard[4][3] + ' | ' + boardhard[4][4] + ' | ' + boardhard[4][5] + ' | ' + boardhard[4][6] + ' | ' + boardhard[4][7] + ' | ' + boardhard[4][8] + ' | ' + boardhard[4][9] + ' | '+ boardhard[4][10] + ' | ' + boardhard[4][11] + ' | ' + '5')
print("-------------------------------------------------")
print ('| ' + boardhard[5][0] + '| ' + boardhard[5][1] + ' | ' + boardhard[5][2] + ' | ' + boardhard[5][3] + ' | ' + boardhard[5][4] + ' | ' + boardhard[5][5] + ' | ' + boardhard[5][6] + ' | ' + boardhard[5][7] + ' | ' + boardhard[5][8] + ' | ' + boardhard[5][9] + ' | '+ boardhard[5][10] + ' | ' + boardhard[5][11] + ' | ' + '6')
print("-------------------------------------------------")
print ('| ' + boardhard[6][0] + '| ' + boardhard[6][1] + ' | ' + boardhard[6][2] + ' | ' + boardhard[6][3] + ' | ' + boardhard[6][4] + ' | ' + boardhard[6][5] + ' | ' + boardhard[6][6] + ' | ' + boardhard[6][7] + ' | ' + boardhard[6][8] + ' | ' + boardhard[6][9] + ' | '+ boardhard[6][10] + ' | ' + boardhard[6][11] + ' | ' + '7')
print("-------------------------------------------------")
print ('| ' + boardhard[7][0] + '| ' + boardhard[7][1] + ' | ' + boardhard[7][2] + ' | ' + boardhard[7][3] + ' | ' + boardhard[7][4] + ' | ' + boardhard[7][5] + ' | ' + boardhard[7][6] + ' | ' + boardhard[7][7] + ' | ' + boardhard[7][8] + ' | ' + boardhard[7][9] + ' | '+ boardhard[7][10] + ' | ' + boardhard[7][11] + ' | ' + '8')
print("-------------------------------------------------")
print ('| ' + boardhard[8][0] + '| ' + boardhard[8][1] + ' | ' + boardhard[8][2] + ' | ' + boardhard[8][3] + ' | ' + boardhard[8][4] + ' | ' + boardhard[8][5] + ' | ' + boardhard[8][6] + ' | ' + boardhard[8][7] + ' | ' + boardhard[8][8] + ' | ' + boardhard[8][9] + ' | '+ boardhard[8][10] + ' | ' + boardhard[8][11] + ' | ' + '9')
print("-------------------------------------------------")
print ('| ' + boardhard[9][0] + '| ' + boardhard[9][1] + ' | ' + boardhard[9][2] + ' | ' + boardhard[9][3] + ' | ' + boardhard[9][4] + ' | ' + boardhard[9][5] + ' | ' + boardhard[9][6] + ' | ' + boardhard[9][7] + ' | ' + boardhard[9][8] + ' | ' + boardhard[9][9] + ' | '+ boardhard[9][10] + ' | ' + boardhard[9][11] + ' | ' + '10')
print("-------------------------------------------------")
print ('| ' + boardhard[10][0] + '| ' + boardhard[10][1] + ' | ' + boardhard[10][2] + ' | ' + boardhard[10][3] + ' | ' + boardhard[10][4] + ' | ' + boardhard[10][5] + ' | ' + boardhard[10][6] + ' | ' + boardhard[10][7] + ' | ' + boardhard[10][8] + ' | ' + boardhard[10][9] + ' | ' + boardhard[10][10] + ' | ' + boardhard[10][11] + ' | ' + '11')
print("-------------------------------------------------")
print ('| ' + boardhard[11][0] + '| ' + boardhard[11][1] + ' | ' + boardhard[11][2] + ' | ' + boardhard[11][3] + ' | ' + boardhard[11][4] + ' | ' + boardhard[11][5] + ' | ' + boardhard[11][6] + ' | ' + boardhard[11][7] + ' | ' + boardhard[11][8] + ' | ' + boardhard[11][9] + ' | ' + boardhard[11][11] + ' | ' + boardhard[11][11] + ' | ' + '12')
print("-------------------------------------------------")
Treasure1_Row = random.randint(0,12)
Treasure1_Col = random.randint(0,12)
Treasure2_Row = random.randint(0,12)
Treasure2_Col = random.randint(0,12)
Treasure3_Row = random.randint(0,12)
Treasure3_Col = random.randint(0,12)
Treasure4_Row = random.randint(0,12)
Treasure4_Col = random.randint(0,12)
Treasure5_Row = random.randint(0,12)
Treasure5_Col = random.randint(0,12)
Treasure6_Row = random.randint(0,12)
Treasure6_Col = random.randint(0,12)
Treasure7_Row = random.randint(0,12)
Treasure7_Col = random.randint(0,12)
Treasure8_Row = random.randint(0,12)
Treasure8_Col = random.randint(0,12)
Treasure9_Row = random.randint(0,12)
Treasure9_Col = random.randint(0,12)
Treasure10_Row = random.randint(0,12)
Treasure10_Col = random.randint(0,12)
Bandit1_Row = random.randint(0,12)
Bandit1_Col = random.randint(0,12)
Bandit2_Row = random.randint(0,12)
Bandit2_Col = random.randint(0,12)
Bandit3_Row = random.randint(0,12)
Bandit3_Col = random.randint(0,12)
Bandit4_Row = random.randint(0,12)
Bandit4_Col = random.randint(0,12)
Bandit5_Row = random.randint(0,12)
Bandit5_Col = random.randint(0,12)
Bandit6_Row = random.randint(0,12)
Bandit6_Col = random.randint(0,12)
Bandit7_Row = random.randint(0,12)
Bandit7_Col = random.randint(0,12)
Bandit8_Row = random.randint(0,12)
Bandit8_Col = random.randint(0,12)
Bandit9_Row = random.randint(0,12)
Bandit9_Col = random.randint(0,12)
#this function is in charge of downwards movement
def down(num,lev):
num=(num+current[0])%lev#The % formats this equation
current[0]=num
#this function is in charge of downwards movement
def right(num,lev):
num=(num+current[1])%lev #The % formats this equation
current[1]=num
#this function is in charge of downwards movement
def left(num,lev):
if current[1]-num>=0:
current[1]=current[1]-num
else:
current[1]=current[1]-num+lev
#this function is in charge of downwards movement
def up(num,lev):
if current[0]-num>=0:
current[0]=current[0]-num
else:
current[0]=current[0]-num+lev
def easy_level(Coins):
#This function is for the movement of the game in easy difficulty
while True:
oldcurrent=current
boardeasy[oldcurrent[0]][oldcurrent[1]]='*'
table_game_easy()
boardeasy[oldcurrent[0]][oldcurrent[1]]=' '
n = input('Enter the direction followed by the number Ex:Up 5 , Number should be < 8 n')
n=n.split()
if n[0].lower() not in ['up','left','down','right']:#Validates input
print('Wrong command, please input again')
continue
elif n[0].lower()=='up':
up(int(n[1].lower()),8)#Boundary is set to 8 as the 'easy' grid is a 8^8
elif n[0].lower()=='down':
down(int(n[1].lower()),8)
elif n[0].lower()=='left':
left(int(n[1].lower()),8)
elif n[0].lower()=='right':
right(int(n[1].lower()),8)
print("8 chests left")
print("8 bandits left")
print("Coins:",Coins)#Acts as a counter, displays the number of coins that the player has
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
or current[0] == Treasure3_Row and current[1] == Treasure3_Col
or current[0] == Treasure4_Row and current[1] == Treasure4_Col
or current[0] == Treasure5_Row and current[1] == Treasure5_Col
or current[0] == Treasure6_Row and current[1] == Treasure6_Col
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col:
print("Hooray! You have found booty! +10 gold")
Coins = Coins+10 #Adds an additional 10 points
print("Coins:",Coins)
if current[0] == Bandit1_Row and current[1] == Bandit1_Col
or current[0] == Bandit2_Row and current[1] == Bandit2_Col
or current[0] == Bandit3_Row and current[1] == Bandit3_Col
or current[0] == Bandit4_Row and current[1] == Bandit4_Col
or current[0] == Bandit5_Row and current[1] == Bandit5_Col
or current[0] == Bandit6_Row and current[1] == Bandit6_Col
or current[0] == Bandit7_Row and current[1] == Bandit7_Col
or current[0] == Bandit8_Row and current[1] == Bandit8_Col:
print("Oh no! You have landed on a bandit...they steal all your coins!")
Coins = Coins-Coins #Removes all coins
print("Coins:",Coins)
boardeasy[current[0]][current[1]]='*'#sets value to players position
def med_level(Coins):
#This function is for the movement of the game in medium difficulty
while True:
oldcurrent=current
boardmed[oldcurrent[0]][oldcurrent[1]]='*'
table_game_meduim()
boardmed[oldcurrent[0]][oldcurrent[1]]=' '
n = input('Enter the direction followed by the number Ex:Up 5 , Number should be < 10 n')
n=n.split()
if n[0].lower() not in ['up','left','down','right']:
print('wrong command')
continue
elif n[0].lower()=='up':
up(int(n[1].lower()),10)#Boundary is set to 10 as the 'easy' grid is a 10^10
elif n[0].lower()=='down':
down(int(n[1].lower()),10)
elif n[0].lower()=='left':
left(int(n[1].lower()),10)
elif n[0].lower()=='right':
right(int(n[1].lower()),10)
print("10 chests left")
print("10 bandits left")
print("Coins:",Coins)#Acts as a counter, displays the number of coins that the player has
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
or current[0] == Treasure3_Row and current[1] == Treasure3_Col
or current[0] == Treasure4_Row and current[1] == Treasure4_Col
or current[0] == Treasure5_Row and current[1] == Treasure5_Col
or current[0] == Treasure6_Row and current[1] == Treasure6_Col
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col
or current[0] == Treasure9_Row and current[1] == Treasure9_Col
or current[0] == Treasure10_Row and current[1] == Treasure10_Col:
print("Hooray! You have found booty! +10 gold")
Coins = Coins+10 #Adds an additional 10 points
print("Coins:",Coins)
if current[0] == Bandit1_Row and current[1] == Bandit1_Col
or current[0] == Bandit2_Row and current[1] == Bandit2_Col
or current[0] == Bandit3_Row and current[1] == Bandit3_Col
or current[0] == Bandit4_Row and current[1] == Bandit4_Col
or current[0] == Bandit5_Row and current[1] == Bandit5_Col
or current[0] == Bandit6_Row and current[1] == Bandit6_Col
or current[0] == Bandit7_Row and current[1] == Bandit7_Col
or current[0] == Bandit8_Row and current[1] == Bandit8_Col
or current[0] == Bandit9_Row and current[1] == Bandit9_Col
or current[0] == Bandit10_Row and current[1] == Bandit10_Col:
print("Oh no! You have landed on a bandit...they steal all your coins!")
Coins = Coins-Coins #Removes all coins
print("Coins:",Coins)
boardmed[current[0]][current[1]]='*'
def hard_level(Coins):
#This function is for the movement of the game in hard difficulty
while True:
oldcurrent=current
boardhard[oldcurrent[0]][oldcurrent[1]]='*'
table_game_hard()
boardhard[oldcurrent[0]][oldcurrent[1]]=' '
n = input('Enter the direction followed by the number Ex:Up 5 , Number should be < 12 n')
n=n.split()
if n[0].lower() not in ['up','left','down','right']:
print('wrong command')
continue
elif n[0].lower()=='up':
up(int(n[1].lower()),12)#Boundary is set to 12 as the 'hard' grid is a 12^12
elif n[0].lower()=='down':
down(int(n[1].lower()),12)
elif n[0].lower()=='left':
left(int(n[1].lower()),12)
elif n[0].lower()=='right':
right(int(n[1].lower()),12)
print("12 chests left")
print("12 bandits left")
print("Coins:",Coins)#Acts as a counter, displays the number of coins that the player has
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
or current[0] == Treasure3_Row and current[1] == Treasure3_Col
or current[0] == Treasure4_Row and current[1] == Treasure4_Col
or current[0] == Treasure5_Row and current[1] == Treasure5_Col
or current[0] == Treasure6_Row and current[1] == Treasure6_Col
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col
or current[0] == Treasure9_Row and current[1] == Treasure9_Col
or current[0] == Treasure10_Row and current[1] == Treasure10_Col
or current[0] == Treasure11_Row and current[1] == Treasure11_Col
or current[0] == Treasure12_Row and current[1] == Treasure12_Col:
print("Hooray! You have found booty! +10 gold")
Coins = Coins+10 #Adds an additional 10 points
print("Coins:",Coins)
if current[0] == Bandit1_Row and current[1] == Bandit1_Col
or current[0] == Bandit2_Row and current[1] == Bandit2_Col
or current[0] == Bandit3_Row and current[1] == Bandit3_Col
or current[0] == Bandit4_Row and current[1] == Bandit4_Col
or current[0] == Bandit5_Row and current[1] == Bandit5_Col
or current[0] == Bandit6_Row and current[1] == Bandit6_Col
or current[0] == Bandit7_Row and current[1] == Bandit7_Col
or current[0] == Bandit8_Row and current[1] == Bandit8_Col
or current[0] == Bandit9_Row and current[1] == Bandit9_Col
or current[0] == Bandit10_Row and current[1] == Bandit10_Col
or current[0] == Bandit11_Row and current[1] == Bandit11_Col
or current[0] == Bandit12_Row and current[1] == Bandit12_Col:
print("Oh no! You have landed on a bandit...they steal all your coins!")
Coins = Coins-Coins #Removes all coins
print("Coins:",Coins)
boardhard[current[0]][current[1]]='*'
def instruct():
difficulty = input("""Before the game starts, please consider what difficulty
would you like to play in, easy, medium or (if you're brave) hard.
""")
if difficulty == "easy":
print("That's great! Lets continue.")
time.sleep(1)#Allows the user time to get ready
print("initiating game in...")
print()
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
for i in range(3):
print("")
easy_level(Coins)
elif difficulty == "medium":
print("That's great! Lets continue.")
time.sleep(1)#Allows the user time to get ready
print("initiating game in...")
print()
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
for i in range(3):
print("")
med_level(Coins)
elif difficulty == "hard":
print("That's great! Lets continue.")
time.sleep(1)#Allows the user time to get ready
print("initiating game in...")
print()
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
for i in range(3):
print("")
hard_level(Coins)
else:
print("Sorry, that is an invalid answer. Please restart the programme")
def menu():
#This function lets the user quit the application or progress to playing.
print("")
print ("Are you sure you wish to play this game? Please answer either yes or no.")
choice1 = input() # Sets variable to user input
if choice1.lower().startswith('y'):
print("Okay lets continue then!")
elif choice1.lower().startswith('n'):
print("Thank you, I hope you will play next time!")
print("")
quit("Thank you for playing!")#Terminates the programme
else:
print("Sorry, that is an invalid answer. Please restart the programme")
print("")
quit()
instruct()
def showInstructions():
time.sleep(1.0)
print("Instructions of the game:")
time.sleep(1.0)
print("""
You are a treasure hunter, your goal is to collect atleast 100 gold by the end
of the game from treasure chests randomly scattered across the grid. There are
10 chests within a grid (this can be changed based on difficulty) and each
treasure chest is worth 10 gold but can only be reclaimed 3 times before it is
replaced by a bandit. Landing on a bandit will cause you to lose all of your
gold and if all the chests have been replaced by bandits and you have less then
100 gold this means you lose!
Press enter to continue...""")
input()
print("""
At the start of the game, you always begin at the top right of the grid.
Below is a representation of the game:
* 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Press enter to continue...""")
input()
print("""
When deciding where to move, you should input the direct co-ordinates of your
desired location. For instance:
Enter the direction followed by the number Ex: Up 5 , Number should be < 8
right 3
0 0 0 * 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Unlucky move! You have found nothing!
If nothing on the grid changes , this means that your move was a bust! Landing
on nothing isn't displayed on the grid.
Press enter to continue...""")
input()
print("""
Enter the direction followed by the number Ex: Up 5 , Number should be < 8
down 4
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Hooray! You have found booty! +10 gold
Here you can see that the use has landed on a chest!
As you land on chest, they get replaced by bandits. Be sure to remember the
previous locations so you don't accidently land on a bandit and lose all
your gold!
Press enter to continue...""")
input()
#Introduces the user
print('Welcome to the Treasure hunt!')
time.sleep(0.3)
print()
time.sleep(0.3)
print('Would you like to see the instructions? (yes/no)')
if input().lower().startswith('y'):#If function checks for the first letter
showInstructions()
elif input == 'no' or 'No':
print("Lets continue then!")#Calls the function which displays instructions
else:
print("Please check your input and try again.")
menu()
python game
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
The code below is for a board game program, which works by placing the user on a grid which then tasks the user with finding all the treasure chests. Each chest landed on gives 10 gold however landing on a bandit means all your gold is taken. The end score should be given once the user has found all the chests.
A few features including the end game, score board, timer and more are still required to be added. For now I just want feedback on how to improve the code at its current state (the code could very well be simplified).
import random
import sys
import time
boardeasy=
boardmed=
boardhard=
current=[0,0]
Treasure1_Row = random.randint(0,8)
Treasure1_Col = random.randint(0,8)
Treasure2_Row = random.randint(0,8)
Treasure2_Col = random.randint(0,8)
Treasure3_Row = random.randint(0,8)
Treasure3_Col = random.randint(0,8)
Treasure4_Row = random.randint(0,8)
Treasure4_Col = random.randint(0,8)
Treasure5_Row = random.randint(0,8)
Treasure5_Col = random.randint(0,8)
Treasure6_Row = random.randint(0,8)
Treasure6_Col = random.randint(0,8)
Treasure7_Row = random.randint(0,8)
Treasure7_Col = random.randint(0,8)
Treasure8_Row = random.randint(0,8)
Treasure8_Col = random.randint(0,8)
Treasure9_Row = random.randint(0,8)
Treasure9_Col = random.randint(0,8)
Treasure10_Row = random.randint(0,8)
Treasure10_Col = random.randint(0,8)
Treasure11_Row = random.randint(0,8)
Treasure11_Col = random.randint(0,8)
Treasure12_Row = random.randint(0,8)
Treasure12_Col = random.randint(0,8)
Bandit1_Row = random.randint(0,8)
Bandit1_Col = random.randint(0,8)
Bandit2_Row = random.randint(0,8)
Bandit2_Col = random.randint(0,8)
Bandit3_Row = random.randint(0,8)
Bandit3_Col = random.randint(0,8)
Bandit4_Row = random.randint(0,8)
Bandit4_Col = random.randint(0,8)
Bandit5_Row = random.randint(0,8)
Bandit5_Col = random.randint(0,8)
Bandit6_Row = random.randint(0,8)
Bandit6_Col = random.randint(0,8)
Bandit7_Row = random.randint(0,8)
Bandit7_Col = random.randint(0,8)
Bandit8_Row = random.randint(0,8)
Bandit8_Col = random.randint(0,8)
Bandit9_Row = random.randint(0,8)
Bandit9_Col = random.randint(0,8)
Bandit10_Row = random.randint(0,8)
Bandit10_Col = random.randint(0,8)
Bandit11_Row = random.randint(0,8)
Bandit11_Col = random.randint(0,8)
Bandit12_Row = random.randint(0,8)
Bandit12_Col = random.randint(0,8)
Coins = 0
class user():
def __init__(self, username, userscore, usertime):
self.username = username
self.userscore = userscore
self.usertime = usertime
#For loop prints a new 8*8 grid after every move
for i in range(8):
b=
for j in range(8):
b.append(' ')
boardeasy.append(b)
def table_game_easy():
print(" 1 2 3 4 5 6 7 8")
print("---------------------------------")
print ('| ' + boardeasy[0][0] + '| ' + boardeasy[0][1] + ' | ' + boardeasy[0][2] + ' | ' + boardeasy[0][3] + ' | ' + boardeasy[0][4] + ' | ' + boardeasy[0][5] + ' | ' + boardeasy[0][6] + ' | ' + boardeasy[0][7] + ' | ' + '1')
print("---------------------------------")
print ('| ' + boardeasy[1][0] + '| ' + boardeasy[1][1] + ' | ' + boardeasy[1][2] + ' | ' + boardeasy[1][3] + ' | ' + boardeasy[1][4] + ' | ' + boardeasy[1][5] + ' | ' + boardeasy[1][6] + ' | ' + boardeasy[1][7] + ' | ' + '2')
print("---------------------------------")
print ('| ' + boardeasy[2][0] + '| ' + boardeasy[2][1] + ' | ' + boardeasy[2][2] + ' | ' + boardeasy[2][3] + ' | ' + boardeasy[2][4] + ' | ' + boardeasy[2][5] + ' | ' + boardeasy[2][6] + ' | ' + boardeasy[2][7] + ' | ' + '3')
print("---------------------------------")
print ('| ' + boardeasy[3][0] + '| ' + boardeasy[3][1] + ' | ' + boardeasy[3][2] + ' | ' + boardeasy[3][3] + ' | ' + boardeasy[3][4] + ' | ' + boardeasy[3][5] + ' | ' + boardeasy[3][6] + ' | ' + boardeasy[3][7] + ' | ' + '4')
print("---------------------------------")
print ('| ' + boardeasy[4][0] + '| ' + boardeasy[4][1] + ' | ' + boardeasy[4][2] + ' | ' + boardeasy[4][3] + ' | ' + boardeasy[4][4] + ' | ' + boardeasy[4][5] + ' | ' + boardeasy[4][6] + ' | ' + boardeasy[4][7] + ' | ' + '5')
print("---------------------------------")
print ('| ' + boardeasy[5][0] + '| ' + boardeasy[5][1] + ' | ' + boardeasy[4][2] + ' | ' + boardeasy[5][3] + ' | ' + boardeasy[5][4] + ' | ' + boardeasy[5][5] + ' | ' + boardeasy[5][6] + ' | ' + boardeasy[5][7] + ' | ' + '6')
print("---------------------------------")
print ('| ' + boardeasy[6][0] + '| ' + boardeasy[6][1] + ' | ' + boardeasy[5][2] + ' | ' + boardeasy[6][3] + ' | ' + boardeasy[6][4] + ' | ' + boardeasy[6][5] + ' | ' + boardeasy[6][6] + ' | ' + boardeasy[6][7] + ' | ' + '7')
print("---------------------------------")
print ('| ' + boardeasy[7][0] + '| ' + boardeasy[7][1] + ' | ' + boardeasy[7][2] + ' | ' + boardeasy[7][3] + ' | ' + boardeasy[7][4] + ' | ' + boardeasy[7][5] + ' | ' + boardeasy[7][6] + ' | ' + boardeasy[7][7] + ' | ' + '8')
print("---------------------------------")
#Variables which will store a certain range
Treasure1_Row = random.randint(0,8)
Treasure1_Col = random.randint(0,8)
Treasure2_Row = random.randint(0,8)
Treasure2_Col = random.randint(0,8)
Treasure3_Row = random.randint(0,8)
Treasure3_Col = random.randint(0,8)
Treasure4_Row = random.randint(0,8)
Treasure4_Col = random.randint(0,8)
Treasure5_Row = random.randint(0,8)
Treasure5_Col = random.randint(0,8)
Treasure6_Row = random.randint(0,8)
Treasure6_Col = random.randint(0,8)
Treasure7_Row = random.randint(0,8)
Treasure7_Col = random.randint(0,8)
Treasure8_Row = random.randint(0,8)
Treasure8_Col = random.randint(0,8)
Treasure9_Row = random.randint(0,8)
Treasure9_Col = random.randint(0,8)
Treasure10_Row = random.randint(0,8)
Treasure10_Col = random.randint(0,8)
Bandit1_Row = random.randint(0,8)
Bandit1_Col = random.randint(0,8)
Bandit2_Row = random.randint(0,8)
Bandit2_Col = random.randint(0,8)
Bandit3_Row = random.randint(0,8)
Bandit3_Col = random.randint(0,8)
Bandit4_Row = random.randint(0,8)
Bandit4_Col = random.randint(0,8)
Bandit5_Row = random.randint(0,8)
Bandit5_Col = random.randint(0,8)
# For loop prints a new 10*10 grid after every move
for i in range(10):
b=
for j in range(10):
b.append(' ')
boardmed.append(b)
def table_game_meduim():
print(" 1 2 3 4 5 6 7 8 9 10")
print("-----------------------------------------")
print ('| ' + boardmed[0][0] + '| ' + boardmed[0][1] + ' | ' + boardmed[0][2] + ' | ' + boardmed[0][3] + ' | ' + boardmed[0][4] + ' | ' + boardmed[0][5] + ' | ' + boardmed[0][6] + ' | ' + boardmed[0][7] + ' | ' + boardmed[0][8] + ' | ' + boardmed[0][9] + ' | ' + '1')
print("-----------------------------------------")
print ('| ' + boardmed[1][0] + '| ' + boardmed[1][1] + ' | ' + boardmed[1][2] + ' | ' + boardmed[1][3] + ' | ' + boardmed[1][4] + ' | ' + boardmed[1][5] + ' | ' + boardmed[1][6] + ' | ' + boardmed[1][7] + ' | ' + boardmed[1][8] + ' | ' + boardmed[1][9] + ' | ' + '2')
print("-----------------------------------------")
print ('| ' + boardmed[2][0] + '| ' + boardmed[2][1] + ' | ' + boardmed[2][2] + ' | ' + boardmed[2][3] + ' | ' + boardmed[2][4] + ' | ' + boardmed[2][5] + ' | ' + boardmed[2][6] + ' | ' + boardmed[2][7] + ' | ' + boardmed[2][8] + ' | ' + boardmed[2][9] + ' | ' + '3')
print("-----------------------------------------")
print ('| ' + boardmed[3][0] + '| ' + boardmed[3][1] + ' | ' + boardmed[3][2] + ' | ' + boardmed[3][3] + ' | ' + boardmed[3][4] + ' | ' + boardmed[3][5] + ' | ' + boardmed[3][6] + ' | ' + boardmed[3][7] + ' | ' + boardmed[3][8] + ' | ' + boardmed[3][9] + ' | ' + '4')
print("-----------------------------------------")
print ('| ' + boardmed[4][0] + '| ' + boardmed[4][1] + ' | ' + boardmed[4][2] + ' | ' + boardmed[4][3] + ' | ' + boardmed[4][4] + ' | ' + boardmed[4][5] + ' | ' + boardmed[4][6] + ' | ' + boardmed[4][7] + ' | ' + boardmed[4][8] + ' | ' + boardmed[4][9] + ' | ' + '5')
print("-----------------------------------------")
print ('| ' + boardmed[5][0] + '| ' + boardmed[5][1] + ' | ' + boardmed[5][2] + ' | ' + boardmed[5][3] + ' | ' + boardmed[5][4] + ' | ' + boardmed[5][5] + ' | ' + boardmed[5][6] + ' | ' + boardmed[5][7] + ' | ' + boardmed[5][8] + ' | ' + boardmed[5][9] + ' | ' + '6')
print("-----------------------------------------")
print ('| ' + boardmed[6][0] + '| ' + boardmed[6][1] + ' | ' + boardmed[6][2] + ' | ' + boardmed[6][3] + ' | ' + boardmed[6][4] + ' | ' + boardmed[6][5] + ' | ' + boardmed[6][6] + ' | ' + boardmed[6][7] + ' | ' + boardmed[6][8] + ' | ' + boardmed[6][9] + ' | ' + '7')
print("-----------------------------------------")
print ('| ' + boardmed[7][0] + '| ' + boardmed[7][1] + ' | ' + boardmed[7][2] + ' | ' + boardmed[7][3] + ' | ' + boardmed[7][4] + ' | ' + boardmed[7][5] + ' | ' + boardmed[7][6] + ' | ' + boardmed[7][7] + ' | ' + boardmed[7][8] + ' | ' + boardmed[7][9] + ' | ' + '8')
print("-----------------------------------------")
print ('| ' + boardmed[8][0] + '| ' + boardmed[8][1] + ' | ' + boardmed[8][2] + ' | ' + boardmed[8][3] + ' | ' + boardmed[8][4] + ' | ' + boardmed[8][5] + ' | ' + boardmed[8][6] + ' | ' + boardmed[8][7] + ' | ' + boardmed[8][8] + ' | ' + boardmed[8][9] + ' | ' + '9')
print("-----------------------------------------")
print ('| ' + boardmed[9][0] + '| ' + boardmed[9][1] + ' | ' + boardmed[9][2] + ' | ' + boardmed[9][3] + ' | ' + boardmed[9][4] + ' | ' + boardmed[9][5] + ' | ' + boardmed[9][6] + ' | ' + boardmed[9][7] + ' | ' + boardmed[9][8] + ' | ' + boardmed[9][9] + ' | ' + '10')
print("-----------------------------------------")
Treasure1_Row = random.randint(0,10)
Treasure1_Col = random.randint(0,10)
Treasure2_Row = random.randint(0,10)
Treasure2_Col = random.randint(0,10)
Treasure3_Row = random.randint(0,10)
Treasure3_Col = random.randint(0,10)
Treasure4_Row = random.randint(0,10)
Treasure4_Col = random.randint(0,10)
Treasure5_Row = random.randint(0,10)
Treasure5_Col = random.randint(0,10)
Treasure6_Row = random.randint(0,10)
Treasure6_Col = random.randint(0,10)
Treasure7_Row = random.randint(0,10)
Treasure7_Col = random.randint(0,10)
Treasure8_Row = random.randint(0,10)
Treasure8_Col = random.randint(0,10)
Treasure9_Row = random.randint(0,10)
Treasure9_Col = random.randint(0,10)
Treasure10_Row = random.randint(0,10)
Treasure10_Col = random.randint(0,10)
Bandit1_Row = random.randint(0,10)
Bandit1_Col = random.randint(0,10)
Bandit2_Row = random.randint(0,10)
Bandit2_Col = random.randint(0,10)
Bandit3_Row = random.randint(0,10)
Bandit3_Col = random.randint(0,10)
Bandit4_Row = random.randint(0,10)
Bandit4_Col = random.randint(0,10)
Bandit5_Row = random.randint(0,10)
Bandit5_Col = random.randint(0,10)
Bandit6_Row = random.randint(0,10)
Bandit6_Col = random.randint(0,10)
Bandit7_Row = random.randint(0,10)
Bandit7_Col = random.randint(0,10)
# For loop prints a new 12*12 grid after every move
for i in range(12):
b=
for j in range(12):
b.append(' ')
boardhard.append(b)
def table_game_hard():
print(" 1 2 3 4 5 6 7 8 9 10 11 12")
print("-------------------------------------------------")
print ('| ' + boardhard[0][0] + '| ' + boardhard[0][1] + ' | ' + boardhard[0][2] + ' | ' + boardhard[0][3] + ' | ' + boardhard[0][4] + ' | ' + boardhard[0][5] + ' | ' + boardhard[0][6] + ' | ' + boardhard[0][7] + ' | ' + boardhard[0][8] + ' | ' + boardhard[0][9] + ' | '+ boardhard[0][10] + ' | ' + boardhard[0][11] + ' | ' + '1')
print("-------------------------------------------------")
print ('| ' + boardhard[1][0] + '| ' + boardhard[1][1] + ' | ' + boardhard[1][2] + ' | ' + boardhard[1][3] + ' | ' + boardhard[1][4] + ' | ' + boardhard[1][5] + ' | ' + boardhard[1][6] + ' | ' + boardhard[1][7] + ' | ' + boardhard[1][8] + ' | ' + boardhard[1][9] + ' | '+ boardhard[1][10] + ' | ' + boardhard[1][11] + ' | ' + '2')
print("-------------------------------------------------")
print ('| ' + boardhard[2][0] + '| ' + boardhard[2][1] + ' | ' + boardhard[2][2] + ' | ' + boardhard[2][3] + ' | ' + boardhard[2][4] + ' | ' + boardhard[2][5] + ' | ' + boardhard[2][6] + ' | ' + boardhard[2][7] + ' | ' + boardhard[2][8] + ' | ' + boardhard[2][9] + ' | '+ boardhard[2][10] + ' | ' + boardhard[2][11] + ' | ' + '3')
print("-------------------------------------------------")
print ('| ' + boardhard[3][0] + '| ' + boardhard[3][1] + ' | ' + boardhard[3][2] + ' | ' + boardhard[3][3] + ' | ' + boardhard[3][4] + ' | ' + boardhard[3][5] + ' | ' + boardhard[3][6] + ' | ' + boardhard[3][7] + ' | ' + boardhard[3][8] + ' | ' + boardhard[3][9] + ' | '+ boardhard[3][10] + ' | ' + boardhard[3][11] + ' | ' + '4')
print("-------------------------------------------------")
print ('| ' + boardhard[4][0] + '| ' + boardhard[4][1] + ' | ' + boardhard[4][2] + ' | ' + boardhard[4][3] + ' | ' + boardhard[4][4] + ' | ' + boardhard[4][5] + ' | ' + boardhard[4][6] + ' | ' + boardhard[4][7] + ' | ' + boardhard[4][8] + ' | ' + boardhard[4][9] + ' | '+ boardhard[4][10] + ' | ' + boardhard[4][11] + ' | ' + '5')
print("-------------------------------------------------")
print ('| ' + boardhard[5][0] + '| ' + boardhard[5][1] + ' | ' + boardhard[5][2] + ' | ' + boardhard[5][3] + ' | ' + boardhard[5][4] + ' | ' + boardhard[5][5] + ' | ' + boardhard[5][6] + ' | ' + boardhard[5][7] + ' | ' + boardhard[5][8] + ' | ' + boardhard[5][9] + ' | '+ boardhard[5][10] + ' | ' + boardhard[5][11] + ' | ' + '6')
print("-------------------------------------------------")
print ('| ' + boardhard[6][0] + '| ' + boardhard[6][1] + ' | ' + boardhard[6][2] + ' | ' + boardhard[6][3] + ' | ' + boardhard[6][4] + ' | ' + boardhard[6][5] + ' | ' + boardhard[6][6] + ' | ' + boardhard[6][7] + ' | ' + boardhard[6][8] + ' | ' + boardhard[6][9] + ' | '+ boardhard[6][10] + ' | ' + boardhard[6][11] + ' | ' + '7')
print("-------------------------------------------------")
print ('| ' + boardhard[7][0] + '| ' + boardhard[7][1] + ' | ' + boardhard[7][2] + ' | ' + boardhard[7][3] + ' | ' + boardhard[7][4] + ' | ' + boardhard[7][5] + ' | ' + boardhard[7][6] + ' | ' + boardhard[7][7] + ' | ' + boardhard[7][8] + ' | ' + boardhard[7][9] + ' | '+ boardhard[7][10] + ' | ' + boardhard[7][11] + ' | ' + '8')
print("-------------------------------------------------")
print ('| ' + boardhard[8][0] + '| ' + boardhard[8][1] + ' | ' + boardhard[8][2] + ' | ' + boardhard[8][3] + ' | ' + boardhard[8][4] + ' | ' + boardhard[8][5] + ' | ' + boardhard[8][6] + ' | ' + boardhard[8][7] + ' | ' + boardhard[8][8] + ' | ' + boardhard[8][9] + ' | '+ boardhard[8][10] + ' | ' + boardhard[8][11] + ' | ' + '9')
print("-------------------------------------------------")
print ('| ' + boardhard[9][0] + '| ' + boardhard[9][1] + ' | ' + boardhard[9][2] + ' | ' + boardhard[9][3] + ' | ' + boardhard[9][4] + ' | ' + boardhard[9][5] + ' | ' + boardhard[9][6] + ' | ' + boardhard[9][7] + ' | ' + boardhard[9][8] + ' | ' + boardhard[9][9] + ' | '+ boardhard[9][10] + ' | ' + boardhard[9][11] + ' | ' + '10')
print("-------------------------------------------------")
print ('| ' + boardhard[10][0] + '| ' + boardhard[10][1] + ' | ' + boardhard[10][2] + ' | ' + boardhard[10][3] + ' | ' + boardhard[10][4] + ' | ' + boardhard[10][5] + ' | ' + boardhard[10][6] + ' | ' + boardhard[10][7] + ' | ' + boardhard[10][8] + ' | ' + boardhard[10][9] + ' | ' + boardhard[10][10] + ' | ' + boardhard[10][11] + ' | ' + '11')
print("-------------------------------------------------")
print ('| ' + boardhard[11][0] + '| ' + boardhard[11][1] + ' | ' + boardhard[11][2] + ' | ' + boardhard[11][3] + ' | ' + boardhard[11][4] + ' | ' + boardhard[11][5] + ' | ' + boardhard[11][6] + ' | ' + boardhard[11][7] + ' | ' + boardhard[11][8] + ' | ' + boardhard[11][9] + ' | ' + boardhard[11][11] + ' | ' + boardhard[11][11] + ' | ' + '12')
print("-------------------------------------------------")
Treasure1_Row = random.randint(0,12)
Treasure1_Col = random.randint(0,12)
Treasure2_Row = random.randint(0,12)
Treasure2_Col = random.randint(0,12)
Treasure3_Row = random.randint(0,12)
Treasure3_Col = random.randint(0,12)
Treasure4_Row = random.randint(0,12)
Treasure4_Col = random.randint(0,12)
Treasure5_Row = random.randint(0,12)
Treasure5_Col = random.randint(0,12)
Treasure6_Row = random.randint(0,12)
Treasure6_Col = random.randint(0,12)
Treasure7_Row = random.randint(0,12)
Treasure7_Col = random.randint(0,12)
Treasure8_Row = random.randint(0,12)
Treasure8_Col = random.randint(0,12)
Treasure9_Row = random.randint(0,12)
Treasure9_Col = random.randint(0,12)
Treasure10_Row = random.randint(0,12)
Treasure10_Col = random.randint(0,12)
Bandit1_Row = random.randint(0,12)
Bandit1_Col = random.randint(0,12)
Bandit2_Row = random.randint(0,12)
Bandit2_Col = random.randint(0,12)
Bandit3_Row = random.randint(0,12)
Bandit3_Col = random.randint(0,12)
Bandit4_Row = random.randint(0,12)
Bandit4_Col = random.randint(0,12)
Bandit5_Row = random.randint(0,12)
Bandit5_Col = random.randint(0,12)
Bandit6_Row = random.randint(0,12)
Bandit6_Col = random.randint(0,12)
Bandit7_Row = random.randint(0,12)
Bandit7_Col = random.randint(0,12)
Bandit8_Row = random.randint(0,12)
Bandit8_Col = random.randint(0,12)
Bandit9_Row = random.randint(0,12)
Bandit9_Col = random.randint(0,12)
#this function is in charge of downwards movement
def down(num,lev):
num=(num+current[0])%lev#The % formats this equation
current[0]=num
#this function is in charge of downwards movement
def right(num,lev):
num=(num+current[1])%lev #The % formats this equation
current[1]=num
#this function is in charge of downwards movement
def left(num,lev):
if current[1]-num>=0:
current[1]=current[1]-num
else:
current[1]=current[1]-num+lev
#this function is in charge of downwards movement
def up(num,lev):
if current[0]-num>=0:
current[0]=current[0]-num
else:
current[0]=current[0]-num+lev
def easy_level(Coins):
#This function is for the movement of the game in easy difficulty
while True:
oldcurrent=current
boardeasy[oldcurrent[0]][oldcurrent[1]]='*'
table_game_easy()
boardeasy[oldcurrent[0]][oldcurrent[1]]=' '
n = input('Enter the direction followed by the number Ex:Up 5 , Number should be < 8 n')
n=n.split()
if n[0].lower() not in ['up','left','down','right']:#Validates input
print('Wrong command, please input again')
continue
elif n[0].lower()=='up':
up(int(n[1].lower()),8)#Boundary is set to 8 as the 'easy' grid is a 8^8
elif n[0].lower()=='down':
down(int(n[1].lower()),8)
elif n[0].lower()=='left':
left(int(n[1].lower()),8)
elif n[0].lower()=='right':
right(int(n[1].lower()),8)
print("8 chests left")
print("8 bandits left")
print("Coins:",Coins)#Acts as a counter, displays the number of coins that the player has
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
or current[0] == Treasure3_Row and current[1] == Treasure3_Col
or current[0] == Treasure4_Row and current[1] == Treasure4_Col
or current[0] == Treasure5_Row and current[1] == Treasure5_Col
or current[0] == Treasure6_Row and current[1] == Treasure6_Col
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col:
print("Hooray! You have found booty! +10 gold")
Coins = Coins+10 #Adds an additional 10 points
print("Coins:",Coins)
if current[0] == Bandit1_Row and current[1] == Bandit1_Col
or current[0] == Bandit2_Row and current[1] == Bandit2_Col
or current[0] == Bandit3_Row and current[1] == Bandit3_Col
or current[0] == Bandit4_Row and current[1] == Bandit4_Col
or current[0] == Bandit5_Row and current[1] == Bandit5_Col
or current[0] == Bandit6_Row and current[1] == Bandit6_Col
or current[0] == Bandit7_Row and current[1] == Bandit7_Col
or current[0] == Bandit8_Row and current[1] == Bandit8_Col:
print("Oh no! You have landed on a bandit...they steal all your coins!")
Coins = Coins-Coins #Removes all coins
print("Coins:",Coins)
boardeasy[current[0]][current[1]]='*'#sets value to players position
def med_level(Coins):
#This function is for the movement of the game in medium difficulty
while True:
oldcurrent=current
boardmed[oldcurrent[0]][oldcurrent[1]]='*'
table_game_meduim()
boardmed[oldcurrent[0]][oldcurrent[1]]=' '
n = input('Enter the direction followed by the number Ex:Up 5 , Number should be < 10 n')
n=n.split()
if n[0].lower() not in ['up','left','down','right']:
print('wrong command')
continue
elif n[0].lower()=='up':
up(int(n[1].lower()),10)#Boundary is set to 10 as the 'easy' grid is a 10^10
elif n[0].lower()=='down':
down(int(n[1].lower()),10)
elif n[0].lower()=='left':
left(int(n[1].lower()),10)
elif n[0].lower()=='right':
right(int(n[1].lower()),10)
print("10 chests left")
print("10 bandits left")
print("Coins:",Coins)#Acts as a counter, displays the number of coins that the player has
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
or current[0] == Treasure3_Row and current[1] == Treasure3_Col
or current[0] == Treasure4_Row and current[1] == Treasure4_Col
or current[0] == Treasure5_Row and current[1] == Treasure5_Col
or current[0] == Treasure6_Row and current[1] == Treasure6_Col
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col
or current[0] == Treasure9_Row and current[1] == Treasure9_Col
or current[0] == Treasure10_Row and current[1] == Treasure10_Col:
print("Hooray! You have found booty! +10 gold")
Coins = Coins+10 #Adds an additional 10 points
print("Coins:",Coins)
if current[0] == Bandit1_Row and current[1] == Bandit1_Col
or current[0] == Bandit2_Row and current[1] == Bandit2_Col
or current[0] == Bandit3_Row and current[1] == Bandit3_Col
or current[0] == Bandit4_Row and current[1] == Bandit4_Col
or current[0] == Bandit5_Row and current[1] == Bandit5_Col
or current[0] == Bandit6_Row and current[1] == Bandit6_Col
or current[0] == Bandit7_Row and current[1] == Bandit7_Col
or current[0] == Bandit8_Row and current[1] == Bandit8_Col
or current[0] == Bandit9_Row and current[1] == Bandit9_Col
or current[0] == Bandit10_Row and current[1] == Bandit10_Col:
print("Oh no! You have landed on a bandit...they steal all your coins!")
Coins = Coins-Coins #Removes all coins
print("Coins:",Coins)
boardmed[current[0]][current[1]]='*'
def hard_level(Coins):
#This function is for the movement of the game in hard difficulty
while True:
oldcurrent=current
boardhard[oldcurrent[0]][oldcurrent[1]]='*'
table_game_hard()
boardhard[oldcurrent[0]][oldcurrent[1]]=' '
n = input('Enter the direction followed by the number Ex:Up 5 , Number should be < 12 n')
n=n.split()
if n[0].lower() not in ['up','left','down','right']:
print('wrong command')
continue
elif n[0].lower()=='up':
up(int(n[1].lower()),12)#Boundary is set to 12 as the 'hard' grid is a 12^12
elif n[0].lower()=='down':
down(int(n[1].lower()),12)
elif n[0].lower()=='left':
left(int(n[1].lower()),12)
elif n[0].lower()=='right':
right(int(n[1].lower()),12)
print("12 chests left")
print("12 bandits left")
print("Coins:",Coins)#Acts as a counter, displays the number of coins that the player has
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
or current[0] == Treasure3_Row and current[1] == Treasure3_Col
or current[0] == Treasure4_Row and current[1] == Treasure4_Col
or current[0] == Treasure5_Row and current[1] == Treasure5_Col
or current[0] == Treasure6_Row and current[1] == Treasure6_Col
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col
or current[0] == Treasure9_Row and current[1] == Treasure9_Col
or current[0] == Treasure10_Row and current[1] == Treasure10_Col
or current[0] == Treasure11_Row and current[1] == Treasure11_Col
or current[0] == Treasure12_Row and current[1] == Treasure12_Col:
print("Hooray! You have found booty! +10 gold")
Coins = Coins+10 #Adds an additional 10 points
print("Coins:",Coins)
if current[0] == Bandit1_Row and current[1] == Bandit1_Col
or current[0] == Bandit2_Row and current[1] == Bandit2_Col
or current[0] == Bandit3_Row and current[1] == Bandit3_Col
or current[0] == Bandit4_Row and current[1] == Bandit4_Col
or current[0] == Bandit5_Row and current[1] == Bandit5_Col
or current[0] == Bandit6_Row and current[1] == Bandit6_Col
or current[0] == Bandit7_Row and current[1] == Bandit7_Col
or current[0] == Bandit8_Row and current[1] == Bandit8_Col
or current[0] == Bandit9_Row and current[1] == Bandit9_Col
or current[0] == Bandit10_Row and current[1] == Bandit10_Col
or current[0] == Bandit11_Row and current[1] == Bandit11_Col
or current[0] == Bandit12_Row and current[1] == Bandit12_Col:
print("Oh no! You have landed on a bandit...they steal all your coins!")
Coins = Coins-Coins #Removes all coins
print("Coins:",Coins)
boardhard[current[0]][current[1]]='*'
def instruct():
difficulty = input("""Before the game starts, please consider what difficulty
would you like to play in, easy, medium or (if you're brave) hard.
""")
if difficulty == "easy":
print("That's great! Lets continue.")
time.sleep(1)#Allows the user time to get ready
print("initiating game in...")
print()
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
for i in range(3):
print("")
easy_level(Coins)
elif difficulty == "medium":
print("That's great! Lets continue.")
time.sleep(1)#Allows the user time to get ready
print("initiating game in...")
print()
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
for i in range(3):
print("")
med_level(Coins)
elif difficulty == "hard":
print("That's great! Lets continue.")
time.sleep(1)#Allows the user time to get ready
print("initiating game in...")
print()
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
for i in range(3):
print("")
hard_level(Coins)
else:
print("Sorry, that is an invalid answer. Please restart the programme")
def menu():
#This function lets the user quit the application or progress to playing.
print("")
print ("Are you sure you wish to play this game? Please answer either yes or no.")
choice1 = input() # Sets variable to user input
if choice1.lower().startswith('y'):
print("Okay lets continue then!")
elif choice1.lower().startswith('n'):
print("Thank you, I hope you will play next time!")
print("")
quit("Thank you for playing!")#Terminates the programme
else:
print("Sorry, that is an invalid answer. Please restart the programme")
print("")
quit()
instruct()
def showInstructions():
time.sleep(1.0)
print("Instructions of the game:")
time.sleep(1.0)
print("""
You are a treasure hunter, your goal is to collect atleast 100 gold by the end
of the game from treasure chests randomly scattered across the grid. There are
10 chests within a grid (this can be changed based on difficulty) and each
treasure chest is worth 10 gold but can only be reclaimed 3 times before it is
replaced by a bandit. Landing on a bandit will cause you to lose all of your
gold and if all the chests have been replaced by bandits and you have less then
100 gold this means you lose!
Press enter to continue...""")
input()
print("""
At the start of the game, you always begin at the top right of the grid.
Below is a representation of the game:
* 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Press enter to continue...""")
input()
print("""
When deciding where to move, you should input the direct co-ordinates of your
desired location. For instance:
Enter the direction followed by the number Ex: Up 5 , Number should be < 8
right 3
0 0 0 * 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Unlucky move! You have found nothing!
If nothing on the grid changes , this means that your move was a bust! Landing
on nothing isn't displayed on the grid.
Press enter to continue...""")
input()
print("""
Enter the direction followed by the number Ex: Up 5 , Number should be < 8
down 4
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Hooray! You have found booty! +10 gold
Here you can see that the use has landed on a chest!
As you land on chest, they get replaced by bandits. Be sure to remember the
previous locations so you don't accidently land on a bandit and lose all
your gold!
Press enter to continue...""")
input()
#Introduces the user
print('Welcome to the Treasure hunt!')
time.sleep(0.3)
print()
time.sleep(0.3)
print('Would you like to see the instructions? (yes/no)')
if input().lower().startswith('y'):#If function checks for the first letter
showInstructions()
elif input == 'no' or 'No':
print("Lets continue then!")#Calls the function which displays instructions
else:
print("Please check your input and try again.")
menu()
python game
New contributor
The code below is for a board game program, which works by placing the user on a grid which then tasks the user with finding all the treasure chests. Each chest landed on gives 10 gold however landing on a bandit means all your gold is taken. The end score should be given once the user has found all the chests.
A few features including the end game, score board, timer and more are still required to be added. For now I just want feedback on how to improve the code at its current state (the code could very well be simplified).
import random
import sys
import time
boardeasy=
boardmed=
boardhard=
current=[0,0]
Treasure1_Row = random.randint(0,8)
Treasure1_Col = random.randint(0,8)
Treasure2_Row = random.randint(0,8)
Treasure2_Col = random.randint(0,8)
Treasure3_Row = random.randint(0,8)
Treasure3_Col = random.randint(0,8)
Treasure4_Row = random.randint(0,8)
Treasure4_Col = random.randint(0,8)
Treasure5_Row = random.randint(0,8)
Treasure5_Col = random.randint(0,8)
Treasure6_Row = random.randint(0,8)
Treasure6_Col = random.randint(0,8)
Treasure7_Row = random.randint(0,8)
Treasure7_Col = random.randint(0,8)
Treasure8_Row = random.randint(0,8)
Treasure8_Col = random.randint(0,8)
Treasure9_Row = random.randint(0,8)
Treasure9_Col = random.randint(0,8)
Treasure10_Row = random.randint(0,8)
Treasure10_Col = random.randint(0,8)
Treasure11_Row = random.randint(0,8)
Treasure11_Col = random.randint(0,8)
Treasure12_Row = random.randint(0,8)
Treasure12_Col = random.randint(0,8)
Bandit1_Row = random.randint(0,8)
Bandit1_Col = random.randint(0,8)
Bandit2_Row = random.randint(0,8)
Bandit2_Col = random.randint(0,8)
Bandit3_Row = random.randint(0,8)
Bandit3_Col = random.randint(0,8)
Bandit4_Row = random.randint(0,8)
Bandit4_Col = random.randint(0,8)
Bandit5_Row = random.randint(0,8)
Bandit5_Col = random.randint(0,8)
Bandit6_Row = random.randint(0,8)
Bandit6_Col = random.randint(0,8)
Bandit7_Row = random.randint(0,8)
Bandit7_Col = random.randint(0,8)
Bandit8_Row = random.randint(0,8)
Bandit8_Col = random.randint(0,8)
Bandit9_Row = random.randint(0,8)
Bandit9_Col = random.randint(0,8)
Bandit10_Row = random.randint(0,8)
Bandit10_Col = random.randint(0,8)
Bandit11_Row = random.randint(0,8)
Bandit11_Col = random.randint(0,8)
Bandit12_Row = random.randint(0,8)
Bandit12_Col = random.randint(0,8)
Coins = 0
class user():
def __init__(self, username, userscore, usertime):
self.username = username
self.userscore = userscore
self.usertime = usertime
#For loop prints a new 8*8 grid after every move
for i in range(8):
b=
for j in range(8):
b.append(' ')
boardeasy.append(b)
def table_game_easy():
print(" 1 2 3 4 5 6 7 8")
print("---------------------------------")
print ('| ' + boardeasy[0][0] + '| ' + boardeasy[0][1] + ' | ' + boardeasy[0][2] + ' | ' + boardeasy[0][3] + ' | ' + boardeasy[0][4] + ' | ' + boardeasy[0][5] + ' | ' + boardeasy[0][6] + ' | ' + boardeasy[0][7] + ' | ' + '1')
print("---------------------------------")
print ('| ' + boardeasy[1][0] + '| ' + boardeasy[1][1] + ' | ' + boardeasy[1][2] + ' | ' + boardeasy[1][3] + ' | ' + boardeasy[1][4] + ' | ' + boardeasy[1][5] + ' | ' + boardeasy[1][6] + ' | ' + boardeasy[1][7] + ' | ' + '2')
print("---------------------------------")
print ('| ' + boardeasy[2][0] + '| ' + boardeasy[2][1] + ' | ' + boardeasy[2][2] + ' | ' + boardeasy[2][3] + ' | ' + boardeasy[2][4] + ' | ' + boardeasy[2][5] + ' | ' + boardeasy[2][6] + ' | ' + boardeasy[2][7] + ' | ' + '3')
print("---------------------------------")
print ('| ' + boardeasy[3][0] + '| ' + boardeasy[3][1] + ' | ' + boardeasy[3][2] + ' | ' + boardeasy[3][3] + ' | ' + boardeasy[3][4] + ' | ' + boardeasy[3][5] + ' | ' + boardeasy[3][6] + ' | ' + boardeasy[3][7] + ' | ' + '4')
print("---------------------------------")
print ('| ' + boardeasy[4][0] + '| ' + boardeasy[4][1] + ' | ' + boardeasy[4][2] + ' | ' + boardeasy[4][3] + ' | ' + boardeasy[4][4] + ' | ' + boardeasy[4][5] + ' | ' + boardeasy[4][6] + ' | ' + boardeasy[4][7] + ' | ' + '5')
print("---------------------------------")
print ('| ' + boardeasy[5][0] + '| ' + boardeasy[5][1] + ' | ' + boardeasy[4][2] + ' | ' + boardeasy[5][3] + ' | ' + boardeasy[5][4] + ' | ' + boardeasy[5][5] + ' | ' + boardeasy[5][6] + ' | ' + boardeasy[5][7] + ' | ' + '6')
print("---------------------------------")
print ('| ' + boardeasy[6][0] + '| ' + boardeasy[6][1] + ' | ' + boardeasy[5][2] + ' | ' + boardeasy[6][3] + ' | ' + boardeasy[6][4] + ' | ' + boardeasy[6][5] + ' | ' + boardeasy[6][6] + ' | ' + boardeasy[6][7] + ' | ' + '7')
print("---------------------------------")
print ('| ' + boardeasy[7][0] + '| ' + boardeasy[7][1] + ' | ' + boardeasy[7][2] + ' | ' + boardeasy[7][3] + ' | ' + boardeasy[7][4] + ' | ' + boardeasy[7][5] + ' | ' + boardeasy[7][6] + ' | ' + boardeasy[7][7] + ' | ' + '8')
print("---------------------------------")
#Variables which will store a certain range
Treasure1_Row = random.randint(0,8)
Treasure1_Col = random.randint(0,8)
Treasure2_Row = random.randint(0,8)
Treasure2_Col = random.randint(0,8)
Treasure3_Row = random.randint(0,8)
Treasure3_Col = random.randint(0,8)
Treasure4_Row = random.randint(0,8)
Treasure4_Col = random.randint(0,8)
Treasure5_Row = random.randint(0,8)
Treasure5_Col = random.randint(0,8)
Treasure6_Row = random.randint(0,8)
Treasure6_Col = random.randint(0,8)
Treasure7_Row = random.randint(0,8)
Treasure7_Col = random.randint(0,8)
Treasure8_Row = random.randint(0,8)
Treasure8_Col = random.randint(0,8)
Treasure9_Row = random.randint(0,8)
Treasure9_Col = random.randint(0,8)
Treasure10_Row = random.randint(0,8)
Treasure10_Col = random.randint(0,8)
Bandit1_Row = random.randint(0,8)
Bandit1_Col = random.randint(0,8)
Bandit2_Row = random.randint(0,8)
Bandit2_Col = random.randint(0,8)
Bandit3_Row = random.randint(0,8)
Bandit3_Col = random.randint(0,8)
Bandit4_Row = random.randint(0,8)
Bandit4_Col = random.randint(0,8)
Bandit5_Row = random.randint(0,8)
Bandit5_Col = random.randint(0,8)
# For loop prints a new 10*10 grid after every move
for i in range(10):
b=
for j in range(10):
b.append(' ')
boardmed.append(b)
def table_game_meduim():
print(" 1 2 3 4 5 6 7 8 9 10")
print("-----------------------------------------")
print ('| ' + boardmed[0][0] + '| ' + boardmed[0][1] + ' | ' + boardmed[0][2] + ' | ' + boardmed[0][3] + ' | ' + boardmed[0][4] + ' | ' + boardmed[0][5] + ' | ' + boardmed[0][6] + ' | ' + boardmed[0][7] + ' | ' + boardmed[0][8] + ' | ' + boardmed[0][9] + ' | ' + '1')
print("-----------------------------------------")
print ('| ' + boardmed[1][0] + '| ' + boardmed[1][1] + ' | ' + boardmed[1][2] + ' | ' + boardmed[1][3] + ' | ' + boardmed[1][4] + ' | ' + boardmed[1][5] + ' | ' + boardmed[1][6] + ' | ' + boardmed[1][7] + ' | ' + boardmed[1][8] + ' | ' + boardmed[1][9] + ' | ' + '2')
print("-----------------------------------------")
print ('| ' + boardmed[2][0] + '| ' + boardmed[2][1] + ' | ' + boardmed[2][2] + ' | ' + boardmed[2][3] + ' | ' + boardmed[2][4] + ' | ' + boardmed[2][5] + ' | ' + boardmed[2][6] + ' | ' + boardmed[2][7] + ' | ' + boardmed[2][8] + ' | ' + boardmed[2][9] + ' | ' + '3')
print("-----------------------------------------")
print ('| ' + boardmed[3][0] + '| ' + boardmed[3][1] + ' | ' + boardmed[3][2] + ' | ' + boardmed[3][3] + ' | ' + boardmed[3][4] + ' | ' + boardmed[3][5] + ' | ' + boardmed[3][6] + ' | ' + boardmed[3][7] + ' | ' + boardmed[3][8] + ' | ' + boardmed[3][9] + ' | ' + '4')
print("-----------------------------------------")
print ('| ' + boardmed[4][0] + '| ' + boardmed[4][1] + ' | ' + boardmed[4][2] + ' | ' + boardmed[4][3] + ' | ' + boardmed[4][4] + ' | ' + boardmed[4][5] + ' | ' + boardmed[4][6] + ' | ' + boardmed[4][7] + ' | ' + boardmed[4][8] + ' | ' + boardmed[4][9] + ' | ' + '5')
print("-----------------------------------------")
print ('| ' + boardmed[5][0] + '| ' + boardmed[5][1] + ' | ' + boardmed[5][2] + ' | ' + boardmed[5][3] + ' | ' + boardmed[5][4] + ' | ' + boardmed[5][5] + ' | ' + boardmed[5][6] + ' | ' + boardmed[5][7] + ' | ' + boardmed[5][8] + ' | ' + boardmed[5][9] + ' | ' + '6')
print("-----------------------------------------")
print ('| ' + boardmed[6][0] + '| ' + boardmed[6][1] + ' | ' + boardmed[6][2] + ' | ' + boardmed[6][3] + ' | ' + boardmed[6][4] + ' | ' + boardmed[6][5] + ' | ' + boardmed[6][6] + ' | ' + boardmed[6][7] + ' | ' + boardmed[6][8] + ' | ' + boardmed[6][9] + ' | ' + '7')
print("-----------------------------------------")
print ('| ' + boardmed[7][0] + '| ' + boardmed[7][1] + ' | ' + boardmed[7][2] + ' | ' + boardmed[7][3] + ' | ' + boardmed[7][4] + ' | ' + boardmed[7][5] + ' | ' + boardmed[7][6] + ' | ' + boardmed[7][7] + ' | ' + boardmed[7][8] + ' | ' + boardmed[7][9] + ' | ' + '8')
print("-----------------------------------------")
print ('| ' + boardmed[8][0] + '| ' + boardmed[8][1] + ' | ' + boardmed[8][2] + ' | ' + boardmed[8][3] + ' | ' + boardmed[8][4] + ' | ' + boardmed[8][5] + ' | ' + boardmed[8][6] + ' | ' + boardmed[8][7] + ' | ' + boardmed[8][8] + ' | ' + boardmed[8][9] + ' | ' + '9')
print("-----------------------------------------")
print ('| ' + boardmed[9][0] + '| ' + boardmed[9][1] + ' | ' + boardmed[9][2] + ' | ' + boardmed[9][3] + ' | ' + boardmed[9][4] + ' | ' + boardmed[9][5] + ' | ' + boardmed[9][6] + ' | ' + boardmed[9][7] + ' | ' + boardmed[9][8] + ' | ' + boardmed[9][9] + ' | ' + '10')
print("-----------------------------------------")
Treasure1_Row = random.randint(0,10)
Treasure1_Col = random.randint(0,10)
Treasure2_Row = random.randint(0,10)
Treasure2_Col = random.randint(0,10)
Treasure3_Row = random.randint(0,10)
Treasure3_Col = random.randint(0,10)
Treasure4_Row = random.randint(0,10)
Treasure4_Col = random.randint(0,10)
Treasure5_Row = random.randint(0,10)
Treasure5_Col = random.randint(0,10)
Treasure6_Row = random.randint(0,10)
Treasure6_Col = random.randint(0,10)
Treasure7_Row = random.randint(0,10)
Treasure7_Col = random.randint(0,10)
Treasure8_Row = random.randint(0,10)
Treasure8_Col = random.randint(0,10)
Treasure9_Row = random.randint(0,10)
Treasure9_Col = random.randint(0,10)
Treasure10_Row = random.randint(0,10)
Treasure10_Col = random.randint(0,10)
Bandit1_Row = random.randint(0,10)
Bandit1_Col = random.randint(0,10)
Bandit2_Row = random.randint(0,10)
Bandit2_Col = random.randint(0,10)
Bandit3_Row = random.randint(0,10)
Bandit3_Col = random.randint(0,10)
Bandit4_Row = random.randint(0,10)
Bandit4_Col = random.randint(0,10)
Bandit5_Row = random.randint(0,10)
Bandit5_Col = random.randint(0,10)
Bandit6_Row = random.randint(0,10)
Bandit6_Col = random.randint(0,10)
Bandit7_Row = random.randint(0,10)
Bandit7_Col = random.randint(0,10)
# For loop prints a new 12*12 grid after every move
for i in range(12):
b=
for j in range(12):
b.append(' ')
boardhard.append(b)
def table_game_hard():
print(" 1 2 3 4 5 6 7 8 9 10 11 12")
print("-------------------------------------------------")
print ('| ' + boardhard[0][0] + '| ' + boardhard[0][1] + ' | ' + boardhard[0][2] + ' | ' + boardhard[0][3] + ' | ' + boardhard[0][4] + ' | ' + boardhard[0][5] + ' | ' + boardhard[0][6] + ' | ' + boardhard[0][7] + ' | ' + boardhard[0][8] + ' | ' + boardhard[0][9] + ' | '+ boardhard[0][10] + ' | ' + boardhard[0][11] + ' | ' + '1')
print("-------------------------------------------------")
print ('| ' + boardhard[1][0] + '| ' + boardhard[1][1] + ' | ' + boardhard[1][2] + ' | ' + boardhard[1][3] + ' | ' + boardhard[1][4] + ' | ' + boardhard[1][5] + ' | ' + boardhard[1][6] + ' | ' + boardhard[1][7] + ' | ' + boardhard[1][8] + ' | ' + boardhard[1][9] + ' | '+ boardhard[1][10] + ' | ' + boardhard[1][11] + ' | ' + '2')
print("-------------------------------------------------")
print ('| ' + boardhard[2][0] + '| ' + boardhard[2][1] + ' | ' + boardhard[2][2] + ' | ' + boardhard[2][3] + ' | ' + boardhard[2][4] + ' | ' + boardhard[2][5] + ' | ' + boardhard[2][6] + ' | ' + boardhard[2][7] + ' | ' + boardhard[2][8] + ' | ' + boardhard[2][9] + ' | '+ boardhard[2][10] + ' | ' + boardhard[2][11] + ' | ' + '3')
print("-------------------------------------------------")
print ('| ' + boardhard[3][0] + '| ' + boardhard[3][1] + ' | ' + boardhard[3][2] + ' | ' + boardhard[3][3] + ' | ' + boardhard[3][4] + ' | ' + boardhard[3][5] + ' | ' + boardhard[3][6] + ' | ' + boardhard[3][7] + ' | ' + boardhard[3][8] + ' | ' + boardhard[3][9] + ' | '+ boardhard[3][10] + ' | ' + boardhard[3][11] + ' | ' + '4')
print("-------------------------------------------------")
print ('| ' + boardhard[4][0] + '| ' + boardhard[4][1] + ' | ' + boardhard[4][2] + ' | ' + boardhard[4][3] + ' | ' + boardhard[4][4] + ' | ' + boardhard[4][5] + ' | ' + boardhard[4][6] + ' | ' + boardhard[4][7] + ' | ' + boardhard[4][8] + ' | ' + boardhard[4][9] + ' | '+ boardhard[4][10] + ' | ' + boardhard[4][11] + ' | ' + '5')
print("-------------------------------------------------")
print ('| ' + boardhard[5][0] + '| ' + boardhard[5][1] + ' | ' + boardhard[5][2] + ' | ' + boardhard[5][3] + ' | ' + boardhard[5][4] + ' | ' + boardhard[5][5] + ' | ' + boardhard[5][6] + ' | ' + boardhard[5][7] + ' | ' + boardhard[5][8] + ' | ' + boardhard[5][9] + ' | '+ boardhard[5][10] + ' | ' + boardhard[5][11] + ' | ' + '6')
print("-------------------------------------------------")
print ('| ' + boardhard[6][0] + '| ' + boardhard[6][1] + ' | ' + boardhard[6][2] + ' | ' + boardhard[6][3] + ' | ' + boardhard[6][4] + ' | ' + boardhard[6][5] + ' | ' + boardhard[6][6] + ' | ' + boardhard[6][7] + ' | ' + boardhard[6][8] + ' | ' + boardhard[6][9] + ' | '+ boardhard[6][10] + ' | ' + boardhard[6][11] + ' | ' + '7')
print("-------------------------------------------------")
print ('| ' + boardhard[7][0] + '| ' + boardhard[7][1] + ' | ' + boardhard[7][2] + ' | ' + boardhard[7][3] + ' | ' + boardhard[7][4] + ' | ' + boardhard[7][5] + ' | ' + boardhard[7][6] + ' | ' + boardhard[7][7] + ' | ' + boardhard[7][8] + ' | ' + boardhard[7][9] + ' | '+ boardhard[7][10] + ' | ' + boardhard[7][11] + ' | ' + '8')
print("-------------------------------------------------")
print ('| ' + boardhard[8][0] + '| ' + boardhard[8][1] + ' | ' + boardhard[8][2] + ' | ' + boardhard[8][3] + ' | ' + boardhard[8][4] + ' | ' + boardhard[8][5] + ' | ' + boardhard[8][6] + ' | ' + boardhard[8][7] + ' | ' + boardhard[8][8] + ' | ' + boardhard[8][9] + ' | '+ boardhard[8][10] + ' | ' + boardhard[8][11] + ' | ' + '9')
print("-------------------------------------------------")
print ('| ' + boardhard[9][0] + '| ' + boardhard[9][1] + ' | ' + boardhard[9][2] + ' | ' + boardhard[9][3] + ' | ' + boardhard[9][4] + ' | ' + boardhard[9][5] + ' | ' + boardhard[9][6] + ' | ' + boardhard[9][7] + ' | ' + boardhard[9][8] + ' | ' + boardhard[9][9] + ' | '+ boardhard[9][10] + ' | ' + boardhard[9][11] + ' | ' + '10')
print("-------------------------------------------------")
print ('| ' + boardhard[10][0] + '| ' + boardhard[10][1] + ' | ' + boardhard[10][2] + ' | ' + boardhard[10][3] + ' | ' + boardhard[10][4] + ' | ' + boardhard[10][5] + ' | ' + boardhard[10][6] + ' | ' + boardhard[10][7] + ' | ' + boardhard[10][8] + ' | ' + boardhard[10][9] + ' | ' + boardhard[10][10] + ' | ' + boardhard[10][11] + ' | ' + '11')
print("-------------------------------------------------")
print ('| ' + boardhard[11][0] + '| ' + boardhard[11][1] + ' | ' + boardhard[11][2] + ' | ' + boardhard[11][3] + ' | ' + boardhard[11][4] + ' | ' + boardhard[11][5] + ' | ' + boardhard[11][6] + ' | ' + boardhard[11][7] + ' | ' + boardhard[11][8] + ' | ' + boardhard[11][9] + ' | ' + boardhard[11][11] + ' | ' + boardhard[11][11] + ' | ' + '12')
print("-------------------------------------------------")
Treasure1_Row = random.randint(0,12)
Treasure1_Col = random.randint(0,12)
Treasure2_Row = random.randint(0,12)
Treasure2_Col = random.randint(0,12)
Treasure3_Row = random.randint(0,12)
Treasure3_Col = random.randint(0,12)
Treasure4_Row = random.randint(0,12)
Treasure4_Col = random.randint(0,12)
Treasure5_Row = random.randint(0,12)
Treasure5_Col = random.randint(0,12)
Treasure6_Row = random.randint(0,12)
Treasure6_Col = random.randint(0,12)
Treasure7_Row = random.randint(0,12)
Treasure7_Col = random.randint(0,12)
Treasure8_Row = random.randint(0,12)
Treasure8_Col = random.randint(0,12)
Treasure9_Row = random.randint(0,12)
Treasure9_Col = random.randint(0,12)
Treasure10_Row = random.randint(0,12)
Treasure10_Col = random.randint(0,12)
Bandit1_Row = random.randint(0,12)
Bandit1_Col = random.randint(0,12)
Bandit2_Row = random.randint(0,12)
Bandit2_Col = random.randint(0,12)
Bandit3_Row = random.randint(0,12)
Bandit3_Col = random.randint(0,12)
Bandit4_Row = random.randint(0,12)
Bandit4_Col = random.randint(0,12)
Bandit5_Row = random.randint(0,12)
Bandit5_Col = random.randint(0,12)
Bandit6_Row = random.randint(0,12)
Bandit6_Col = random.randint(0,12)
Bandit7_Row = random.randint(0,12)
Bandit7_Col = random.randint(0,12)
Bandit8_Row = random.randint(0,12)
Bandit8_Col = random.randint(0,12)
Bandit9_Row = random.randint(0,12)
Bandit9_Col = random.randint(0,12)
#this function is in charge of downwards movement
def down(num,lev):
num=(num+current[0])%lev#The % formats this equation
current[0]=num
#this function is in charge of downwards movement
def right(num,lev):
num=(num+current[1])%lev #The % formats this equation
current[1]=num
#this function is in charge of downwards movement
def left(num,lev):
if current[1]-num>=0:
current[1]=current[1]-num
else:
current[1]=current[1]-num+lev
#this function is in charge of downwards movement
def up(num,lev):
if current[0]-num>=0:
current[0]=current[0]-num
else:
current[0]=current[0]-num+lev
def easy_level(Coins):
#This function is for the movement of the game in easy difficulty
while True:
oldcurrent=current
boardeasy[oldcurrent[0]][oldcurrent[1]]='*'
table_game_easy()
boardeasy[oldcurrent[0]][oldcurrent[1]]=' '
n = input('Enter the direction followed by the number Ex:Up 5 , Number should be < 8 n')
n=n.split()
if n[0].lower() not in ['up','left','down','right']:#Validates input
print('Wrong command, please input again')
continue
elif n[0].lower()=='up':
up(int(n[1].lower()),8)#Boundary is set to 8 as the 'easy' grid is a 8^8
elif n[0].lower()=='down':
down(int(n[1].lower()),8)
elif n[0].lower()=='left':
left(int(n[1].lower()),8)
elif n[0].lower()=='right':
right(int(n[1].lower()),8)
print("8 chests left")
print("8 bandits left")
print("Coins:",Coins)#Acts as a counter, displays the number of coins that the player has
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
or current[0] == Treasure3_Row and current[1] == Treasure3_Col
or current[0] == Treasure4_Row and current[1] == Treasure4_Col
or current[0] == Treasure5_Row and current[1] == Treasure5_Col
or current[0] == Treasure6_Row and current[1] == Treasure6_Col
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col:
print("Hooray! You have found booty! +10 gold")
Coins = Coins+10 #Adds an additional 10 points
print("Coins:",Coins)
if current[0] == Bandit1_Row and current[1] == Bandit1_Col
or current[0] == Bandit2_Row and current[1] == Bandit2_Col
or current[0] == Bandit3_Row and current[1] == Bandit3_Col
or current[0] == Bandit4_Row and current[1] == Bandit4_Col
or current[0] == Bandit5_Row and current[1] == Bandit5_Col
or current[0] == Bandit6_Row and current[1] == Bandit6_Col
or current[0] == Bandit7_Row and current[1] == Bandit7_Col
or current[0] == Bandit8_Row and current[1] == Bandit8_Col:
print("Oh no! You have landed on a bandit...they steal all your coins!")
Coins = Coins-Coins #Removes all coins
print("Coins:",Coins)
boardeasy[current[0]][current[1]]='*'#sets value to players position
def med_level(Coins):
#This function is for the movement of the game in medium difficulty
while True:
oldcurrent=current
boardmed[oldcurrent[0]][oldcurrent[1]]='*'
table_game_meduim()
boardmed[oldcurrent[0]][oldcurrent[1]]=' '
n = input('Enter the direction followed by the number Ex:Up 5 , Number should be < 10 n')
n=n.split()
if n[0].lower() not in ['up','left','down','right']:
print('wrong command')
continue
elif n[0].lower()=='up':
up(int(n[1].lower()),10)#Boundary is set to 10 as the 'easy' grid is a 10^10
elif n[0].lower()=='down':
down(int(n[1].lower()),10)
elif n[0].lower()=='left':
left(int(n[1].lower()),10)
elif n[0].lower()=='right':
right(int(n[1].lower()),10)
print("10 chests left")
print("10 bandits left")
print("Coins:",Coins)#Acts as a counter, displays the number of coins that the player has
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
or current[0] == Treasure3_Row and current[1] == Treasure3_Col
or current[0] == Treasure4_Row and current[1] == Treasure4_Col
or current[0] == Treasure5_Row and current[1] == Treasure5_Col
or current[0] == Treasure6_Row and current[1] == Treasure6_Col
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col
or current[0] == Treasure9_Row and current[1] == Treasure9_Col
or current[0] == Treasure10_Row and current[1] == Treasure10_Col:
print("Hooray! You have found booty! +10 gold")
Coins = Coins+10 #Adds an additional 10 points
print("Coins:",Coins)
if current[0] == Bandit1_Row and current[1] == Bandit1_Col
or current[0] == Bandit2_Row and current[1] == Bandit2_Col
or current[0] == Bandit3_Row and current[1] == Bandit3_Col
or current[0] == Bandit4_Row and current[1] == Bandit4_Col
or current[0] == Bandit5_Row and current[1] == Bandit5_Col
or current[0] == Bandit6_Row and current[1] == Bandit6_Col
or current[0] == Bandit7_Row and current[1] == Bandit7_Col
or current[0] == Bandit8_Row and current[1] == Bandit8_Col
or current[0] == Bandit9_Row and current[1] == Bandit9_Col
or current[0] == Bandit10_Row and current[1] == Bandit10_Col:
print("Oh no! You have landed on a bandit...they steal all your coins!")
Coins = Coins-Coins #Removes all coins
print("Coins:",Coins)
boardmed[current[0]][current[1]]='*'
def hard_level(Coins):
#This function is for the movement of the game in hard difficulty
while True:
oldcurrent=current
boardhard[oldcurrent[0]][oldcurrent[1]]='*'
table_game_hard()
boardhard[oldcurrent[0]][oldcurrent[1]]=' '
n = input('Enter the direction followed by the number Ex:Up 5 , Number should be < 12 n')
n=n.split()
if n[0].lower() not in ['up','left','down','right']:
print('wrong command')
continue
elif n[0].lower()=='up':
up(int(n[1].lower()),12)#Boundary is set to 12 as the 'hard' grid is a 12^12
elif n[0].lower()=='down':
down(int(n[1].lower()),12)
elif n[0].lower()=='left':
left(int(n[1].lower()),12)
elif n[0].lower()=='right':
right(int(n[1].lower()),12)
print("12 chests left")
print("12 bandits left")
print("Coins:",Coins)#Acts as a counter, displays the number of coins that the player has
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
or current[0] == Treasure3_Row and current[1] == Treasure3_Col
or current[0] == Treasure4_Row and current[1] == Treasure4_Col
or current[0] == Treasure5_Row and current[1] == Treasure5_Col
or current[0] == Treasure6_Row and current[1] == Treasure6_Col
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col
or current[0] == Treasure9_Row and current[1] == Treasure9_Col
or current[0] == Treasure10_Row and current[1] == Treasure10_Col
or current[0] == Treasure11_Row and current[1] == Treasure11_Col
or current[0] == Treasure12_Row and current[1] == Treasure12_Col:
print("Hooray! You have found booty! +10 gold")
Coins = Coins+10 #Adds an additional 10 points
print("Coins:",Coins)
if current[0] == Bandit1_Row and current[1] == Bandit1_Col
or current[0] == Bandit2_Row and current[1] == Bandit2_Col
or current[0] == Bandit3_Row and current[1] == Bandit3_Col
or current[0] == Bandit4_Row and current[1] == Bandit4_Col
or current[0] == Bandit5_Row and current[1] == Bandit5_Col
or current[0] == Bandit6_Row and current[1] == Bandit6_Col
or current[0] == Bandit7_Row and current[1] == Bandit7_Col
or current[0] == Bandit8_Row and current[1] == Bandit8_Col
or current[0] == Bandit9_Row and current[1] == Bandit9_Col
or current[0] == Bandit10_Row and current[1] == Bandit10_Col
or current[0] == Bandit11_Row and current[1] == Bandit11_Col
or current[0] == Bandit12_Row and current[1] == Bandit12_Col:
print("Oh no! You have landed on a bandit...they steal all your coins!")
Coins = Coins-Coins #Removes all coins
print("Coins:",Coins)
boardhard[current[0]][current[1]]='*'
def instruct():
difficulty = input("""Before the game starts, please consider what difficulty
would you like to play in, easy, medium or (if you're brave) hard.
""")
if difficulty == "easy":
print("That's great! Lets continue.")
time.sleep(1)#Allows the user time to get ready
print("initiating game in...")
print()
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
for i in range(3):
print("")
easy_level(Coins)
elif difficulty == "medium":
print("That's great! Lets continue.")
time.sleep(1)#Allows the user time to get ready
print("initiating game in...")
print()
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
for i in range(3):
print("")
med_level(Coins)
elif difficulty == "hard":
print("That's great! Lets continue.")
time.sleep(1)#Allows the user time to get ready
print("initiating game in...")
print()
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
for i in range(3):
print("")
hard_level(Coins)
else:
print("Sorry, that is an invalid answer. Please restart the programme")
def menu():
#This function lets the user quit the application or progress to playing.
print("")
print ("Are you sure you wish to play this game? Please answer either yes or no.")
choice1 = input() # Sets variable to user input
if choice1.lower().startswith('y'):
print("Okay lets continue then!")
elif choice1.lower().startswith('n'):
print("Thank you, I hope you will play next time!")
print("")
quit("Thank you for playing!")#Terminates the programme
else:
print("Sorry, that is an invalid answer. Please restart the programme")
print("")
quit()
instruct()
def showInstructions():
time.sleep(1.0)
print("Instructions of the game:")
time.sleep(1.0)
print("""
You are a treasure hunter, your goal is to collect atleast 100 gold by the end
of the game from treasure chests randomly scattered across the grid. There are
10 chests within a grid (this can be changed based on difficulty) and each
treasure chest is worth 10 gold but can only be reclaimed 3 times before it is
replaced by a bandit. Landing on a bandit will cause you to lose all of your
gold and if all the chests have been replaced by bandits and you have less then
100 gold this means you lose!
Press enter to continue...""")
input()
print("""
At the start of the game, you always begin at the top right of the grid.
Below is a representation of the game:
* 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Press enter to continue...""")
input()
print("""
When deciding where to move, you should input the direct co-ordinates of your
desired location. For instance:
Enter the direction followed by the number Ex: Up 5 , Number should be < 8
right 3
0 0 0 * 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Unlucky move! You have found nothing!
If nothing on the grid changes , this means that your move was a bust! Landing
on nothing isn't displayed on the grid.
Press enter to continue...""")
input()
print("""
Enter the direction followed by the number Ex: Up 5 , Number should be < 8
down 4
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Hooray! You have found booty! +10 gold
Here you can see that the use has landed on a chest!
As you land on chest, they get replaced by bandits. Be sure to remember the
previous locations so you don't accidently land on a bandit and lose all
your gold!
Press enter to continue...""")
input()
#Introduces the user
print('Welcome to the Treasure hunt!')
time.sleep(0.3)
print()
time.sleep(0.3)
print('Would you like to see the instructions? (yes/no)')
if input().lower().startswith('y'):#If function checks for the first letter
showInstructions()
elif input == 'no' or 'No':
print("Lets continue then!")#Calls the function which displays instructions
else:
print("Please check your input and try again.")
menu()
python game
python game
New contributor
New contributor
edited Nov 15 at 13:41
200_success
127k15148410
127k15148410
New contributor
asked Nov 15 at 12:43
J.Peggy
61
61
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Generally, when reading your code, I think you need to consider the DRY principle. I will elaborate on this through several specific examples, but it's generally a good thing to keep in mind and understand when you code anything. This principle also means you should almost never copy-paste code exactly: that's a sign that there's a cleaner way to organize the code.
If you have any questions about any of my suggestions, feel free to ask them in comments.
Avoid similarly named hardcoded variables
You hard define a set of very similarly named variables (multiple times):
Treasure1_Row = random.randint(0,8)
Treasure1_Col = random.randint(0,8)
Treasure2_Row = random.randint(0,8)
Treasure2_Col = random.randint(0,8)
# ...
Treasure11_Row = random.randint(0,8)
Treasure11_Col = random.randint(0,8)
Treasure12_Row = random.randint(0,8)
Treasure12_Col = random.randint(0,8)
Bandit1_Row = random.randint(0,8)
Bandit1_Col = random.randint(0,8)
Bandit2_Row = random.randint(0,8)
Bandit2_Col = random.randint(0,8)
# ...
Bandit11_Row = random.randint(0,8)
Bandit11_Col = random.randint(0,8)
Bandit12_Row = random.randint(0,8)
Bandit12_Col = random.randint(0,8)
Generally you should use lists instead, and use a loop to assign all the list elements the same thing. List comprehensions can make your code even more succinct. Here's how:
treasures_row = [random.randint(0,8) for i in range(12)]
treasures_col = [random.randint(0,8) for i in range(12)]
bandits_row = [random.randint(0,8) for i in range(12)]
bandits_col = [random.randint(0,8) for i in range(12)]
Even more idiomatically, you could store the coordinates as two-tuples, instead of having separate lists for the rows and cols:
treasures = [(random.randint(0,8), random.randint(0,8)) for i in range(12)]
bandits = [(random.randint(0,8), random.randint(0,8)) for i in range(12)]
This will of course require reorganizing other parts of your code, but the benefits far outweigh the downsides. I will now go over how to fix all the places that reference the treasure and bandit lists.
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
# ...
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col:
can now become:
if (current[0], current[1]) in treasures[:8]:
The [:8]
is called a slice, and it returns a list containing the first 8 elements of the original list.
Use more loops
Mostly, you want to create your lists in list-comprehensions; so this:
for i in range(8):
b=
for j in range(8):
b.append(' ')
boardeasy.append(b)
will become:
boardeasy = [[' ' for j in range(8)] for i in range(8)]
This block can be shrunk significantly because it repeats:
print(" 1 2 3 4 5 6 7 8")
print("---------------------------------")
print ('| ' + boardeasy[0][0] + '| ' + boardeasy[0][1] + ' | ' + boardeasy[0][2] + ' | ' + boardeasy[0][3] + ' | ' + boardeasy[0][4] + ' | ' + boardeasy[0][5] + ' | ' + boardeasy[0][6] + ' | ' + boardeasy[0][7] + ' | ' + '1')
print("---------------------------------")
# ...
print ('| ' + boardeasy[7][0] + '| ' + boardeasy[7][1] + ' | ' + boardeasy[7][2] + ' | ' + boardeasy[7][3] + ' | ' + boardeasy[7][4] + ' | ' + boardeasy[7][5] + ' | ' + boardeasy[7][6] + ' | ' + boardeasy[7][7] + ' | ' + '8')
print("---------------------------------")
To start with, most print statement repeat |
. Python has a built in string operation for this: it's called str.join()
. Here's how to use it; you go from this:
print('| ' + boardeasy[0][0] + '| ' + boardeasy[0][1] + ' | ' + boardeasy[0][2] + ' | ' + boardeasy[0][3] + ' | ' + boardeasy[0][4] + ' | ' + boardeasy[0][5] + ' | ' + boardeasy[0][6] + ' | ' + boardeasy[0][7] + ' | ' + '1')
to this (while also centering the character in the first column):
print('| ' + ' | '.join(boardeasy[0][:8]) + ' | ' + '1')
But we can make it even better because the board rows are also very repetitious among each other. You can fix this with a simple for-loop:
print(" 1 2 3 4 5 6 7 8")
print("---------------------------------")
for row in range(8):
print ('| ' + ' | '.join(boardeasy[row][:8]) + ' | ' + str(row + 1))
print("---------------------------------")
(I could also automate the first line, but we'll get to that in a second.)
Don't split functions with the same content; use parameters
table_game_easy
,table_game_medium
, andtable_game_hard
should be combined into one function.
easy_level
,med_level
, andhard_level
should be combined into one function.
There are more ways to reduce redundancy, but try to find them on your own. If you have any more questions, you can comment on this post or ask a new question.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Generally, when reading your code, I think you need to consider the DRY principle. I will elaborate on this through several specific examples, but it's generally a good thing to keep in mind and understand when you code anything. This principle also means you should almost never copy-paste code exactly: that's a sign that there's a cleaner way to organize the code.
If you have any questions about any of my suggestions, feel free to ask them in comments.
Avoid similarly named hardcoded variables
You hard define a set of very similarly named variables (multiple times):
Treasure1_Row = random.randint(0,8)
Treasure1_Col = random.randint(0,8)
Treasure2_Row = random.randint(0,8)
Treasure2_Col = random.randint(0,8)
# ...
Treasure11_Row = random.randint(0,8)
Treasure11_Col = random.randint(0,8)
Treasure12_Row = random.randint(0,8)
Treasure12_Col = random.randint(0,8)
Bandit1_Row = random.randint(0,8)
Bandit1_Col = random.randint(0,8)
Bandit2_Row = random.randint(0,8)
Bandit2_Col = random.randint(0,8)
# ...
Bandit11_Row = random.randint(0,8)
Bandit11_Col = random.randint(0,8)
Bandit12_Row = random.randint(0,8)
Bandit12_Col = random.randint(0,8)
Generally you should use lists instead, and use a loop to assign all the list elements the same thing. List comprehensions can make your code even more succinct. Here's how:
treasures_row = [random.randint(0,8) for i in range(12)]
treasures_col = [random.randint(0,8) for i in range(12)]
bandits_row = [random.randint(0,8) for i in range(12)]
bandits_col = [random.randint(0,8) for i in range(12)]
Even more idiomatically, you could store the coordinates as two-tuples, instead of having separate lists for the rows and cols:
treasures = [(random.randint(0,8), random.randint(0,8)) for i in range(12)]
bandits = [(random.randint(0,8), random.randint(0,8)) for i in range(12)]
This will of course require reorganizing other parts of your code, but the benefits far outweigh the downsides. I will now go over how to fix all the places that reference the treasure and bandit lists.
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
# ...
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col:
can now become:
if (current[0], current[1]) in treasures[:8]:
The [:8]
is called a slice, and it returns a list containing the first 8 elements of the original list.
Use more loops
Mostly, you want to create your lists in list-comprehensions; so this:
for i in range(8):
b=
for j in range(8):
b.append(' ')
boardeasy.append(b)
will become:
boardeasy = [[' ' for j in range(8)] for i in range(8)]
This block can be shrunk significantly because it repeats:
print(" 1 2 3 4 5 6 7 8")
print("---------------------------------")
print ('| ' + boardeasy[0][0] + '| ' + boardeasy[0][1] + ' | ' + boardeasy[0][2] + ' | ' + boardeasy[0][3] + ' | ' + boardeasy[0][4] + ' | ' + boardeasy[0][5] + ' | ' + boardeasy[0][6] + ' | ' + boardeasy[0][7] + ' | ' + '1')
print("---------------------------------")
# ...
print ('| ' + boardeasy[7][0] + '| ' + boardeasy[7][1] + ' | ' + boardeasy[7][2] + ' | ' + boardeasy[7][3] + ' | ' + boardeasy[7][4] + ' | ' + boardeasy[7][5] + ' | ' + boardeasy[7][6] + ' | ' + boardeasy[7][7] + ' | ' + '8')
print("---------------------------------")
To start with, most print statement repeat |
. Python has a built in string operation for this: it's called str.join()
. Here's how to use it; you go from this:
print('| ' + boardeasy[0][0] + '| ' + boardeasy[0][1] + ' | ' + boardeasy[0][2] + ' | ' + boardeasy[0][3] + ' | ' + boardeasy[0][4] + ' | ' + boardeasy[0][5] + ' | ' + boardeasy[0][6] + ' | ' + boardeasy[0][7] + ' | ' + '1')
to this (while also centering the character in the first column):
print('| ' + ' | '.join(boardeasy[0][:8]) + ' | ' + '1')
But we can make it even better because the board rows are also very repetitious among each other. You can fix this with a simple for-loop:
print(" 1 2 3 4 5 6 7 8")
print("---------------------------------")
for row in range(8):
print ('| ' + ' | '.join(boardeasy[row][:8]) + ' | ' + str(row + 1))
print("---------------------------------")
(I could also automate the first line, but we'll get to that in a second.)
Don't split functions with the same content; use parameters
table_game_easy
,table_game_medium
, andtable_game_hard
should be combined into one function.
easy_level
,med_level
, andhard_level
should be combined into one function.
There are more ways to reduce redundancy, but try to find them on your own. If you have any more questions, you can comment on this post or ask a new question.
add a comment |
up vote
1
down vote
Generally, when reading your code, I think you need to consider the DRY principle. I will elaborate on this through several specific examples, but it's generally a good thing to keep in mind and understand when you code anything. This principle also means you should almost never copy-paste code exactly: that's a sign that there's a cleaner way to organize the code.
If you have any questions about any of my suggestions, feel free to ask them in comments.
Avoid similarly named hardcoded variables
You hard define a set of very similarly named variables (multiple times):
Treasure1_Row = random.randint(0,8)
Treasure1_Col = random.randint(0,8)
Treasure2_Row = random.randint(0,8)
Treasure2_Col = random.randint(0,8)
# ...
Treasure11_Row = random.randint(0,8)
Treasure11_Col = random.randint(0,8)
Treasure12_Row = random.randint(0,8)
Treasure12_Col = random.randint(0,8)
Bandit1_Row = random.randint(0,8)
Bandit1_Col = random.randint(0,8)
Bandit2_Row = random.randint(0,8)
Bandit2_Col = random.randint(0,8)
# ...
Bandit11_Row = random.randint(0,8)
Bandit11_Col = random.randint(0,8)
Bandit12_Row = random.randint(0,8)
Bandit12_Col = random.randint(0,8)
Generally you should use lists instead, and use a loop to assign all the list elements the same thing. List comprehensions can make your code even more succinct. Here's how:
treasures_row = [random.randint(0,8) for i in range(12)]
treasures_col = [random.randint(0,8) for i in range(12)]
bandits_row = [random.randint(0,8) for i in range(12)]
bandits_col = [random.randint(0,8) for i in range(12)]
Even more idiomatically, you could store the coordinates as two-tuples, instead of having separate lists for the rows and cols:
treasures = [(random.randint(0,8), random.randint(0,8)) for i in range(12)]
bandits = [(random.randint(0,8), random.randint(0,8)) for i in range(12)]
This will of course require reorganizing other parts of your code, but the benefits far outweigh the downsides. I will now go over how to fix all the places that reference the treasure and bandit lists.
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
# ...
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col:
can now become:
if (current[0], current[1]) in treasures[:8]:
The [:8]
is called a slice, and it returns a list containing the first 8 elements of the original list.
Use more loops
Mostly, you want to create your lists in list-comprehensions; so this:
for i in range(8):
b=
for j in range(8):
b.append(' ')
boardeasy.append(b)
will become:
boardeasy = [[' ' for j in range(8)] for i in range(8)]
This block can be shrunk significantly because it repeats:
print(" 1 2 3 4 5 6 7 8")
print("---------------------------------")
print ('| ' + boardeasy[0][0] + '| ' + boardeasy[0][1] + ' | ' + boardeasy[0][2] + ' | ' + boardeasy[0][3] + ' | ' + boardeasy[0][4] + ' | ' + boardeasy[0][5] + ' | ' + boardeasy[0][6] + ' | ' + boardeasy[0][7] + ' | ' + '1')
print("---------------------------------")
# ...
print ('| ' + boardeasy[7][0] + '| ' + boardeasy[7][1] + ' | ' + boardeasy[7][2] + ' | ' + boardeasy[7][3] + ' | ' + boardeasy[7][4] + ' | ' + boardeasy[7][5] + ' | ' + boardeasy[7][6] + ' | ' + boardeasy[7][7] + ' | ' + '8')
print("---------------------------------")
To start with, most print statement repeat |
. Python has a built in string operation for this: it's called str.join()
. Here's how to use it; you go from this:
print('| ' + boardeasy[0][0] + '| ' + boardeasy[0][1] + ' | ' + boardeasy[0][2] + ' | ' + boardeasy[0][3] + ' | ' + boardeasy[0][4] + ' | ' + boardeasy[0][5] + ' | ' + boardeasy[0][6] + ' | ' + boardeasy[0][7] + ' | ' + '1')
to this (while also centering the character in the first column):
print('| ' + ' | '.join(boardeasy[0][:8]) + ' | ' + '1')
But we can make it even better because the board rows are also very repetitious among each other. You can fix this with a simple for-loop:
print(" 1 2 3 4 5 6 7 8")
print("---------------------------------")
for row in range(8):
print ('| ' + ' | '.join(boardeasy[row][:8]) + ' | ' + str(row + 1))
print("---------------------------------")
(I could also automate the first line, but we'll get to that in a second.)
Don't split functions with the same content; use parameters
table_game_easy
,table_game_medium
, andtable_game_hard
should be combined into one function.
easy_level
,med_level
, andhard_level
should be combined into one function.
There are more ways to reduce redundancy, but try to find them on your own. If you have any more questions, you can comment on this post or ask a new question.
add a comment |
up vote
1
down vote
up vote
1
down vote
Generally, when reading your code, I think you need to consider the DRY principle. I will elaborate on this through several specific examples, but it's generally a good thing to keep in mind and understand when you code anything. This principle also means you should almost never copy-paste code exactly: that's a sign that there's a cleaner way to organize the code.
If you have any questions about any of my suggestions, feel free to ask them in comments.
Avoid similarly named hardcoded variables
You hard define a set of very similarly named variables (multiple times):
Treasure1_Row = random.randint(0,8)
Treasure1_Col = random.randint(0,8)
Treasure2_Row = random.randint(0,8)
Treasure2_Col = random.randint(0,8)
# ...
Treasure11_Row = random.randint(0,8)
Treasure11_Col = random.randint(0,8)
Treasure12_Row = random.randint(0,8)
Treasure12_Col = random.randint(0,8)
Bandit1_Row = random.randint(0,8)
Bandit1_Col = random.randint(0,8)
Bandit2_Row = random.randint(0,8)
Bandit2_Col = random.randint(0,8)
# ...
Bandit11_Row = random.randint(0,8)
Bandit11_Col = random.randint(0,8)
Bandit12_Row = random.randint(0,8)
Bandit12_Col = random.randint(0,8)
Generally you should use lists instead, and use a loop to assign all the list elements the same thing. List comprehensions can make your code even more succinct. Here's how:
treasures_row = [random.randint(0,8) for i in range(12)]
treasures_col = [random.randint(0,8) for i in range(12)]
bandits_row = [random.randint(0,8) for i in range(12)]
bandits_col = [random.randint(0,8) for i in range(12)]
Even more idiomatically, you could store the coordinates as two-tuples, instead of having separate lists for the rows and cols:
treasures = [(random.randint(0,8), random.randint(0,8)) for i in range(12)]
bandits = [(random.randint(0,8), random.randint(0,8)) for i in range(12)]
This will of course require reorganizing other parts of your code, but the benefits far outweigh the downsides. I will now go over how to fix all the places that reference the treasure and bandit lists.
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
# ...
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col:
can now become:
if (current[0], current[1]) in treasures[:8]:
The [:8]
is called a slice, and it returns a list containing the first 8 elements of the original list.
Use more loops
Mostly, you want to create your lists in list-comprehensions; so this:
for i in range(8):
b=
for j in range(8):
b.append(' ')
boardeasy.append(b)
will become:
boardeasy = [[' ' for j in range(8)] for i in range(8)]
This block can be shrunk significantly because it repeats:
print(" 1 2 3 4 5 6 7 8")
print("---------------------------------")
print ('| ' + boardeasy[0][0] + '| ' + boardeasy[0][1] + ' | ' + boardeasy[0][2] + ' | ' + boardeasy[0][3] + ' | ' + boardeasy[0][4] + ' | ' + boardeasy[0][5] + ' | ' + boardeasy[0][6] + ' | ' + boardeasy[0][7] + ' | ' + '1')
print("---------------------------------")
# ...
print ('| ' + boardeasy[7][0] + '| ' + boardeasy[7][1] + ' | ' + boardeasy[7][2] + ' | ' + boardeasy[7][3] + ' | ' + boardeasy[7][4] + ' | ' + boardeasy[7][5] + ' | ' + boardeasy[7][6] + ' | ' + boardeasy[7][7] + ' | ' + '8')
print("---------------------------------")
To start with, most print statement repeat |
. Python has a built in string operation for this: it's called str.join()
. Here's how to use it; you go from this:
print('| ' + boardeasy[0][0] + '| ' + boardeasy[0][1] + ' | ' + boardeasy[0][2] + ' | ' + boardeasy[0][3] + ' | ' + boardeasy[0][4] + ' | ' + boardeasy[0][5] + ' | ' + boardeasy[0][6] + ' | ' + boardeasy[0][7] + ' | ' + '1')
to this (while also centering the character in the first column):
print('| ' + ' | '.join(boardeasy[0][:8]) + ' | ' + '1')
But we can make it even better because the board rows are also very repetitious among each other. You can fix this with a simple for-loop:
print(" 1 2 3 4 5 6 7 8")
print("---------------------------------")
for row in range(8):
print ('| ' + ' | '.join(boardeasy[row][:8]) + ' | ' + str(row + 1))
print("---------------------------------")
(I could also automate the first line, but we'll get to that in a second.)
Don't split functions with the same content; use parameters
table_game_easy
,table_game_medium
, andtable_game_hard
should be combined into one function.
easy_level
,med_level
, andhard_level
should be combined into one function.
There are more ways to reduce redundancy, but try to find them on your own. If you have any more questions, you can comment on this post or ask a new question.
Generally, when reading your code, I think you need to consider the DRY principle. I will elaborate on this through several specific examples, but it's generally a good thing to keep in mind and understand when you code anything. This principle also means you should almost never copy-paste code exactly: that's a sign that there's a cleaner way to organize the code.
If you have any questions about any of my suggestions, feel free to ask them in comments.
Avoid similarly named hardcoded variables
You hard define a set of very similarly named variables (multiple times):
Treasure1_Row = random.randint(0,8)
Treasure1_Col = random.randint(0,8)
Treasure2_Row = random.randint(0,8)
Treasure2_Col = random.randint(0,8)
# ...
Treasure11_Row = random.randint(0,8)
Treasure11_Col = random.randint(0,8)
Treasure12_Row = random.randint(0,8)
Treasure12_Col = random.randint(0,8)
Bandit1_Row = random.randint(0,8)
Bandit1_Col = random.randint(0,8)
Bandit2_Row = random.randint(0,8)
Bandit2_Col = random.randint(0,8)
# ...
Bandit11_Row = random.randint(0,8)
Bandit11_Col = random.randint(0,8)
Bandit12_Row = random.randint(0,8)
Bandit12_Col = random.randint(0,8)
Generally you should use lists instead, and use a loop to assign all the list elements the same thing. List comprehensions can make your code even more succinct. Here's how:
treasures_row = [random.randint(0,8) for i in range(12)]
treasures_col = [random.randint(0,8) for i in range(12)]
bandits_row = [random.randint(0,8) for i in range(12)]
bandits_col = [random.randint(0,8) for i in range(12)]
Even more idiomatically, you could store the coordinates as two-tuples, instead of having separate lists for the rows and cols:
treasures = [(random.randint(0,8), random.randint(0,8)) for i in range(12)]
bandits = [(random.randint(0,8), random.randint(0,8)) for i in range(12)]
This will of course require reorganizing other parts of your code, but the benefits far outweigh the downsides. I will now go over how to fix all the places that reference the treasure and bandit lists.
if current[0] == Treasure1_Row and current[1] == Treasure1_Col
or current[0] == Treasure2_Row and current[1] == Treasure2_Col
# ...
or current[0] == Treasure7_Row and current[1] == Treasure7_Col
or current[0] == Treasure8_Row and current[1] == Treasure8_Col:
can now become:
if (current[0], current[1]) in treasures[:8]:
The [:8]
is called a slice, and it returns a list containing the first 8 elements of the original list.
Use more loops
Mostly, you want to create your lists in list-comprehensions; so this:
for i in range(8):
b=
for j in range(8):
b.append(' ')
boardeasy.append(b)
will become:
boardeasy = [[' ' for j in range(8)] for i in range(8)]
This block can be shrunk significantly because it repeats:
print(" 1 2 3 4 5 6 7 8")
print("---------------------------------")
print ('| ' + boardeasy[0][0] + '| ' + boardeasy[0][1] + ' | ' + boardeasy[0][2] + ' | ' + boardeasy[0][3] + ' | ' + boardeasy[0][4] + ' | ' + boardeasy[0][5] + ' | ' + boardeasy[0][6] + ' | ' + boardeasy[0][7] + ' | ' + '1')
print("---------------------------------")
# ...
print ('| ' + boardeasy[7][0] + '| ' + boardeasy[7][1] + ' | ' + boardeasy[7][2] + ' | ' + boardeasy[7][3] + ' | ' + boardeasy[7][4] + ' | ' + boardeasy[7][5] + ' | ' + boardeasy[7][6] + ' | ' + boardeasy[7][7] + ' | ' + '8')
print("---------------------------------")
To start with, most print statement repeat |
. Python has a built in string operation for this: it's called str.join()
. Here's how to use it; you go from this:
print('| ' + boardeasy[0][0] + '| ' + boardeasy[0][1] + ' | ' + boardeasy[0][2] + ' | ' + boardeasy[0][3] + ' | ' + boardeasy[0][4] + ' | ' + boardeasy[0][5] + ' | ' + boardeasy[0][6] + ' | ' + boardeasy[0][7] + ' | ' + '1')
to this (while also centering the character in the first column):
print('| ' + ' | '.join(boardeasy[0][:8]) + ' | ' + '1')
But we can make it even better because the board rows are also very repetitious among each other. You can fix this with a simple for-loop:
print(" 1 2 3 4 5 6 7 8")
print("---------------------------------")
for row in range(8):
print ('| ' + ' | '.join(boardeasy[row][:8]) + ' | ' + str(row + 1))
print("---------------------------------")
(I could also automate the first line, but we'll get to that in a second.)
Don't split functions with the same content; use parameters
table_game_easy
,table_game_medium
, andtable_game_hard
should be combined into one function.
easy_level
,med_level
, andhard_level
should be combined into one function.
There are more ways to reduce redundancy, but try to find them on your own. If you have any more questions, you can comment on this post or ask a new question.
answered Nov 16 at 5:44
Graham
528113
528113
add a comment |
add a comment |
J.Peggy is a new contributor. Be nice, and check out our Code of Conduct.
J.Peggy is a new contributor. Be nice, and check out our Code of Conduct.
J.Peggy is a new contributor. Be nice, and check out our Code of Conduct.
J.Peggy is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207718%2fpython-treasure-hunt-board-game%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown