Skip to content

Instantly share code, notes, and snippets.

@windfall-shogi
Created September 23, 2019 05:39
Show Gist options
  • Save windfall-shogi/eac9e3644720d5abfc665b3179b76444 to your computer and use it in GitHub Desktop.
Save windfall-shogi/eac9e3644720d5abfc665b3179b76444 to your computer and use it in GitHub Desktop.
cshogiを用いたfv40
def fv40(board):
p_index, q_index = [], []
for square in range(81):
piece = board.piece(square)
if piece == 0:
continue
piece_type = piece & 0xF
if piece_type != 8:
piece_color = piece >> 4
p_value, q_value = 12 + piece_color, 12 + 1 - piece_color
offset = PIECE_OFFSET[piece_type]
p_offset, q_offset = p_value + offset, q_value + offset
p_index.append(square + BonaPiece[p_offset])
q_index.append(80 - square + BonaPiece[q_offset])
else:
if piece == 8:
p = square
else:
q = 80 - square
pieces_in_hand = board.pieces_in_hand
for color in range(2):
for piece_type in range(7):
p_hand = BonaPiece[color + piece_type * 2]
q_hand = BonaPiece[1 - color + piece_type * 2]
piece_count = pieces_in_hand[color][piece_type]
for i in range(piece_count):
p_index.append(p_hand + i)
q_index.append(q_hand + i)
if board.turn == cshogi.BLACK:
# noinspection PyUnboundLocalVariable
return p, q, p_index, q_index
else:
# noinspection PyUnboundLocalVariable
return q, p, q_index, p_index
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import unittest
import random
import shogi
import cshogi
# python-shogiで作られたfv40を使っているモジュール
import make_record as make_record1
# cshogiで作られたfv40を使っているモジュール
import make_record2
__author__ = 'Yasuhiro'
__date__ = '2019/9/22'
class MyTestCase(unittest.TestCase):
def _check(self, board1, board2):
val1 = make_record1.fv40(board1)
val2 = make_record2.fv40(board2)
self.assertEqual(val1[0], val2[0])
self.assertEqual(val1[1], val2[1])
self.assertSetEqual(set(val1[2]), set(val2[2]))
self.assertSetEqual(set(val1[3]), set(val2[3]))
def test_hirate(self):
board1 = shogi.Board()
board2 = cshogi.Board()
self._check(board1, board2)
def test_cshogi_sample(self):
# https://github.com/TadaoYamaoka/cshogi/blob/master/README.rst
sfen = ('ln4skl/3r1g3/1p2pgnp1/p1ppsbp1p/5p3/2PPP1P1P/PPBSSG1P1'
'/2R3GK1/LN5NL b P 43')
board1 = shogi.Board(sfen)
board2 = cshogi.Board(sfen)
self._check(board1, board2)
def test_evaltest_nnue_sample1(self):
# https://github.com/bleu48/shogi-eval/blob/master/evaltest_nnue.py
sfen = ('lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL '
'b - 1')
board1 = shogi.Board(sfen)
board2 = cshogi.Board(sfen)
self._check(board1, board2)
def test_evaltest_nnue_sample2(self):
# https://github.com/bleu48/shogi-eval/blob/master/evaltest_nnue.py
sfen = ('lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL '
'w - 1')
board1 = shogi.Board(sfen)
board2 = cshogi.Board(sfen)
self._check(board1, board2)
def test_evaltest_nnue_sample3(self):
# https://github.com/bleu48/shogi-eval/blob/master/evaltest_nnue.py
sfen = ('l6nl/5+P1gk/2np1S3/p1p4Pp/3P2Sp1/1PPb2P1P/P5GS1/R8/LN4bKL '
'w RGgsn5p 1')
board1 = shogi.Board(sfen)
board2 = cshogi.Board(sfen)
self._check(board1, board2)
def test_evaltest_nnue_sample4(self):
# https://github.com/bleu48/shogi-eval/blob/master/evaltest_nnue.py
sfen = ('l6nl/5+P1gk/2np1S3/p1p4Pp/3P2Sp1/1PPb2P1P/P5GS1/R8/LN4bKL '
'b RGgsn5p 1')
board1 = shogi.Board(sfen)
board2 = cshogi.Board(sfen)
self._check(board1, board2)
def test_random1(self):
random.seed(1)
board1 = shogi.Board()
board2 = cshogi.Board()
for i in range(512):
legal_moves = list(board2.legal_moves)
if len(legal_moves) == 0:
break
move = random.choice(legal_moves)
move = cshogi.move_to_usi(move)
board1.push_usi(move)
board2.push_usi(move)
with self.subTest(i=i, board1=board1, board2=board2):
self._check(board1, board2)
def test_random2(self):
random.seed(2)
board1 = shogi.Board()
board2 = cshogi.Board()
for i in range(512):
legal_moves = list(board2.legal_moves)
if len(legal_moves) == 0:
break
move = random.choice(legal_moves)
move = cshogi.move_to_usi(move)
board1.push_usi(move)
board2.push_usi(move)
with self.subTest(i=i, board1=board1, board2=board2):
self._check(board1, board2)
def test_random3(self):
random.seed(3)
board1 = shogi.Board()
board2 = cshogi.Board()
for i in range(512):
legal_moves = list(board2.legal_moves)
if len(legal_moves) == 0:
break
move = random.choice(legal_moves)
move = cshogi.move_to_usi(move)
board1.push_usi(move)
board2.push_usi(move)
with self.subTest(i=i, board1=board1, board2=board2):
self._check(board1, board2)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment