# Function in a board game with chests and bandits

This function is part of a python board game program. The game is a board game with chests and bandits hidden throughout the board. The function is dedicated to the "easy" section of the game (where it is a 8x8 grid).

def easy_level(Coins):
#This function is for the movement of the game in easy difficulty
while  True:
oldcurrent=current
boardeasy[oldcurrent][oldcurrent]='*'
table_game_easy()
boardeasy[oldcurrent][oldcurrent]=' '

n = input('Enter the direction followed by the number Ex:Up 5 , Number should be < 8 \n')
n=n.split()
if n.lower() not in ['up','left','down','right']:#Validates input
continue
elif n.lower()=='up':
up(int(n.lower()),8)#Boundary is set to 8 as the 'easy' grid is a 8^8
elif n.lower()=='down':
down(int(n.lower()),8)
elif n.lower()=='left':
left(int(n.lower()),8)
elif n.lower()=='right':
right(int(n.lower()),8)

print("5 chests left")
print("8 bandits left")
print("Coins:",Coins)#Acts as a counter, displays the number of coins that the player has
if current == Treasure1_Row and current == Treasure1_Col\
or current == Treasure2_Row and current == Treasure2_Col\
or current == Treasure3_Row and current == Treasure3_Col\
or current == Treasure4_Row and current == Treasure4_Col\
or current == Treasure5_Row and current == Treasure5_Col\
or current == Treasure6_Row and current == Treasure6_Col\
or current == Treasure7_Row and current == Treasure7_Col\
or current == Treasure8_Row and current == Treasure8_Col\
or current == Treasure9_Row and current == Treasure9_Col\
or current == Treasure10_Row and current == Treasure10_Col:
print("Hooray! You have found booty! +10 gold")
print("Coins:",Coins)

if current == Bandit1_Row and current == Bandit1_Col\
or current == Bandit2_Row and current == Bandit2_Col\
or current == Bandit3_Row and current == Bandit3_Col\
or current == Bandit4_Row and current == Bandit4_Col\
or current == Bandit5_Row and current == Bandit5_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][current]='*'#sets value to players position

• what do you mean more efficient?
– SuperStew
Nov 14 '18 at 0:00
• Reduce the amount of code needed perhaps?
– J.Peggy
Nov 14 '18 at 0:01

We're definitely missing some context here to run your code or understand what it is supposed to do, but there is enough to detect a few things that could be easily improved.

        Coins = Coins-Coins #Removes all coins


This should be:

        Coins = 0  #Removes all coins


Also

        Coins = Coins+10 #Adds an additional 10 points


can be written

        Coins += 10  #Adds an additional 10 points


Don't perform the same operations more than needed. In particular when handling the user input, you can limit the number of index accesses, to call to lower function, to call to int function:

    user_input = input('Enter the direction followed by the number Ex:Up 5 , Number should be < 8 \n').split()
if len(user_input) != 2:
continue
direct, number = user_input
direct = direct.lower()
number = int(number.lower())
if direct not in ['up','left','down','right']:#Validates input
continue
elif direct == 'up':
up(number, 8)  #Boundary is set to 8 as the 'easy' grid is a 8^8
elif direct == 'down':
down(number, 8)
elif direct == 'left':
left(number, 8)
elif direct == 'right':
right(number, 8)


Then, you can actually change the condition order so that you don't need to list twice the valid directions:

    if direct == 'up':
up(number, 8)  #Boundary is set to 8 as the 'easy' grid is a 8^8
elif direct == 'down':
down(number, 8)
elif direct == 'left':
left(number, 8)
elif direct == 'right':
right(number, 8)
else:
continue


You could probably rewrite the comparisons to have something like:

    if current == Treasure1_Pos\
or current == Treasure2_Pos\
or current == Treasure3_Pos\
or current == Treasure4_Pos\
or current == Treasure5_Pos\
or current == Treasure6_Pos\
or current == Treasure7_Pos\
or current == Treasure8_Pos\
or current == Treasure9_Pos\
or current == Treasure10_Pos:
print("Hooray! You have found booty! +10 gold")
print("Coins:",Coins)

if current == Bandit1_Pos\
or current == Bandit2_Pos\
or current == Bandit3_Pos\
or current == Bandit4_Pos\
or current == Bandit5_Pos:
print("Oh no! You have landed on a bandit...they steal all your coins!")
Coins = 0  #Removes all coins
print("Coins:",Coins)


And you could even define a data structure (list, set) to hold all the relevant positions and write something like:

    if current in Treasure_Positions:
print("Hooray! You have found booty! +10 gold")

Then more things look wrong/improvable about boardeasy but we'd need to see what it does.