Skip to content

Commit

Permalink
Now searches for single advance even when double advance is possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Iain-Cheverton committed May 1, 2017
1 parent 7dd743e commit 6885948
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
26 changes: 13 additions & 13 deletions Iain_AI_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,7 @@ def moves(board, white_turn):
break
if piece == 'P' and y <= 6:
if board[y+1][x] == '.':
if y == 1 and board[y+2][x] == '.':
legal_moves.append(
(move(board, y, x, y+2, x),
Piece_score[piece][y+2][x] -
Piece_score[piece][y][x]))
elif y == 6:
if y == 6:
new_board = board.copy()
new_board[y] = new_board[y].copy()
new_board[y+1] = new_board[y+1].copy()
Expand All @@ -174,6 +169,11 @@ def moves(board, white_turn):
legal_moves.append((move(board, y, x, y + 1, x),
Piece_score[piece][y + 1][x] -
Piece_score[piece][y][x]))
if y == 1 and board[y+2][x] == '.':
legal_moves.append(
(move(board, y, x, y+2, x),
Piece_score[piece][y+2][x] -
Piece_score[piece][y][x]))
if x < 7 and board[y+1][x+1].islower():
if y == 6:
new_board = board.copy()
Expand Down Expand Up @@ -236,12 +236,7 @@ def moves(board, white_turn):
break
if piece == 'p' and y >= 1:
if board[y-1][x] == '.':
if y == 6 and board[y-2][x] == '.':
legal_moves.append((
move(board, y, x, y-2, x),
Piece_score[piece][y-2][x] -
Piece_score[piece][y][x]))
elif y == 1:
if y == 1:
new_board = board.copy()
new_board[y] = new_board[y].copy()
new_board[y-1] = new_board[y-1].copy()
Expand All @@ -256,6 +251,11 @@ def moves(board, white_turn):
move(board, y, x, y - 1, x),
Piece_score[piece][y - 1][x] -
Piece_score[piece][y][x]))
if y == 6 and board[y-2][x] == '.':
legal_moves.append((
move(board, y, x, y-2, x),
Piece_score[piece][y-2][x] -
Piece_score[piece][y][x]))
if x < 7 and board[y-1][x+1].isupper():
if y == 1:
new_board = board.copy()
Expand Down Expand Up @@ -301,7 +301,7 @@ def search(board, white_turn, depth):
best_score = -99_900_000 # this will only matter if there are no moves and avoids crashing runner
best_move = board
for move_, score_difference in moves(board, white_turn):
assert abs(score_difference + score_board - score(move_)) < 0.1
# assert abs(score_difference + score_board - score(move_)) < 0.1
current_score = score_difference + score_board if white_turn else -(score_difference + score_board)
if depth > 1 and abs(current_score) < 1_000_000:
current_score = -search(move_, not white_turn, depth - 1)[1]
Expand Down
16 changes: 12 additions & 4 deletions Iain_AI_v2_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@
. . . . . . . .
. . . . p . p .
. . . . . R . R'''
x = list([[piece for piece in row.replace(' ', '')] for row in promotionPosition.split('\n')].__reversed__())

x = list([[piece for piece in row.replace(' ', '')] for row in difficultPosition.split('\n')].__reversed__())
for move in Iain_AI_v2.moves(x, True):
print(move)
print(len(Iain_AI_v2.moves(x, True)))
# print(x)
for depth in range(1, 5):
start_time = time.perf_counter()
best_move, best_score = Iain_AI_v2.search(x, False, depth)
best_move, best_score = Iain_AI_v2.search(x, True, depth)
end_time = time.perf_counter()
print(depth, best_score,end_time - start_time, best_move)
print(depth, best_score, end_time - start_time) # , best_move)


''' performance before optimisation
1 80 0.00220390942092681
Expand All @@ -70,4 +73,9 @@
2 -205 0.00410338173704801
3 25 0.14609174623899446
4 -125 5.050977404292655
Now searches single pawn advancements even when double advance is possible
1 310 0.00013698256600105544
2 5 0.00473279577567581
3 240 0.15794250261052842
4 5 5.883278342478872
'''

0 comments on commit 6885948

Please sign in to comment.