Znn3bq.jpeg
²é¿´: 734  |  »Ø¸´: 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 µÄÖ÷Ìâ¸üÐÂ
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[¿¼ÑÐ] 291Çóµ÷¼Á +7 ¹ØÒä±±. 2026-04-14 7/350 2026-04-16 01:30 by L0alice
[¿¼ÑÐ] 327Çóµ÷¼Á +25 Xxjc1107. 2026-04-13 27/1350 2026-04-15 23:22 by Equinoxhua
[¿¼ÑÐ] 291 Çóµ÷¼Á +40 »¯¹¤2026½ì±ÏÒµÉ 2026-04-09 41/2050 2026-04-15 21:54 by noqvsozv
[¿¼ÑÐ] ͨÐŹ¤³ÌÇóµ÷¼Á£¡£¡£¡ +6 zlb770521 2026-04-14 6/300 2026-04-15 20:00 by ѧԱJpLReM
[¿¼ÑÐ] Ò»Ö¾Ô¸»ªÖÐũҵ071010£¬320Çóµ÷¼Á +11 À§À§À§À§À¤À¤ 2026-04-14 12/600 2026-04-15 18:58 by zhuwenxu
[¿¼ÑÐ] Ò»Ö¾Ô¸»¦9£¬ÉúÎïѧ326Çóµ÷¼Á +7 Áõīī 2026-04-15 7/350 2026-04-15 18:51 by ¸¡ÔÆ166
[¿¼ÑÐ] 279Çóµ÷¼Á +12 ÕÅ·¬ÇѲ»³´µ° 2026-04-11 12/600 2026-04-14 15:38 by zs92450
[¿¼ÑÐ] 332Çóµ÷¼Á +15 ½¶½¶123 2026-04-10 15/750 2026-04-13 23:12 by pies112
[¿¼ÑÐ] BÇø0809 £¬ÊýÒ»Ó¢Ò»£¬290 Çóµ÷¼Á +3 ãöΫ1111 2026-04-12 4/200 2026-04-13 20:35 by ѧԱJpLReM
[¿¼ÑÐ] 302Çóµ÷¼Á +10 Ò×£¡? 2026-04-13 10/500 2026-04-13 19:04 by lbsjt
[¿¼ÑÐ] 280Çóµ÷¼Á +13 wzzzÍõ 2026-04-09 13/650 2026-04-12 00:31 by ÓÂÅʸ߷å0126
[¿¼ÑÐ] Ò»Ö¾Ô¸Ïôó0856£¬306Çóµ÷¼Á +15 Bblinging 2026-04-11 15/750 2026-04-11 22:53 by 314126402
[¿¼ÑÐ] 343Çóµ÷¼Á +9 Íõ¹ú˧ 2026-04-10 9/450 2026-04-11 20:31 by dongdian1
[¿¼ÑÐ] 284Çóµ÷¼Á +11 archer.. 2026-04-09 12/600 2026-04-11 20:23 by À¶ÔÆË¼Óê
[¿¼ÑÐ] 352 Çóµ÷¼Á +6 yzion 2026-04-11 8/400 2026-04-11 16:24 by Ã÷Ô´ËʱÓÐ
[¿¼ÑÐ] Çóµ÷¼Á£¬Ò»Ö¾Ô¸´óÁ¬Àí¹¤´óѧ354·Ö +5 ÓêÉùÓàÉú 2026-04-11 6/300 2026-04-11 16:12 by ÓêÉùÓàÉú
[¿¼ÑÐ] 263ÄÜÔ´¶¯Á¦×¨Ë¶Çóµ÷¼Á +3 ¼Ó´óºÅ·¹ºÐ´ü 2026-04-10 3/150 2026-04-10 22:23 by 286640313
[¿¼ÑÐ] »úеר368 ÓÐÈ¥´¦Âð +4 ÖÖ´óÊ÷ 2026-04-10 4/200 2026-04-10 15:31 by jiajinhpu
[¿¼ÑÐ] 085800 ÄÜÔ´¶¯Á¦Çóµ÷¼Á +6 °¢biu°¡°¡°¡°¡°¡ 2026-04-10 6/300 2026-04-10 15:03 by hemengdong
[¿¼ÑÐ] »¯Ñ§¹¤³ÌÓë¼¼Êõרҵһ־Ը¹þ¹¤³Ì 291·ÖBÇø ¹ú¼Ò¼¶´ó´´¸ºÔðÈË ÓÐÒ»×÷ÂÛÎÄ +13 Emmy~ 2026-04-09 13/650 2026-04-09 14:47 by onlyÖÜ
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û