I am making my own program that can play mahjong with Python.
This time, it ’s a mahjong program for one person, just cutting the tiles
The ultimate goal is to gradually add functions and create Mahjong AI.
--Manko (man's), tube (pins), cordo (swords) with numbers from 1 to 9 written on it A total of 136 tiles, including 34 types of tiles with the words "East", "South", "West", "North", "White", "Departure", and "Middle", are used. ――The player holds 13 tiles (hand tiles), draws one tile from the remaining tiles (mountain), and replaces it with one hand tile. It is a game that clears the game when the Tehai meets certain conditions. ――14 tiles in the mountain are called king tiles, and the player cannot check the contents or draw tiles from them. Depending on the king's tile, it may not be possible to go up.
The source code looks like this
#!/usr/bin/env python
#Mahjong program
#Currently, only one mahjong can be cut by cutting
#Unimplemented:Agari judgment,Hand judgment,Squeal,river,
from enum import Enum
import sys
import random
import datetime
class Pai(Enum): #Tile class Defines the type of tile
#Manko
m1 = 'Ichiman'
m2 = 'Niman'
m3 = 'Sanman'
m4 = 'Shima'
m5 = 'Goman'
m6 = 'Rokuman'
m7 = 'Nanaman'
m8 = 'Hachiman'
m9 = 'Kuman'
#Tsutsuko
p1 = 'One cylinder'
p2 = 'Two cylinders'
p3 = 'Three cylinders'
p4 = 'Four cylinders'
p5 = 'Five cylinders'
p6 = 'Six cylinders'
p7 = 'Seven cylinders'
p8 = 'Eight cylinders'
p9 = 'Nine cylinders'
#Cord
s1 = 'One search'
s2 = 'Two ropes'
s3 = 'Three ropes'
s4 = 'Four ropes'
s5 = 'Five ropes'
s6 = 'Six ropes'
s7 = 'Seven ropes'
s8 = 'Eight ropes'
s9 = 'Nine ropes'
#Tile
z1 = 'east'
z2 = 'South'
z3 = 'West'
z4 = 'North'
z5 = 'White'
z6 = 'Departure'
z7 = 'During ~'
def __str__(self): #Define the display method in the print function
return self.value
class Yama: #Management of mountain class mountains, kings, etc.
def __init__(self):
self.yama = [*Pai] * 4 #Mountain initialization
random.shuffle(self.yama)
self.wanpai = self.yama[0:14] #Initialization of the king
del self.yama[0:14]
def tsumo(self): #Return the last tile of the mountain
return self.yama.pop()
def haipai(self): #13 Give the tile to the player
haipai = self.yama[0:13]
del self.yama[0:13]
return haipai
class Player: #Player class Tehai management
def __init__(self):
self.tehai = []
def haipai(self,pai): #Take 13 tiles from the mountain at the beginning of the game
self.tehai = pai
self.tehai.sort(key = lambda x: str(x.name))
print('Distribution\n',*self.tehai,'\n')
def tsumo(self,pai): #Take one tile from the mountain and discard one from the hand tile
print(*self.tehai, 'Tsumo', pai)
self.tehai.append(pai)
sutehai = self.tehai.pop(int(input()))
#Specify the tile to cut by keyboard input
self.tehai.sort(key = lambda x: str(x.name))
print('Hit',sutehai,'\n')
return sutehai
random.seed(datetime.datetime.now())
yama1 = Yama() #Mountain generation
player1 = Player() #Player generation
player1.haipai(yama1.haipai())
while(len(yama1.yama)!=0): #Until the number of tiles in the mountain reaches 0
player1.tsumo(yama1.tsumo()) #Cut one piece
(Edit history)
--2021/01/01 I learned about the existence of the official Python coding standard PEP8 and decided to modify it for readability. How to delete encoding settings, write import, etc.
――At first, I wrote the part where the tiles are generated in the list comprehensive notation, but I could write it smarter by using * (asterisk).
self.yama = [pai for pai in Pai] * 4 #List comprehensive notation
self.yama = [*Pai] * 4 #Notation using an asterisk
# [Pai.m1,Pai.m2,...Pai.z7] *Same meaning as 4
--When you output the Tehai in the top module, you bothered to write pai.value
, but by overloading the special method __str__
, you could define the output in the print statement.
class Pai:
def __str__(self):
return self.value
tehai = [Pai.m1,Pai.p2,Pai.s3]
print(*tehai) # print(tehai[0],tehai[1],tehai[2])Same meaning as
#Execution result
#Ichiman, two cylinders, three ropes
I tried to run it on the command prompt. I think that you can display, sort, and replace the tiles normally.
――It is a little difficult to see because the hand tiles are displayed in letters. --Agari judgment is not implemented
I will write these two points in the next article.
I tried it to practice python. This is my first time to publish the code I wrote, so I would be grateful if you could point it out in the comments if there is something to dig into.
How to write python https://note.nkmk.me/ I referred to this site.
Recommended Posts