Beispiel #1
0
def simulate_naive3(hand, hand_open, unaccounted_tiles):
    """
    #naive simulation

    hand, hand_open -- hand in 34 format
    unaccounted_tiles -- all the unused tiles in 34 format
    turn -- a number from 0-3 (0 is the player)
    """
    shanten_calc = Shanten()
    hand = list(hand)
    unaccounted = list(unaccounted_tiles)
    tiles_left = sum(unaccounted_tiles)
    unaccounted_nonzero = np.nonzero(unaccounted)
    #14 in dead wall 13*3= 39 in other hand -> total 53
    shanten_sum = 0.
    for i in range(tiles_left - 119):
        shanten = shanten_calc.calculate_shanten(hand, hand_open)
        if shanten <= 0:  #if tenpai
            break
        shanten_sum += shanten**2
        hand_nonzero = np.nonzero(hand)[0]  #discard something random
        discard = random.choice(hand_nonzero)
        hand[discard] -= 1
        unaccounted_nonzero = np.nonzero(unaccounted)[0]  #get a random card
        draw_tile = random.choice(unaccounted_nonzero)
        unaccounted[draw_tile] -= 1
        hand[draw_tile] += 1
    return shanten_sum
Beispiel #2
0
def simulate_weighted3(hand, hand_open, unaccounted_tiles):
    """
    Does a weighted simulation (Incomplete)
    
    hand, hand_open -- hand in 34 format
    unaccounted_tiles -- all the unused tiles in 34 format
    turn -- a number from 0-3 (0 is the player)
    """
    shanten_calc = Shanten()
    hand = list(hand)
    unaccounted = list(unaccounted_tiles)
    tiles_left = sum(unaccounted_tiles)
    unaccounted_nonzero = np.nonzero(unaccounted)
    #14 in dead wall 13*3= 39 in other hand -> total 53
    shanten_sum = 0
    for i in range(tiles_left - 119):
        shanten = shanten_calc.calculate_shanten(hand, hand_open)
        if shanten <= 0:  #if tenpai
            break
        shanten_sum += shanten
        hand_nonzero = np.nonzero(hand)[0]
        nonzero_inverted = [4 - hand[i] for i in hand_nonzero]
        weight = np.array(nonzero_inverted) / sum(nonzero_inverted)
        discard = np.random.choice(hand_nonzero, 1, p=weight, replace=False)[0]
        hand[discard] -= 1
        #print(weight, nonzero_inverted)
        unaccounted_nonzero = np.nonzero(unaccounted)[0]  #get a random card
        draw_tile = random.choice(unaccounted_nonzero)
        unaccounted[draw_tile] -= 1
        hand[draw_tile] += 1
    return shanten_sum
Beispiel #3
0
def simulate_weighted2(hand, hand_open, unaccounted_tiles):
    """
    Does a weighted simulation (Incomplete)
    
    hand, hand_open -- hand in 34 format
    unaccounted_tiles -- all the unused tiles in 34 format
    turn -- a number from 0-3 (0 is the player)
    """
    shanten = Shanten()
    hand = copy.deepcopy(hand)
    unaccounted = copy.deepcopy(unaccounted_tiles)
    tiles_left = sum(unaccounted_tiles)
    #14 in dead wall 13*3= 39 in other hand -> total 53
    for i in range(tiles_left - 53):
        if shanten.calculate_shanten(hand, None) <= 0:
            return True
        weight = [0] * 34
        for i, count in enumerate(hand):
            if count >= 3:
                weight[i] = 0
            else:
                weight[i] = 4 - count
        weight = np.array(weight) / sum(weight)
        discard = np.random.choice(range(34), 1, p=weight, replace=False)[0]
        hand[discard] -= 1
        #print(weight, nonzero_inverted)
        unaccounted_nonzero = np.nonzero(unaccounted)[0]  #get a random card
        draw_tile = random.choice(unaccounted_nonzero)
        unaccounted[draw_tile] -= 1
        hand[draw_tile] += 1
    return False
Beispiel #4
0
def simulate_weighted(hand, hand_open, unaccounted_tiles):
    """
    Does a weighted simulation
    
    hand, hand_open -- hand in 34 format
    unaccounted_tiles -- all the unused tiles in 34 format
    turn -- a number from 0-3 (0 is the player)
    """
    shanten = Shanten()
    hand = list(hand)
    unaccounted = copy.deepcopy(unaccounted_tiles)
    tiles_left = sum(unaccounted_tiles)
    #14 in dead wall 13*3= 39 in other hand -> total 53
    for i in range(tiles_left):
        if shanten.calculate_shanten(hand, hand_open) <= 0:  #if tenpai
            return True
        hand_nonzero = np.nonzero(hand)[0]
        nonzero_inverted = [4 - hand[i] for i in hand_nonzero]
        weight = np.array(nonzero_inverted) / sum(nonzero_inverted)
        discard = np.random.choice(hand_nonzero, 1, p=weight, replace=False)[0]
        hand[discard] -= 1
        #print(weight, nonzero_inverted)
        unaccounted_nonzero = np.nonzero(unaccounted)[0]  #get a random card
        draw_tile = random.choice(unaccounted_nonzero)
        unaccounted[draw_tile] -= 1
        hand[draw_tile] += 1
    return False
Beispiel #5
0
def simulate_strategic(hand, hand_open, unaccounted_tiles):
    """
    #naive simulation

    hand, hand_open -- hand in 34 format
    unaccounted_tiles -- all the unused tiles in 34 format
    """
    shanten_calc = Shanten()
    hand = list(hand)
    unaccounted = list(unaccounted_tiles)
    tiles_left = sum(unaccounted_tiles)
    unaccounted_nonzero = np.nonzero(unaccounted)
    #14 in dead wall 13*3= 39 in other hand -> total 53
    shanten_sum = 0
    for i in range(tiles_left - 119):
        unaccounted_nonzero = np.nonzero(unaccounted)[0]  #get a random card
        draw_tile = random.choice(unaccounted_nonzero)
        unaccounted[draw_tile] -= 1
        hand[draw_tile] += 1
        choices = []
        for tile in range(0, 34):
            # discard the tile from hand
            if hand[tile] == 0:
                continue
            hand[tile] -= 1

            # calculate shanten and store
            shanten = shanten_calc.calculate_shanten(hand, hand_open)
            choices.append((shanten, tile))

            # return tile to hand
            hand[tile] += 1
        min_shanten, discard = min(choices)
        if min_shanten <= 0:
            print('#')
            break
        shanten_sum += min_shanten
    return shanten_sum
Beispiel #6
0
# import copy
import cProfile

# from game.ai.base.main import InterfaceAI
from Shanten import Shanten
from mahjong.tile import TilesConverter
# from mahjong.meld import Meld
# from mahjong.utils import is_man, is_pin, is_sou, is_pon, is_chi
# from mahjong.hand_calculating.divider import HandDivider
import numpy as np
import re

import time
import csv

shantenCalc = Shanten()
shdict = {}

# def discard_tile(discard_tile):
#
#     if discard_tile is not None:
#         return discard_tile
#
#     tiles_34 = TilesConverter.to_34_array(self.player.tiles)
#     closed_tiles_34 = TilesConverter.to_34_array(self.player.closed_hand)
#
#     results = []
#
#     for tile in range(0, 34):
#         # Can the tile be discarded from the concealed hand?
#         if not closed_tiles_34[tile]: