Skip to content

Instantly share code, notes, and snippets.

@ehabkost
Created September 18, 2010 15:09
Show Gist options
  • Save ehabkost/585739 to your computer and use it in GitHub Desktop.
Save ehabkost/585739 to your computer and use it in GitHub Desktop.
#/usr/bin/env python
# Game of Death
# quick and dirty implemenation of http://spikedmath.com/299.html
# Copyright (c) 2010 Eduardo Habkost <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import time, sys
import random
import curses
WIDTH = int(sys.argv[1])
HEIGHT = int(sys.argv[2])
colors = {'Z':curses.COLOR_RED, 'H':curses.COLOR_GREEN, 'D':curses.COLOR_BLUE}
def allpos():
for i in range(HEIGHT):
for j in range(WIDTH):
yield i,j
def neighs(pos):
i,j = pos
for i2 in i-1,i,i+1:
for j2 in j-1,j,j+1:
if i2 <> i or j2 <> j:
yield i2,j2
def redraw(screen, m):
for pos in allpos():
i,j = pos
item = m.get(pos, ' ')
screen.addstr(i, j, item, colors.get(item, 0))
screen.refresh()
def init(matrix):
for i,j in allpos():
matrix[(i,j)] = random.choice(['Z', 'H', 'D'])
def step(m):
oldm = m.copy()
for pos in allpos():
cur = oldm[pos]
if cur == 'H':
new = 'D'
elif cur == 'D':
new = 'Z'
elif cur == 'Z':
new = 'Z'
humans = 0
for neigh in neighs(pos):
if oldm.get(neigh, ' ') == 'H':
humans += 1
if humans == 2:
new = 'H'
m[pos] = new
def run(screen, m):
pair = 0
for item,color in colors.items():
pair +=1
curses.init_pair(pair, colors[item], curses.COLOR_BLACK)
colors[item] = curses.color_pair(pair)
init(m)
while True:
redraw(screen, m)
#time.sleep(0.2)
step(m)
curses.wrapper(run, {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment