@@ -46,38 +46,38 @@ def __init__(self, scr, char=ord('*')):
4646 self .state = {}
4747 self .scr = scr
4848 Y , X = self .scr .getmaxyx ()
49- self .X , self .Y = X - 2 , Y - 2 - 1
49+ self .X , self .Y = X - 2 , Y - 2 - 1
5050 self .char = char
5151 self .scr .clear ()
5252
5353 # Draw a border around the board
54- border_line = '+' + (self .X * '-' )+ '+'
54+ border_line = '+' + (self .X * '-' ) + '+'
5555 self .scr .addstr (0 , 0 , border_line )
56- self .scr .addstr (self .Y + 1 , 0 , border_line )
56+ self .scr .addstr (self .Y + 1 , 0 , border_line )
5757 for y in range (0 , self .Y ):
58- self .scr .addstr (1 + y , 0 , '|' )
59- self .scr .addstr (1 + y , self .X + 1 , '|' )
58+ self .scr .addstr (1 + y , 0 , '|' )
59+ self .scr .addstr (1 + y , self .X + 1 , '|' )
6060 self .scr .refresh ()
6161
6262 def set (self , y , x ):
6363 """Set a cell to the live state"""
64- 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
64+ 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
6767
6868 def toggle (self , y , x ):
6969 """Toggle a cell's state between live and dead"""
7070 if x < 0 or self .X <= x or y < 0 or self .Y <= y :
71- raise ValueError ("Coordinates out of range %i,%i" % (y , x ))
71+ raise ValueError ("Coordinates out of range %i,%i" % (y , x ))
7272 if (x , y ) in self .state :
7373 del self .state [x , y ]
74- self .scr .addch (y + 1 , x + 1 , ' ' )
74+ self .scr .addch (y + 1 , x + 1 , ' ' )
7575 else :
7676 self .state [x , y ] = 1
7777 if curses .has_colors ():
7878 # Let's pick a random color!
7979 self .scr .attrset (curses .color_pair (random .randrange (1 , 7 )))
80- self .scr .addch (y + 1 , x + 1 , self .char )
80+ self .scr .addch (y + 1 , x + 1 , self .char )
8181 self .scr .attrset (0 )
8282 self .scr .refresh ()
8383
@@ -88,43 +88,46 @@ def erase(self):
8888
8989 def display (self , update_board = True ):
9090 """Display the whole board, optionally computing one generation"""
91- M ,N = self .X , self .Y
91+ M , N = self .X , self .Y
9292 if not update_board :
9393 for i in range (0 , M ):
9494 for j in range (0 , N ):
95- if (i ,j ) in self .state :
96- self .scr .addch (j + 1 , i + 1 , self .char )
95+ if (i , j ) in self .state :
96+ self .scr .addch (j + 1 , i + 1 , self .char )
9797 else :
98- self .scr .addch (j + 1 , i + 1 , ' ' )
98+ self .scr .addch (j + 1 , i + 1 , ' ' )
9999 self .scr .refresh ()
100100 return
101101
102102 d = {}
103103 self .boring = 1
104104 for i in range (0 , M ):
105- L = range ( max (0 , i - 1 ), min (M , i + 2 ) )
105+ L = range (max (0 , i - 1 ), min (M , i + 2 ) )
106106 for j in range (0 , N ):
107107 s = 0
108- live = (i ,j ) in self .state
109- for k in range ( max (0 , j - 1 ), min (N , j + 2 ) ):
108+ live = (i , j ) in self .state
109+ for k in range (max (0 , j - 1 ), min (N , j + 2 ) ):
110110 for l in L :
111- if (l ,k ) in self .state :
111+ if (l , k ) in self .state :
112112 s += 1
113113 s -= live
114114 if s == 3 :
115115 # Birth
116- d [i ,j ] = 1
116+ d [i , j ] = 1
117117 if curses .has_colors ():
118118 # Let's pick a random color!
119119 self .scr .attrset (curses .color_pair (
120120 random .randrange (1 , 7 )))
121- self .scr .addch (j + 1 , i + 1 , self .char )
121+ self .scr .addch (j + 1 , i + 1 , self .char )
122122 self .scr .attrset (0 )
123- if not live : self .boring = 0
124- elif s == 2 and live : d [i ,j ] = 1 # Survival
123+ if not live :
124+ self .boring = 0
125+ elif s == 2 and live :
126+ # Survival
127+ d [i , j ] = 1
125128 elif live :
126129 # Death
127- self .scr .addch (j + 1 , i + 1 , ' ' )
130+ self .scr .addch (j + 1 , i + 1 , ' ' )
128131 self .boring = 0
129132 self .state = d
130133 self .scr .refresh ()
@@ -135,16 +138,17 @@ def make_random(self):
135138 for i in range (0 , self .X ):
136139 for j in range (0 , self .Y ):
137140 if random .random () > 0.5 :
138- self .set (j ,i )
141+ self .set (j , i )
139142
140143
141144def erase_menu (stdscr , menu_y ):
142145 "Clear the space where the menu resides"
143146 stdscr .move (menu_y , 0 )
144147 stdscr .clrtoeol ()
145- stdscr .move (menu_y + 1 , 0 )
148+ stdscr .move (menu_y + 1 , 0 )
146149 stdscr .clrtoeol ()
147150
151+
148152def display_menu (stdscr , menu_y ):
149153 "Display the menu of possible keystroke commands"
150154 erase_menu (stdscr , menu_y )
@@ -154,15 +158,16 @@ def display_menu(stdscr, menu_y):
154158 stdscr .attrset (curses .color_pair (1 ))
155159 stdscr .addstr (menu_y , 4 ,
156160 'Use the cursor keys to move, and space or Enter to toggle a cell.' )
157- stdscr .addstr (menu_y + 1 , 4 ,
161+ stdscr .addstr (menu_y + 1 , 4 ,
158162 'E)rase the board, R)andom fill, S)tep once or C)ontinuously, Q)uit' )
159163 stdscr .attrset (0 )
160164
165+
161166def keyloop (stdscr ):
162167 # Clear the screen and display the menu of keys
163168 stdscr .clear ()
164169 stdscr_y , stdscr_x = stdscr .getmaxyx ()
165- menu_y = (stdscr_y - 3 ) - 1
170+ menu_y = (stdscr_y - 3 ) - 1
166171 display_menu (stdscr , menu_y )
167172
168173 # If color, then initialize the color pairs
@@ -179,16 +184,16 @@ def keyloop(stdscr):
179184 curses .mousemask (curses .BUTTON1_CLICKED )
180185
181186 # Allocate a subwindow for the Life board and create the board object
182- subwin = stdscr .subwin (stdscr_y - 3 , stdscr_x , 0 , 0 )
187+ subwin = stdscr .subwin (stdscr_y - 3 , stdscr_x , 0 , 0 )
183188 board = LifeBoard (subwin , char = ord ('*' ))
184189 board .display (update_board = False )
185190
186191 # xpos, ypos are the cursor's position
187- xpos , ypos = board .X // 2 , board .Y // 2
192+ xpos , ypos = board .X // 2 , board .Y // 2
188193
189194 # Main loop:
190195 while True :
191- stdscr .move (1 + ypos , 1 + xpos ) # Move the cursor
196+ stdscr .move (1 + ypos , 1 + xpos ) # Move the cursor
192197 c = stdscr .getch () # Get a keystroke
193198 if 0 < c < 256 :
194199 c = chr (c )
@@ -224,15 +229,21 @@ def keyloop(stdscr):
224229 board .display (update_board = False )
225230 elif c in 'Ss' :
226231 board .display ()
227- else : pass # Ignore incorrect keys
228- elif c == curses .KEY_UP and ypos > 0 : ypos -= 1
229- elif c == curses .KEY_DOWN and ypos < board .Y - 1 : ypos += 1
230- elif c == curses .KEY_LEFT and xpos > 0 : xpos -= 1
231- elif c == curses .KEY_RIGHT and xpos < board .X - 1 : xpos += 1
232+ 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
232243 elif c == curses .KEY_MOUSE :
233244 mouse_id , mouse_x , mouse_y , mouse_z , button_state = curses .getmouse ()
234- if (mouse_x > 0 and mouse_x < board .X + 1 and
235- mouse_y > 0 and mouse_y < board .Y + 1 ):
245+ if (mouse_x > 0 and mouse_x < board .X + 1 and
246+ mouse_y > 0 and mouse_y < board .Y + 1 ):
236247 xpos = mouse_x - 1
237248 ypos = mouse_y - 1
238249 board .toggle (ypos , xpos )
@@ -245,7 +256,7 @@ def keyloop(stdscr):
245256
246257
247258def main (stdscr ):
248- keyloop (stdscr ) # Enter the main loop
259+ keyloop (stdscr ) # Enter the main loop
249260
250261if __name__ == '__main__' :
251262 curses .wrapper (main )
0 commit comments