Advertisement
Guest User

life1.py

a guest
Sep 10th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | None | 0 0
  1. import curses
  2.  
  3. def conways(input_state):
  4.     output_state = list(input_state)
  5.     for y in range (0, curses.LINES):
  6.         for x in range(0, curses.COLS):
  7.             neighbours = 0
  8.             yu = y-1
  9.             yl = y+1
  10.             xl = x-1
  11.             xr = x+1
  12.             if y == curses.LINES-1:
  13.                 yl = 0
  14.             if y == 0:
  15.                 yu = curses.LINES-1
  16.             if x == curses.COLS-1:
  17.                 xr = 0
  18.             if x == 0:
  19.                 xl = curses.COLS-1
  20.             """if input_state[y][x] == 1:
  21.                win.addstr(y, x, str(input_state[y][x]))
  22.                win.addstr(yu, x, "A")
  23.                win.addstr(yl, x, "B")
  24.                win.addstr(y, xl, "C")
  25.                win.addstr(y, xr, "D")
  26.            win.refresh()
  27.            win.getch()"""
  28.             if input_state[yl][x] == 1:
  29.                 neighbours += 1
  30.             if input_state[yu][x] == 1:
  31.                 neighbours += 1
  32.             if input_state[y][xr] == 1:
  33.                 neighbours += 1
  34.             if input_state[y][xl] == 1:
  35.                 neighbours += 1
  36.                 return 0
  37.             if input_state[y][x] == 1:
  38.                 if neighbours in range(2,4):
  39.                     output_state[y][x] = 1
  40.                 else:
  41.                     output_state[y][x] = 0
  42.             else:
  43.                 if neighbours == 3:
  44.                     output_state[y][x] = 1
  45.                 else:
  46.                     output_state[y][x] = 0
  47.     return output_state
  48.  
  49. def main(stdscr):
  50.    
  51.     curses.raw()
  52.     curses.curs_set(2)
  53.  
  54.     current_state = [[0 for x in range(0, curses.COLS)] for y in range(0, curses.LINES)]
  55.     global win
  56.     win = curses.newwin(curses.LINES, curses.COLS, 0, 0)
  57.     while True:
  58.         c = stdscr.getch()
  59.         if c == ord(' '):
  60.             win.addstr("X")
  61.             win.move(win.getyx()[0], win.getyx()[1]-1)
  62.             current_state[win.getyx()[0]][win.getyx()[1]] = 1
  63.             win.refresh()
  64.         if c == curses.KEY_BACKSPACE:
  65.             win.addstr(" ")
  66.             win.move(win.getyx()[0],win.getyx()[1]-1)
  67.             current_state[win.getyx()[0]][win.getyx()[1]] = 0
  68.             win.refresh()
  69.         elif c == curses.KEY_UP:
  70.             win.move(win.getyx()[0]-1, win.getyx()[1])
  71.             win.refresh()
  72.         elif c == curses.KEY_DOWN:
  73.             win.move(win.getyx()[0]+1, win.getyx()[1])
  74.             win.refresh()
  75.         elif c == curses.KEY_LEFT:
  76.             win.move(win.getyx()[0], win.getyx()[1]-1)
  77.             win.refresh()
  78.         elif c == curses.KEY_RIGHT:
  79.             win.move(win.getyx()[0], win.getyx()[1]+1)
  80.             win.refresh()
  81.         elif c == ord('q'):
  82.             curses.endwin()
  83.             return 0
  84.         elif c == ord('\n'):
  85.             break
  86.    
  87.     """    win.move(0,0)
  88.    for y in range(curses.LINES):
  89.        for x in range(curses.COLS):
  90.            win.insstr(y,x,str(current_state[y][x]))
  91.    win.refresh()
  92.    stdscr.getch()"""
  93.     while True:
  94.         c = stdscr.getch()
  95.         if c == ord('\n'):
  96.             current_state = list(conways(current_state))
  97.             for y in range(0,curses.LINES):
  98.                 for x in range(0,curses.COLS):
  99.                     if current_state[y][x] == 1:
  100. #this is never true ???
  101.                         win.insstr(y, x, "X")
  102.                     else:
  103.                         win.insstr(y, x, " ")
  104.  
  105.             win.refresh()
  106.         elif c == ord('q'):
  107.             curses.endwin()
  108.             return 0
  109.  
  110. curses.wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement