Skip to main content

doudizhu engine

Project description

Copyright (c) 2018 Larry He

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Description-Content-Type: UNKNOWN
Description: # 斗地主引擎

枚举了37种细分牌型,制作一个花色无关、顺序无关的字典,能够在O(1)时间内判断出牌是否有效,在O(1)时间内比较大小。

扑克出牌是54张牌的组合,牌型和排列顺序无关,在斗地主游戏中,牌型及大小和花色无关,两个王不算对子。

[详细文档](docs/engine.md)

## Quickstart

### Installing

`pip install doudizhu`

or

- git clone https://github.com/onestraw/doudizhu
- cd doudizhu && pip install .

### 开始一局游戏
```python
>>> import doudizhu
>>> from doudizhu import Card
>>> cards_groups = doudizhu.new_game()
>>>
>>> cards_groups
[[13, 32, 36, 43, 140, 21, 138, 42, 37, 131, 129, 16, 17, 26, 76, 70, 71], [27, 130, 67, 65, 35, 72, 69, 75, 33, 34, 68, 66, 135, 24, 134, 19, 25], [136, 20, 38, 28, 22, 23, 74, 73, 14, 44, 39, 133, 40, 132, 128, 18, 137], [64, 41, 139]]
>>>
>>> for cards_group in cards_groups:
... Card.print_pretty_cards(cards_group)
...
[ BJ ] , [ 3 ❤ ] , [ 7 ❤ ] , [ A ❤ ] , [ 2 ♣ ] , [ 8 ♠ ] , [ K ♣ ] , [ K ❤ ] , [ 8 ❤ ] , [ 6 ♣ ] , [ 4 ♣ ] , [ 3 ♠ ] , [ 4 ♠ ] , [ K ♠ ] , [ 2 ♦ ] , [ 9 ♦ ] , [ 10 ♦ ]
[ A ♠ ] , [ 5 ♣ ] , [ 6 ♦ ] , [ 4 ♦ ] , [ 6 ❤ ] , [ J ♦ ] , [ 8 ♦ ] , [ A ♦ ] , [ 4 ❤ ] , [ 5 ❤ ] , [ 7 ♦ ] , [ 5 ♦ ] , [ 10 ♣ ] , [ J ♠ ] , [ 9 ♣ ] , [ 6 ♠ ] , [ Q ♠ ]
[ J ♣ ] , [ 7 ♠ ] , [ 9 ❤ ] , [ 2 ♠ ] , [ 9 ♠ ] , [ 10 ♠ ] , [ K ♦ ] , [ Q ♦ ] , [ CJ ] , [ 2 ❤ ] , [ 10 ❤ ] , [ 8 ♣ ] , [ J ❤ ] , [ 7 ♣ ] , [ 3 ♣ ] , [ 5 ♠ ] , [ Q ♣ ]
[ 3 ♦ ] , [ Q ❤ ] , [ A ♣ ]
```

### 检查牌型
```python
>>> test_chain = [Card.new('3c'), Card.new('4d'), Card.new('5h'), Card.new('6s'), Card.new('7s'), Card.new('8h')]
>>>
>>> test_four_two = [Card.new('2c'), Card.new('2d'), Card.new('2h'), Card.new('2s'), Card.new('BJ'), Card.new('CJ')]
>>>
>>> doudizhu.check_card_type(test_four_two)
(True, [('four_two_solo', 13)])
>>> doudizhu.check_card_type(test_chain)
(True, [('solo_chain_6', 0)])
>>> doudizhu.check_card_type(test_chain[:4])
(False, ValueError('invalid card type',))
```

### 比较大小
```python
>>> chain = [Card.new('3c'), Card.new('4d'), Card.new('5h'), Card.new('6s'), Card.new('7s'), Card.new('8h'), Card.new('9h')]
>>> bomb = [Card.new('8h'), Card.new('8s'), Card.new('8d'), Card.new('8c')]
>>> rocket = [Card.new('BJ'), Card.new('CJ')]
>>>
>>> doudizhu.cards_greater(chain, chain)
(False, 'solo_chain_7')
>>> doudizhu.cards_greater(chain[:6], chain[1:7])
(False, 'solo_chain_6')
>>>
>>> doudizhu.cards_greater(chain[1:7], chain[:6])
(True, 'solo_chain_6')
>>> doudizhu.cards_greater(bomb, chain)
(True, 'bomb')
>>> doudizhu.cards_greater(rocket, bomb)
(True, 'rocket')
```

Platform: UNKNOWN
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX
Classifier: Programming Language :: Python :: 2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

doudizhu-0.1.1.tar.gz (8.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page