HaileyStorm commited on
Commit
c216eb0
1 Parent(s): 5dcd8bc

Update chess-gpt-eval/mainvs.py

Browse files
Files changed (1) hide show
  1. chess-gpt-eval/mainvs.py +20 -18
chess-gpt-eval/mainvs.py CHANGED
@@ -355,7 +355,7 @@ def get_player_titles_and_time(
355
 
356
  used_openings = []
357
  def random_book_opening(
358
- game_state: str, board: chess.Board
359
  ) -> Tuple[str, chess.Board]:
360
  global used_openings
361
  with open("openings.csv", "r") as file:
@@ -364,11 +364,9 @@ def random_book_opening(
364
  while moves_string in used_openings:
365
  moves_string = random.choice(lines)
366
  used_openings.append(moves_string)
367
- if move_num_in_gamestate:
368
- game_state = moves_string.rstrip() + " "
369
- else:
370
- game_state = ' '.join(['.' + m.split(".")[-1] if "." in m else m for m in moves_string.split()])
371
- game_state = game_state.rstrip() + " "
372
  # Splitting the moves string on spaces
373
  tokens = moves_string.split()
374
 
@@ -380,11 +378,11 @@ def random_book_opening(
380
  move = token
381
 
382
  board.push_san(move)
383
- return game_state.rstrip(), board, len(tokens) // 2
384
 
385
 
386
  def add_random_moves(
387
- game_state: str, board: chess.Board, num_moves: int = 20
388
  ) -> Tuple[str, chess.Board, int]:
389
  for i in range(num_moves * 2): # Full moves to half moves
390
  legal_moves = list(board.legal_moves)
@@ -395,15 +393,16 @@ def add_random_moves(
395
  board.push(board.parse_san(move))
396
 
397
  if board.turn == chess.BLACK:
398
- game_state += f" {i//2 + 1}.{move}" if move_num_in_gamestate else f" .{move}"
 
399
  else:
400
- game_state += f" {move}"
 
401
 
402
  if board.is_game_over():
403
  return None, None, 0 # Game over, discard the game
404
 
405
- game_state = game_state.strip()
406
- return game_state, board, num_moves
407
 
408
 
409
  # Return is (move_san, move_uci, attempts, is_resignation, is_illegal_move)
@@ -508,11 +507,14 @@ def play_games(
508
  board = chess.Board()
509
 
510
  if book_opening:
511
- game_state, board, opening_moves = random_book_opening(game_state, board)
512
  elif random_opening:
513
- while True:
514
- game_state, board, opening_moves = add_random_moves(game_state, board, random_opening_moves)
515
- if game_state is not None:
 
 
 
516
  break
517
  else:
518
  opening_moves = 0
@@ -601,14 +603,14 @@ def play_games(
601
  print(f"Result: {board.result()}")
602
  print(board)
603
  print()
604
- game_transcript = game_state.strip()
605
  if game_transcript not in unique_games:
606
  unique_games.add(game_transcript)
607
  record_results(
608
  board,
609
  player_one,
610
  player_two,
611
- game_state,
612
  player_one_illegal_moves,
613
  player_one_illegal_attempts,
614
  player_two_illegal_moves,
 
355
 
356
  used_openings = []
357
  def random_book_opening(
358
+ game_state_with_move_num: str, game_state_without_move_num: str, board: chess.Board
359
  ) -> Tuple[str, chess.Board]:
360
  global used_openings
361
  with open("openings.csv", "r") as file:
 
364
  while moves_string in used_openings:
365
  moves_string = random.choice(lines)
366
  used_openings.append(moves_string)
367
+ game_state_with_move_num = moves_string.rstrip() + " "
368
+ game_state_without_move_num = ' '.join(['.' + m.split(".")[-1] if "." in m else m for m in moves_string.split()])
369
+ game_state_without_move_num = game_state_without_move_num.rstrip() + " "
 
 
370
  # Splitting the moves string on spaces
371
  tokens = moves_string.split()
372
 
 
378
  move = token
379
 
380
  board.push_san(move)
381
+ return game_state_with_move_num.rstrip(), game_state_without_move_num.rstrip(), board, len(tokens) // 2
382
 
383
 
384
  def add_random_moves(
385
+ game_state_with_move_num: str, game_state_without_move_num: str, board: chess.Board, num_moves: int = 20
386
  ) -> Tuple[str, chess.Board, int]:
387
  for i in range(num_moves * 2): # Full moves to half moves
388
  legal_moves = list(board.legal_moves)
 
393
  board.push(board.parse_san(move))
394
 
395
  if board.turn == chess.BLACK:
396
+ game_state_with_move_num += f" {i//2 + 1}.{move}"
397
+ game_state_without_move_num += f" .{move}"
398
  else:
399
+ game_state_with_move_num += f" {move}"
400
+ game_state_without_move_num += f" {move}"
401
 
402
  if board.is_game_over():
403
  return None, None, 0 # Game over, discard the game
404
 
405
+ return game_state_with_move_num.strip(), game_state_without_move_num.strip(), board, num_moves
 
406
 
407
 
408
  # Return is (move_san, move_uci, attempts, is_resignation, is_illegal_move)
 
507
  board = chess.Board()
508
 
509
  if book_opening:
510
+ game_state_with_move_num, game_state_without_move_num, board, opening_moves = random_book_opening(game_state_with_move_num, game_state_without_move_num, board)
511
  elif random_opening:
512
+ for _ in range(10):
513
+ g1, g2, b, opening_moves = add_random_moves(game_state_with_move_num, game_state_without_move_num, board, random_opening_moves)
514
+ if g1 is not None:
515
+ game_state_with_move_num = g1
516
+ game_state_without_move_num = g2
517
+ board = b
518
  break
519
  else:
520
  opening_moves = 0
 
603
  print(f"Result: {board.result()}")
604
  print(board)
605
  print()
606
+ game_transcript = game_state_without_move_num.strip()
607
  if game_transcript not in unique_games:
608
  unique_games.add(game_transcript)
609
  record_results(
610
  board,
611
  player_one,
612
  player_two,
613
+ game_transcript,
614
  player_one_illegal_moves,
615
  player_one_illegal_attempts,
616
  player_two_illegal_moves,