#!/usr/local/env python

import random as rand
import curses
import time
from datetime import datetime

stdscr = 0
n_win = 0
width = 16
height = 16
mat_game = []

# create 1D array containing game cells
def init_mat(matrix, width, height):
	matrix = [(1 if rand.randint(0, 100) < 20 else 0) for x in range(width * height)]
	return matrix
	
def draw_grid(n_win, matrix, width, height):
	count = 0
	n_win.clear()
	for i in matrix:
		count += 1
		n_win.addch(' ')
		n_win.addch(str(i) if i == 1 else 'o')
#		print (str(i) if i == 1 else 'o'),
		n_win.addch(' ')
		if count % height == 0:
			n_win.addch('\n')
#			print '\n'
	n_win.refresh()
		
def calc_grid(matrix, width, height):


	for i in range(width * height):
		
		n_cells = 0
		
		# check top neighbor
		if int(i / height) > 0:
			if matrix[int((i / height - 1) + (i % height))] == 1:
				n_cells += 1
			
			# check top left neighbor
			if int(i % height) > 0 and matrix[int((i / height - 1) + ((i % height) - 1))] == 1:
				n_cells += 1	
			# check top right neighbor
			if int(i % height) < width - 1 and matrix[int((i / height - 1) + ((i % height) + 1))] == 1:
				n_cells += 1
						
		# check bottom neighbor
		if int(i / height) < width - 1:
			if matrix[int((i / height + 1) + (i % height))] == 1:
				n_cells += 1
			
			# check bottom left neighbor
			if int(i % height) > 0 and matrix[int((i / height + 1) + ((i % height) - 1))] == 1:
				n_cells += 1			
			# check bottom right neighbor
			if int(i % height) < height - 1 and matrix[int((i / height + 1) + ((i % height) + 1))] == 1:
				n_cells += 1

		# check left neighbor
		if int(i % height) > 0:
			if matrix[int((i / height) + ((i % height) - 1))] == 1:
				n_cells += 1	
		# check right neighbor
		if int(i % height) < height - 1:
			if matrix[int((i / height) + ((i % height) + 1))] == 1:
				n_cells += 1


		if matrix[int((i / height) + (i % height))] == 0 and n_cells == 3:
			matrix[int((i / height) + (i % height))] = 1
		if n_cells < 2 or n_cells > 3:
			matrix[int((i / height) + (i % height))] = 0

		#matrix[int((i / height) + ((i % height) + 1))] = 1
	
	return matrix

# init curses
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)
n_win = curses.newwin(50, 50, 0, 0)

m_game = init_mat(mat_game, width, height)
while(True):
	m_game = calc_grid(m_game, width, height)
	draw_grid(n_win, m_game, width, height)
	time.sleep(0.1)

# end curses
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()

@@ -277,10 +277,10 @@
 ) %3C 
-20
+15
  els
@@ -472,51 +472,11 @@
 dch(
-str(i) if i == 1 else 'o')%0A#%09%09print (str(i)
+'x'
  if 
@@ -492,12 +492,11 @@
 se '
-o
+ 
 ')
-,
 %0A%09%09n
@@ -562,23 +562,8 @@
 n')%0A
-#%09%09%09print '%5Cn'%0A
 %09n_w
@@ -617,17 +617,16 @@
 ight):%0A%0A
-%0A
 %09for i i
@@ -729,32 +729,33 @@
 %09if matrix%5Bint((
+(
 i / height - 1) 
@@ -749,24 +749,34 @@
  height - 1)
+ * height)
  + (i %25 heig
@@ -871,32 +871,33 @@
 and matrix%5Bint((
+(
 i / height - 1) 
@@ -887,32 +887,42 @@
 (i / height - 1)
+ * height)
  + ((i %25 height)
@@ -1025,32 +1025,33 @@
 and matrix%5Bint((
+(
 i / height - 1) 
@@ -1049,16 +1049,26 @@
 ght - 1)
+ * height)
  + ((i %25
@@ -1183,32 +1183,33 @@
 %09if matrix%5Bint((
+(
 i / height + 1) 
@@ -1203,24 +1203,34 @@
  height + 1)
+ * height)
  + (i %25 heig
@@ -1328,32 +1328,33 @@
 and matrix%5Bint((
+(
 i / height + 1) 
@@ -1344,32 +1344,42 @@
 (i / height + 1)
+ * height)
  + ((i %25 height)
@@ -1488,32 +1488,33 @@
 and matrix%5Bint((
+(
 i / height + 1) 
@@ -1512,16 +1512,26 @@
 ght + 1)
+ * height)
  + ((i %25
@@ -1633,32 +1633,41 @@
  matrix%5Bint((i /
+ height *
  height) + ((i %25
@@ -1779,32 +1779,41 @@
  matrix%5Bint((i /
+ height *
  height) + ((i %25
@@ -1870,39 +1870,8 @@
 ix%5Bi
-nt((i / height) + (i %25 height))
 %5D ==
@@ -1906,39 +1906,8 @@
 ix%5Bi
-nt((i / height) + (i %25 height))
 %5D = 
@@ -1956,101 +1956,14 @@
 ix%5Bi
-nt((i / height) + (i %25 height))%5D = 0%0A%0A%09%09#matrix%5Bint((i / height) + ((i %25 height) + 1))%5D = 1%0A%09
+%5D = 0%0A
 %0A%09re
@@ -2337,8 +2337,42 @@
 ndwin()%0A
+%0Aif __name == '__main__':%0A%09main()%0A

fix calc_grid