You are not logged in.

#1 2010-02-15 02:20:59

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

peagol: C implementation of Conway's Game of Life

I was unable to find a simple, fast Life implementation, so I wrote my own. Please tell me what you think.

You absolutely must read the README file in the source tree.

AUR package: http://aur.archlinux.org/packages.php?ID=34640
git view: http://peasantoid.org/git/peagol
git clone: git://peasantoid.org/peagol

(And yes, I do name all my projects after myself.)

Last edited by Peasantoid (2010-02-15 02:21:38)

Offline

#2 2010-02-15 06:30:51

tomd123
Developer
Registered: 2008-08-12
Posts: 565

Re: peagol: C implementation of Conway's Game of Life

Very nice!
I guess you really do need to read the README smile

Here's one I made some time ago because I wanted one in a terminal.

#!/usr/bin/python

import copy
import random
import time

random.seed()

class gameoflife:
    def __init__(self, width, height):
        self.board = []
        for i in range(width):
            col = []
            for j in range(height):
                col.append(random.randint(0, 1))
            self.board.append(col)
        self.width = width
        self.height = height
        self.futureboard = copy.deepcopy(self.board)

    def checkpoint(self, x, y):
        if x < 0 or x >= self.width or y < 0 or y >= self.height:
            return 0
        return self.board[x][y]

    # returns the amount of neighbors that are checked given a point
    def neighbors(self, x, y):
        xlist = [x - 1, x, x + 1]
        ylist = [y - 1, y, y + 1]
        total = 0
        for i in range(len(xlist)):
            for j in range(len(ylist)):
                if xlist[i] != x or ylist[j] != y:
                    total += self.checkpoint(xlist[i], ylist[j])
        return total

    def run(self):
        for i in range(self.height):
            for j in range(self.width):
                numNeighbors = self.neighbors(j, i)
                self.futureboard[j][i] = 0
                if numNeighbors == 3:
                    self.futureboard[j][i] = 1
                if numNeighbors == 2 and self.board[j][i] == 1:
                    self.futureboard[j][i] = 1

        # print board
        for i in range(self.height):
            for j in range(self.width):
                #print self.board[j][i],
                if self.board[j][i] == 0:
                    print 0,
                else:
                    print ' ',
            print

        self.board = copy.deepcopy(self.futureboard)

test = gameoflife(90, 60)
while 1 == 1:
    test.run()
    print
    #time.sleep(0.2)

Offline

#3 2010-02-15 06:43:31

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: peagol: C implementation of Conway's Game of Life

tomd123 wrote:

Very nice!
I guess you really do need to read the README smile

Yep. I wanted to make the interface as simple as possible. Would've written the thing in Python, but it ate all my CPU time so I had to switch to something faster.

// Nice script. Not nearly as friendly, though. wink

Last edited by Peasantoid (2010-02-15 06:45:29)

Offline

Board footer

Powered by FluxBB