国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python實現生命游戲的示例代碼(Game of Life)

python實現生命游戲的示例代碼(Game of Life)

2021-01-08 00:22其實我是一頭豬 Python

這篇文章主要介紹了python實現生命游戲的示例代碼(Game of Life),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

生命游戲的算法就不多解釋了,百度一下介紹隨處可見。

因為網上大多數版本都是基于pygame,matlab等外部庫實現的,二維數組大多是用numpy,使用起來學習成本比較高,所以閑暇之余寫一個不用外部依賴庫,console輸出的版本。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
from time import sleep
from copy import deepcopy
 
WORLD_HIGH = 20 #世界長度
WORLD_WIDE = 40 #世界寬度
ALIVE_CON = 3 #復活條件
KEEP_CON = 2 #保有條件
 
class Cell(object):
  '''''細胞對象'''
  def __init__(self, pos):
    '''''自身坐標x,y, 已經是否還存活'''
    self.point, self.is_alive = pos, False
    self.x, self.y = self.point
   
  def setAlive(self):
    self.is_alive = True
     
  def setDied(self):
    self.is_alive = False
     
  def display(self):
    if self.is_alive:
      return '*'
    return ' '
     
  def displayLinux(self):
    '''''在linux環境下可以打印黑白塊'''
    if self.is_alive:
      return '\033[0;37;47m \033[0m'
    return '\033[0;30;40m \033[0m'
     
class GameManager(object):
  def __init__(self):
    self.world = self.initWorld()
    self.initAliveCell()
    
  def initWorld(self):
    world = []
    for pos_x in xrange(WORLD_WIDE):
      column = []
      for pos_y in xrange(WORLD_HIGH):
        column.append(Cell((pos_x, pos_y)))
      world.append(column)
    return world
   
  def initAliveCell(self):
    from random import choice
    for high in self.world:
      for cell in high:
        if choice((0, 1)) == 0:
          continue
        cell.setAlive()
   
  def getNeighbours(self, cell_obj):
    alive_count = 0
    for x_of in xrange(-1, 2):
      for y_of in xrange(-1, 2):
        c_x, c_y = cell_obj.x + x_of, cell_obj.y + y_of
        if ((c_x, c_y) == cell_obj.point) or \
          (c_x < 0 or c_x >= WORLD_WIDE) or \
          (c_y < 0 or c_y >= WORLD_HIGH):
          '''''排除自身和越界的點'''
          continue
        if self.world[c_x][c_y].is_alive:
          alive_count += 1
    return alive_count
        
  def display(self):
    print '='*WORLD_WIDE #等號分割線
    for index in xrange(WORLD_HIGH):
      print ''.join([high[index].displayLinux() for high in self.world])
    print '='*WORLD_WIDE
 
  def gameStart(self):
    while True:
      self.display()
      new_world = deepcopy(self.world)
      for p_x, wide_list in enumerate(self.world):
        for p_y, _ in enumerate(wide_list):
          current_cell = new_world[p_x][p_y]
          nei_num = self.getNeighbours(current_cell)
          if nei_num == ALIVE_CON:
            current_cell.setAlive()
          elif nei_num != KEEP_CON:
            current_cell.setDied()       
      self.world = new_world
      sleep(0.2)
 
if __name__ == '__main__':
  world = GameManager()
  try:
    world.gameStart()
  except KeyboardInterrupt:
    '''''防止ctrl+c退出報錯'''
    pass

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/valiensun/article/details/77533596

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人av观看| 亚洲精品一区中文字幕乱码 | 国产精品国产三级国产aⅴ 成人在线免费看 | 免费观看av大片 | 一级一片免费 | 国产精品视频一二三 | 国产色视频在线观看免费 | 欧美精品系列 | 国产一区二区综合 | 久久白虎 | 欧美日韩成人在线观看 | 色综合天天综合网国产成人网 | 蜜桃av一区 | 欧美精品一区二区三区四区五区 | 久久久久高清 | 欧美视频二区 | 九九热在线播放 | 色女人av| av在线一区二区 | 在线免费日韩 | 天天操夜夜操 | 国产精品高清在线 | 午夜国产 | 成人一级黄色大片 | 亚洲国产精品成人 | 精品国产髙清在线看国产毛片 | 精品视频久久久 | 国产在线观看一区二区三区 | 国产成人精品一区二区三区四区 | 成人精品视频99在线观看免费 | 欧美中文字幕一区二区三区亚洲 | 青娱乐99| 精品天堂| 亚洲视频一区二区三区 | 国产一级一级国产 | 欧美日韩在线视频观看 | 亚洲a在线观看 | a在线看| 亚洲欧美综合乱码精品成人网 | 小视频免费在线观看 | 欧美一区二区三区不卡 |