-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
100 lines (91 loc) · 2.74 KB
/
game.py
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
98
99
100
import os
import random
import time
field = []
newField = []
h = 10
w = 40
live = True
score = 0
def create(field, newField, h, w):
for i in range(0, h):
field.append([])
newField.append([])
for j in range(0, w):
field[i].append(0)
newField[i].append(0)
def fill(field, chance):
for i in range(0, len(field)):
for j in range(0, len(field[i])):
if (random.randint(0, 100) < chance):
field[i][j] = 1
def draw(field):
global score
if (os.uname().sysname == "Linux"):
os.system("clear")
else:
os.system("cls")
for i in range(0, len(field)):
buffer = ""
for j in range(0, len(field[i])):
if (field[i][j] == 1):
buffer += "x"
else:
buffer += " "
print(buffer)
print("Поколение: " + str(score))
time.sleep(0.1)
score += 1
def neighbours(field, x, y):
count = 0
for i in range(-1, 2):
newx = (x - i + len(field)) % len(field)
for j in range(-1, 2):
newy = (y - j + len(field[i])) % len(field[i])
if (field[newx][newy] == 1 and (i != 0 or j != 0)):
count += 1
return count
def generation(field, newField):
for i in range(0, len(field)):
for j in range(0, len(field[i])):
c = neighbours(field, i, j)
if (field[i][j] == 0):
if (c == 3):
newField[i][j] = 1
if (field[i][j] == 1):
if (c < 2 or c > 3):
newField[i][j] = 0
else:
newField[i][j] = field[i][j]
for i in range(0, len(field)):
for j in range(0, len(field[i])):
field[i][j] = newField[i][j]
def logic():
global field
global newField
global live
for i in range(0, len(field)):
for j in range(0, len(field[i])):
if (field[i][j] == 1):
return
live = False
def glider(field, x, y):
field[x + 1][y] = 1
field[x + 2][y + 1] = 1
field[x + 2][y + 2] = 1
field[x + 1][y + 2] = 1
field[x][y + 2] = 1
if __name__ == "__main__":
data_in = input("Введите целые числа высоту и ширину поля через пробел соответственно: ").split(" ")
h = int(data_in[0])
w = int(data_in[1])
chance = int(input("Введите плотность первого поколения(целое число от 1 до 100): "))
create(field, newField, h, w)
fill(field, chance)
#glider(field, 4, 4)
draw(field)
while (live):
logic()
generation(field, newField)
draw(field)
input("Конец! Нажми Enter...")