1 | n/a | #!/usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """ |
---|
4 | n/a | A curses-based version of Conway's Game of Life. |
---|
5 | n/a | |
---|
6 | n/a | An empty board will be displayed, and the following commands are available: |
---|
7 | n/a | E : Erase the board |
---|
8 | n/a | R : Fill the board randomly |
---|
9 | n/a | S : Step for a single generation |
---|
10 | n/a | C : Update continuously until a key is struck |
---|
11 | n/a | Q : Quit |
---|
12 | n/a | Cursor keys : Move the cursor around the board |
---|
13 | n/a | Space or Enter : Toggle the contents of the cursor's position |
---|
14 | n/a | |
---|
15 | n/a | Contributed by Andrew Kuchling, Mouse support and color by Dafydd Crosby. |
---|
16 | n/a | """ |
---|
17 | n/a | |
---|
18 | n/a | import curses |
---|
19 | n/a | import random |
---|
20 | n/a | |
---|
21 | n/a | |
---|
22 | n/a | class LifeBoard: |
---|
23 | n/a | """Encapsulates a Life board |
---|
24 | n/a | |
---|
25 | n/a | Attributes: |
---|
26 | n/a | X,Y : horizontal and vertical size of the board |
---|
27 | n/a | state : dictionary mapping (x,y) to 0 or 1 |
---|
28 | n/a | |
---|
29 | n/a | Methods: |
---|
30 | n/a | display(update_board) -- If update_board is true, compute the |
---|
31 | n/a | next generation. Then display the state |
---|
32 | n/a | of the board and refresh the screen. |
---|
33 | n/a | erase() -- clear the entire board |
---|
34 | n/a | make_random() -- fill the board randomly |
---|
35 | n/a | set(y,x) -- set the given cell to Live; doesn't refresh the screen |
---|
36 | n/a | toggle(y,x) -- change the given cell from live to dead, or vice |
---|
37 | n/a | versa, and refresh the screen display |
---|
38 | n/a | |
---|
39 | n/a | """ |
---|
40 | n/a | def __init__(self, scr, char=ord('*')): |
---|
41 | n/a | """Create a new LifeBoard instance. |
---|
42 | n/a | |
---|
43 | n/a | scr -- curses screen object to use for display |
---|
44 | n/a | char -- character used to render live cells (default: '*') |
---|
45 | n/a | """ |
---|
46 | n/a | self.state = {} |
---|
47 | n/a | self.scr = scr |
---|
48 | n/a | Y, X = self.scr.getmaxyx() |
---|
49 | n/a | self.X, self.Y = X - 2, Y - 2 - 1 |
---|
50 | n/a | self.char = char |
---|
51 | n/a | self.scr.clear() |
---|
52 | n/a | |
---|
53 | n/a | # Draw a border around the board |
---|
54 | n/a | border_line = '+' + (self.X * '-') + '+' |
---|
55 | n/a | self.scr.addstr(0, 0, border_line) |
---|
56 | n/a | self.scr.addstr(self.Y + 1, 0, border_line) |
---|
57 | n/a | for y in range(0, self.Y): |
---|
58 | n/a | self.scr.addstr(1 + y, 0, '|') |
---|
59 | n/a | self.scr.addstr(1 + y, self.X + 1, '|') |
---|
60 | n/a | self.scr.refresh() |
---|
61 | n/a | |
---|
62 | n/a | def set(self, y, x): |
---|
63 | n/a | """Set a cell to the live state""" |
---|
64 | n/a | if x < 0 or self.X <= x or y < 0 or self.Y <= y: |
---|
65 | n/a | raise ValueError("Coordinates out of range %i,%i" % (y, x)) |
---|
66 | n/a | self.state[x, y] = 1 |
---|
67 | n/a | |
---|
68 | n/a | def toggle(self, y, x): |
---|
69 | n/a | """Toggle a cell's state between live and dead""" |
---|
70 | n/a | if x < 0 or self.X <= x or y < 0 or self.Y <= y: |
---|
71 | n/a | raise ValueError("Coordinates out of range %i,%i" % (y, x)) |
---|
72 | n/a | if (x, y) in self.state: |
---|
73 | n/a | del self.state[x, y] |
---|
74 | n/a | self.scr.addch(y + 1, x + 1, ' ') |
---|
75 | n/a | else: |
---|
76 | n/a | self.state[x, y] = 1 |
---|
77 | n/a | if curses.has_colors(): |
---|
78 | n/a | # Let's pick a random color! |
---|
79 | n/a | self.scr.attrset(curses.color_pair(random.randrange(1, 7))) |
---|
80 | n/a | self.scr.addch(y + 1, x + 1, self.char) |
---|
81 | n/a | self.scr.attrset(0) |
---|
82 | n/a | self.scr.refresh() |
---|
83 | n/a | |
---|
84 | n/a | def erase(self): |
---|
85 | n/a | """Clear the entire board and update the board display""" |
---|
86 | n/a | self.state = {} |
---|
87 | n/a | self.display(update_board=False) |
---|
88 | n/a | |
---|
89 | n/a | def display(self, update_board=True): |
---|
90 | n/a | """Display the whole board, optionally computing one generation""" |
---|
91 | n/a | M, N = self.X, self.Y |
---|
92 | n/a | if not update_board: |
---|
93 | n/a | for i in range(0, M): |
---|
94 | n/a | for j in range(0, N): |
---|
95 | n/a | if (i, j) in self.state: |
---|
96 | n/a | self.scr.addch(j + 1, i + 1, self.char) |
---|
97 | n/a | else: |
---|
98 | n/a | self.scr.addch(j + 1, i + 1, ' ') |
---|
99 | n/a | self.scr.refresh() |
---|
100 | n/a | return |
---|
101 | n/a | |
---|
102 | n/a | d = {} |
---|
103 | n/a | self.boring = 1 |
---|
104 | n/a | for i in range(0, M): |
---|
105 | n/a | L = range(max(0, i - 1), min(M, i + 2)) |
---|
106 | n/a | for j in range(0, N): |
---|
107 | n/a | s = 0 |
---|
108 | n/a | live = (i, j) in self.state |
---|
109 | n/a | for k in range(max(0, j - 1), min(N, j + 2)): |
---|
110 | n/a | for l in L: |
---|
111 | n/a | if (l, k) in self.state: |
---|
112 | n/a | s += 1 |
---|
113 | n/a | s -= live |
---|
114 | n/a | if s == 3: |
---|
115 | n/a | # Birth |
---|
116 | n/a | d[i, j] = 1 |
---|
117 | n/a | if curses.has_colors(): |
---|
118 | n/a | # Let's pick a random color! |
---|
119 | n/a | self.scr.attrset(curses.color_pair( |
---|
120 | n/a | random.randrange(1, 7))) |
---|
121 | n/a | self.scr.addch(j + 1, i + 1, self.char) |
---|
122 | n/a | self.scr.attrset(0) |
---|
123 | n/a | if not live: |
---|
124 | n/a | self.boring = 0 |
---|
125 | n/a | elif s == 2 and live: |
---|
126 | n/a | # Survival |
---|
127 | n/a | d[i, j] = 1 |
---|
128 | n/a | elif live: |
---|
129 | n/a | # Death |
---|
130 | n/a | self.scr.addch(j + 1, i + 1, ' ') |
---|
131 | n/a | self.boring = 0 |
---|
132 | n/a | self.state = d |
---|
133 | n/a | self.scr.refresh() |
---|
134 | n/a | |
---|
135 | n/a | def make_random(self): |
---|
136 | n/a | "Fill the board with a random pattern" |
---|
137 | n/a | self.state = {} |
---|
138 | n/a | for i in range(0, self.X): |
---|
139 | n/a | for j in range(0, self.Y): |
---|
140 | n/a | if random.random() > 0.5: |
---|
141 | n/a | self.set(j, i) |
---|
142 | n/a | |
---|
143 | n/a | |
---|
144 | n/a | def erase_menu(stdscr, menu_y): |
---|
145 | n/a | "Clear the space where the menu resides" |
---|
146 | n/a | stdscr.move(menu_y, 0) |
---|
147 | n/a | stdscr.clrtoeol() |
---|
148 | n/a | stdscr.move(menu_y + 1, 0) |
---|
149 | n/a | stdscr.clrtoeol() |
---|
150 | n/a | |
---|
151 | n/a | |
---|
152 | n/a | def display_menu(stdscr, menu_y): |
---|
153 | n/a | "Display the menu of possible keystroke commands" |
---|
154 | n/a | erase_menu(stdscr, menu_y) |
---|
155 | n/a | |
---|
156 | n/a | # If color, then light the menu up :-) |
---|
157 | n/a | if curses.has_colors(): |
---|
158 | n/a | stdscr.attrset(curses.color_pair(1)) |
---|
159 | n/a | stdscr.addstr(menu_y, 4, |
---|
160 | n/a | 'Use the cursor keys to move, and space or Enter to toggle a cell.') |
---|
161 | n/a | stdscr.addstr(menu_y + 1, 4, |
---|
162 | n/a | 'E)rase the board, R)andom fill, S)tep once or C)ontinuously, Q)uit') |
---|
163 | n/a | stdscr.attrset(0) |
---|
164 | n/a | |
---|
165 | n/a | |
---|
166 | n/a | def keyloop(stdscr): |
---|
167 | n/a | # Clear the screen and display the menu of keys |
---|
168 | n/a | stdscr.clear() |
---|
169 | n/a | stdscr_y, stdscr_x = stdscr.getmaxyx() |
---|
170 | n/a | menu_y = (stdscr_y - 3) - 1 |
---|
171 | n/a | display_menu(stdscr, menu_y) |
---|
172 | n/a | |
---|
173 | n/a | # If color, then initialize the color pairs |
---|
174 | n/a | if curses.has_colors(): |
---|
175 | n/a | curses.init_pair(1, curses.COLOR_BLUE, 0) |
---|
176 | n/a | curses.init_pair(2, curses.COLOR_CYAN, 0) |
---|
177 | n/a | curses.init_pair(3, curses.COLOR_GREEN, 0) |
---|
178 | n/a | curses.init_pair(4, curses.COLOR_MAGENTA, 0) |
---|
179 | n/a | curses.init_pair(5, curses.COLOR_RED, 0) |
---|
180 | n/a | curses.init_pair(6, curses.COLOR_YELLOW, 0) |
---|
181 | n/a | curses.init_pair(7, curses.COLOR_WHITE, 0) |
---|
182 | n/a | |
---|
183 | n/a | # Set up the mask to listen for mouse events |
---|
184 | n/a | curses.mousemask(curses.BUTTON1_CLICKED) |
---|
185 | n/a | |
---|
186 | n/a | # Allocate a subwindow for the Life board and create the board object |
---|
187 | n/a | subwin = stdscr.subwin(stdscr_y - 3, stdscr_x, 0, 0) |
---|
188 | n/a | board = LifeBoard(subwin, char=ord('*')) |
---|
189 | n/a | board.display(update_board=False) |
---|
190 | n/a | |
---|
191 | n/a | # xpos, ypos are the cursor's position |
---|
192 | n/a | xpos, ypos = board.X // 2, board.Y // 2 |
---|
193 | n/a | |
---|
194 | n/a | # Main loop: |
---|
195 | n/a | while True: |
---|
196 | n/a | stdscr.move(1 + ypos, 1 + xpos) # Move the cursor |
---|
197 | n/a | c = stdscr.getch() # Get a keystroke |
---|
198 | n/a | if 0 < c < 256: |
---|
199 | n/a | c = chr(c) |
---|
200 | n/a | if c in ' \n': |
---|
201 | n/a | board.toggle(ypos, xpos) |
---|
202 | n/a | elif c in 'Cc': |
---|
203 | n/a | erase_menu(stdscr, menu_y) |
---|
204 | n/a | stdscr.addstr(menu_y, 6, ' Hit any key to stop continuously ' |
---|
205 | n/a | 'updating the screen.') |
---|
206 | n/a | stdscr.refresh() |
---|
207 | n/a | # Activate nodelay mode; getch() will return -1 |
---|
208 | n/a | # if no keystroke is available, instead of waiting. |
---|
209 | n/a | stdscr.nodelay(1) |
---|
210 | n/a | while True: |
---|
211 | n/a | c = stdscr.getch() |
---|
212 | n/a | if c != -1: |
---|
213 | n/a | break |
---|
214 | n/a | stdscr.addstr(0, 0, '/') |
---|
215 | n/a | stdscr.refresh() |
---|
216 | n/a | board.display() |
---|
217 | n/a | stdscr.addstr(0, 0, '+') |
---|
218 | n/a | stdscr.refresh() |
---|
219 | n/a | |
---|
220 | n/a | stdscr.nodelay(0) # Disable nodelay mode |
---|
221 | n/a | display_menu(stdscr, menu_y) |
---|
222 | n/a | |
---|
223 | n/a | elif c in 'Ee': |
---|
224 | n/a | board.erase() |
---|
225 | n/a | elif c in 'Qq': |
---|
226 | n/a | break |
---|
227 | n/a | elif c in 'Rr': |
---|
228 | n/a | board.make_random() |
---|
229 | n/a | board.display(update_board=False) |
---|
230 | n/a | elif c in 'Ss': |
---|
231 | n/a | board.display() |
---|
232 | n/a | else: |
---|
233 | n/a | # Ignore incorrect keys |
---|
234 | n/a | pass |
---|
235 | n/a | elif c == curses.KEY_UP and ypos > 0: |
---|
236 | n/a | ypos -= 1 |
---|
237 | n/a | elif c == curses.KEY_DOWN and ypos + 1 < board.Y: |
---|
238 | n/a | ypos += 1 |
---|
239 | n/a | elif c == curses.KEY_LEFT and xpos > 0: |
---|
240 | n/a | xpos -= 1 |
---|
241 | n/a | elif c == curses.KEY_RIGHT and xpos + 1 < board.X: |
---|
242 | n/a | xpos += 1 |
---|
243 | n/a | elif c == curses.KEY_MOUSE: |
---|
244 | n/a | mouse_id, mouse_x, mouse_y, mouse_z, button_state = curses.getmouse() |
---|
245 | n/a | if (mouse_x > 0 and mouse_x < board.X + 1 and |
---|
246 | n/a | mouse_y > 0 and mouse_y < board.Y + 1): |
---|
247 | n/a | xpos = mouse_x - 1 |
---|
248 | n/a | ypos = mouse_y - 1 |
---|
249 | n/a | board.toggle(ypos, xpos) |
---|
250 | n/a | else: |
---|
251 | n/a | # They've clicked outside the board |
---|
252 | n/a | curses.flash() |
---|
253 | n/a | else: |
---|
254 | n/a | # Ignore incorrect keys |
---|
255 | n/a | pass |
---|
256 | n/a | |
---|
257 | n/a | |
---|
258 | n/a | def main(stdscr): |
---|
259 | n/a | keyloop(stdscr) # Enter the main loop |
---|
260 | n/a | |
---|
261 | n/a | if __name__ == '__main__': |
---|
262 | n/a | curses.wrapper(main) |
---|