kago-utils 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ release:
5
+ types:
6
+ - released
7
+
8
+ jobs:
9
+ release:
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - uses: actions/checkout@v4
14
+
15
+ - name: Setup Python
16
+ uses: actions/setup-python@v5
17
+ with:
18
+ python-version-file: .python-version
19
+
20
+ - name: Install dependencies
21
+ run: |
22
+ python -m pip install --upgrade pip
23
+ python -m pip install build twine hatch-vcs
24
+
25
+ - name: Build the package
26
+ run: python -m build
27
+
28
+ - name: Publish to PyPI
29
+ env:
30
+ TWINE_USERNAME: __token__
31
+ TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
32
+ run: python -m twine upload dist/*
33
+
@@ -0,0 +1,33 @@
1
+ name: Publish to Test PyPI
2
+
3
+ on:
4
+ release:
5
+ types:
6
+ - prereleased
7
+
8
+ jobs:
9
+ release:
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - uses: actions/checkout@v4
14
+
15
+ - name: Setup Python
16
+ uses: actions/setup-python@v5
17
+ with:
18
+ python-version-file: .python-version
19
+
20
+ - name: Install dependencies
21
+ run: |
22
+ python -m pip install --upgrade pip
23
+ python -m pip install build twine hatch-vcs
24
+
25
+ - name: Build the package
26
+ run: python -m build
27
+
28
+ - name: Publish to Test PyPI
29
+ env:
30
+ TWINE_USERNAME: __token__
31
+ TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }}
32
+ run: python -m twine upload --repository testpypi dist/*
33
+
@@ -0,0 +1,33 @@
1
+ name: Test
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request:
8
+ branches:
9
+ - '**'
10
+
11
+ jobs:
12
+ test:
13
+ runs-on: ubuntu-latest
14
+
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+
18
+ - name: Setup Python
19
+ uses: actions/setup-python@v5
20
+ with:
21
+ python-version-file: .python-version
22
+ cache: 'pipenv'
23
+
24
+ - name: Install pipenv
25
+ run: |
26
+ python -m pip install --upgrade pip
27
+ python -m pip install pipenv
28
+
29
+ - name: Install dependencies
30
+ run: python -m pipenv install --dev
31
+
32
+ - name: Run tests
33
+ run: python -m pipenv run test
@@ -0,0 +1,165 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
110
+ .pdm.toml
111
+ .pdm-python
112
+ .pdm-build/
113
+
114
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
115
+ __pypackages__/
116
+
117
+ # Celery stuff
118
+ celerybeat-schedule
119
+ celerybeat.pid
120
+
121
+ # SageMath parsed files
122
+ *.sage.py
123
+
124
+ # Environments
125
+ .env
126
+ .venv
127
+ env/
128
+ venv/
129
+ ENV/
130
+ env.bak/
131
+ venv.bak/
132
+
133
+ # Spyder project settings
134
+ .spyderproject
135
+ .spyproject
136
+
137
+ # Rope project settings
138
+ .ropeproject
139
+
140
+ # mkdocs documentation
141
+ /site
142
+
143
+ # mypy
144
+ .mypy_cache/
145
+ .dmypy.json
146
+ dmypy.json
147
+
148
+ # Pyre type checker
149
+ .pyre/
150
+
151
+ # pytype static type analyzer
152
+ .pytype/
153
+
154
+ # Cython debug symbols
155
+ cython_debug/
156
+
157
+ # PyCharm
158
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
159
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
161
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
162
+ #.idea/
163
+
164
+ # MacOS
165
+ .DS_Store
@@ -0,0 +1 @@
1
+ 3.10
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 zurukumo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,17 @@
1
+ Metadata-Version: 2.3
2
+ Name: kago-utils
3
+ Version: 0.1.0
4
+ Summary: Mahjong Library for Python
5
+ Project-URL: Homepage, https://github.com/zurukumo/kago-utils
6
+ Project-URL: Issues, https://github.com/zurukumo/kago-utils/issues
7
+ Author-email: zurukumo <zurukumo@gmail.com>
8
+ License-File: LICENSE
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Requires-Python: >=3.10
15
+ Description-Content-Type: text/markdown
16
+
17
+ # kago-utils
@@ -0,0 +1,16 @@
1
+ [[source]]
2
+ url = "https://pypi.org/simple"
3
+ verify_ssl = true
4
+ name = "pypi"
5
+
6
+ [packages]
7
+
8
+ [dev-packages]
9
+ numpy = "*"
10
+ shanten-tools = "*"
11
+
12
+ [requires]
13
+ python_version = "3.10"
14
+
15
+ [scripts]
16
+ test = "python -m unittest discover -v tests"
@@ -0,0 +1,97 @@
1
+ {
2
+ "_meta": {
3
+ "hash": {
4
+ "sha256": "abc924915a23d4cdd24cd8b32e7fc7831428a27f2e8e6bd1f5fd47d9b7705df9"
5
+ },
6
+ "pipfile-spec": 6,
7
+ "requires": {
8
+ "python_version": "3.10"
9
+ },
10
+ "sources": [
11
+ {
12
+ "name": "pypi",
13
+ "url": "https://pypi.org/simple",
14
+ "verify_ssl": true
15
+ }
16
+ ]
17
+ },
18
+ "default": {},
19
+ "develop": {
20
+ "numpy": {
21
+ "hashes": [
22
+ "sha256:08458fbf403bff5e2b45f08eda195d4b0c9b35682311da5a5a0a0925b11b9bd8",
23
+ "sha256:0fbb536eac80e27a2793ffd787895242b7f18ef792563d742c2d673bfcb75134",
24
+ "sha256:12f5d865d60fb9734e60a60f1d5afa6d962d8d4467c120a1c0cda6eb2964437d",
25
+ "sha256:15eb4eca47d36ec3f78cde0a3a2ee24cf05ca7396ef808dda2c0ddad7c2bde67",
26
+ "sha256:173a00b9995f73b79eb0191129f2455f1e34c203f559dd118636858cc452a1bf",
27
+ "sha256:1b902ce0e0a5bb7704556a217c4f63a7974f8f43e090aff03fcf262e0b135e02",
28
+ "sha256:1f682ea61a88479d9498bf2091fdcd722b090724b08b31d63e022adc063bad59",
29
+ "sha256:1f87fec1f9bc1efd23f4227becff04bd0e979e23ca50cc92ec88b38489db3b55",
30
+ "sha256:24a0e1befbfa14615b49ba9659d3d8818a0f4d8a1c5822af8696706fbda7310c",
31
+ "sha256:2c3a346ae20cfd80b6cfd3e60dc179963ef2ea58da5ec074fd3d9e7a1e7ba97f",
32
+ "sha256:36d3a9405fd7c511804dc56fc32974fa5533bdeb3cd1604d6b8ff1d292b819c4",
33
+ "sha256:3fdabe3e2a52bc4eff8dc7a5044342f8bd9f11ef0934fcd3289a788c0eb10018",
34
+ "sha256:4127d4303b9ac9f94ca0441138acead39928938660ca58329fe156f84b9f3015",
35
+ "sha256:4658c398d65d1b25e1760de3157011a80375da861709abd7cef3bad65d6543f9",
36
+ "sha256:485b87235796410c3519a699cfe1faab097e509e90ebb05dcd098db2ae87e7b3",
37
+ "sha256:529af13c5f4b7a932fb0e1911d3a75da204eff023ee5e0e79c1751564221a5c8",
38
+ "sha256:5a3d94942c331dd4e0e1147f7a8699a4aa47dffc11bf8a1523c12af8b2e91bbe",
39
+ "sha256:5daab361be6ddeb299a918a7c0864fa8618af66019138263247af405018b04e1",
40
+ "sha256:61728fba1e464f789b11deb78a57805c70b2ed02343560456190d0501ba37b0f",
41
+ "sha256:6790654cb13eab303d8402354fabd47472b24635700f631f041bd0b65e37298a",
42
+ "sha256:69ff563d43c69b1baba77af455dd0a839df8d25e8590e79c90fcbe1499ebde42",
43
+ "sha256:6bf4e6f4a2a2e26655717a1983ef6324f2664d7011f6ef7482e8c0b3d51e82ac",
44
+ "sha256:6e4eeb6eb2fced786e32e6d8df9e755ce5be920d17f7ce00bc38fcde8ccdbf9e",
45
+ "sha256:72dc22e9ec8f6eaa206deb1b1355eb2e253899d7347f5e2fae5f0af613741d06",
46
+ "sha256:75b4e316c5902d8163ef9d423b1c3f2f6252226d1aa5cd8a0a03a7d01ffc6268",
47
+ "sha256:7b9853803278db3bdcc6cd5beca37815b133e9e77ff3d4733c247414e78eb8d1",
48
+ "sha256:7d6fddc5fe258d3328cd8e3d7d3e02234c5d70e01ebe377a6ab92adb14039cb4",
49
+ "sha256:81b0893a39bc5b865b8bf89e9ad7807e16717f19868e9d234bdaf9b1f1393868",
50
+ "sha256:8efc84f01c1cd7e34b3fb310183e72fcdf55293ee736d679b6d35b35d80bba26",
51
+ "sha256:8fae4ebbf95a179c1156fab0b142b74e4ba4204c87bde8d3d8b6f9c34c5825ef",
52
+ "sha256:99d0d92a5e3613c33a5f01db206a33f8fdf3d71f2912b0de1739894668b7a93b",
53
+ "sha256:9adbd9bb520c866e1bfd7e10e1880a1f7749f1f6e5017686a5fbb9b72cf69f82",
54
+ "sha256:a1e01dcaab205fbece13c1410253a9eea1b1c9b61d237b6fa59bcc46e8e89343",
55
+ "sha256:a8fc2de81ad835d999113ddf87d1ea2b0f4704cbd947c948d2f5513deafe5a7b",
56
+ "sha256:b83e16a5511d1b1f8a88cbabb1a6f6a499f82c062a4251892d9ad5d609863fb7",
57
+ "sha256:bb2124fdc6e62baae159ebcfa368708867eb56806804d005860b6007388df171",
58
+ "sha256:bfc085b28d62ff4009364e7ca34b80a9a080cbd97c2c0630bb5f7f770dae9414",
59
+ "sha256:cbab9fc9c391700e3e1287666dfd82d8666d10e69a6c4a09ab97574c0b7ee0a7",
60
+ "sha256:e5eeca8067ad04bc8a2a8731183d51d7cbaac66d86085d5f4766ee6bf19c7f87",
61
+ "sha256:e9e81fa9017eaa416c056e5d9e71be93d05e2c3c2ab308d23307a8bc4443c368",
62
+ "sha256:ea2326a4dca88e4a274ba3a4405eb6c6467d3ffbd8c7d38632502eaae3820587",
63
+ "sha256:eacf3291e263d5a67d8c1a581a8ebbcfd6447204ef58828caf69a5e3e8c75990",
64
+ "sha256:ec87f5f8aca726117a1c9b7083e7656a9d0d606eec7299cc067bb83d26f16e0c",
65
+ "sha256:f1659887361a7151f89e79b276ed8dff3d75877df906328f14d8bb40bb4f5101",
66
+ "sha256:f9cf5ea551aec449206954b075db819f52adc1638d46a6738253a712d553c7b4"
67
+ ],
68
+ "index": "pypi",
69
+ "markers": "python_version >= '3.9'",
70
+ "version": "==2.0.1"
71
+ },
72
+ "shanten-tools": {
73
+ "hashes": [
74
+ "sha256:0530752f8dc1b5b0a73c7b2724c91404d9123d8777cbcebc892f5203baeca9ba",
75
+ "sha256:1b9e2ace7377a4e05f7a76fe794aaabc4717612427ef6648d41714fe1b212454",
76
+ "sha256:1e471ba0f50a52f9f1108656f96644ea6977d73373d4fcefcbf2eae194e38593",
77
+ "sha256:1e7301736942b02ccd40079ad19ba34b3bcc8bc6c31ff92634fa541bf1a4daaa",
78
+ "sha256:21a95c312a28b41084092410e0f2705cf48d563f4a63bb15788adebfe9af28db",
79
+ "sha256:2caa1d86a223aa5613003da778362c293c3bd7d3158039ddef3a902957a12b9c",
80
+ "sha256:54dcdfbb726e13e952d19cb2fcecb0f8a5c01455d112d529fba674add6ad17f5",
81
+ "sha256:64cc3fa25dcc9eefcff8440397917d4b19b31d21f0eb8fe80202bb07c18fd375",
82
+ "sha256:756cf523c6dff1707e1d6ba4cfb0d6e7eeb96fc4aea6179f8593973a3dfc7f85",
83
+ "sha256:76d0bd55aa36a9e7ec11787bc5799bba8ec10e5c6b1dbbe8d1fc6a4ef7cc760b",
84
+ "sha256:ba5fef7e0d6ae9a57b8914e77794512e50d39fbb219b421845d7b0c40c25a65b",
85
+ "sha256:d0ac4442734dd589d762035319c1fc2da8e0eb7d88cf64c2332df98b2ff19c65",
86
+ "sha256:d49e5e0e7cdd627d42930c676281375d9736f940d06788c4cc4921c8e2d59e3c",
87
+ "sha256:dea9bac83c6a00ce5a2a30333fa7dd8bfe47627471dcd1fcc525c97e5e7aa059",
88
+ "sha256:e592fed3e2187aff4da9262c264a6eb9ab117ec96744210bf5ae310ff1725296",
89
+ "sha256:e646bafe00da80a926dbc3c6c9d45bb8c6f3c36a7578da429b919bfcc4bc908b",
90
+ "sha256:e8a820493589833fd687f61f2f0d2a3a3e0a598133fc4339d3c2ea7811978e0d"
91
+ ],
92
+ "index": "pypi",
93
+ "markers": "python_version >= '3.7'",
94
+ "version": "==0.1.8"
95
+ }
96
+ }
97
+ }
@@ -0,0 +1 @@
1
+ # kago-utils
@@ -0,0 +1,6 @@
1
+ from importlib.metadata import PackageNotFoundError, version
2
+
3
+ try:
4
+ __version__ = version(__package__)
5
+ except PackageNotFoundError:
6
+ __version__ = "unknown"
@@ -0,0 +1,205 @@
1
+ import pickle
2
+ from collections import deque
3
+ from itertools import product
4
+
5
+
6
+ def extract_shuntsu(tehai):
7
+ N = len(tehai)
8
+
9
+ # queue format: [tehai, n_shuntsu, start]
10
+ queue = deque([])
11
+ queue.append([tehai.copy(), 0, 0])
12
+
13
+ while queue:
14
+ tehai, n_shuntsu, start = queue.popleft()
15
+ yield tehai.copy(), n_shuntsu
16
+
17
+ for i in range(start, N):
18
+ if i + 2 < N and i <= 6 and tehai[i] >= 1 and tehai[i+1] >= 1 and tehai[i+2] >= 1:
19
+ tehai[i] -= 1
20
+ tehai[i+1] -= 1
21
+ tehai[i+2] -= 1
22
+ queue.append([tehai.copy(), n_shuntsu + 1, i])
23
+ tehai[i] += 1
24
+ tehai[i+1] += 1
25
+ tehai[i+2] += 1
26
+
27
+
28
+ def extract_kootsu(tehai):
29
+ N = len(tehai)
30
+
31
+ # queue format: [tehai, n_kootsu, start]
32
+ queue = deque([])
33
+ queue.append([tehai.copy(), 0, 0])
34
+
35
+ while queue:
36
+ tehai, n_kootsu, start = queue.popleft()
37
+ yield tehai.copy(), n_kootsu
38
+
39
+ for i in range(start, N):
40
+ if tehai[i] >= 3:
41
+ tehai[i] -= 3
42
+ queue.append([tehai.copy(), n_kootsu + 1, i])
43
+ tehai[i] += 3
44
+
45
+
46
+ def extract_toitsu(tehai):
47
+ N = len(tehai)
48
+
49
+ # queue format: [tehai, n_toitsu, start]
50
+ queue = deque([])
51
+ queue.append([tehai.copy(), 0, 0])
52
+
53
+ while queue:
54
+ tehai, n_toitsu, start = queue.popleft()
55
+ yield tehai.copy(), n_toitsu
56
+
57
+ for i in range(start, N):
58
+ if tehai[i] >= 2:
59
+ tehai[i] -= 2
60
+ queue.append([tehai.copy(), n_toitsu + 1, i])
61
+ tehai[i] += 2
62
+
63
+
64
+ def extract_taatsu(tehai):
65
+ N = len(tehai)
66
+
67
+ # queue format: [tehai, n_taatsu, start]
68
+ queue = deque([])
69
+ queue.append([tehai.copy(), 0, 0])
70
+
71
+ while queue:
72
+ tehai, n_taatsu, start = queue.popleft()
73
+ yield tehai.copy(), n_taatsu
74
+
75
+ for i in range(start, N):
76
+ if i + 1 < N and tehai[i] >= 1 and tehai[i+1] >= 1:
77
+ tehai[i] -= 1
78
+ tehai[i+1] -= 1
79
+ queue.append([tehai.copy(), n_taatsu + 1, i])
80
+ tehai[i] += 1
81
+ tehai[i+1] += 1
82
+
83
+ if i + 2 < N and tehai[i] >= 1 and tehai[i+2] >= 1:
84
+ tehai[i] -= 1
85
+ tehai[i+2] -= 1
86
+ queue.append([tehai.copy(), n_taatsu + 1, i])
87
+ tehai[i] += 1
88
+ tehai[i+2] += 1
89
+
90
+
91
+ def list_suuhai_patterns(tehai):
92
+ tehai = list(tehai)
93
+ for rest_tehai, n_kootsu in extract_kootsu(tehai):
94
+ for rest_tehai, n_shuntsu in extract_shuntsu(rest_tehai):
95
+ for rest_tehai, n_toitsu in extract_toitsu(rest_tehai):
96
+ for rest_tehai, n_taatsu in extract_taatsu(rest_tehai):
97
+ yield n_kootsu, n_shuntsu, n_toitsu, n_taatsu
98
+
99
+
100
+ def list_zihai_patterns(tehai):
101
+ tehai = list(tehai)
102
+ for rest_tehai, n_kootsu in extract_kootsu(tehai):
103
+ for rest_tehai, n_toitsu in extract_toitsu(rest_tehai):
104
+ yield n_kootsu, 0, n_toitsu, 0
105
+
106
+
107
+ def select_better_pattern1(max1, current):
108
+ max_score = max1[0] * 2 + max1[1]
109
+ current_score = current[0] * 2 + current[1]
110
+ if current_score > max_score or (current_score == max_score and current[2]):
111
+ return current
112
+ else:
113
+ return max1
114
+
115
+
116
+ def select_better_pattern2(max2, current):
117
+ max_score = max2[0] * 8 + max2[1]
118
+ current_score = current[0] * 8 + current[1]
119
+ if current_score > max_score or (current_score == max_score and current[2]):
120
+ return current
121
+ else:
122
+ return max2
123
+
124
+
125
+ def select_better_pattern3(max3, current):
126
+ max_score = max3[0] * 2 + max3[1] + (100 if max3[2] else 0)
127
+ current_score = current[0] * 2 + current[1] + (100 if current[2] else 0)
128
+ if current_score > max_score or (current_score == max_score and current[2]):
129
+ return current
130
+ else:
131
+ return max3
132
+
133
+
134
+ def select_better_pattern4(max4, current):
135
+ max_score = max4[0] * 8 + max4[1] + (100 if max4[2] else 0)
136
+ current_score = current[0] * 8 + current[1] + (100 if current[2] else 0)
137
+ if current_score > max_score or (current_score == max_score and current[2]):
138
+ return current
139
+ else:
140
+ return max4
141
+
142
+
143
+ # generate patterns
144
+ def generate_patterns():
145
+ suuhai_patterns = dict()
146
+ zihai_patterns = dict()
147
+
148
+ # generate suuhai patterns
149
+ for tehai in product([0, 1, 2, 3, 4], repeat=9):
150
+ if sum(tehai) > 14:
151
+ continue
152
+
153
+ print(tehai)
154
+
155
+ # pattern format: [n_mentsu, n_pre_mentsu, has_toitsu]
156
+ max_pattern1 = 0, 0, False # maximize n_mentsu * 2 + n_pre_mentsu
157
+ max_pattern2 = 0, 0, False # maximize n_mentsu * 8 + n_pre_mentsu
158
+ max_pattern3 = 0, 0, False # maximize n_mentsu * 2 + n_pre_mentsu + 100 if has_toitsu
159
+ max_pattern4 = 0, 0, False # maximize n_mentsu * 8 + n_pre_mentsu + 100 if has_toitsu
160
+ for n_kootsu, n_shuntsu, n_toitsu, n_taatsu in list_suuhai_patterns(tehai):
161
+ n_mentsu = n_kootsu + n_shuntsu
162
+ n_pre_mentsu = n_toitsu + n_taatsu
163
+ has_toitsu = n_toitsu > 0
164
+ current_pattern = n_mentsu, n_pre_mentsu, has_toitsu
165
+ max_pattern1 = select_better_pattern1(max_pattern1, current_pattern)
166
+ max_pattern2 = select_better_pattern2(max_pattern2, current_pattern)
167
+ max_pattern3 = select_better_pattern3(max_pattern3, current_pattern)
168
+ max_pattern4 = select_better_pattern4(max_pattern4, current_pattern)
169
+
170
+ # when max1 is same as max2, only store max1 because of memory efficiency
171
+ suuhai_patterns[tehai] = list(set([max_pattern1, max_pattern2, max_pattern3, max_pattern4]))
172
+
173
+ # generate zihai patterns
174
+ for tehai in product([0, 1, 2, 3, 4], repeat=7):
175
+ if sum(tehai) > 14:
176
+ continue
177
+
178
+ print(tehai)
179
+
180
+ # pattern format: [n_mentsu, n_pre_mentsu, has_toitsu]
181
+ max_pattern1 = 0, 0, False # maximize n_mentsu * 2 + n_pre_mentsu
182
+ max_pattern2 = 0, 0, False # maximize n_mentsu * 8 + n_pre_mentsu
183
+ max_pattern3 = 0, 0, False # maximize n_mentsu * 2 + n_pre_mentsu + (100 if has_toitsu)
184
+ max_pattern4 = 0, 0, False # maximize n_mentsu * 8 + n_pre_mentsu + (100 if has_toitsu)
185
+ for n_kootsu, n_shuntsu, n_toitsu, n_taatsu in list_zihai_patterns(tehai):
186
+ n_mentsu = n_kootsu + n_shuntsu
187
+ n_pre_mentsu = n_toitsu + n_taatsu
188
+ has_toitsu = n_toitsu > 0
189
+ current_pattern = n_mentsu, n_pre_mentsu, has_toitsu
190
+ max_pattern1 = select_better_pattern1(max_pattern1, current_pattern)
191
+ max_pattern2 = select_better_pattern2(max_pattern2, current_pattern)
192
+ max_pattern3 = select_better_pattern3(max_pattern3, current_pattern)
193
+ max_pattern4 = select_better_pattern4(max_pattern4, current_pattern)
194
+
195
+ # when max1 is same as max2, only store max1 because of memory efficiency
196
+ zihai_patterns[tehai] = list(set([max_pattern1, max_pattern2, max_pattern3, max_pattern4]))
197
+
198
+ with open('suuhai_patterns.pickle', 'wb') as f:
199
+ pickle.dump(suuhai_patterns, f)
200
+ with open('zihai_patterns.pickle', 'wb') as f:
201
+ pickle.dump(zihai_patterns, f)
202
+
203
+
204
+ if __name__ == '__main__':
205
+ generate_patterns()