Advertisement
Guest User

Untitled

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