6 Ball puzzle implemented in python

Introduction

When I was thinking of implementing a 6-ball puzzle on python, the shit code was completed. I'm tired of it, so let's release it to the sea on the net and finish ...

Method of operation a: Right 60deg rotation b: Left 60deg rotation j: Move left l: Move right k: Start falling

Reference video https://www.youtube.com/watch?v=Q6As1gRT6fE&t=660s

I feel like I'm doing a motion simulation, but it's a little difficult, so I don't think I can make the exact same thing.

algorithm

1.png Define four directions a, b, c, d for the ball.

conditions

  1. The ball only moves to $ b, c $
  2. There is no ball in $ a, b $ to move to $ b $
  3. To move to $ c $, there is no ball in $ c, d $
  4. If you can move to either $ b or c $, it doesn't matter if it is in $ a or d $, the direction of movement is $ b, c $ random

2.png This situation is a little difficult.

Search direction Check if you can move from the lower layer, and move until you can not move 3.png

data structure

It's a little tricky because the column locations are off by 0.5 at the row locations. Number as follows so as not to bother you

4.png

Red, blue, yellow, purple, green, sky 6 kinds of Let the sky be 0

19 columns, 12 rows

Fall-Test Program

fall1.gif (This result is a little different because it is not drawn with opencv in the program below, but the drop processing is the same. There are only two next displays, but it was a simple mistake)

Another pattern 1 fall2.gif

Another pattern 2 fall3.gif

I am satisfied with the results that I feel quite good.

Pattern 1 which may be a little problem 1.jpg

This is fixed and does not move. I'm worried about whether to drop the blue on the left end. At present, this is because the wall is recognized as having spheres on the left, right, lower right, and lower left.

fall4.gif

fall5.gif This is a random pattern chosen

The program has been uploaded to github.

Input system

When the ball set (3 pieces) is moved with a keyboard etc. and dropped I want to calculate where the ball set will fall. Move the ball set with the amount of left and right movement entered with the keyboard, and when the ball set drop button is entered, drop it linearly, and when any of the three balls collides with an existing ball and becomes unable to move linearly. The ball set collapses, and from there I'm going to use the drop method I mentioned earlier.

5.png 6.png 7.png

Judgment when falling linearly-inverted triangle

8.png 9.png

Judgment when falling straight-triangular

There are many places to look up, so let's do our best 10.png

Result 1-Floor collision

fall6.gif

fall7.gif

Not bad, but bad starting point fall8.gif With this kind of feeling, it becomes necessary to shift by 0.5. The direction of shifting was to the left if it was to the right, and to the right if it was to the left.

Result 2-Inverted triangle-just below

fall9.gif

It is properly shifted and dropped.

Result 2-Inverted triangle-at the bottom left

fall10.gif

It's a little unnatural, and the light blue next to yellow should fall to the lower right, if possible, instead of under yellow. .. .. However, at random fall11.gif

In some cases This is because in the current processing, if you can go to either the lower right or the lower left, it is randomly selected.

Postscript:

Conditions

  1. The ball only moves to $ b, c $
  2. There is no ball in $ a, b $ to move to $ b $
  3. There is no ball in $ c, d $ to move to $ c $
  4. If you can move to either $ b or c $, it doesn't matter if it is in $ a or d $, the direction of movement is $ b, c $ random

Rewrite condition 4 4. If you can move to either $ b or c $, $ c $ if there is a sphere in $ a $, $ b $ if there is a sphere in $ d $, $ b, c $ if neither is random

Change to. It's no longer random, it's natural ...

Hexagon pyramid detection

Finally, think about the detection method I could only think of a primitive method

11.png

Like this, scan the start point and try to detect it Do the same for the pyramids.

Delete when 7 or more are connected

Detection method to do this 12.png

Final result

The code has become so long that I can't put it here for a moment. I will put it on a guy called github for the first time. ball6_puzzle_exe.py is the game execution program.

Method of operation a: Right 60deg rotation b: Left 60deg rotation j: Move left l: Move right k: Start falling

q: End of game

Recommended Posts

6 Ball puzzle implemented in python
Implemented SimRank in Python
Implemented Shiritori in Python
Sudoku solver implemented in Python 3
Implemented image segmentation in python (Union-Find)
Implemented label propagation method in Python
Implemented Perceptron learning rules in Python
Implemented in 1 minute! LINE Notify in Python
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Implemented in Python PRML Chapter 7 Nonlinear SVM
Discord in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
I implemented Cousera's logistic regression in Python
Plink in Python
Constant in python
Lifegame in Python.
Sqlite in python
StepAIC in Python
N-gram in python
Implemented in Python PRML Chapter 5 Neural Networks
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Implemented in Python PRML Chapter 1 Bayesian Inference
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Implemented in Python PRML Chapter 3 Bayesian Linear Regression
I implemented Robinson's Bayesian Spam Filter in python
Implemented memoization recursion and exploration in Python and Go
Implemented in Python PRML Chapter 1 Polynomial Curve Fitting
I implemented the inverse gamma function 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