feat: add IDDFS player
This commit is contained in:
parent
78b90d4027
commit
614bb5acb2
@ -1,26 +1,6 @@
|
|||||||
import chess
|
import chess
|
||||||
import math
|
import math
|
||||||
|
|
||||||
def b(board: chess.Board, color: chess.Color):
|
|
||||||
piece_values = {
|
|
||||||
chess.PAWN: 1,
|
|
||||||
chess.KNIGHT: 3,
|
|
||||||
chess.BISHOP: 3,
|
|
||||||
chess.ROOK: 5,
|
|
||||||
chess.QUEEN: 9,
|
|
||||||
chess.KING: 0
|
|
||||||
}
|
|
||||||
|
|
||||||
score = 0
|
|
||||||
for square in chess.SQUARES:
|
|
||||||
piece = board.piece_at(square)
|
|
||||||
if piece is not None:
|
|
||||||
if piece.color == chess.WHITE:
|
|
||||||
score += piece_values[piece.piece_type]
|
|
||||||
else:
|
|
||||||
score -= piece_values[piece.piece_type]
|
|
||||||
return score
|
|
||||||
|
|
||||||
def h_shannon(board: chess.Board, color: chess.Color):
|
def h_shannon(board: chess.Board, color: chess.Color):
|
||||||
piece_values = {
|
piece_values = {
|
||||||
chess.PAWN: 1,
|
chess.PAWN: 1,
|
||||||
@ -42,6 +22,7 @@ def h_shannon(board: chess.Board, color: chess.Color):
|
|||||||
|
|
||||||
for square in board.piece_map():
|
for square in board.piece_map():
|
||||||
piece = board.piece_at(square)
|
piece = board.piece_at(square)
|
||||||
|
|
||||||
if piece is not None:
|
if piece is not None:
|
||||||
n = 1 if piece.color == color else -1
|
n = 1 if piece.color == color else -1
|
||||||
acc += n * piece_values[piece.piece_type]
|
acc += n * piece_values[piece.piece_type]
|
||||||
|
@ -24,14 +24,21 @@ def play(white: player.PlayerInterface, black: player.PlayerInterface):
|
|||||||
board = chess.Board()
|
board = chess.Board()
|
||||||
white.newGame(chess.WHITE)
|
white.newGame(chess.WHITE)
|
||||||
black.newGame(chess.BLACK)
|
black.newGame(chess.BLACK)
|
||||||
|
whiteTime = 0
|
||||||
|
blackTime = 0
|
||||||
|
|
||||||
i = 1
|
i = 1
|
||||||
while not board.is_game_over():
|
while not board.is_game_over():
|
||||||
try:
|
try:
|
||||||
|
st = time.time()
|
||||||
if board.turn == chess.WHITE:
|
if board.turn == chess.WHITE:
|
||||||
move = step(board, white, black)
|
move = step(board, white, black)
|
||||||
|
nd = time.time()
|
||||||
|
whiteTime += (nd - st)
|
||||||
else:
|
else:
|
||||||
move = step(board, black, white)
|
move = step(board, black, white)
|
||||||
|
nd = time.time()
|
||||||
|
blackTime += (nd - st)
|
||||||
except:
|
except:
|
||||||
print(board)
|
print(board)
|
||||||
print(f"{color2str(board.turn)} cheated, end the game")
|
print(f"{color2str(board.turn)} cheated, end the game")
|
||||||
@ -42,9 +49,12 @@ def play(white: player.PlayerInterface, black: player.PlayerInterface):
|
|||||||
print(board)
|
print(board)
|
||||||
print(f"Move {move}")
|
print(f"Move {move}")
|
||||||
print(f"Turn {i}")
|
print(f"Turn {i}")
|
||||||
|
print(f"Time consumed : {nd - st:.5}")
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
print(board.outcome())
|
print(board.outcome())
|
||||||
|
print(f"White time : {whiteTime:.5}")
|
||||||
|
print(f"Black time : {blackTime:.5}")
|
||||||
|
|
||||||
return board
|
return board
|
||||||
|
|
||||||
@ -74,6 +84,7 @@ def t_enum(board, depth):
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
black = player.RandomPlayer()
|
black = player.RandomPlayer()
|
||||||
white = player.AlphaBetaPlayer()
|
white = player.AlphaBetaPlayer()
|
||||||
|
# white = player.IDDFSPlayer()
|
||||||
|
|
||||||
board = play(white, black)
|
board = play(white, black)
|
||||||
print(board)
|
print(board)
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from random import randint, choice
|
from random import randint, choice
|
||||||
|
import time
|
||||||
import math
|
import math
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
import chess
|
import chess
|
||||||
@ -125,13 +126,8 @@ class MinMaxPlayer(PlayerInterface):
|
|||||||
return move
|
return move
|
||||||
|
|
||||||
|
|
||||||
def alphabeta(
|
# Returns heuristic, move
|
||||||
board: chess.Board,
|
def _alphabeta(
|
||||||
heuristic: Callable[[chess.Board, chess.Color], float],
|
|
||||||
color: chess.Color,
|
|
||||||
depth: int = 3,
|
|
||||||
) -> chess.Move:
|
|
||||||
def aux(
|
|
||||||
board: chess.Board,
|
board: chess.Board,
|
||||||
heuristic: Callable[[chess.Board, chess.Color], float],
|
heuristic: Callable[[chess.Board, chess.Color], float],
|
||||||
color: chess.Color,
|
color: chess.Color,
|
||||||
@ -148,7 +144,7 @@ def alphabeta(
|
|||||||
for move in board.generate_legal_moves():
|
for move in board.generate_legal_moves():
|
||||||
board.push(move)
|
board.push(move)
|
||||||
value = (
|
value = (
|
||||||
aux(
|
_alphabeta(
|
||||||
board,
|
board,
|
||||||
alpha=alpha,
|
alpha=alpha,
|
||||||
beta=beta,
|
beta=beta,
|
||||||
@ -174,7 +170,7 @@ def alphabeta(
|
|||||||
for move in board.generate_legal_moves():
|
for move in board.generate_legal_moves():
|
||||||
board.push(move)
|
board.push(move)
|
||||||
value = (
|
value = (
|
||||||
aux(
|
_alphabeta(
|
||||||
board,
|
board,
|
||||||
alpha=alpha,
|
alpha=alpha,
|
||||||
beta=beta,
|
beta=beta,
|
||||||
@ -197,7 +193,15 @@ def alphabeta(
|
|||||||
|
|
||||||
return acc
|
return acc
|
||||||
|
|
||||||
_, move = aux(board, heuristic=heuristic, color=color, depth=depth)
|
|
||||||
|
def alphabeta(
|
||||||
|
board: chess.Board,
|
||||||
|
heuristic: Callable[[chess.Board, chess.Color], float],
|
||||||
|
color: chess.Color,
|
||||||
|
depth: int = 3,
|
||||||
|
) -> chess.Move:
|
||||||
|
|
||||||
|
_, move = _alphabeta(board, heuristic=heuristic, color=color, depth=depth)
|
||||||
return move
|
return move
|
||||||
|
|
||||||
|
|
||||||
@ -214,3 +218,27 @@ class AlphaBetaPlayer(PlayerInterface):
|
|||||||
)
|
)
|
||||||
self.board.push(move)
|
self.board.push(move)
|
||||||
return move
|
return move
|
||||||
|
|
||||||
|
|
||||||
|
def IDDFS(
|
||||||
|
board: chess.Board, heuristic, color: chess.Color, duration: float, maxdepth=42
|
||||||
|
) -> chess.Move:
|
||||||
|
st = time.time()
|
||||||
|
depth = 1
|
||||||
|
move = chess.Move.null()
|
||||||
|
while time.time() - st < duration and depth < maxdepth:
|
||||||
|
move = _alphabeta(board, heuristic, color, alpha=-10, beta=10, depth=depth)[1]
|
||||||
|
|
||||||
|
depth += 1
|
||||||
|
return move
|
||||||
|
|
||||||
|
|
||||||
|
class IDDFSPlayer(PlayerInterface):
|
||||||
|
# Returns your player name , as to be displayed during the game
|
||||||
|
def getPlayerName(self) -> str:
|
||||||
|
return "AlphaBeta Player"
|
||||||
|
|
||||||
|
def getPlayerMove(self):
|
||||||
|
move = IDDFS(self.board, h_shannon, self.color, 1)
|
||||||
|
self.board.push(move)
|
||||||
|
return move
|
||||||
|
Loading…
x
Reference in New Issue
Block a user