Created
September 10, 2017 05:59
-
-
Save seinosuke/a481196230676e038e779fdd968feed5 to your computer and use it in GitHub Desktop.
リバーシのGGFファイルから局面と評価値のセットを抽出するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "reversi" | |
# 盤面の状態を出力する際の見た目設定 | |
Reversi.configure do |config| | |
config.disk_color_b = 'cyan' | |
config.disk_b = "O" | |
config.disk_w = "O" | |
end | |
# 初期状態から始まっている8x8盤面のみ読み込む | |
valid_board = /BO\[8 -------- -------- -------- ---O\*--- ---\*O--- -------- -------- -------- \*\]/ | |
# 読み込むGGFファイル | |
ggf_path = "./hoge.ggf" | |
File.open(ggf_path, "r") do |ggf_file| | |
# 同じ階層に読み込み結果のcsvファイルを生成 | |
csv_path = ggf_path << ".csv" | |
File.open(csv_path, "w") do |csv_file| | |
ggf_file.each_line do |line| | |
# 初期状態が不正なものは除く | |
next unless line.match(valid_board) | |
# 大文字に統一し着手履歴のみ抽出 | |
moves_info = line | |
.gsub("(;", "").gsub(";)", "").chomp | |
.gsub("]/", "/").upcase | |
.split("*]").last.split("]") | |
# 着手履歴から試合を再生 | |
game = Reversi::Game.new | |
moves_info.each do |move_info| | |
col, info = move_info.split("[") | |
info = info.split("/") | |
move = info.first | |
# 一手進める | |
unless ["PASS", "PA"].include?(move) | |
move = move.tr("A-H", "1-8").split("")[0..1].map(&:to_i) | |
case col | |
when "B" then game.player_b.put_disk(*move) | |
when "W" then game.player_w.put_disk(*move) | |
end | |
game.board.push_stack | |
end | |
# 評価値と局面のセットを記録 | |
if (info.size > 1) && (!info[1].empty?) | |
val = info[1].to_f | |
val = -val if col == "W" | |
b_bb = game.board.stack.last[:black] | |
w_bb = game.board.stack.last[:white] | |
e_bb = 0xFFFF_FFFF_FFFF_FFFF ^ (b_bb | w_bb) | |
csv_file.puts "#{b_bb}, #{w_bb}, #{e_bb}, #{val}" | |
end | |
end | |
# puts game.board | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment