def processPgnHalfmove(move, state): """ Halfmove format: (Piece)(rank)(file)(x)[endloc](promotion)(check(m)) """ if re.match("O-O[^-]*", move): if state.getTurn() == Turn.WHITE: return Move.constructCastle(Castle.WKING) else: return Move.constructCastle(Castle.BKING) elif re.match("O-O-O[^-]*", move): if state.getTurn() == Turn.WHITE: return Move.constructCastle(Castle.WQUEEN) else: return Move.constructCastle(Castle.BQUEEN) # regstr = "([QKNRB]?)([a-h]?)([1-8]?)x?([a-h][1-8])((?=QNRB)?)[+#]?" regstr = "([QKNRB]?)([a-h]?)([1-8]?)x?([a-h][1-8])=?([QNRB]?)[+#]?" match = re.match(regstr, move) if match == None: logging.error(move) Piece, File, Rank, Endloc, Promotion = match.groups() return Move.constructFromPgnHalfmove(state, Piece, Rank, File, Endloc, Promotion)
def get_king_moves_func(game_state, row, col): moves = [] possibleMoves = [(row - 1, col - 1), (row - 1, col), (row - 1, col + 1), (row, col - 1), (row, col + 1), (row + 1, col - 1), (row + 1, col), (row + 1, col + 1)] if game_state.board[row][col][ 0] == game_state.cell_occupation_code_black: #if black player for move in possibleMoves: if (0 <= move[0] < 8) and (0 <= move[1] < 8): if game_state.board[move[0]][move[1]][ 0] == game_state.cell_occupation_code_black: #guarding move moves.append( Move(game_state, [row, col], [move[0], move[1]])) elif game_state.board[move[0]][move[1]][ 0] == game_state.cell_occupation_code_white: #capturing move moves.append( Move(game_state, [row, col], [move[0], move[1]])) if game_state.board[row][col][ 0] == game_state.cell_occupation_code_white: #if white player for move in possibleMoves: if (0 <= move[0] < 8) and (0 <= move[1] < 8): if game_state.board[move[0]][move[1]][ 0] == game_state.cell_occupation_code_white: #guarding move moves.append( Move(game_state, [row, col], [move[0], move[1]])) elif game_state.board[move[0]][move[1]][ 0] == game_state.cell_occupation_code_black: #capturing move moves.append( Move(game_state, [row, col], [move[0], move[1]])) return moves
def play_game(self): """ Load the model and check if the model performs better and save the result. """ self.reset() i = 0 while self.winner is None: if i % 2 == 0: legal_move = self.legal_move() move = self.chessClassW.getMovePreference( self.myState, legal_move) self.log_move(move) self.myState = move.apply(self.myState) else: while (True): move = input("Enter a move: ") if (self.board.is_legal(chess.Move.from_uci(move))): break else: print("Invalid Move Try Again") print() start_loc = move[:2] end_loc = move[2:] my_move = Move(start_loc, end_loc) self.log_move(my_move) self.myState = my_move.apply(self.myState) self.num_halfmoves += 1 if self.board.result(claim_draw=True) != "*": if self.winner is None: self.result = self.board.result(claim_draw=True) if self.result == '1-0': self.winner = 1 elif self.result == '0-1': self.winner = -1 else: self.winner = 0.5 print(move) print() i = i + 1 print(self.result)
def play(self, game_state, possible_moves): if self.is_ai_player: best_score = -1000000 best_move = None for move in possible_moves: next_state = GameState() fogged_move = Move( game_state.generate_fog_of_war_state(), [move.from_row_col[0], move.from_row_col[1]], [move.to_row_col[0], move.to_row_col[1]]) next_state.set_from_move(fogged_move) move_score = h(next_state, self.heuristic_weights) if move_score > best_score: best_score = move_score best_move = move return best_move else: game_state.generate_fog_of_war_state().print_board() print("Select a move: ") for i in range(len(possible_moves)): print( str(i) + ': ' + GameState.row_col_to_chess_position_str( possible_moves[i].from_row_col[0], possible_moves[i].from_row_col[1]) + ' -> ' + GameState.row_col_to_chess_position_str( possible_moves[i].to_row_col[0], possible_moves[i].to_row_col[1])) move_index = int(input()) return possible_moves[move_index]
def get_pawn_moves_func(game_state, row, col): moves = [] # Capture if (row < 7 and col < 7 and game_state.board[row][col][0] == game_state.cell_occupation_code_black and game_state.board[row + 1][col + 1][0] == game_state.cell_occupation_code_white): moves.append(Move(game_state, [row, col], [row + 1, col + 1])) if (row < 7 and col < 7 and game_state.board[row][col][0] == game_state.cell_occupation_code_black and game_state.board[row + 1][col + 1][0] == game_state.cell_occupation_code_black): moves.append(Move(game_state, [row, col], [row + 1, col + 1])) if (row < 7 and col > 0 and game_state.board[row][col][0] == game_state.cell_occupation_code_black and game_state.board[row + 1][col - 1][0] == game_state.cell_occupation_code_white): moves.append(Move(game_state, [row, col], [row + 1, col - 1])) if (row < 7 and col > 0 and game_state.board[row][col][0] == game_state.cell_occupation_code_black and game_state.board[row + 1][col - 1][0] == game_state.cell_occupation_code_black): moves.append(Move(game_state, [row, col], [row + 1, col - 1])) if (row > 0 and col < 7 and game_state.board[row][col][0] == game_state.cell_occupation_code_white and game_state.board[row - 1][col + 1][0] == game_state.cell_occupation_code_black): moves.append(Move(game_state, [row, col], [row - 1, col + 1])) if (row > 0 and col < 7 and game_state.board[row][col][0] == game_state.cell_occupation_code_white and game_state.board[row - 1][col + 1][0] == game_state.cell_occupation_code_white): moves.append(Move(game_state, [row, col], [row - 1, col + 1])) if (row > 0 and col > 0 and game_state.board[row][col][0] == game_state.cell_occupation_code_white and game_state.board[row - 1][col - 1][0] == game_state.cell_occupation_code_black): moves.append(Move(game_state, [row, col], [row - 1, col - 1])) if (row > 0 and col > 0 and game_state.board[row][col][0] == game_state.cell_occupation_code_white and game_state.board[row - 1][col - 1][0] == game_state.cell_occupation_code_white): moves.append(Move(game_state, [row, col], [row - 1, col - 1])) return moves
def legal_move(self): """ Function to get all the legal moves. """ # Portion of code before the move is made. legal_moves = [] for move in self.board.legal_moves: move_str = move.uci() start_loc = move_str[:2] end_loc = move_str[2:] my_move = Move(start_loc, end_loc) legal_moves.append(my_move) return legal_moves
def legal_move(self): """ Function to get all the legal moves. """ # used python-chess to get all legal moves. legal_moves = [] for move in self.board.legal_moves: move_str = move.uci() start_loc = move_str[:2] end_loc = move_str[2:] # Casting the move returned by the python-chess to our implementation. my_move = Move(start_loc, end_loc) legal_moves.append(my_move) return legal_moves
def main(): parser = argparse.ArgumentParser() parser.add_argument( "-T", "--outTensor", help="File to which to write out loaded and parsed data", type=argparse.FileType("wb"), nargs="?") parser.add_argument( "-M", "--outModel", help="File to which to write out loaded, parsed, and trained model", type=argparse.FileType("wb"), nargs="?") group = parser.add_argument_group("Source Data", "Data source for the Supervised model") group.add_argument("-m", "--modelIn", help="File containing saved trained model", type=argparse.FileType("rb"), nargs="?") group.add_argument("-p", "--pgnFiles", help="Filename(s) of pgn games to load", type=argparse.FileType("r"), nargs="+") group.add_argument( "-t", "--tensorData", help="Filename of file containing saved tensor data of a dataset", type=argparse.FileType("rb"), nargs="?") args = parser.parse_args() if not (args.modelIn or args.pgnFiles or args.tensorData): parser.error( "Must have Source Data; use --modelIn, --pgnFiles, or --tensorData option" ) exit() supChess = SupervisedChess(savedModel=args.modelIn) supChess.trainModel(pgnTensors=args.tensorData, pgnFiles=args.pgnFiles, outTensor=args.outTensor, outModel=args.outModel) myState = GameState.getInitialState() myMove = Move("b2", "b4") legalMoves = [ Move('a2', 'a3'), Move('a2', 'a4'), Move('b2', 'b3'), Move('b2', 'b4'), Move('c2', 'c3'), Move('c2', 'c4'), Move('d2', 'd3'), Move('d2', 'd4'), Move('e2', 'e3'), Move('e2', 'e4'), Move('f2', 'f3'), Move('f2', 'f4'), Move('g2', 'g3'), Move('g2', 'g4'), Move('h2', 'h3'), Move('h2', 'h4'), Move('b1', 'a3'), Move('b1', 'c3'), Move('g1', 'f3'), Move('g1', 'h3'), ] bestMove = supChess.getMovePreference(myState, legalMoves) logging.info("Best move registered as %s" % bestMove)
def get_bishop_moves_func(game_state, row, col): moves = [] if game_state.board[row][col][ 0] == game_state.cell_occupation_code_black: #if black player for r, c in zip(range(row - 1, -1, -1), range(col - 1, -1, -1)): #seacrhes diag1 if game_state.board[r][c][ 0] == game_state.cell_occupation_code_white: #capturing move moves.append(Move(game_state, [row, col], [r, c])) break elif game_state.board[r][c][ 0] == game_state.cell_occupation_code_black: #guarding move moves.append(Move(game_state, [row, col], [r, c])) break for r, c in zip(range(row + 1, 8, 1), range(col + 1, 8, 1)): #seacrhes diag2 if game_state.board[r][c][ 0] == game_state.cell_occupation_code_white: #capturing move moves.append(Move(game_state, [row, col], [r, c])) break elif game_state.board[r][c][ 0] == game_state.cell_occupation_code_black: #guarding move moves.append(Move(game_state, [row, col], [r, c])) break for r, c in zip(range(row + 1, 8, 1), range(col - 1, -1, -1)): #seacrhes diag3 if game_state.board[r][c][ 0] == game_state.cell_occupation_code_white: #capturing move moves.append(Move(game_state, [row, col], [r, c])) break elif game_state.board[r][c][ 0] == game_state.cell_occupation_code_black: #guarding move moves.append(Move(game_state, [row, col], [r, c])) break for r, c in zip(range(row - 1, -1, -1), range(col + 1, 8, 1)): #seacrhes diag3 if game_state.board[r][c][ 0] == game_state.cell_occupation_code_white: #capturing move moves.append(Move(game_state, [row, col], [r, c])) break elif game_state.board[r][c][ 0] == game_state.cell_occupation_code_black: #guarding move moves.append(Move(game_state, [row, col], [r, c])) break if game_state.board[row][col][ 0] == game_state.cell_occupation_code_white: #if white player for r, c in zip(range(row - 1, -1, -1), range(col - 1, -1, -1)): #seacrhes diag1 if game_state.board[r][c][ 0] == game_state.cell_occupation_code_black: #capturing move moves.append(Move(game_state, [row, col], [r, c])) break elif game_state.board[r][c][ 0] == game_state.cell_occupation_code_white: #guarding move moves.append(Move(game_state, [row, col], [r, c])) break for r, c in zip(range(row + 1, 8, 1), range(col + 1, 8, 1)): #seacrhes diag2 if game_state.board[r][c][ 0] == game_state.cell_occupation_code_black: #capturing move moves.append(Move(game_state, [row, col], [r, c])) break elif game_state.board[r][c][ 0] == game_state.cell_occupation_code_white: #guarding move moves.append(Move(game_state, [row, col], [r, c])) break for r, c in zip(range(row + 1, 8, 1), range(col - 1, -1, -1)): #seacrhes diag3 if game_state.board[r][c][ 0] == game_state.cell_occupation_code_black: #capturing move moves.append(Move(game_state, [row, col], [r, c])) break elif game_state.board[r][c][ 0] == game_state.cell_occupation_code_white: #guarding move moves.append(Move(game_state, [row, col], [r, c])) break for r, c in zip(range(row - 1, -1, -1), range(col + 1, 8, 1)): #seacrhes diag3 if game_state.board[r][c][ 0] == game_state.cell_occupation_code_black: #capturing move moves.append(Move(game_state, [row, col], [r, c])) break elif game_state.board[r][c][ 0] == game_state.cell_occupation_code_white: #guarding move moves.append(Move(game_state, [row, col], [r, c])) break return moves
def get_rook_moves_func(game_state, row, col): #excuse me for this awfully long function moves = [] if game_state.board[row][col][ 0] == game_state.cell_occupation_code_black: #if black player for i in range(row - 1, -1, -1): #searches vertically, upwards if game_state.board[i][col][ 0] == game_state.cell_occupation_code_white: #capturing move moves.append(Move(game_state, [row, col], [i, col])) break elif game_state.board[i][col][ 0] == game_state.cell_occupation_code_black: #guarding move moves.append(Move(game_state, [row, col], [i, col])) break for i in range(row + 1, 8, 1): #searches vertically, downwards if game_state.board[i][col][ 0] == game_state.cell_occupation_code_white: #capturing move moves.append(Move(game_state, [row, col], [i, col])) break elif game_state.board[i][col][ 0] == game_state.cell_occupation_code_black: #guarding move moves.append(Move(game_state, [row, col], [i, col])) break for i in range(col + 1, 8, 1): #searches horizontally to the right of the piece if game_state.board[row][i][ 0] == game_state.cell_occupation_code_white: #capturing move moves.append(Move(game_state, [row, col], [row, i])) break elif game_state.board[row][i][ 0] == game_state.cell_occupation_code_black: #guarding move moves.append(Move(game_state, [row, col], [row, i])) break for i in range(col - 1, -1, -1): #searches horizontally to the left of the piece if game_state.board[row][i][ 0] == game_state.cell_occupation_code_white: #capturing move moves.append(Move(game_state, [row, col], [row, i])) break elif game_state.board[row][i][ 0] == game_state.cell_occupation_code_black: #guarding move moves.append(Move(game_state, [row, col], [row, i])) break elif game_state.board[row][col][ 0] == game_state.cell_occupation_code_white: #if white player for i in range(row - 1, -1, -1): #searches vertically, upwards if game_state.board[i][col][ 0] == game_state.cell_occupation_code_black: #capturing move moves.append(Move(game_state, [row, col], [i, col])) break elif game_state.board[i][col][ 0] == game_state.cell_occupation_code_white: #guarding move moves.append(Move(game_state, [row, col], [i, col])) break for i in range(row + 1, 8, 1): #searches vertically, downwards if game_state.board[i][col][ 0] == game_state.cell_occupation_code_black: #capturing move moves.append(Move(game_state, [row, col], [i, col])) break elif game_state.board[i][col][ 0] == game_state.cell_occupation_code_white: #guarding move moves.append(Move(game_state, [row, col], [i, col])) break for i in range(col + 1, 8, 1): #searches horizontally to the right of the piece if game_state.board[row][i][ 0] == game_state.cell_occupation_code_black: #capturing move moves.append(Move(game_state, [row, col], [row, i])) break elif game_state.board[row][i][ 0] == game_state.cell_occupation_code_white: #guarding move moves.append(Move(game_state, [row, col], [row, i])) break for i in range(col - 1, -1, -1): #searches horizontally to the left of the piece if game_state.board[row][i][ 0] == game_state.cell_occupation_code_black: #capturing move moves.append(Move(game_state, [row, col], [row, i])) break elif game_state.board[row][i][ 0] == game_state.cell_occupation_code_white: #guarding move moves.append(Move(game_state, [row, col], [row, i])) break return moves
def reset(self): self.num_halfmoves = 0 self.winner = None # type: Winner self.turn = None self.resigned = False self.result = None self.board = chess.Board() self.myState = GameState.getInitialState() self.legalMoves = [ Move('a2', 'a3'), Move('a2', 'a4'), Move('b2', 'b3'), Move('b2', 'b4'), Move('c2', 'c3'), Move('c2', 'c4'), Move('d2', 'd3'), Move('d2', 'd4'), Move('e2', 'e3'), Move('e2', 'e4'), Move('f2', 'f3'), Move('f2', 'f4'), Move('g2', 'g3'), Move('g2', 'g4'), Move('h2', 'h3'), Move('h2', 'h4'), Move('b1', 'a3'), Move('b1', 'c3'), Move('g1', 'f3'), Move('g1', 'h3'), ]