Skip to content

Commit

Permalink
Merge c8132e3 into 38f1c0d
Browse files Browse the repository at this point in the history
  • Loading branch information
ishidakei committed Nov 18, 2017
2 parents 38f1c0d + c8132e3 commit 23c0031
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
23 changes: 13 additions & 10 deletions shogi/CSA.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,18 @@ def wait_server_message(self, board, block=True):
line = self.read_line(block)
if line is None:
return None
if line[0] in COLOR_SYMBOLS:
(move_str, time_str) = line.split(',')
(color, usi) = Parser.parse_move_str(move_str, board)
return (color, usi, self.parse_consumed_time_str(time_str), None)
elif line[0] in ['#', '%']:
message = SERVER_MESSAGE_SYMBOLS.index(line[1:])
return (None, None, None, message)
else:
raise ValueError('Invalid lines')
return self.parse_server_message(line, board)

def parse_server_message(self, line, board):
if line[0] in COLOR_SYMBOLS:
(move_str, time_str) = line.split(',')
(color, usi) = Parser.parse_move_str(move_str, board)
return (color, usi, self.parse_consumed_time_str(time_str), None)
elif line[0] in ['#', '%']:
message = SERVER_MESSAGE_SYMBOLS.index(line[1:])
return (None, None, None, message)
else:
raise ValueError('Invalid lines')

def parse_consumed_time_str(self, time_str):
# This function always returns float seconds.
Expand Down Expand Up @@ -503,7 +506,7 @@ def move(self, piece_type, color, move):
from_square,
SQUARE_NAMES[move.to_square],
PIECE_SYMBOLS[piece_type])
line = self.command(command)
return self.command(command)

def resign(self):
# TODO: check RESIGN
Expand Down
10 changes: 10 additions & 0 deletions tests/csa_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,18 @@ def test_match(self):
self.add_response(tcp, '+5756FU,T1\n')
(turn, usi, spend_time, message) = tcp.wait_server_message(board)
board.push(shogi.Move.from_usi(usi))
self.assertEqual(turn, shogi.BLACK)
self.assertEqual(spend_time, 1.0)

self.assertEqual(board.sfen(), 'lnsgkgsnl/1r5b1/ppppppppp/9/9/4P4/PPPP1PPPP/1B5R1/LNSGKGSNL w - 2')

next_move = shogi.Move.from_usi('8c8d')
board.push(next_move)
self.add_response(tcp, '-8384FU,T2\n')
response_line = tcp.move(board.pieces[next_move.to_square], shogi.WHITE, next_move)
(turn, usi, spend_time, message) = tcp.parse_server_message(response_line, board)
self.assertEqual(turn, shogi.WHITE)
self.assertEqual(spend_time, 2.0)

if __name__ == '__main__':
unittest.main()

0 comments on commit 23c0031

Please sign in to comment.