Puyo Puyo in python

Puyo Puyo in python

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

Puyo Puyo in python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python
Logistic distribution in Python
Daily AtCoder # 7 in Python
LU decomposition in Python
One liner in Python