python - Why is my Conway's Game of Life program not working properly? -


i came across john conway's game of life , decided code in python. did, doesn't work should.

for example, have 3 live cells in row horizontally. if working correctly, oscillate 3 live cells in row 3 live cells in column, not that.

my current code this:

# ("o" == dead cell, "x" == live cell) # generation 1: ooo xxx ooo  # generation 2: ooo ooo ooo 

what should this:

# (o == dead cell, x == live cell) # generation 1: ooo xxx ooo  # generation 2: oxo oxo oxo 

i not know in area code gremlin lives posted whole code below.

import os import time  # rules of life #   1.  live cell less 2 live neighbours dies, if caused under-population. #   2.  live cell 2 or 3 live neighbours lives on next generation. #   3.  live cell more 3 live neighbours dies, if overcrowding. #   4.  dead cell 3 live neighbours becomes live cell, if reproduction.  def seed():     board[12][11] = "x"     board[12][12] = "x"     board[12][13] = "x"  def display():     row in board:         print("".join(row))  def update():     row in range(0, len(board)):         column in range(0, len(board[row])):             neighbors = 0             r in range(-1, 2):                 c in range(-1, 2):                     if r != 0 , c != 0:                         try:                             if board[r][c] == "x":                                 neighbors += 1                          except indexerror:                             pass              if board[row][column] == " ": # dead cell                 if neighbors == 3: # rule 4.                     temp_board[row][column] = "x"              elif board[row][column] == "x": # live cell                 if neighbors < 2: # rule 1.                     temp_board[row][column] = " "                  elif neighbors == 2 or neighbors == 3: # rule 2.                     pass                  elif neighbors > 3: # rule 3.                     temp_board[row][column] = " "  height = 25 width = 25 board = [[" " column in range(0, width)] row in range(0, height)] temp_board = list(board) seed() while true:     display()     update()     board = list(temp_board)     time.sleep(1)     os.system("cls") 

what missing?

i found 3 mistakes, , there might more. here summary of found. explain these in more detail below:

  1. you need include offset when checking neighbor cells.
  2. you need include surrounding cells when checking neighbors, not cells diagonal center cell.
  3. you need deepcopy of board.

offsets necessary

you start setup code handle offsets here:

for r in range(-1, 2):     c in range(-1, 2): 

however, never add offsets original values. essentially, checking top few spaces of grid. so, need change if board[r][c] == "x" if board[row+ r][column + c] == "x".

include surrounding cells

you made code check surrounding cells (the loops shown above), , knew needed exempt cell being tested. used code exclude cell itself:

if r != 0 , c != 0 

however, condition fails whenever either of offset variables, r , c, zero. happens above, below, right, , left of cell. need change condition this:

if (r != 0) or (c != 0)  # parenthesis added readability, not functionality 

this way, if either of offsets nonzero, check corresponding cell.

use deepcopy

assignments temp_board = list(board) make new reference same data. essentially, same assigning name same variable. so, when updating board in update function, not updating temporary copy of board, rather board through alternate name. then, when subsequent tests made, state of board has changed.

to deepcopy in python, need add import copy file, , replace current copy attempts code temp_board = copy.deepcopy(board).


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -