Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # life.py -- A curses-based version of Conway's Game of Life. |
Andrew M. Kuchling | 10f9c07 | 2001-11-05 21:25:42 +0000 | [diff] [blame] | 3 | # Contributed by AMK |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 4 | # |
| 5 | # An empty board will be displayed, and the following commands are available: |
| 6 | # E : Erase the board |
| 7 | # R : Fill the board randomly |
| 8 | # S : Step for a single generation |
| 9 | # C : Update continuously until a key is struck |
| 10 | # Q : Quit |
| 11 | # Cursor keys : Move the cursor around the board |
| 12 | # Space or Enter : Toggle the contents of the cursor's position |
| 13 | # |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 14 | # TODO : |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 15 | # Support the mouse |
| 16 | # Use colour if available |
| 17 | # Make board updates faster |
| 18 | # |
| 19 | |
Andrew M. Kuchling | 9a62448 | 2002-04-10 14:50:16 +0000 | [diff] [blame] | 20 | import random, string, traceback |
| 21 | import curses |
| 22 | |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 23 | class LifeBoard: |
| 24 | """Encapsulates a Life board |
| 25 | |
| 26 | Attributes: |
| 27 | X,Y : horizontal and vertical size of the board |
| 28 | state : dictionary mapping (x,y) to 0 or 1 |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 29 | |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 30 | Methods: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 31 | display(update_board) -- If update_board is true, compute the |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 32 | next generation. Then display the state |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 33 | of the board and refresh the screen. |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 34 | erase() -- clear the entire board |
| 35 | makeRandom() -- fill the board randomly |
| 36 | set(y,x) -- set the given cell to Live; doesn't refresh the screen |
| 37 | toggle(y,x) -- change the given cell from live to dead, or vice |
| 38 | versa, and refresh the screen display |
| 39 | |
| 40 | """ |
| 41 | def __init__(self, scr, char=ord('*')): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 42 | """Create a new LifeBoard instance. |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 43 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 44 | scr -- curses screen object to use for display |
| 45 | char -- character used to render live cells (default: '*') |
| 46 | """ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 47 | self.state = {} |
| 48 | self.scr = scr |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 49 | Y, X = self.scr.getmaxyx() |
| 50 | self.X, self.Y = X-2, Y-2-1 |
| 51 | self.char = char |
| 52 | self.scr.clear() |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 53 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 54 | # Draw a border around the board |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 55 | border_line = '+'+(self.X*'-')+'+' |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 56 | self.scr.addstr(0, 0, border_line) |
| 57 | self.scr.addstr(self.Y+1,0, border_line) |
| 58 | for y in range(0, self.Y): |
| 59 | self.scr.addstr(1+y, 0, '|') |
| 60 | self.scr.addstr(1+y, self.X+1, '|') |
| 61 | self.scr.refresh() |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 62 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 63 | def set(self, y, x): |
| 64 | """Set a cell to the live state""" |
| 65 | if x<0 or self.X<=x or y<0 or self.Y<=y: |
| 66 | raise ValueError, "Coordinates out of range %i,%i"% (y,x) |
| 67 | self.state[x,y] = 1 |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 68 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 69 | def toggle(self, y, x): |
| 70 | """Toggle a cell's state between live and dead""" |
| 71 | if x<0 or self.X<=x or y<0 or self.Y<=y: |
| 72 | raise ValueError, "Coordinates out of range %i,%i"% (y,x) |
| 73 | if self.state.has_key( (x,y) ): |
| 74 | del self.state[x,y] |
| 75 | self.scr.addch(y+1, x+1, ' ') |
| 76 | else: |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 77 | self.state[x,y] = 1 |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 78 | self.scr.addch(y+1, x+1, self.char) |
| 79 | self.scr.refresh() |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 80 | |
| 81 | def erase(self): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 82 | """Clear the entire board and update the board display""" |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 83 | self.state = {} |
| 84 | self.display(update_board=False) |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 85 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 86 | def display(self, update_board=True): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 87 | """Display the whole board, optionally computing one generation""" |
| 88 | M,N = self.X, self.Y |
| 89 | if not update_board: |
| 90 | for i in range(0, M): |
| 91 | for j in range(0, N): |
| 92 | if self.state.has_key( (i,j) ): |
| 93 | self.scr.addch(j+1, i+1, self.char) |
| 94 | else: |
| 95 | self.scr.addch(j+1, i+1, ' ') |
| 96 | self.scr.refresh() |
| 97 | return |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 98 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 99 | d = {} |
| 100 | self.boring = 1 |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 101 | for i in range(0, M): |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 102 | L = range( max(0, i-1), min(M, i+2) ) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 103 | for j in range(0, N): |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 104 | s = 0 |
| 105 | live = self.state.has_key( (i,j) ) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 106 | for k in range( max(0, j-1), min(N, j+2) ): |
| 107 | for l in L: |
| 108 | if self.state.has_key( (l,k) ): |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 109 | s += 1 |
| 110 | s -= live |
| 111 | if s == 3: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 112 | # Birth |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 113 | d[i,j] = 1 |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 114 | self.scr.addch(j+1, i+1, self.char) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 115 | if not live: self.boring = 0 |
| 116 | elif s == 2 and live: d[i,j] = 1 # Survival |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 117 | elif live: |
| 118 | # Death |
| 119 | self.scr.addch(j+1, i+1, ' ') |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 120 | self.boring = 0 |
| 121 | self.state = d |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 122 | self.scr.refresh() |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 123 | |
| 124 | def makeRandom(self): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 125 | "Fill the board with a random pattern" |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 126 | self.state = {} |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 127 | for i in range(0, self.X): |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 128 | for j in range(0, self.Y): |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 129 | if random.random() > 0.5: |
| 130 | self.set(j,i) |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 131 | |
| 132 | |
| 133 | def erase_menu(stdscr, menu_y): |
| 134 | "Clear the space where the menu resides" |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 135 | stdscr.move(menu_y, 0) |
| 136 | stdscr.clrtoeol() |
| 137 | stdscr.move(menu_y+1, 0) |
| 138 | stdscr.clrtoeol() |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 139 | |
| 140 | def display_menu(stdscr, menu_y): |
| 141 | "Display the menu of possible keystroke commands" |
| 142 | erase_menu(stdscr, menu_y) |
| 143 | stdscr.addstr(menu_y, 4, |
| 144 | 'Use the cursor keys to move, and space or Enter to toggle a cell.') |
| 145 | stdscr.addstr(menu_y+1, 4, |
| 146 | 'E)rase the board, R)andom fill, S)tep once or C)ontinuously, Q)uit') |
| 147 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 148 | def keyloop(stdscr): |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 149 | # Clear the screen and display the menu of keys |
| 150 | stdscr.clear() |
| 151 | stdscr_y, stdscr_x = stdscr.getmaxyx() |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 152 | menu_y = (stdscr_y-3)-1 |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 153 | display_menu(stdscr, menu_y) |
| 154 | |
| 155 | # Allocate a subwindow for the Life board and create the board object |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 156 | subwin = stdscr.subwin(stdscr_y-3, stdscr_x, 0, 0) |
| 157 | board = LifeBoard(subwin, char=ord('*')) |
| 158 | board.display(update_board=False) |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 159 | |
| 160 | # xpos, ypos are the cursor's position |
| 161 | xpos, ypos = board.X/2, board.Y/2 |
| 162 | |
| 163 | # Main loop: |
| 164 | while (1): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 165 | stdscr.move(1+ypos, 1+xpos) # Move the cursor |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 166 | c = stdscr.getch() # Get a keystroke |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 167 | if 0<c<256: |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 168 | c = chr(c) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 169 | if c in ' \n': |
| 170 | board.toggle(ypos, xpos) |
| 171 | elif c in 'Cc': |
| 172 | erase_menu(stdscr, menu_y) |
| 173 | stdscr.addstr(menu_y, 6, ' Hit any key to stop continuously ' |
| 174 | 'updating the screen.') |
| 175 | stdscr.refresh() |
| 176 | # Activate nodelay mode; getch() will return -1 |
| 177 | # if no keystroke is available, instead of waiting. |
| 178 | stdscr.nodelay(1) |
| 179 | while (1): |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 180 | c = stdscr.getch() |
| 181 | if c != -1: |
| 182 | break |
| 183 | stdscr.addstr(0,0, '/') |
| 184 | stdscr.refresh() |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 185 | board.display() |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 186 | stdscr.addstr(0,0, '+') |
| 187 | stdscr.refresh() |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 188 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 189 | stdscr.nodelay(0) # Disable nodelay mode |
| 190 | display_menu(stdscr, menu_y) |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 191 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 192 | elif c in 'Ee': |
| 193 | board.erase() |
| 194 | elif c in 'Qq': |
| 195 | break |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 196 | elif c in 'Rr': |
| 197 | board.makeRandom() |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 198 | board.display(update_board=False) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 199 | elif c in 'Ss': |
| 200 | board.display() |
| 201 | else: pass # Ignore incorrect keys |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 202 | elif c == curses.KEY_UP and ypos>0: ypos -= 1 |
| 203 | elif c == curses.KEY_DOWN and ypos<board.Y-1: ypos += 1 |
| 204 | elif c == curses.KEY_LEFT and xpos>0: xpos -= 1 |
| 205 | elif c == curses.KEY_RIGHT and xpos<board.X-1: xpos += 1 |
| 206 | else: |
| 207 | # Ignore incorrect keys |
| 208 | pass |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 209 | |
Andrew M. Kuchling | a3b5a5f | 2000-12-13 03:50:20 +0000 | [diff] [blame] | 210 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 211 | def main(stdscr): |
| 212 | keyloop(stdscr) # Enter the main loop |
| 213 | |
| 214 | |
| 215 | if __name__ == '__main__': |
| 216 | curses.wrapper(main) |