blob: fc4cb4952dce69d658821e2aa059d97436976003 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +00002
Georg Brandl856898b2010-12-30 22:11:50 +00003"""
4A curses-based version of Conway's Game of Life.
5
6An empty board will be displayed, and the following commands are available:
7 E : Erase the board
8 R : Fill the board randomly
9 S : Step for a single generation
10 C : Update continuously until a key is struck
11 Q : Quit
12 Cursor keys : Move the cursor around the board
13 Space or Enter : Toggle the contents of the cursor's position
14
15Contributed by Andrew Kuchling, Mouse support and color by Dafydd Crosby.
16"""
17
Andrew M. Kuchling9a624482002-04-10 14:50:16 +000018import curses
Georg Brandl856898b2010-12-30 22:11:50 +000019import random
20
Andrew M. Kuchling9a624482002-04-10 14:50:16 +000021
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +000022class LifeBoard:
23 """Encapsulates a Life board
24
25 Attributes:
26 X,Y : horizontal and vertical size of the board
27 state : dictionary mapping (x,y) to 0 or 1
Tim Peterse6ddc8b2004-07-18 05:56:09 +000028
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +000029 Methods:
Tim Peterse6ddc8b2004-07-18 05:56:09 +000030 display(update_board) -- If update_board is true, compute the
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +000031 next generation. Then display the state
Tim Peterse6ddc8b2004-07-18 05:56:09 +000032 of the board and refresh the screen.
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +000033 erase() -- clear the entire board
Georg Brandl856898b2010-12-30 22:11:50 +000034 make_random() -- fill the board randomly
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +000035 set(y,x) -- set the given cell to Live; doesn't refresh the screen
36 toggle(y,x) -- change the given cell from live to dead, or vice
37 versa, and refresh the screen display
38
39 """
40 def __init__(self, scr, char=ord('*')):
Tim Peterse6ddc8b2004-07-18 05:56:09 +000041 """Create a new LifeBoard instance.
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +000042
Tim Peterse6ddc8b2004-07-18 05:56:09 +000043 scr -- curses screen object to use for display
44 char -- character used to render live cells (default: '*')
45 """
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000046 self.state = {}
47 self.scr = scr
Tim Peterse6ddc8b2004-07-18 05:56:09 +000048 Y, X = self.scr.getmaxyx()
Florent Xiclunac2074012012-07-07 17:03:54 +020049 self.X, self.Y = X - 2, Y - 2 - 1
Tim Peterse6ddc8b2004-07-18 05:56:09 +000050 self.char = char
51 self.scr.clear()
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +000052
Tim Peterse6ddc8b2004-07-18 05:56:09 +000053 # Draw a border around the board
Florent Xiclunac2074012012-07-07 17:03:54 +020054 border_line = '+' + (self.X * '-') + '+'
Tim Peterse6ddc8b2004-07-18 05:56:09 +000055 self.scr.addstr(0, 0, border_line)
Florent Xiclunac2074012012-07-07 17:03:54 +020056 self.scr.addstr(self.Y + 1, 0, border_line)
Tim Peterse6ddc8b2004-07-18 05:56:09 +000057 for y in range(0, self.Y):
Florent Xiclunac2074012012-07-07 17:03:54 +020058 self.scr.addstr(1 + y, 0, '|')
59 self.scr.addstr(1 + y, self.X + 1, '|')
Tim Peterse6ddc8b2004-07-18 05:56:09 +000060 self.scr.refresh()
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +000061
Tim Peterse6ddc8b2004-07-18 05:56:09 +000062 def set(self, y, x):
63 """Set a cell to the live state"""
Florent Xiclunac2074012012-07-07 17:03:54 +020064 if x < 0 or self.X <= x or y < 0 or self.Y <= y:
65 raise ValueError("Coordinates out of range %i,%i" % (y, x))
66 self.state[x, y] = 1
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +000067
Tim Peterse6ddc8b2004-07-18 05:56:09 +000068 def toggle(self, y, x):
69 """Toggle a cell's state between live and dead"""
Georg Brandl856898b2010-12-30 22:11:50 +000070 if x < 0 or self.X <= x or y < 0 or self.Y <= y:
Florent Xiclunac2074012012-07-07 17:03:54 +020071 raise ValueError("Coordinates out of range %i,%i" % (y, x))
Georg Brandl856898b2010-12-30 22:11:50 +000072 if (x, y) in self.state:
73 del self.state[x, y]
Florent Xiclunac2074012012-07-07 17:03:54 +020074 self.scr.addch(y + 1, x + 1, ' ')
Tim Peterse6ddc8b2004-07-18 05:56:09 +000075 else:
Georg Brandl856898b2010-12-30 22:11:50 +000076 self.state[x, y] = 1
Senthil Kumaranc1d98d62010-11-25 14:56:44 +000077 if curses.has_colors():
Georg Brandl856898b2010-12-30 22:11:50 +000078 # Let's pick a random color!
79 self.scr.attrset(curses.color_pair(random.randrange(1, 7)))
Florent Xiclunac2074012012-07-07 17:03:54 +020080 self.scr.addch(y + 1, x + 1, self.char)
Senthil Kumaranc1d98d62010-11-25 14:56:44 +000081 self.scr.attrset(0)
Tim Peterse6ddc8b2004-07-18 05:56:09 +000082 self.scr.refresh()
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +000083
84 def erase(self):
Tim Peterse6ddc8b2004-07-18 05:56:09 +000085 """Clear the entire board and update the board display"""
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000086 self.state = {}
87 self.display(update_board=False)
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +000088
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000089 def display(self, update_board=True):
Tim Peterse6ddc8b2004-07-18 05:56:09 +000090 """Display the whole board, optionally computing one generation"""
Florent Xiclunac2074012012-07-07 17:03:54 +020091 M, N = self.X, self.Y
Tim Peterse6ddc8b2004-07-18 05:56:09 +000092 if not update_board:
93 for i in range(0, M):
94 for j in range(0, N):
Florent Xiclunac2074012012-07-07 17:03:54 +020095 if (i, j) in self.state:
96 self.scr.addch(j + 1, i + 1, self.char)
Tim Peterse6ddc8b2004-07-18 05:56:09 +000097 else:
Florent Xiclunac2074012012-07-07 17:03:54 +020098 self.scr.addch(j + 1, i + 1, ' ')
Tim Peterse6ddc8b2004-07-18 05:56:09 +000099 self.scr.refresh()
100 return
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000101
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000102 d = {}
103 self.boring = 1
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000104 for i in range(0, M):
Florent Xiclunac2074012012-07-07 17:03:54 +0200105 L = range(max(0, i - 1), min(M, i + 2))
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000106 for j in range(0, N):
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000107 s = 0
Florent Xiclunac2074012012-07-07 17:03:54 +0200108 live = (i, j) in self.state
109 for k in range(max(0, j - 1), min(N, j + 2)):
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000110 for l in L:
Florent Xiclunac2074012012-07-07 17:03:54 +0200111 if (l, k) in self.state:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000112 s += 1
113 s -= live
114 if s == 3:
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000115 # Birth
Florent Xiclunac2074012012-07-07 17:03:54 +0200116 d[i, j] = 1
Senthil Kumaranc1d98d62010-11-25 14:56:44 +0000117 if curses.has_colors():
Georg Brandl856898b2010-12-30 22:11:50 +0000118 # Let's pick a random color!
119 self.scr.attrset(curses.color_pair(
120 random.randrange(1, 7)))
Florent Xiclunac2074012012-07-07 17:03:54 +0200121 self.scr.addch(j + 1, i + 1, self.char)
Senthil Kumaranc1d98d62010-11-25 14:56:44 +0000122 self.scr.attrset(0)
Florent Xiclunac2074012012-07-07 17:03:54 +0200123 if not live:
124 self.boring = 0
125 elif s == 2 and live:
126 # Survival
127 d[i, j] = 1
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000128 elif live:
129 # Death
Florent Xiclunac2074012012-07-07 17:03:54 +0200130 self.scr.addch(j + 1, i + 1, ' ')
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000131 self.boring = 0
132 self.state = d
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000133 self.scr.refresh()
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000134
Georg Brandl856898b2010-12-30 22:11:50 +0000135 def make_random(self):
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000136 "Fill the board with a random pattern"
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000137 self.state = {}
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000138 for i in range(0, self.X):
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000139 for j in range(0, self.Y):
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000140 if random.random() > 0.5:
Florent Xiclunac2074012012-07-07 17:03:54 +0200141 self.set(j, i)
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000142
143
144def erase_menu(stdscr, menu_y):
145 "Clear the space where the menu resides"
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000146 stdscr.move(menu_y, 0)
147 stdscr.clrtoeol()
Florent Xiclunac2074012012-07-07 17:03:54 +0200148 stdscr.move(menu_y + 1, 0)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000149 stdscr.clrtoeol()
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000150
Florent Xiclunac2074012012-07-07 17:03:54 +0200151
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000152def display_menu(stdscr, menu_y):
153 "Display the menu of possible keystroke commands"
154 erase_menu(stdscr, menu_y)
Senthil Kumaranc1d98d62010-11-25 14:56:44 +0000155
Senthil Kumaran67f953c2010-11-26 02:20:04 +0000156 # If color, then light the menu up :-)
Senthil Kumaranc1d98d62010-11-25 14:56:44 +0000157 if curses.has_colors():
158 stdscr.attrset(curses.color_pair(1))
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000159 stdscr.addstr(menu_y, 4,
Georg Brandl856898b2010-12-30 22:11:50 +0000160 'Use the cursor keys to move, and space or Enter to toggle a cell.')
Florent Xiclunac2074012012-07-07 17:03:54 +0200161 stdscr.addstr(menu_y + 1, 4,
Georg Brandl856898b2010-12-30 22:11:50 +0000162 'E)rase the board, R)andom fill, S)tep once or C)ontinuously, Q)uit')
Senthil Kumaranc1d98d62010-11-25 14:56:44 +0000163 stdscr.attrset(0)
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000164
Florent Xiclunac2074012012-07-07 17:03:54 +0200165
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000166def keyloop(stdscr):
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000167 # Clear the screen and display the menu of keys
168 stdscr.clear()
169 stdscr_y, stdscr_x = stdscr.getmaxyx()
Florent Xiclunac2074012012-07-07 17:03:54 +0200170 menu_y = (stdscr_y - 3) - 1
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000171 display_menu(stdscr, menu_y)
172
Senthil Kumaran67f953c2010-11-26 02:20:04 +0000173 # If color, then initialize the color pairs
Senthil Kumaranc1d98d62010-11-25 14:56:44 +0000174 if curses.has_colors():
175 curses.init_pair(1, curses.COLOR_BLUE, 0)
176 curses.init_pair(2, curses.COLOR_CYAN, 0)
177 curses.init_pair(3, curses.COLOR_GREEN, 0)
178 curses.init_pair(4, curses.COLOR_MAGENTA, 0)
179 curses.init_pair(5, curses.COLOR_RED, 0)
180 curses.init_pair(6, curses.COLOR_YELLOW, 0)
181 curses.init_pair(7, curses.COLOR_WHITE, 0)
182
183 # Set up the mask to listen for mouse events
184 curses.mousemask(curses.BUTTON1_CLICKED)
185
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000186 # Allocate a subwindow for the Life board and create the board object
Florent Xiclunac2074012012-07-07 17:03:54 +0200187 subwin = stdscr.subwin(stdscr_y - 3, stdscr_x, 0, 0)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000188 board = LifeBoard(subwin, char=ord('*'))
189 board.display(update_board=False)
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000190
191 # xpos, ypos are the cursor's position
Florent Xiclunac2074012012-07-07 17:03:54 +0200192 xpos, ypos = board.X // 2, board.Y // 2
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000193
194 # Main loop:
Georg Brandl856898b2010-12-30 22:11:50 +0000195 while True:
Florent Xiclunac2074012012-07-07 17:03:54 +0200196 stdscr.move(1 + ypos, 1 + xpos) # Move the cursor
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000197 c = stdscr.getch() # Get a keystroke
Georg Brandl856898b2010-12-30 22:11:50 +0000198 if 0 < c < 256:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000199 c = chr(c)
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000200 if c in ' \n':
201 board.toggle(ypos, xpos)
202 elif c in 'Cc':
203 erase_menu(stdscr, menu_y)
204 stdscr.addstr(menu_y, 6, ' Hit any key to stop continuously '
205 'updating the screen.')
206 stdscr.refresh()
207 # Activate nodelay mode; getch() will return -1
208 # if no keystroke is available, instead of waiting.
209 stdscr.nodelay(1)
Georg Brandl856898b2010-12-30 22:11:50 +0000210 while True:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000211 c = stdscr.getch()
212 if c != -1:
213 break
Georg Brandl856898b2010-12-30 22:11:50 +0000214 stdscr.addstr(0, 0, '/')
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000215 stdscr.refresh()
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000216 board.display()
Georg Brandl856898b2010-12-30 22:11:50 +0000217 stdscr.addstr(0, 0, '+')
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000218 stdscr.refresh()
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000219
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000220 stdscr.nodelay(0) # Disable nodelay mode
221 display_menu(stdscr, menu_y)
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000222
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000223 elif c in 'Ee':
224 board.erase()
225 elif c in 'Qq':
226 break
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000227 elif c in 'Rr':
Georg Brandl856898b2010-12-30 22:11:50 +0000228 board.make_random()
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000229 board.display(update_board=False)
Tim Peterse6ddc8b2004-07-18 05:56:09 +0000230 elif c in 'Ss':
231 board.display()
Florent Xiclunac2074012012-07-07 17:03:54 +0200232 else:
233 # Ignore incorrect keys
234 pass
235 elif c == curses.KEY_UP and ypos > 0:
236 ypos -= 1
237 elif c == curses.KEY_DOWN and ypos + 1 < board.Y:
238 ypos += 1
239 elif c == curses.KEY_LEFT and xpos > 0:
240 xpos -= 1
241 elif c == curses.KEY_RIGHT and xpos + 1 < board.X:
242 xpos += 1
Senthil Kumaranc1d98d62010-11-25 14:56:44 +0000243 elif c == curses.KEY_MOUSE:
Georg Brandl856898b2010-12-30 22:11:50 +0000244 mouse_id, mouse_x, mouse_y, mouse_z, button_state = curses.getmouse()
Florent Xiclunac2074012012-07-07 17:03:54 +0200245 if (mouse_x > 0 and mouse_x < board.X + 1 and
246 mouse_y > 0 and mouse_y < board.Y + 1):
Senthil Kumaranc1d98d62010-11-25 14:56:44 +0000247 xpos = mouse_x - 1
248 ypos = mouse_y - 1
249 board.toggle(ypos, xpos)
250 else:
251 # They've clicked outside the board
252 curses.flash()
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000253 else:
254 # Ignore incorrect keys
255 pass
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000256
Andrew M. Kuchlinga3b5a5f2000-12-13 03:50:20 +0000257
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000258def main(stdscr):
Florent Xiclunac2074012012-07-07 17:03:54 +0200259 keyloop(stdscr) # Enter the main loop
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000260
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000261if __name__ == '__main__':
262 curses.wrapper(main)