From da75e49e23ef2d982afc2c146aad092595d38c6e Mon Sep 17 00:00:00 2001 From: jake Date: Tue, 21 Feb 2023 17:27:52 -0500 Subject: [PATCH] added purely random ai (doesn't care if it gets checked or not) and made some changes so that AI can do castling and en passant kills. --- Main.gd | 139 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 100 insertions(+), 39 deletions(-) diff --git a/Main.gd b/Main.gd index 34ca12f..82146c8 100644 --- a/Main.gd +++ b/Main.gd @@ -35,21 +35,15 @@ var team2 = 2 func _ready(): make_player1("green") make_player2("red") - #spawn_piece('pawn', 'teal', 1, 6, team1) - #spawn_piece('pawn', 'orange', 2, 4, team2) #checkmatet1() #checkmatet2() - - #spawn_piece('rook', 'orange', 0, 0, team2) - #spawn_piece('rook', 'orange', 7, 0, team2) - #team1_king = spawn_piece('rook', 'teal', 5, 3, team1) - #spawn_piece('pawn', 'blue', 6, 0, team1) var captures = update_capture_tables(board_to_text_board(board)) team1_capture = captures[0] team2_capture = captures[1] save_turn() OS.set_window_size(Vector2(700,700)) + randomize() #OS.set_window_always_on_top(true) @@ -80,21 +74,62 @@ func _process(_delta): if click_spot() == Vector2(2,0): print(board_to_text_board(board)) if click_spot() == Vector2(4,0): - for i in 8: - for k in 8: - if team2_capture[i][k] == 1: - var move_tile = movetile_scene.instance() - add_child(move_tile) - move_tile.set_color("red") - move_tile.position = in_square(Vector2(i * board_cell, k * board_cell)) + color_tiles(team2_capture, "red") if click_spot() == Vector2(5,0): - for i in 8: - for k in 8: - if team1_capture[i][k] == 1: - var move_tile = movetile_scene.instance() - add_child(move_tile) - move_tile.set_color("red") - move_tile.position = in_square(Vector2(i * board_cell, k * board_cell)) + color_tiles(team1_capture, "green") + if click_spot() == Vector2(7,0): + ai_move(team2, board_to_text_board(board)) + if click_spot() == Vector2(7,1): + ai_move(team1, board_to_text_board(board)) + +enum ai_modes { + purely_random, +} + +func ai_move(team, text_board, ai_mode=ai_modes.purely_random): + var legal_every_move = team_every_legal_move(text_board, team) + legal_every_move.shuffle() + if ai_mode == ai_modes.purely_random: + while 1: + var piece_and_moves = legal_every_move.pop_back() + if piece_and_moves: + pass + else: + break # AI literally cannot move this turn, game should be offically over before this occurs + var piece = piece_and_moves[0] + var moves = piece_and_moves[1] + if moves: + print(moves) + moves.shuffle() + var move = moves.pop_back() + var pos = position_to_board_cell(Vector2(piece[0] * board_cell, piece[1] * board_cell)) + var board_piece = board[pos[0]][pos[1]] + board_piece.position = in_square(Vector2(move[0] * board_cell, move[1] * board_cell)) + if board[move[0]][move[1]]: + board[move[0]][move[1]].kill() + board[move[0]][move[1]] = board_piece + board[pos[0]][pos[1]] = 0 + + if move.size() == 3: + if move[2] == movement_condition.en_passant_kill: + kill_en_passant_pawn([move[0], move[1]]) + elif move[2] == movement_condition.king_side_castling: + # TODO hard coding rook positions like this seems bad + var rook = board[7][pos[1]] + rook.position = in_square(Vector2(5 * board_cell, pos[1] * board_cell)) + board[5][pos[1]] = board[7][pos[1]] + board[7][pos[1]] = 0 + elif move[2] == movement_condition.queen_side_castling: + # TODO hard coding rook positions like this seems bad + var rook = board[0][pos[1]] + rook.position = in_square(Vector2(3 * board_cell, pos[1] * board_cell)) + board[3][pos[1]] = board[0][pos[1]] + board[0][pos[1]] = 0 + #print("piece at (%s,%s), to (%s,%s)" % [ piece[0], piece[1], move[0], move[1] ]) + break + else: + continue + new_turn() func new_turn(): check_for_promotion() @@ -143,6 +178,14 @@ func board_to_text_board(boarde): text_board[i][k] = 0 return text_board +enum movement_condition { + not_attacking, + must_attack, + en_passant_kill, + king_side_castling, + queen_side_castling, + } + func update_capture_tables(text_board, setting=tile_setting.hide_tiles_and_cover_allies): var t1_capture = new_board() var t2_capture = new_board() @@ -160,13 +203,13 @@ func update_capture_tables(text_board, setting=tile_setting.hide_tiles_and_cover if e[chess_enum.team] == team1: for c in captured: if c.size() == 3: - if c[2] == "not attacking": + if c[2] == movement_condition.not_attacking: continue t1_capture[c[0]][c[1]] = 1 else: # team2 for c in captured: if c.size() == 3: - if c[2] == "not attacking": + if c[2] == movement_condition.not_attacking: continue t2_capture[c[0]][c[1]] = 1 #print(team1_capture) @@ -501,70 +544,82 @@ func can_chess_move(pattern, coords, text_board, create_tiles=tile_setting.show_ move_1_down_pawn: var test = make_tiles(coords, [0,1], 1, true, curr_team, {}, create_tiles, text_board) if test: - test[0].push_back("not attacking") + test[0].push_back(movement_condition.not_attacking) can_move.append_array(test) move_2_down_pawn: var test = make_tiles(coords, [0,1], 1, true, curr_team, {}, create_tiles, text_board) if test: - test[0].push_back("not attacking") + test[0].push_back(movement_condition.not_attacking) can_move.append_array(test) var test2 = make_tiles(coords, [0,2], 1, true, curr_team, {'tile_is_en_passant': true}, create_tiles, text_board) if test2: - test2[0].push_back("not attacking") + test2[0].push_back(movement_condition.not_attacking) can_move.append_array(test2) attack_1_sw: var test = make_tiles(coords, [-1,1], 1, false, curr_team, {"must_attack": true}, create_tiles, text_board) if test: - test[0].push_back("must attack") + test[0].push_back(movement_condition.must_attack) can_move.append_array(test) attack_1_se: var test = make_tiles(coords, [1,1], 1, false, curr_team, {"must_attack": true}, create_tiles, text_board) if test: - test[0].push_back("must attack") + test[0].push_back(movement_condition.must_attack) can_move.append_array(test) en_passant_sw: var pawn_maybe = text_board[coords[0]-1][coords[1]] if pawn_maybe and pawn_maybe[chess_enum.piece_enum] == piece_names["pawn"] and pawn_maybe[chess_enum.en_passant] == 1 and pawn_maybe[chess_enum.team] != curr_team: - can_move.append_array(make_tiles(coords, [-1,1], 1, true, curr_team, {"en_passant_pawn": pawn_maybe}, create_tiles, text_board)) + var test = make_tiles(coords, [-1,1], 1, true, curr_team, {"en_passant_pawn": pawn_maybe}, create_tiles, text_board) + if test: + test[0].push_back(movement_condition.en_passant_kill) + can_move.append_array(test) en_passant_se: if ! coords[0] + 1 > BOARD_WIDTH: var pawn_maybe = text_board[coords[0]+1][coords[1]] if pawn_maybe and pawn_maybe[chess_enum.piece_enum] == piece_names["pawn"] and pawn_maybe[chess_enum.en_passant] == 1 and pawn_maybe[chess_enum.team] != curr_team: - can_move.append_array(make_tiles(coords, [1,1], 1, true, curr_team, {"en_passant_pawn": pawn_maybe}, create_tiles, text_board)) + var test = make_tiles(coords, [1,1], 1, true, curr_team, {"en_passant_pawn": pawn_maybe}, create_tiles, text_board) + if test: + test[0].push_back(movement_condition.en_passant_kill) + can_move.append_array(test) move_1_up_pawn: var test = make_tiles(coords, [0,-1], 1, true, curr_team, {}, create_tiles, text_board) if test: - test[0].push_back("not attacking") + test[0].push_back(movement_condition.not_attacking) can_move.append_array(test) move_2_up_pawn: var test = make_tiles(coords, [0,-1], 1, true, curr_team, {}, create_tiles, text_board) if test: - test[0].push_back("not attacking") + test[0].push_back(movement_condition.not_attacking) can_move.append_array(test) var test2 = make_tiles(coords, [0,-2], 1, true, curr_team, {'tile_is_en_passant': true}, create_tiles, text_board) if test2: - test2[0].push_back("not attacking") + test2[0].push_back(movement_condition.not_attacking) can_move.append_array(test2) attack_1_nw: var test = make_tiles(coords, [-1,-1], 1, false, curr_team, {"must_attack": true}, create_tiles, text_board) if test: - test[0].push_back("must attack") + test[0].push_back(movement_condition.must_attack) can_move.append_array(test) attack_1_ne: var test = make_tiles(coords, [1,-1], 1, false, curr_team, {"must_attack": true}, create_tiles, text_board) if test: - test[0].push_back("must attack") + test[0].push_back(movement_condition.must_attack) can_move.append_array(test) en_passant_nw: var pawn_maybe = text_board[coords[0]-1][coords[1]] if pawn_maybe and pawn_maybe[chess_enum.piece_enum] == piece_names["pawn"] and pawn_maybe[chess_enum.en_passant] == 1 and pawn_maybe[chess_enum.team] != curr_team: - can_move.append_array(make_tiles(coords, [-1,-1], 1, true, curr_team, {"en_passant_pawn": pawn_maybe}, create_tiles, text_board)) + var test = make_tiles(coords, [-1,-1], 1, true, curr_team, {"en_passant_pawn": pawn_maybe}, create_tiles, text_board) + if test: + test[0].push_back(movement_condition.en_passant_kill) + can_move.append_array(test) en_passant_ne: if ! coords[0] + 1 > BOARD_WIDTH: var pawn_maybe = text_board[coords[0]+1][coords[1]] if pawn_maybe and pawn_maybe[chess_enum.piece_enum] == piece_names["pawn"] and pawn_maybe[chess_enum.en_passant] == 1 and pawn_maybe[chess_enum.team] != curr_team: - can_move.append_array(make_tiles(coords, [1,-1], 1, true, curr_team, {"en_passant_pawn": pawn_maybe}, create_tiles, text_board)) + var test = make_tiles(coords, [1,-1], 1, true, curr_team, {"en_passant_pawn": pawn_maybe}, create_tiles, text_board) + if test: + test[0].push_back(movement_condition.en_passant_kill) + can_move.append_array(test) move_up_inf: can_move.append_array(make_tiles(coords, [0,-1], BOARD_HEIGHT, false, curr_team, {}, create_tiles, text_board)) @@ -601,7 +656,10 @@ func can_chess_move(pattern, coords, text_board, create_tiles=tile_setting.show_ #rint("There is something at (%s,%s) %s" % [king_x + i,y, board[king_x + i][y].get_piece() ]) if ! blocked: - can_move.append_array(make_tiles(coords, [2,0], 1, false, curr_team, {"castling_rook": ele}, create_tiles, text_board)) + var test = make_tiles(coords, [2,0], 1, false, curr_team, {"castling_rook": ele}, create_tiles, text_board) + if test: + test[0].push_back(movement_condition.king_side_castling) + can_move.append_array(test) else: var blocked = false var diff = king_x - rook_x @@ -618,7 +676,10 @@ func can_chess_move(pattern, coords, text_board, create_tiles=tile_setting.show_ blocked = true #rint("There is something at (%s,%s) %s" % [rook_x + i, y, board[rook_x + i][y].get_piece()]) if ! blocked: - can_move.append_array(make_tiles(coords, [-2,0], 1, false, curr_team, {"castling_rook": ele}, create_tiles, text_board)) + var test = make_tiles(coords, [-2,0], 1, false, curr_team, {"castling_rook": ele}, create_tiles, text_board) + if test: + test[0].push_back(movement_condition.queen_side_castling) + can_move.append_array(test) move_ne_inf: can_move.append_array(make_tiles(coords, [1,-1], 8, false, curr_team, {}, create_tiles, text_board))