---
# System prepended metadata

title: UnitTestAnswer.py
tags: [UnitTest_FileProcess]

---

# UnitTestAnswer.py
###### tags: `UnitTest_FileProcess`
```
import os
import math
import MJ_tiletest_new
from re import T

from MJ_tiletest_new import hu_test
# C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\turn3_0327\\
# C:\\Users\\chsta\\Desktop\\Majong_Project\\windows_MJ_new\\windows\\enter\\Board\\Mahjong\\33test\\
#dir = 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\turn3_0327\\'

#read_dir = 'C:\\Users\\chsta\\Desktop\\Majong_Project\\windows_MJ_new\\windows\\open\\Board\\Mahjong\\a1_vs_a3_vs_a4_vs_a2_2022_03_21_03_09_54\\'
#read_number = 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\turn4_0330\\test_number.txt'
#out_dir = 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\turn3_0330\\'

read_dir = 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\2973r\\a1\\'
read_number = 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\2973r\\test\\test_number.txt'
out_dir = 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\2973r\\test\\'

'''
def check_chucker(txt_list):
    player_game_wind = txt_list[2].split()[2]
    
    for i in range(len(txt_list)-1, 0, -1):
        if txt_list[i].find('/throw') != -1:
            chucker_game_wind = txt_list[i].split()[1]
            if player_game_wind == chucker_game_wind:
                return True
            else:
                break
    return False
'''

wind2id = {'E': 0, 'S': 1, 'W': 2, 'N': 3}

def print_info(txt_list, file, player_ID, last,player_card, answer_list, chucker):
    print("  file_name = " + file)
    #print("  txt_list = " + str(txt_list))
    #print("  txt_list_len = " + str(len(txt_list)))
    #print(player_ID.items())
    print('  master = ' + master)
    print('  chucker =', chucker)
    print('  last-2 = ' + str(last-2))
    print('  player_card[0] =', player_card[0])
    print('  player_card[1] =', player_card[1])
    print('  player_card[2] =', player_card[2])
    print('  player_card[3] =', player_card[3])
    #print(txt_list[6].split()[4])
    print('  answer =', answer_list)
    print('next----------------------------------------------------------------------------')

def read_file(file_fullpath):
    f = open(file_fullpath, 'r', encoding = "utf-8")
    txt_list = f.read()
    txt_list = txt_list.split('\n')
    f.close()
    return txt_list

def read_test_number():
    f1 = open(read_number, 'r', encoding = "utf-8")
    test_number = f1.read()
    test_number = test_number.split('\n')
    f1.close()
    return test_number

def modify_players_cards(player_card, txt_list, z, ID):
    commmand_list = txt_list[z].split()
    if (commmand_list[3] == 'HD') or (commmand_list[3] == 'MD'):
        player_card[ID].remove(commmand_list[4])
    elif (commmand_list[3] == 'M'):
        player_card[ID].append(commmand_list[4])
    elif (commmand_list[3] == 'EL') or (commmand_list[3] == 'EM') or (commmand_list[3] == 'ER'):
        player_card[ID].remove(commmand_list[4])
        player_card[ID].remove(commmand_list[6])
    elif (commmand_list[3] == 'P'):
        player_card[ID].remove(commmand_list[5])
        player_card[ID].remove(commmand_list[6])
    elif (commmand_list[3] == 'G'): #lite
        player_card[ID].remove(commmand_list[5])
        player_card[ID].remove(commmand_list[6])
        player_card[ID].remove(commmand_list[7])
    elif (commmand_list[3] == 'HG'): #dark
        player_card[ID].remove(commmand_list[4])
        player_card[ID].remove(commmand_list[5])
        player_card[ID].remove(commmand_list[6])
        player_card[ID].remove(commmand_list[7])
    elif (commmand_list[3] == 'UG'): #plus ex.1002ifle
        player_card[ID].remove(commmand_list[4])
    return player_card[ID]

def get_card_title(card):
    card_title = int(card)
    if(card_title > 999):
        print('[Warnig] - The card_id > 999')
    return str(math.floor(card_title / 10))

def get_answer(player_card, chucker):
    answer_list = []
    id = wind2id[chucker]
    player_card_int =[[],[],[],[]]
    for i in range(4):
        player_card_int[i] = [int(c) for c in player_card[i]]
    #print('player_card_int = ', player_card_int)
    for c in player_card_int[id]:
        # if not ckuck to other players
        b1 = hu_test(player_card_int[(id+1)%4], c)
        b2 = hu_test(player_card_int[(id+2)%4], c)
        b3 = hu_test(player_card_int[(id+3)%4], c)
        if not ((b1) or (b2) or (b3)):
            #print(hu_test(player_card_int[(id+1)%4], c), hu_test(player_card_int[(id+2)%4], c), hu_test(player_card_int[(id+3)%4], c), c)
            #print('c =', c, (id+1)%4,' =', b1, (id+2)%4, ' =', b2, (id+3)%4, ' =', b3)
            answer_list.append(str(c)[:2])
    answer_list_sort = list(set(answer_list))
    answer_list_sort.sort()
    return answer_list_sort

'''
def is_ready_hand(player_card):
    player_card_int = [int(c) for c in player_card]
    for i in range(11, 48):
        if i%10 == 0: continue
        x = i*10 +1
        if hu_test(player_card_int, x):
            return True
    return False
'''

def write_file(answer_list, out_file_fullpath, file):
    f = open(out_file_fullpath, 'a')
    # save the old file name at the top
    #f.write('>>' + file + '\n')
    f.write(' '.join(answer_list))
    f.write('\n')
    f.close()

# main
i = 0
test_number = read_test_number()
for file in os.listdir(read_dir):
    if file.split()[0] == test_number[i]:
        file_fullpath = read_dir+file
        # check (is file) and (the filename have chucker) and (is .command)
        if (os.path.isfile(file_fullpath)) and ('.txt' in file) and ('chucker' in file) and not('.command' in file):
            #print("filename = " + file)
            outfilename = 'answer.txt'
            #print('out: ' + outfilename)
            # read file to get txt_list(list of line)
            file_fullpath = read_dir + file
            txt_list = read_file(file_fullpath)
            # save the players seats
            player_ID = {'E': txt_list[1].split()[1], 'S': txt_list[1].split()[3], 'W': txt_list[1].split()[5], 'N': txt_list[1].split()[7]}
            # save the players cards
            master = txt_list[3].split()[3]
            player_card = [[], [], [], []]
            begin = 2
            end = 18
            player_card[wind2id[master]] = txt_list[5].split()[begin:end+1]
            for j in range(wind2id[master]+1, wind2id[master]+4):
                begin = end+1
                end = begin+15
                player_card[j%4] = txt_list[5].split()[begin:end+1]
            # modify the players cards
            j = 6
            while True:
                if txt_list[j].find('* Win') != -1:
                    last = j
                    break
                else:
                    j = j + 1
            for z in range(6, last-2):
                ID = wind2id[txt_list[z].split()[2]]
                player_card[ID] = modify_players_cards(player_card, txt_list, z, ID)
            for z in range(4):
                player_card[z].sort()
            chucker = txt_list[last-2].split()[2]
            # get cards whitch not chuck
            answer_list = get_answer(player_card, chucker)
            # write the answer the new place
            out_file_fullpath = out_dir + outfilename
            write_file(answer_list, out_file_fullpath, file)
            # print_info for debug
            print_info(txt_list, file, player_ID, last, player_card, answer_list, chucker)
            i = i + 1
