Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import curses
- import copy
- def conways(input_state):
- output_state = copy.deepcopy(input_state)
- for y in range (0, curses.LINES):
- for x in range(0, curses.COLS):
- neighbours = 0
- yu = y-1
- yl = y+1
- xl = x-1
- xr = x+1
- if y == curses.LINES-1:
- yl = 0
- elif y == 0:
- yu = curses.LINES-1
- if x == curses.COLS-1:
- xr = 0
- elif x == 0:
- xl = curses.COLS-1
- if input_state[yl][x] > 0:
- neighbours += 1
- if input_state[yu][x] > 0:
- neighbours += 1
- if input_state[y][xr] > 0:
- neighbours += 1
- if input_state[y][xl] > 0:
- neighbours += 1
- if input_state[yu][xl] > 0:
- neighbours += 1
- if input_state[yl][xl] > 0:
- neighbours += 1
- if input_state[yu][xr] > 0:
- neighbours += 1
- if input_state[yl][xr] > 0:
- neighbours += 1
- if input_state[y][x] > 0:
- if neighbours in range(2,4):
- output_state[y][x] = 2
- else:
- output_state[y][x] = 0
- else:
- if neighbours == 3:
- output_state[y][x] = 1
- else:
- output_state[y][x] = 0
- return output_state
- def main(stdscr):
- curses.raw()
- curses.curs_set(2)
- current_state = [[0 for x in range(0, curses.COLS)] for y in range(0, curses.LINES)]
- win = curses.newwin(curses.LINES, curses.COLS, 0, 0)
- curses.halfdelay(1)
- while True:
- c = stdscr.getch()
- if c == ord(' '):
- win.addstr("X")
- win.move(win.getyx()[0], win.getyx()[1]-1)
- current_state[win.getyx()[0]][win.getyx()[1]] = 1
- win.refresh()
- if c == curses.KEY_BACKSPACE:
- win.addstr(" ")
- win.move(win.getyx()[0],win.getyx()[1]-1)
- current_state[win.getyx()[0]][win.getyx()[1]] = 0
- win.refresh()
- elif c == curses.KEY_UP:
- if win.getyx()[0] == 0:
- win.move(curses.LINES-1, win.getyx()[1])
- else:
- win.move(win.getyx()[0]-1, win.getyx()[1])
- win.refresh()
- elif c == curses.KEY_DOWN:
- if win.getyx()[0] == curses.LINES-1:
- win.move(0, win.getyx()[1])
- else:
- win.move(win.getyx()[0]+1, win.getyx()[1])
- win.refresh()
- elif c == curses.KEY_LEFT:
- if win.getyx()[1] == 0:
- win.move(win.getyx()[0], curses.COLS-1)
- else:
- win.move(win.getyx()[0], win.getyx()[1]-1)
- win.refresh()
- elif c == curses.KEY_RIGHT:
- if win.getyx()[1] == curses.COLS-1:
- win.move(win.getyx()[0], 0)
- else:
- win.move(win.getyx()[0], win.getyx()[1]+1)
- win.refresh()
- elif c == ord('q'):
- curses.endwin()
- return 0
- elif c == ord('\n'):
- currenty = win.getyx()[0]
- currentx = win.getyx()[1]
- current_state = list(conways(current_state))
- for y in range(0,curses.LINES):
- for x in range(0,curses.COLS):
- if current_state[y][x] == 1:
- win.insstr(y, x, "X")
- elif current_state[y][x] == 2:
- win.insstr(y,x, "X", curses.A_BOLD)
- else:
- win.insstr(y, x, " ")
- win.move(currenty, currentx)
- win.refresh()
- elif c == ord('p'):
- curses.curs_set(0)
- currenty = win.getyx()[0]
- currentx = win.getyx()[1]
- while True:
- d = stdscr.getch()
- if d != ord('p'):
- if d == ord('q'):
- curses.endwin()
- return 0
- current_state = list(conways(current_state))
- for y in range(0,curses.LINES):
- for x in range(0,curses.COLS):
- if current_state[y][x] == 1:
- win.insstr(y, x, "X")
- elif current_state[y][x] == 2:
- win.insstr(y,x, "X", curses.A_BOLD)
- else:
- win.insstr(y, x, " ")
- else:
- break
- win.move(currenty, currentx)
- win.refresh()
- curses.curs_set(2)
- curses.wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement