We haven't had one in a while so let's have a new coding contest. I'm open to suggestions for the challenge, but if we don't have any good suggestions we will default to something stupid like "implement cowsay in erlang shitty language X".
Name:
Anonymous2009-11-29 21:04
Fully functional Tetris with a touch of Fibonacci. My first FIOC. Shouldn't be too hard to integrate a Pong if you're into that sort of thing.
#!/usr/bin/python
from curses import *
from curses.wrapper import *
import time
import random
def squaresOccupiedBy(self, (type, rotation, (x, y))):
shape = blockShapes[type][rotation]
return [(x+s[0], y+s[1]) for s in shape]
def blockFits(self, newBlock):
for (x,y) in self.squaresOccupiedBy(newBlock):
if x < 0 or x >= self.ncols or y < 0 or y >= self.nrows or self.board[y][x]:
return False
return True
def placeBlock(self, newBlock, allowClear=True):
for (x,y) in self.squaresOccupiedBy(newBlock):
self.board[y][x] = newBlock[0]+1
if allowClear:
self.clearFullRows()
def clearFullRows(self):
ncleared = 0
runStart = None
def clear(start, until):
n = until - start
self.board = [[0]*self.ncols for y in xrange(n)] \
+ self.board[0:start] + self.board[until:]
return n
for y in xrange(self.nrows):
if 0 in self.board[y]:
if runStart != None:
ncleared += clear(runStart, y)
runStart = None
elif runStart == None:
runStart = y
if runStart != None:
ncleared += clear(runStart, self.nrows)
if ncleared:
self.score += 50 * ncleared * (ncleared + 1)
self.maybeLevelUp()
def addGarbageRows(self, count):
self.board = self.board[count:]
for row in xrange(count):
self.board.append([8*random.randrange(0, 2) for x in xrange(self.ncols)])
def drawBoard(self, window):
window.border(0, 0, 32)
window.addch(0, 0, 32)
window.addch(0, 21, 32)
for y in xrange(self.nrows):
for x in xrange(self.ncols):
window.addstr(1 + y, 1 + 2*x, boardSymbols[self.board[y][x]])
if self.falling:
for (x,y) in self.squaresOccupiedBy(self.falling):
if x >= 0 and x < self.ncols and y >= 0 and y < self.nrows:
window.addstr(1 + y, 1 + 2*x, "[]")
def drawPreview(self, window, x0, y0):
for y in xrange(y0 + 1, y0 + 3):
window.addstr(y, x0, " ")
if self.preview != None:
for (x,y) in blockShapes[self.preview][0]:
window.addstr(y0 + y, x0 + 2*x, "[]")