-4
import pygame
import time
import sys

HEIGHT = 100
WIDTH = 100

SCALE = 10
SCREEN = pygame.display.set_mode((WIDTH*SCALE, HEIGHT*SCALE))

def create_blank():
    """Creates a HEIGHT x WIDTH list containing all zero's. The inner lists 
    will all be WIDTH long and the outer list will HEIGHT long."""
    #TODO
    board = []
    for c in range(HEIGHT):
        board.append([0]*WIDTH)
    return board

def get_cell(board, x, y):
    """Returns the game board at position x,y , this is 1 if the cell at that 
    position is alive and 0 if the cell is dead. If the x or y parameters are
    out of bounds (not valid coordinates), the function returns 0."""
    #TODO
    if board[x][y] == 1:
        return 1
    elif x < 0 or x > WIDTH:
        return 0
    elif y < 0 or y > HEIGHT:
        return 0
    else:
        return 0

def count_neighbours(board, x, y):
    """Counts the number of alive neighbours around the position x,y , including 
    diagonal neighbours. Does not include the cell itself."""
    #TODO
    count = 0
    for c in range(-1, 2):
        for k in range(-1, 2):
            if c == 0 and k == 0:
                count = count
            else:
                count += get_cell(board, x+c, y+k)
    return count

def update(board):
    """Creates a new board copy and applies the game rules to each cell using 
    the old board. Does not modify the old board. Returns the new board."""
    #TODO
    import copy
    new_board = copy.deepcopy(board)
    for x in range(WIDTH):
        for y in range(HEIGHT):
            if count_neighbours(board, x, y) < 2:
                new_board[x][y] = 0
            elif count_neighbours(board, x, y) == 2:
                new_board[x][y] = new_board[x][y]
            elif count_neighbours(board, x, y) == 3:
                new_board[x][y] = 1
            else:
                new_board[x][y] = 0
    return new_board
Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
Johnny
  • 1
  • 1
  • 1
    Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Morgan Thrapp Oct 15 '15 at 20:01
  • Why do you think your update function is wrong? what is it doing? please add some details – SirParselot Oct 15 '15 at 20:01
  • I ran all the other codes separately, and they seem to work fine. I'm a beginner with python and for most of the time i don't really know what i'm doing. It keeps giving me all sorts of errors depending on how i change the code. I really don't know what's wrong. – Johnny Oct 15 '15 at 20:10

1 Answers1

0

Edit: If there are errors being reported by the interpreter then that please share.

The first issue I see with your update routine that you are importing a module within this routine. It is again Python style guide (https://www.python.org/dev/peps/pep-0008/#imports).

The next suspicious code I see is:

new_board[x][y] = new_board[x][y]

I dont understand the purpose of the above self-assignment. Think about why you are doing that and maybe you will realize something else isn't right.

Lastly, consider the rules of the game: (https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life#Rules):

  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by over-population.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

In your update routine, you dont appear to consider if the cell in the old board is alive or dead, which is essential to picking the state of the cell in the new board. I would suggest structuring the if statement in the update routine off the rules of the game. Also, to keep it straight, include the actual rules as comments next to the corresponding code.

Goood luck!

harymitchell
  • 155
  • 7