Advertisement
Guest User

romu

a guest
Aug 4th, 2008
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.41 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding:Utf-8 -*-
  3.  
  4. import pygame
  5. import random
  6.  
  7. ###############################################################################
  8.  
  9. class Life ():
  10.     """The process"""
  11.  
  12. #--------------------------------------
  13.  
  14.     def __init__ (self, screen, size=(800,600), perfection=19, maxval=8, atomsum=85):
  15.         """Initializes the process with the screen size, perfect number value, maximum value, and atom range"""
  16.  
  17.         self.screen = screen
  18.         self.size = self.w, self.h = size
  19.         self.perfection = perfection
  20.         self.maxval = maxval
  21.         self.atomsum = atomsum
  22.         self.atomlist = []
  23.  
  24.         self.axis0 = self.h / 2 - (self.maxval * 2 + self.solution(self.maxval, self.maxval)) / 2
  25.         self.axis1 = self.axis0 + self.maxval
  26.         self.axis2 = self.axis1 + self.maxval
  27.         self.axis3 = self.axis2 + self.solution(self.maxval, self.maxval)
  28.         self.xshft = 0
  29.         self.pagenum = 1
  30.         self.refresh = True
  31.  
  32. #--------------------------------------
  33.  
  34.     def solution (self, x, y):
  35.         """The verification formula to finish process"""
  36.  
  37.         return (x + y) + (x * y)
  38.  
  39. #--------------------------------------
  40.  
  41.     def run (self):
  42.         """Process' main routine"""
  43.  
  44.         # fill the atom list (id sorted)
  45.         for i in range(self.atomsum):
  46.             a = Atom(i,random.randint(0, self.maxval))
  47.             self.atomlist.append(a)
  48.  
  49.         # atom dict (id -> val)
  50.         d = {}
  51.         for a in self.atomlist:
  52.            d[a] = a.getval()
  53.  
  54.         # atom values dict (val -> id)
  55.         d1 = {}
  56.         for a in d.items():
  57.             if a[1] not in d1:
  58.                 x = [a[0]]
  59.             else:
  60.                 x = d1[a[1]]
  61.                 x.append(a[0])
  62.             d1[a[1]] = x
  63.  
  64.         # sorted atom list (value sorted)
  65.         j = 0
  66.         del self.atomlist[:]
  67.         for k, v in d1.items():
  68.             for i in v:
  69.                 self.atomlist.insert(j, i)
  70.                 j +=1
  71.  
  72.         # here start the main process
  73.         x = 0
  74.         values = [0,0,0]
  75.         done = False
  76.  
  77.         while done is False:
  78.             choosen = random.choice(self.atomlist)
  79.             values[0]=choosen.getval()
  80.  
  81.             for a in self.atomlist:
  82.                 # here is the main code
  83.                 compat = a.love(choosen)
  84.                 values[1]=a.getval()
  85.                 values[2]=compat
  86.                 print "(%s)%s op (%s)%s =%s" %\
  87.                       (a.getid(), a.getval(),\
  88.                        choosen.getid(), choosen.getval(),\
  89.                        compat)
  90.  
  91.                 # check succed
  92.                 if compat is self.perfection:
  93.                     self.printval(x, values, 0xFF0000)
  94.                     # finished the job
  95.                     print "Finish !"
  96.                     while pygame.event.wait().type is not pygame.QUIT:
  97.                         done = True
  98.                     break
  99.                 # else...
  100.                 elif compat is False:
  101.                     self.printval(x, values, 0x009900)
  102.                 else:
  103.                     self.printval(x, values, 0xFFFFFF)
  104.  
  105.                 x += 1
  106.  
  107.         print "Merci !"
  108.         return True
  109.  
  110. #--------------------------------------
  111.  
  112.     def printval (self, x, values, color):
  113.         """Display all lines and dots on the screen"""
  114.  
  115.         if x / self.w is self.pagenum:
  116.             self.pagenum += 1
  117.             self.xshft += self.w
  118.             self.screen.fill(0x000000)
  119.             self.refresh = True
  120.  
  121.         if self.refresh is True:
  122.             for i in range(self.w):
  123.                 self.screen.set_at((i, self.axis0), 0x333333)
  124.                 self.screen.set_at((i, self.axis1), 0x333333)
  125.                 self.screen.set_at((i, self.axis2), 0x333333)
  126.                 self.screen.set_at((i, self.axis3), 0x333333)
  127.             self.refresh = False
  128.             for i in range(0, self.w, 4):
  129.                 self.screen.set_at((i, self.axis2+self.perfection), 0x999999)
  130.  
  131.         self.screen.set_at((x-self.xshft, self.axis0 + values[0]), color)
  132.         self.screen.set_at((x-self.xshft, self.axis1 + values[1]), color)
  133.         self.screen.set_at((x-self.xshft, self.axis2 + values[2]), color)
  134.  
  135.         if color == 0xFF0000:
  136.             for i in range(0, self.w, 4):
  137.                 self.screen.set_at((i, self.axis0 + values[0]), 0x666666)
  138.                 self.screen.set_at((i, self.axis1 + values[1]), 0x666666)
  139.  
  140.         pygame.display.flip()
  141.  
  142. ###############################################################################
  143.  
  144. class Atom (Life):
  145.     """A single atom"""
  146.  
  147. #--------------------------------------
  148.  
  149.     def __init__ (self, id, val):
  150.         """Initializes an atom with an id and a value"""
  151.  
  152.         self._id = id
  153.         self._val = val
  154.  
  155.         self._lovers = []
  156.  
  157. #--------------------------------------
  158.  
  159.     def getid (self):
  160.         """Returns atom id"""
  161.  
  162.         return self._id
  163.  
  164. #--------------------------------------
  165.  
  166.     def getval (self):
  167.         """Returns atom value"""
  168.  
  169.         return self._val
  170.  
  171. #--------------------------------------
  172.  
  173.     def taste (self, atom):
  174.         """Check if another atom is known or himself. Returns True if succed"""
  175.  
  176.         atomid = atom.getid()
  177.         if atomid is self._id:
  178.             return False
  179.  
  180.         if len(self._lovers) is not 0:
  181.             try:
  182.                 self._lovers.index(atomid)
  183.             except:
  184.                 return False
  185.             else:
  186.                 self._lovers.append(atom.getval())
  187.         return True
  188.  
  189. #--------------------------------------
  190.  
  191.     def love (self, atom):
  192.         """Check another atom and make love with. Returns the love value to be compared with the perfection ;)"""
  193.  
  194.         if self.taste(atom) is False:
  195.             return False
  196.         else:
  197.             return Life.solution(self, self._val, atom.getval())
  198.  
  199. ###############################################################################
  200.  
  201. def main ():
  202.     """The main routine"""
  203.  
  204.     pygame.init()
  205.     size = (1024, 600)
  206.  
  207.     screen = pygame.display.set_mode(size)
  208.  
  209.     pygame.display.set_caption("Algo Genetique")
  210.  
  211.     l = Life(screen,size)
  212.  
  213.     done = False
  214.     while done is False:
  215.         for event in pygame.event.get():
  216.             if event.type is pygame.QUIT:
  217.                 done = True
  218.  
  219.         if l.run() is True:
  220.             done = True
  221.  
  222. if __name__ == "__main__": main()
  223.  
  224. pygame.quit()
  225.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement