Znn3bq.jpeg
²é¿´: 732  |  »Ø¸´: 1

lajiiwang

½ð³æ (СÓÐÃûÆø)

[ÇóÖú] ×ÔѧpythonÖУ¬ÇóÖúÏÂÃæµÄ³ÌÐòÄı߳ö´í£¬ÈçºÎ¸Ä£¿

×î½üÔÚ¿´¡¶pythonÈëÞ­µä¡·»úе¹¤Òµ³ö°æÉ磬ÕÅÃôÒ루ºÃÏñpythonµÄÊé¶¼ÊÇ·­Òë¹ýÀ´µÄ£©p381Ò³£¬ÔËÐдúÂëÇåµ¥13.6ʱ£¬³ÌÐò³ö´íÁË£¬Çëר¼ÒÃÇ¿´¿´
¸½¼þ1ÊÇÓ¢ÎİæµÄÊ飬µç×Ó°æÎÒÖ»ÓÐÓ¢ÎİæµÄ£¬±§Ç¸£¡
¸½¼þ2ÊÇÔ´³ÌÐò

# -*- coding:utf-8 -*-
import random
class Island(object):
        """
        Island nXn grid where zreo value indicates not occupied.
        """
        def __init__(self, n, preyCnt=0, predatorCnt=0):
                '''
                Initialize grid to all 0's ,then fill with animals
                '''
                print n, preyCnt, predatorCnt
                self.gridSize = n
                self.grid = []
                for i in range(n):
                        row = [0] * n
                        self.grid.append(row)
                self.initAnimals(preyCnt, predatorCnt)  
                                    
        def animal(self,x,y):
                '''
                Return animal at location(x,y)
                '''
                if 0 <= x < self.gridSize and 0 <= y <= self.gridSize:
                        return self.grid[x][y]
                else:
                        return -1    #outside  island boundary
                       
        def initAnimals(self, preyCnt, predatorCnt):
                '''
                Put some intial animals on the island
                '''
                cnt = 0
                while cnt < preyCnt:
                        x = random.randint(0, self.gridSize-1)
                        y = random.randint(1, self.gridSize-1)
                        if not self.animal(x, y):  
                                newPrey = Prey(island=self , x=x, y=y )  
                                cnt += 1
                                self.register(newPrey)
                cnt = 0
                while cnt < predatorCnt:
                        x = random.randint(0, self.gridSize-1)
                        y = random.randint(0, self.gridSize-1)
                        if not self.animal(x,y):
                                newPred = Predator(island=self , x=x, y=y )
                                cnt += 1
                                self.register(newPred)
                                               
        def size(self):  
                '''
                Return size fo the island,i.e. put it at the animal's coordinates
                '''
                return self.gridSize
               
        def register(self,animal):
                '''
                Register animal with island, i.e. put it at the animal's coordinates
                '''
                x = animal.x
                y = animal.y
                self.grid[x][y] = animal
               
        def __str__(self):
                '''
                String representation for printing.
                (0,0) will be in the lower left corner
                '''
                s = ""
                for j in range(self.gridSize-1, -1, -1):
                        for i in range(self.gridSize):
                                if not self.grid[j]:
                                        s+= "%-2s" %'.' + " "   #Ò²¿ÉÒÔд³És+="%-2s" %str('.') + " "
                                else:
                                        s+= "%-2s" %(str(self.grid[j])) + " "  
                        s+="\n"
                       
                return s
               
        def remove(self, x, y):
                self.grid[x][y] = 0
               
class Animal(object):
        def __init__(self, island, x=0 , y=0, s="A":
                '''
                Initialize the animal's and their positions
                '''
                self.island = island
                self.name = s
                self.x = x
                self.y = y
               
        def __str__(self):   
                return self.name  
               
        def position(self):
                '''
                Return coordinates of current position.
                '''
                return self.x, self.y
               
class Prey(Animal):
        def __init__(self, island, x=0, y=0, s="O":
                Animal.__init__(self, island, x, y, s)
               
        def move(self):
                """
                Move to an open, neighboring pisition.
                """
                offset = [(-1,1), (0,1), (1,1), (-1,0), (1,0), (-1,-1), (0,-1), (1,-1)]
                for i in range(len(offset)):
                        x = self.x + offset[0]
                        y = self.y + offset[1]
                        if self.island.animal(x,y) == 0:
                                self.island.remove(self)  
                                self.x = x
                                self.y = y
                                self.island.register(self)
                                break

class Predator(Animal):
        def __init__(self, island, x=0, y=0, s="X":
                Animal.__init__(self, island, x, y, s)
               
               
if __name__ == '__main__':
        royale = Island(10)
        moose1 = Prey(island=royale, x=4, y=8, s='a1')   
        moose2 = Prey(island=royale, x=6, y=4, s='a2')   
        royale.register(moose1)
        royale.register(moose2)   
        #moose1 = Prey(royale, 4, 8, "m1"
        #moose2 = Prey(royale, 6, 4, "m2"
        print royale
        moose1.move()
        moose2.move()
        print royale×ÔѧpythonÖУ¬ÇóÖúÏÂÃæµÄ³ÌÐòÄı߳ö´í£¬ÈçºÎ¸Ä£¿
´íÎóÐÅÏ¢.PNG
»Ø¸´´ËÂ¥

» ±¾Ìû¸½¼þ×ÊÔ´Áбí

  • »¶Ó­¼à¶½ºÍ·´À¡£ºÐ¡Ä¾³æ½öÌṩ½»Á÷ƽ̨£¬²»¶Ô¸ÃÄÚÈݸºÔð¡£
    ±¾ÄÚÈÝÓÉÓû§×ÔÖ÷·¢²¼£¬Èç¹ûÆäÄÚÈÝÉæ¼°µ½ÖªÊ¶²úȨÎÊÌ⣬ÆäÔðÈÎÔÚÓÚÓû§±¾ÈË£¬Èç¶Ô°æÈ¨ÓÐÒìÒ飬ÇëÁªÏµÓÊÏ䣺xiaomuchong@tal.com
  • ¸½¼þ 1 : The_Practice_of_Computing_Using_Python.pdf
  • 2015-10-18 16:07:50, 9.52 M
  • ¸½¼þ 2 : fs.py
  • 2015-10-18 16:11:00, 3.64 K

» ²ÂÄãϲ»¶

ÖÆÀ伯Ëã
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

lajiiwang

½ð³æ (СÓÐÃûÆø)

ÍíÉÏ11µã°ëÌÉÔÚ´²ÉÏÄÔ×ÓÀïÃæÀÏÊDZijöÕâ¸öÎÊÌ⣬ͻȻÓиöеÄÏë·¨£¬´ò¿ªµçÄÔÊäÈë´úÂ룬½â¾öÁË¡£
¿´À´ÊéÉϵijÌÐòÒ²²»¿¿Æ×¡£
½â¾öÁËÕâ¸öÎÊÌ⣬ÄÔ×ÓÀïÃæ¹ØÓÚÀàµÄ¸ÅÄîÓÖÇåÎúÀï²»ÉÙ¡£
ÖÆÀ伯Ëã
2Â¥2015-10-21 11:47:44
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
Ïà¹Ø°æ¿éÌø×ª ÎÒÒª¶©ÔÄÂ¥Ö÷ lajiiwang µÄÖ÷Ìâ¸üÐÂ
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[¿¼ÑÐ] ²ÄÁϹ¤³Ì281»¹Óе÷¼Á»ú»áÂð +43 xaw. 2026-04-11 44/2200 2026-04-15 12:46 by Î÷±±Íû¡ª·çɳ
[¿¼ÑÐ] ¿¼Ñе÷¼Á +13 ³¤¹­°Á 2026-04-13 14/700 2026-04-14 14:44 by zs92450
[»ù½ðÉêÇë] RY£ºÖйú²ú³öµÄ¿ÆÑ§À¬»øÂÛÎÄ£¬¾ø¶ÔÊýÁ¿ºÍ±ÈÀý¶¼ÊÀ½çµÚÒ» +6 zju2000 2026-04-14 17/850 2026-04-14 14:34 by jurkat.1640
[¿¼ÑÐ] Çóµ÷¼Á +3 ÎÒ°®¸ßÊý¸ßÊý°®Î 2026-04-12 3/150 2026-04-14 01:00 by Íõ¬Bè±
[¿¼ÑÐ] Çóµ÷¼Á +12 Á§ÜÔÒ»¶¨Éϰ¶ 2026-04-10 13/650 2026-04-14 00:08 by Equinoxhua
[¿¼ÑÐ] ±¾¿ÆÎ÷¹¤´ó 324Çóµ÷¼Á +5 wysyjs25 2026-04-10 5/250 2026-04-13 23:08 by pies112
[¿¼ÑÐ] 0854µ÷¼Á +10 ³¤¹­°Á 2026-04-11 11/550 2026-04-13 10:38 by wp06
[¿¼ÑÐ] 314Çóµ÷¼Á +24 wakeluofu 2026-04-09 25/1250 2026-04-13 08:58 by lhj2009
[¿¼ÑÐ] 346·Ö£¬¹¤¿Æ0854Çóµ÷¼Á£¬×¨Ë¶ +6 moser233 2026-04-12 7/350 2026-04-12 22:11 by fqwang
[¿¼ÑÐ] ±¾¿ÆÄÏ·½Ò½¿Æ´óѧ Ò»Ö¾Ô¸985 ҩѧѧ˶284·Ö Çóµ÷¼Á +5 ÈõË®ÌýÎÄ 2026-04-09 5/250 2026-04-12 13:16 by shengxi123
[¿¼ÑÐ] 307Çóµ÷¼Á +10 tzq94092 2026-04-10 10/500 2026-04-12 08:18 by wise999
[¿¼ÑÐ] 270Çóµ÷¼Á +14 ÑîÀÖ369 2026-04-11 14/700 2026-04-11 20:16 by À¶ÔÆË¼Óê
[¿¼ÑÐ] 352 Çóµ÷¼Á +6 yzion 2026-04-11 8/400 2026-04-11 16:24 by Ã÷Ô´ËʱÓÐ
[¿¼ÑÐ] 288Çóµ÷¼Á +15 ´úfish 2026-04-09 16/800 2026-04-11 10:26 by wwj2530616
[¿¼ÑÐ] 342µç×ÓÐÅϢר˶Çóµ÷¼Á +9 ÄãÈÃÎÒÔõôÀóÖ¦ 2026-04-10 10/500 2026-04-11 08:33 by zhq0425
[¿¼ÑÐ] ÖпÆÔº×Ü·Ö315Çóµ÷¼Á +8 lallalh 2026-04-09 8/400 2026-04-10 19:30 by dick_runner
[¿¼ÑÐ] 301Çóµ÷¼Á +5 149. 2026-04-10 5/250 2026-04-10 15:45 by ²ñС°×
[¿¼ÑÐ] Çóµ÷¼Á ²ÄÁÏÓ빤³Ì 324·Ö ר˶ +19 ôæôæÒ»ÊéÉú 2026-04-10 21/1050 2026-04-10 11:41 by wp06
[¿¼ÑÐ] 348Çóµ÷¼Á +3 candyyyi 2026-04-09 3/150 2026-04-09 17:20 by ¶ÎΰÑÞ
[¿¼ÑÐ] 083200 ³õÊÔ305·Ö Çóµ÷¼Á Ôݲ»¿¼ÂÇ¿çרҵ +15 Claireyyyy 2026-04-09 15/750 2026-04-09 16:11 by zhuimr
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û