Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import curses
- def conways(input_state):
- output_state = list(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
- if y == 0:
- yu = curses.LINES-1
- if x == curses.COLS-1:
- xr = 0
- if x == 0:
- xl = curses.COLS-1
- """if input_state[y][x] == 1:
- win.addstr(y, x, str(input_state[y][x]))
- win.addstr(yu, x, "A")
- win.addstr(yl, x, "B")
- win.addstr(y, xl, "C")
- win.addstr(y, xr, "D")
- win.refresh()
- win.getch()"""
- if input_state[yl][x] == 1:
- neighbours += 1
- if input_state[yu][x] == 1:
- neighbours += 1
- if input_state[y][xr] == 1:
- neighbours += 1
- if input_state[y][xl] == 1:
- neighbours += 1
- return 0
- if input_state[y][x] == 1:
- if neighbours in range(2,4):
- output_state[y][x] = 1
- 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)]
- global win
- win = curses.newwin(curses.LINES, curses.COLS, 0, 0)
- 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:
- win.move(win.getyx()[0]-1, win.getyx()[1])
- win.refresh()
- elif c == curses.KEY_DOWN:
- win.move(win.getyx()[0]+1, win.getyx()[1])
- win.refresh()
- elif c == curses.KEY_LEFT:
- win.move(win.getyx()[0], win.getyx()[1]-1)
- win.refresh()
- elif c == curses.KEY_RIGHT:
- win.move(win.getyx()[0], win.getyx()[1]+1)
- win.refresh()
- elif c == ord('q'):
- curses.endwin()
- return 0
- elif c == ord('\n'):
- break
- """ win.move(0,0)
- for y in range(curses.LINES):
- for x in range(curses.COLS):
- win.insstr(y,x,str(current_state[y][x]))
- win.refresh()
- stdscr.getch()"""
- while True:
- c = stdscr.getch()
- if c == ord('\n'):
- 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:
- #this is never true ???
- win.insstr(y, x, "X")
- else:
- win.insstr(y, x, " ")
- win.refresh()
- elif c == ord('q'):
- curses.endwin()
- return 0
- curses.wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement