Function in a board game with chests and bandits











up vote
0
down vote

favorite












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[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("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[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:
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









share|improve this question















migrated from stackoverflow.com Nov 22 at 4:08


This question came from our site for professional and enthusiast programmers.











  • 2




    what do you mean more efficient?
    – SuperStew
    Nov 14 at 0:00






  • 1




    Reduce the amount of code needed perhaps?
    – J.Peggy
    Nov 14 at 0:01






  • 1




    How can I make the code in this function more efficient? isn't a good title for Code Review because stateing your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
    – Heslacher
    Nov 22 at 5:30















up vote
0
down vote

favorite












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[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("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[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:
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









share|improve this question















migrated from stackoverflow.com Nov 22 at 4:08


This question came from our site for professional and enthusiast programmers.











  • 2




    what do you mean more efficient?
    – SuperStew
    Nov 14 at 0:00






  • 1




    Reduce the amount of code needed perhaps?
    – J.Peggy
    Nov 14 at 0:01






  • 1




    How can I make the code in this function more efficient? isn't a good title for Code Review because stateing your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
    – Heslacher
    Nov 22 at 5:30













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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[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("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[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:
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









share|improve this question















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[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("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[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:
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






python game






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 9:07









200_success

127k15148412




127k15148412










asked Nov 13 at 23:52







J.Peggy











migrated from stackoverflow.com Nov 22 at 4:08


This question came from our site for professional and enthusiast programmers.






migrated from stackoverflow.com Nov 22 at 4:08


This question came from our site for professional and enthusiast programmers.










  • 2




    what do you mean more efficient?
    – SuperStew
    Nov 14 at 0:00






  • 1




    Reduce the amount of code needed perhaps?
    – J.Peggy
    Nov 14 at 0:01






  • 1




    How can I make the code in this function more efficient? isn't a good title for Code Review because stateing your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
    – Heslacher
    Nov 22 at 5:30














  • 2




    what do you mean more efficient?
    – SuperStew
    Nov 14 at 0:00






  • 1




    Reduce the amount of code needed perhaps?
    – J.Peggy
    Nov 14 at 0:01






  • 1




    How can I make the code in this function more efficient? isn't a good title for Code Review because stateing your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
    – Heslacher
    Nov 22 at 5:30








2




2




what do you mean more efficient?
– SuperStew
Nov 14 at 0:00




what do you mean more efficient?
– SuperStew
Nov 14 at 0:00




1




1




Reduce the amount of code needed perhaps?
– J.Peggy
Nov 14 at 0:01




Reduce the amount of code needed perhaps?
– J.Peggy
Nov 14 at 0:01




1




1




How can I make the code in this function more efficient? isn't a good title for Code Review because stateing your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
– Heslacher
Nov 22 at 5:30




How can I make the code in this function more efficient? isn't a good title for Code Review because stateing your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly.
– Heslacher
Nov 22 at 5:30










1 Answer
1






active

oldest

votes

















up vote
0
down vote













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:
print('Wrong command, please input again')
continue
direct, number = user_input
direct = direct.lower()
number = int(number.lower())
if direct not in ['up','left','down','right']:#Validates input
print('Wrong command, please input again')
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:
print('Wrong command, please input again')
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")
Coins += 10 #Adds an additional 10 points
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")
Coins += 10 #Adds an additional 10 points
print("Coins:",Coins)

if current in Bandit_Positions:
print("Oh no! You have landed on a bandit...they steal all your coins!")
Coins = 0 #Removes all coins
print("Coins:",Coins)




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






share|improve this answer























    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208206%2ffunction-in-a-board-game-with-chests-and-bandits%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown
























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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:
    print('Wrong command, please input again')
    continue
    direct, number = user_input
    direct = direct.lower()
    number = int(number.lower())
    if direct not in ['up','left','down','right']:#Validates input
    print('Wrong command, please input again')
    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:
    print('Wrong command, please input again')
    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")
    Coins += 10 #Adds an additional 10 points
    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")
    Coins += 10 #Adds an additional 10 points
    print("Coins:",Coins)

    if current in Bandit_Positions:
    print("Oh no! You have landed on a bandit...they steal all your coins!")
    Coins = 0 #Removes all coins
    print("Coins:",Coins)




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






    share|improve this answer



























      up vote
      0
      down vote













      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:
      print('Wrong command, please input again')
      continue
      direct, number = user_input
      direct = direct.lower()
      number = int(number.lower())
      if direct not in ['up','left','down','right']:#Validates input
      print('Wrong command, please input again')
      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:
      print('Wrong command, please input again')
      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")
      Coins += 10 #Adds an additional 10 points
      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")
      Coins += 10 #Adds an additional 10 points
      print("Coins:",Coins)

      if current in Bandit_Positions:
      print("Oh no! You have landed on a bandit...they steal all your coins!")
      Coins = 0 #Removes all coins
      print("Coins:",Coins)




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






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        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:
        print('Wrong command, please input again')
        continue
        direct, number = user_input
        direct = direct.lower()
        number = int(number.lower())
        if direct not in ['up','left','down','right']:#Validates input
        print('Wrong command, please input again')
        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:
        print('Wrong command, please input again')
        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")
        Coins += 10 #Adds an additional 10 points
        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")
        Coins += 10 #Adds an additional 10 points
        print("Coins:",Coins)

        if current in Bandit_Positions:
        print("Oh no! You have landed on a bandit...they steal all your coins!")
        Coins = 0 #Removes all coins
        print("Coins:",Coins)




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






        share|improve this answer














        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:
        print('Wrong command, please input again')
        continue
        direct, number = user_input
        direct = direct.lower()
        number = int(number.lower())
        if direct not in ['up','left','down','right']:#Validates input
        print('Wrong command, please input again')
        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:
        print('Wrong command, please input again')
        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")
        Coins += 10 #Adds an additional 10 points
        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")
        Coins += 10 #Adds an additional 10 points
        print("Coins:",Coins)

        if current in Bandit_Positions:
        print("Oh no! You have landed on a bandit...they steal all your coins!")
        Coins = 0 #Removes all coins
        print("Coins:",Coins)




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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 at 9:07









        Toby Speight

        22.6k537109




        22.6k537109










        answered Nov 22 at 8:44









        Josay

        24.5k13783




        24.5k13783






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208206%2ffunction-in-a-board-game-with-chests-and-bandits%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            Terni

            A new problem with tex4ht and tikz

            Sun Ra