When I looked at the haskell article, I found something like this. http://qiita.com/tanakh/items/c2e157f0a48667370b0e
The problem is here. http://okajima.air-nifty.com/b/2011/01/2011-ffac.html
I still don't have enough haskell power (from Haskell) so I can't understand it at all. For the time being, haskell is left behind I decided to write it in python because it was a big deal.
Something
It took a lot of time unexpectedly, but it was just about 2 hours ... No, I think I've exceeded it a little. Well no.
I should have written it in c ++.
It is a great inconvenience that you cannot use loops of the for (int i = 0; i <10; ++ i) format.
The width / height of the Puyo box is not solid, so I think it will move as it is even if it spreads vertically and horizontally. Well, I don't know if it can spread vertically and horizontally.
I moved python 2.7.6.
puyo.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
puyo_array = [
    "  GYRR",
    "RYYGYG",
    "GYGYRR",
    "RYGYRG",
    "YGYRYG",
    "GYRYRG",
    "YGYRYR",
    "YGYRYR",
    "YRRGRG",
    "RYGYGG",
    "GRYGYR",
    "GRYGYR",
    "GRYGYR",
]
#Since the python character string cannot be rewritten by specifying index, it is converted to bytearray.
puyo = [ bytearray(line) for line in puyo_array ]
def show():
    print "-" * 8
    for x in puyo:
        print "|{}|".format(x)
    print "-" * 8
    print
def erase():
    u'''Erase 4 or more connected'''
    class Way:
        u'''Which one did you search for?'''
        UP = 1
        DOWN = 2
        LEFT = 3
        RIGHT = 4
    def mark(dx, dy, p, dst = None):
        u'''Follow the same color adjacent to each other'''
        if dx < 0 or dy < 0:
            return
        try:
            if puyo[dy][dx] == p:
                points.append((dx, dy))
                #If you search in the direction of entry, it will recurse infinitely.
                if dst != Way.RIGHT:
                    mark(dx+1, dy, p, Way.LEFT)
                if dst != Way.UP:
                    mark(dx, dy+1, p, Way.DOWN)
                if dst != Way.LEFT:
                    mark(dx-1, dy, p, Way.RIGHT)
                if dst != Way.DOWN:
                    mark(dx, dy-1, p, Way.UP)
        except IndexError as e:
            pass
    for y, line in enumerate(puyo):
        for x, p in enumerate(line):
            if chr(p) != " ":
                points = []
                mark(x, y, puyo[y][x])
                if len(points) >= 4:    #4 or more connections
                    for x, y in points:
                        puyo[y][x] = " " #Erase
def drop():
    u'''Drop the floating'''
    height = len(puyo)
    assert(height > 0)
    width = len(puyo[0])
    is_zenkeshi = True
    for x in range(width):
        for y in range(0, height)[::-1]:    #Mock from below
            if chr(puyo[y][x]) == " ":
                for y2 in range(0, y+1)[::-1]:
                    if chr(puyo[y2][x]) != " ":
                        puyo[y][x] = puyo[y2][x]
                        puyo[y2][x] = " "
                        break
            else:
                is_zenkeshi = False
    #True if everything disappears
    return is_zenkeshi
if __name__ == '__main__':
    import itertools
    for i in itertools.count():
        print i
        show()
        erase()
        if drop():
            break
    
    print "finish!!"
    show()
Recommended Posts