make sure AI knows that only pawns can kill en passant pawns

This commit is contained in:
jake 2023-02-21 21:58:44 -05:00
parent 43faa277bb
commit 265b50e1de

54
Main.gd
View file

@ -38,6 +38,10 @@ func _ready():
#checkmatet1() #checkmatet1()
#checkmatet2() #checkmatet2()
#spawn_piece("pawn", "teal", 2, 6, team1)
#spawn_piece("pawn", "red", 3, 4, team2)
#spawn_piece("rook", "red", 0, 5, team2)
var captures = update_capture_tables(board_to_text_board(board)) var captures = update_capture_tables(board_to_text_board(board))
team1_capture = captures[0] team1_capture = captures[0]
team2_capture = captures[1] team2_capture = captures[1]
@ -107,8 +111,13 @@ func ai_move(team, text_board, ai_mode=ai_modes.purely_random):
ai_random_move(legal_every_move) ai_random_move(legal_every_move)
elif ai_mode == ai_modes.weighted_random: elif ai_mode == ai_modes.weighted_random:
ai_weighted_random(team, text_board, legal_every_move) ai_weighted_random(team, text_board, legal_every_move)
elif ai_mode == ai_modes.minimax_depth_2:
ai_minimax_depth_2(team, text_board, legal_every_move)
new_turn() new_turn()
func ai_minimax_depth_2(team, text_board, legal_every_move):
pass
func ai_make_move(piece, move): func ai_make_move(piece, move):
var pos = position_to_board_cell(Vector2(piece[0] * board_cell, piece[1] * board_cell)) var pos = position_to_board_cell(Vector2(piece[0] * board_cell, piece[1] * board_cell))
var board_piece = board[pos[0]][pos[1]] var board_piece = board[pos[0]][pos[1]]
@ -163,7 +172,11 @@ func ai_weighted_random(team, text_board, legal_every_move):
var moves = every_move[1] var moves = every_move[1]
if moves: if moves:
for move in moves: for move in moves:
if p_board[move[0]][move[1]] > 0: if p_board[move[0]][move[1]] is Array:
# en passant 'move' specifically does array
if piece[2] == piece_names.pawn:
saved_moves.append([p_board[move[0]][move[1]], piece, move])
elif p_board[move[0]][move[1]] > 0:
saved_moves.append([p_board[move[0]][move[1]], piece, move]) saved_moves.append([p_board[move[0]][move[1]], piece, move])
else: else:
continue continue
@ -188,39 +201,50 @@ func generate_points_board(team, text_board):
for k in 8: for k in 8:
var piece = text_board[i][k] var piece = text_board[i][k]
if piece: if piece:
match piece[0]: match piece[text_board_ele.piece_enum]:
piece_names.pawn: piece_names.pawn:
if piece[1] == team: if piece[text_board_ele.team] == team: # our pawn
p_board[i][k] = -1 * ai_weights.pawn p_board[i][k] = -1 * ai_weights.pawn
else: if piece[text_board_ele.en_passant]:
print("this teams (%s) pawn is en passant, on our team" % piece[text_board_ele.team])
pass
else: # their pawn
p_board[i][k] = ai_weights.pawn p_board[i][k] = ai_weights.pawn
if piece[text_board_ele.en_passant]:
print("this teams (%s) pawn is en passant, on enemy team" % piece[text_board_ele.team])
if team == team1: # they'll be team2 here
p_board[i][(k - 1)] = [ai_weights.pawn, true]
else: # they'll be team1 here
# team1 pawn goes 'up' -1 or -2 each movement, so the space behind it will be +1
p_board[i][(k + 1)] = [ai_weights.pawn, true]
piece_names.rook: piece_names.rook:
if piece[1] == team: if piece[text_board_ele.team] == team:
p_board[i][k] = -1 * ai_weights.rook p_board[i][k] = -1 * ai_weights.rook
else: else:
p_board[i][k] = ai_weights.rook p_board[i][k] = ai_weights.rook
piece_names.knight: piece_names.knight:
if piece[1] == team: if piece[text_board_ele.team] == team:
p_board[i][k] = -1 * ai_weights.knight p_board[i][k] = -1 * ai_weights.knight
else: else:
p_board[i][k] = ai_weights.knight p_board[i][k] = ai_weights.knight
piece_names.bishop: piece_names.bishop:
if piece[1] == team: if piece[text_board_ele.team] == team:
p_board[i][k] = -1 * ai_weights.bishop p_board[i][k] = -1 * ai_weights.bishop
else: else:
p_board[i][k] = ai_weights.bishop p_board[i][k] = ai_weights.bishop
piece_names.queen: piece_names.queen:
if piece[1] == team: if piece[text_board_ele.team] == team:
p_board[i][k] = -1 * ai_weights.queen p_board[i][k] = -1 * ai_weights.queen
else: else:
p_board[i][k] = ai_weights.queen p_board[i][k] = ai_weights.queen
piece_names.king: piece_names.king:
if piece[1] == team: if piece[text_board_ele.team] == team:
p_board[i][k] = -1 * ai_weights.king p_board[i][k] = -1 * ai_weights.king
else: else:
p_board[i][k] = ai_weights.king p_board[i][k] = ai_weights.king
else: else:
p_board[i][k] = 0 if ! p_board[i][k]: # don't clobber en passant points
p_board[i][k] = 0
return p_board return p_board
func new_turn(): func new_turn():
@ -251,6 +275,14 @@ enum piece_names {
queen queen
} }
enum text_board_ele {
piece_enum,
team,
en_passant,
has_moved,
in_check
}
func board_to_text_board(boarde): func board_to_text_board(boarde):
var text_board = new_board() var text_board = new_board()
for i in 8: for i in 8:
@ -409,7 +441,7 @@ func team_every_legal_move(text_board, team):
var move_pattern var move_pattern
#var name_of_this = match_piece_names_enums(piece_name) #var name_of_this = match_piece_names_enums(piece_name)
move_pattern = get_move_pattern([piece_name, team], coords) move_pattern = get_move_pattern([piece_name, team], coords)
every_legal_move.append([[i,k], can_chess_move(move_pattern, coords, text_board, tile_setting.hide_tiles_no_cover_allies)]) every_legal_move.append([[i,k, piece_name], can_chess_move(move_pattern, coords, text_board, tile_setting.hide_tiles_no_cover_allies)])
#rint(every_legal_move) #rint(every_legal_move)
return every_legal_move return every_legal_move