onsdag 22 juli 2009

source:
import random

def fall_down(playing_field):

    anything_fell_down = False

    #Make a temporary copy for checking for empty spaces
    #(modifying the list we use to check for empty spaces will result in errors)
    tmp_list = playing_field[:]
    
    for i, row in enumerate(playing_field):
        for j, square in enumerate(row):
            #Check that we're not at the bottom
            if i < len(playing_field) - 1:
                #If we're not at the bottom, make stuff fall down
                if tmp_list[i + 1][j] == 0:
                    playing_field[i + 1][j] = playing_field[i][j]
                    playing_field[i][j] = 0
                    anything_fell_down = True

    return anything_fell_down


def fall_down2(playing_field):
    anything_fell_down = False

    for i in range(len(playing_field[0])):
        #Keep track of where the lowest hole is
        last_hole = 0
        for j in range(len(playing_field)):

            #Look for holes
            if j < len(playing_field) - 1:
                if playing_field[j][i] != 0 and playing_field[j + 1][i] == 0:
                    #print "hole at ", i, j + 1
                    anything_fell_down = True
                    last_hole = j + 1
       
        #Make what's above the hole fall down one notch             
        if last_hole > 0:
            for m in range(last_hole):
                playing_field[last_hole - m][i] = playing_field[last_hole - m - 1][i]
                #print "moved [", last_hole - m - 1, "][", i, "] to [", last_hole - m , "][", i, "]"
            #The first row in the column should always be "0" if stuff fell down, so set it:
            playing_field[0][i] = 0

    return anything_fell_down

                    
def move(direction, row, playing_field):
    if direction > 0:
        playing_field[row] = playing_field[row][-1:] + playing_field[row][:-1]
    else:
        playing_field[row] = playing_field[row][1:] + playing_field[row][0:1]


def find_lines(playing_field):

    #Find horizontal lines
    for row in playing_field:
        for i in range(len(row)):
            counter = 0
            while i + counter < len(row) and row[i] != 0 and row[i] == row[i + counter]:
                print "compare ", row[i], " to ", row[i + counter]
                counter += 1
            print counter


#initalize playing field
playing_field = []
dimensions = (8, 6)

for i in range(dimensions[0]):
    playing_field.append([None] * dimensions[1])


#populate playing field
for row in playing_field:
    for index, square in enumerate(row):
        if random.random() > 0.5:
            row[index] = random.randint(1, 4)
        else:
            row[index] = 0

print playing_field

print "\n"
for row in playing_field:
    print row

##fall_down2(playing_field)
##
##print "\n"
##for row in playing_field:
##    print row

##print "\n"
##for row in playing_field:
##    print row


while fall_down2(playing_field):
    print "stuff is falling"

print "\n"
for row in playing_field:
    print row

print "\nLet's play!"
print "Type which line you want to move and in which direction (in that order). Use 'l' and 'r' for moving left / right respectively"
print "Example: '5 l' moves line five to the left"

while(True):

    
    input_string = raw_input().split()

    direction = 0
    row = input_string[0]
    
    if input_string[1] == "l":
        direction = -1
    else:
        direction = 1

    move(direction, int(row), playing_field)

    print "\n"
    for row in playing_field:
        print row

    find_lines(playing_field)
    


Inga kommentarer:

Skicka en kommentar