Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 772993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:53:35+00:00 2026-05-14T18:53:35+00:00

First of all, I should explain what I’m trying to do first. I’m creating

  • 0

First of all, I should explain what I’m trying to do first.

I’m creating a dungeon crawler-like game, and I’m trying to program the movement of computer characters/monsters in the map. The map is basically a Cartesian coordinate grid.
The locations of characters are represented by tuples of the x and y values, (x,y).
The game works by turns, and in a turn a character can only move up, down, left or right 1 space. I’m creating a very simple movement system where the character will simply make decisions to move on a turn by turn basis. Essentially a ‘forgetful’ movement system.

A basic flow chart of what I’m intending to do:

  1. Find direction towards destination
  2. Make a priority list of movements to be done using the direction eg.(‘r’,’u’,’d’,’l’) means it would try to move right first, then up, then down, then left.
  3. Try each of the possibilities following the priority order. If the first movement fails (blocked by obstacle etc.), then it would successively try the movements until the first one that is successful, then it would stop.

At step 3, the way I’m trying to do it is like this:

def move(direction,location):
    try:
        -snip- # Tries to move, raises the exception Movementerror if cannot move in the direction
        return 1 # Indicates movement successful
    except Movementerror:
        return 0 # Indicates movement unsuccessful (thus character has not moved yet)

prioritylist = ('r','u','d','l')
if move('r',location): pass
elif move('u',location): pass
elif move('d',location): pass
elif move('l',location): pass
else: pass

In the if/else block, the program would try the first movement on the priority on the priority list. At the move function, the character would try to move. If the character is not blocked and does move, it returns 1, leading to the pass where it would stop. If the character is blocked, it returns 0, then it tries the next movement.

However, this results in 2 problems:

1.How do I edit a variable passed into a function inside the function itself, while returning if the edit is successful?

I have been told that you can’t edit a variable inside a function as it won’t really change the value of the variable, it just makes the variable inside the function refer to something else while the original variable remain unchanged. So, the solution is to return the value and then assign the variable to the returned value. However, I want it to return another value indicating if this edit is successful, so I want to edit this variable inside the function itself. How do I do so?

2.How do I change the order of the if/else statements to follow the order of the priority list? It needs to be able to change during runtime as the priority list can change resulting in a different order of movement to try.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:53:35+00:00Added an answer on May 14, 2026 at 6:53 pm

    Re 2., I would change your relatively-big if/elif tree to a simple loop:

    prioritylist = ('r','u','d','l')
    for direction in prioritylist:
      if move(direction, location): break
    

    less repetitious, easier to extend in the future if you want to allow diagonal steps or whatever — and automatically “follows the priority list” in whatever order it’s in, without any need to change your code itself.

    Re 1., you can return multiple values from a function:

    def move(direction,location):
        try:
            -snip-
            return True, newlocation
        except Movementerror:
            return False, location
    

    and you would then change the above loop to, e.g.:

    prioritylist = ('r','u','d','l')
    for direction in prioritylist:
      success, newlocation = move(direction, location)
      if success: break
    

    else:
    newlocation = location

    using the else branch of the for, which executes if no break happens in the for, to deal with the case in which no attempt produced a success.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, I know how to build a Java application. But I have
First of all, I don't need a textual comparison so Beyond Compare doesn't do
First of all, I'm fairly sure snapping to grid is fairly easy, however I've
First of all: I am not an experienced ClearCase user, but I have lots
First of all (in case this is important) I'm using ActiveState's Perl (v5.8.7 built
First of all there is a partial question regarding this, but it is not
First of all, let's define a few tables: Users table will store information about
First of all, let me say I am very new to rails, have been
First of all, I'm not looking for miracle... I know how PHP works and
First of all, I will admit I am a novice to web services, although

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.