package main

type Cell struct {
	X int
	Y int
	State int
}

func (c *Cell) Display() rune {
	if c.State == 1 {
		return '\u2588'
	} else {
		return ' '
	}
}

func (c *Cell) Update(g *Game) int{
	h, w := g.Screen.Size()
	cs := g.Cells
	n := 0
	if c.X == 0 {
		if c.Y == 0 {
			n =	cs[c.X][c.Y+1].State +
				cs[c.X+1][c.Y].State +
				cs[c.X+1][c.Y+1].State
		} else if c.Y < w-1 {
			n =	cs[c.X][c.Y+1].State + 
				cs[c.X+1][c.Y].State + 
				cs[c.X+1][c.Y+1].State +
				cs[c.X][c.Y-1].State +
				cs[c.X+1][c.Y-1].State 
		} else {
			n =	cs[c.X+1][c.Y].State + 
				cs[c.X][c.Y-1].State +
				cs[c.X+1][c.Y-1].State 
		}
	} else if c.X == h-1 {
		if c.Y == 0 {
			n =	cs[c.X][c.Y+1].State +
				cs[c.X-1][c.Y].State +
				cs[c.X-1][c.Y+1].State
		} else if c.Y < w-1 {
			n =	cs[c.X][c.Y+1].State + 
				cs[c.X-1][c.Y].State + 
				cs[c.X-1][c.Y+1].State +
				cs[c.X][c.Y-1].State +
				cs[c.X-1][c.Y-1].State 
		} else {
			n =	cs[c.X-1][c.Y].State + 
				cs[c.X][c.Y-1].State +
				cs[c.X-1][c.Y-1].State 
		}
	} else if c.Y == 0 {
		n =	cs[c.X-1][c.Y].State +
			cs[c.X-1][c.Y+1].State +
			cs[c.X][c.Y+1].State +
			cs[c.X+1][c.Y+1].State +
			cs[c.X+1][c.Y].State
	} else if c.Y == w-1 {
		n =	cs[c.X-1][c.Y].State +
			cs[c.X-1][c.Y-1].State +
			cs[c.X][c.Y-1].State +
			cs[c.X+1][c.Y-1].State +
			cs[c.X+1][c.Y].State
	} else {
		n =	cs[c.X-1][c.Y-1].State +
			cs[c.X-1][c.Y].State +
			cs[c.X-1][c.Y+1].State +
			cs[c.X][c.Y-1].State +
			cs[c.X][c.Y+1].State +
			cs[c.X+1][c.Y-1].State +
			cs[c.X+1][c.Y].State +
			cs[c.X+1][c.Y+1].State 
	}
	
	if c.State == 1 {
		if n >= 2 && n < 4 {
			return 1
		} else {
			return 0
		}
	} else if c.State == 0 {
		if n == 3 {
			return 1
		}
		return 0
	} else {
		return 0
	}
}
