Sorry I forgot to attach the code file.
Here it is.

--- On Sun, 7/26/09, ammar azif <ceasar...@yahoo.com> wrote:

From: ammar azif <ceasar...@yahoo.com>
Subject: [Tutor] Need help in making this code more pythonic
To: tutor@python.org
Date: Sunday, July 26, 2009, 6:23 AM

Hello,

I need some suggestions on how to make this code pythonic. The program 
basically runs a simulation of an object that moves on a 2D plane. The object 
keeps on moving from its current coordinate to  randomly picked adjacent 
coordinates with the object is not allowed to move move to a previously covered 
coordinate point as the rule. If the object is unable to move the program will 
terminate. Some of the concerns I have in mind is the code in the move method 
in Person class. I am sure there are some built-in methods or features in 
python that I could use to improve the implementation of that method. Hope you 
guys can enlighten me.





      
-----Inline Attachment Follows-----

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



      
import random
import time
class Person(object):
    def __init__(self, position,length,width):
        self.positions = {position:None} #coordinates covered 
        self.position = position #current coordinate
        self.length = length # world length
        self.width= width #world width
        
        
    def move(self):
        x = self.position[0]
        y = self.position[1]
        #set dead end 
        cannot_move = True
        #list of potential next coordinates
        potential_positions =[]
        if x+1>=0 and x+1<=self.length-1:
            if not self.positions.has_key((x+1,y)):
                cannot_move = False
                potential_positions.append((x+1,y))
           
        if x-1>=0 and x-1<=self.length-1:
            if not self.positions.has_key((x-1,y)):
                cannot_move = False
                potential_positions.append((x-1,y))
         
        if y-1>=0 and y-1<=self.width-1:
            if not self.positions.has_key((x,y-1)):
                cannot_move = False
                potential_positions.append((x,y-1))
     
        if y+1>=0 and y+1<=self.width-1:
            if not self.positions.has_key((x,y+1)):
                cannot_move = False
                potential_positions.append((x,y+1))
                

        if cannot_move :
            #return value that signifies the person cannot move
            return False
        else:
            #randomly pick a coordinate from list of potential coordinates
            index=random.randrange(len(potential_positions))
            self.positions[potential_positions[index]]=None
            self.position = potential_positions[index]
            return True


width = 10
length = 10
person = Person((0,0),length,width)
plots = {(0,0):None}
print 'Initial Positions Covered Count:'+str(len(person.positions))
print 'Moving!'


while person.move():
    string = 'Current position:' + str(person.position)+'Total covered:'+str(len(person.positions))+'\n'

    for x in range(width):
        for y in range(length):
            
            if person.positions.has_key((x,y)):
                string+='X'
            else:
                string+=' '
            if y==length-1:
                string+='\n'
    print string
    
    time.sleep(1)

print 'Cannot move!'
print person.positions


    
    



            
            
                
                
        
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to