7.5~7.7 read_kifu.py read_kifu() Argument: A text file that lists the paths of the game record files Output: ("Phase diagram", "Move", "Win / Loss") is calculated for each phase, one phase is added to the list as one element, and when all the game records have been read, it is output as one list. The data for one phase consists of the following five elements. -piece_bb: 15 elements -occupied: 2 elements -pieces_in_hand: 2 elements -move_label: 1 element -win: 1 element The final output is [([15 elements], [2 elements], [2 elements], [1 element], [1 element]), (same set), ... is the number of steps x number of games]
python-dlshogi\pydlshogi\read_kifu.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import shogi
import shogi.CSA
import copy
from pydlshogi.features import *
from pydlshogi.common import * ##################test
# read kifu
def read_kifu(kifu_list_file):
i = 0
positions = []
with open(kifu_list_file, 'r') as f:
for line in f.readlines():
filepath = line.rstrip('\r\n')
i += 1
print(i)
#Assign the game record data to the variable kifu.
#The game record data is a dictionary with four keys: names, sfen, moves, and win.
#This dictionary is included in the list as an element.[0]Take out only the dictionary with.
kifu = shogi.CSA.Parser.parse_file(filepath)[0]
win_color = shogi.BLACK if kifu['win'] == 'b' else shogi.WHITE
board = shogi.Board()
for move in kifu['moves']:
# ■board:print(board)You can display the board in two dimensions with.
# ■piece_bb: An array of 15 elements. Each element indicates the arrangement of each piece. 0:Blank, 1:Ayumu, 2:Incense ...
#bit board. The bit board is as follows.
#Each element has 81 digits (=It is a decimal number display of a binary number (that is, a bit board) of 81 squares).
#Print if you want to display 81 binary digits('{:0=81b}'.format(Decimal value))Can be done with.
#■ occupied: An array of two elements. Each element is the position of the piece occupied by the first move and the second move. bit board.
# ■pieces_in_hand: An array of two elements. Which piece is the first move and the second move for each element(=key)How many(=value)A dictionary type that indicates whether you have it.
if board.turn == shogi.BLACK:
piece_bb = copy.deepcopy(board.piece_bb)
occupied = copy.deepcopy((board.occupied[shogi.BLACK], board.occupied[shogi.WHITE]))
pieces_in_hand = copy.deepcopy((board.pieces_in_hand[shogi.BLACK], board.pieces_in_hand[shogi.WHITE]))
else:
piece_bb = [bb_rotate_180(bb) for bb in board.piece_bb]
occupied = (bb_rotate_180(board.occupied[shogi.WHITE]), bb_rotate_180(board.occupied[shogi.BLACK]))
pieces_in_hand = copy.deepcopy((board.pieces_in_hand[shogi.WHITE], board.pieces_in_hand[shogi.BLACK]))
# move label
i_move = shogi.Move.from_usi(move) #Create an instance of the Move class with the move variable move as an argument
move_label = make_output_label(i_move, board.turn)
#■ Move class
# from_square variable: The value of the movement source when the board surface is represented by a numerical value from 0 to 80.
#The quotient when divided by 9 is the y coordinate, and the remainder is the x coordinate. The xy coordinate is 0 origin.
# to_square variable: Same as above (destination).
#
#x coordinate
# 0 1 2 3 4 5 6 7 8
#
#0 1 2 3 4 5 6 7 8 0 y coordinates
# 9 10 11 12 13 14 15 16 17 1
# 18 19 20 21 22 23 24 25 26 2
# 27 28 29 30 31 32 33 34 35 3
# 36 37 38 39 40 41 42 43 44 4
# 45 46 47 48 49 50 51 52 53 5
# 54 55 56 57 58 59 60 61 62 6
# 63 64 65 66 67 68 69 70 71 7
# 72 73 74 75 76 77 78 79 80 8
#
# print(board)
#
# try:
# y_from, x_from = divmod(s.from_square, 9)
# y_to, x_to = divmod(s.to_square, 9)
# print('from:',x_from, y_from)
# print('to :',x_to, y_to)
#
# move_direction = DOWN
# print('moved:', move_direction)
# except:
# pass
# result
win = 1 if win_color == board.turn else 0
#Added aspects (first three variables), moves in that aspect, and wins / losses to the variable positions
positions.append((piece_bb, occupied, pieces_in_hand, move_label, win))
#1 move forward
board.push_usi(move)
return positions
Recommended Posts