Browse code

Fixed wrapping bug

haywalk authored on 03/12/2021 14:43:00
Showing 1 changed files

... ...
@@ -2,7 +2,7 @@
2 2
 conway.py
3 3
 
4 4
 Copyright 2021 Hayden D. Walker <planethaywalk@aol.com>
5
- 
5
+
6 6
 This program is free software: you can redistribute it and/or modify
7 7
 it under the terms of the GNU General Public License as published by
8 8
 the Free Software Foundation, either version 3 of the License, or
... ...
@@ -15,22 +15,21 @@ GNU General Public License for more details.
15 15
 
16 16
 You should have received a copy of the GNU General Public License
17 17
 along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
-'''
19 18
 
20
-'''
21 19
 Conway's Game of Life
22 20
 Implemented by Hayden Walker (www.haywalk.ca)
23 21
 2021-11-30
24 22
 '''
25 23
 
26 24
 import random
25
+import sys
27 26
 
28 27
 # Attempt to import Pygame
29 28
 try:
30 29
     import pygame
31 30
 except ModuleNotFoundError:
32 31
     print('This program requires the Pygame library to be installed.')
33
-    quit()
32
+    sys.exit()
34 33
 
35 34
 
36 35
 class Cell:
... ...
@@ -52,16 +51,19 @@ class Cell:
52 51
         '''
53 52
         num_of_neighbours = 0
54 53
 
54
+		# All of the neighbouring cells' relative positions
55 55
         neighbour_positions = [[-1, -1], [-1, 0], [-1, 1],
56 56
             [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
57 57
 
58
-        for position in neighbour_positions:
59
-            nr = position[0]
60
-            nc = position[1]
61
-
58
+        for pos in neighbour_positions:
62 59
             try:
63
-                if cell_array[self.row + nr][self.col + nc].is_living:
60
+                if cell_array[self.row + pos[0]][self.col + pos[1]].is_living:
64 61
                     num_of_neighbours += 1
62
+
63
+                    # Compensate for python's negative indexes
64
+                    if self.row + pos[0] < 0 or self.col + pos[1] < 0:
65
+                        num_of_neighbours -= 1
66
+            
65 67
             except IndexError:
66 68
                 continue
67 69
 
... ...
@@ -87,10 +89,6 @@ class Cell:
87 89
         else:
88 90
             pygame.draw.rect(win, (0, 0, 0), (self.col * 10, self.row * 10, 10, 10))
89 91
 
90
-'''
91
-Setup
92
-'''
93
-
94 92
 # Get number of rows and columns
95 93
 dimensions = int(input('Number of rows and columns: '))
96 94
 delay = int(input('Delay between cycles (ms): '))
... ...
@@ -101,49 +99,43 @@ screen = pygame.display.set_mode((dimensions * 10, dimensions * 10))
101 99
 pygame.display.set_caption("Life")
102 100
 
103 101
 # Create array of rows of cells
104
-cell_array = list()
102
+cells = []
105 103
 
106 104
 # Add rows of cells to the array
107
-for row in range(dimensions):
105
+for cellrow in range(dimensions):
108 106
     # Create the row as a list of cells
109
-    this_row = list()
107
+    this_row = []
110 108
 
111 109
     # Add cells to the row
112
-    for col in range(dimensions):
113
-        this_row.append(Cell(random.randint(0, 1), col, row))
110
+    for column in range(dimensions):
111
+        this_row.append(Cell(random.randint(0, 1), column, cellrow))
114 112
 
115 113
     # Add row to list of rows
116
-    cell_array.append(this_row)
117
-
118
-'''
119
-Main loop
120
-'''
114
+    cells.append(this_row)
121 115
 
116
+# Main loop
122 117
 while True:
123 118
     # Check for quit
124 119
     for event in pygame.event.get():
125 120
         if event.type == pygame.QUIT:
126
-            quit()
127
-    
121
+            sys.exit()
122
+
128 123
     # Clear screen
129 124
     screen.fill((0, 0, 0))
130 125
 
131 126
     # Draw each cell and count neighbours
132
-    for row in cell_array:
133
-        for cell in row:
127
+    for cellrow in cells:
128
+        for cell in cellrow:
134 129
             cell.draw(screen)
135
-            cell.get_neighbours(cell_array)
130
+            cell.get_neighbours(cells)
136 131
 
137 132
     # Update the screen
138 133
     pygame.display.update()
139 134
 
140 135
     # Make each cell evolve
141
-    for row in cell_array:
142
-        for cell in row:
136
+    for cellrow in cells:
137
+        for cell in cellrow:
143 138
             cell.evolve()
144 139
 
145 140
     # Wait a second
146 141
     pygame.time.delay(delay)
147
-
148
-
149
-