diff options
author | jake <jake@jakes-mail.top> | 2023-02-21 13:32:39 -0500 |
---|---|---|
committer | jake <jake@jakes-mail.top> | 2023-02-21 13:32:39 -0500 |
commit | e31a2fd04f6664c2976e8eaca9cbb7ffd8f0f8cc (patch) | |
tree | 31bcda78eeabe17731e249092571350e69acfb89 | |
parent | 5163f85f12078b6573ddc28f546581343f5c65fe (diff) |
correctly calculate checkmate.
move check_for_promotion() at the very start of new turn.
add green color for MoveTile.gd (for showing map control)
add some functions that show board control.
modify some functions so that they will use 'text_board' rather than the player presented board.
-rw-r--r-- | Main.gd | 182 | ||||
-rw-r--r-- | MoveTile.gd | 5 |
2 files changed, 111 insertions, 76 deletions
@@ -38,12 +38,9 @@ func _ready(): #spawn_piece('pawn', 'teal', 1, 6, team1) #spawn_piece('pawn', 'orange', 2, 4, team2) - #team1_king = spawn_piece('king', 'orange', 4, 7, team1) - #spawn_piece('bishop', 'orange', 3, 7, team1) - #spawn_piece('pawn', 'orange', 5, 6, team1) - #spawn_piece('pawn', 'orange', 3, 6, team1) - #spawn_piece('pawn', 'orange', 4, 6, team1) - #spawn_piece('rook', 'teal', 7, 3, 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) @@ -55,7 +52,23 @@ func _ready(): OS.set_window_size(Vector2(700,700)) #OS.set_window_always_on_top(true) -# horrifying discovery: this occurs after the signal capturing functions + +func checkmatet1(): + team1_king = spawn_piece('king', 'orange', 4, 7, team1) + spawn_piece('bishop', 'orange', 3, 7, team1) + spawn_piece('pawn', 'orange', 5, 6, team1) + spawn_piece('pawn', 'orange', 3, 6, team1) + spawn_piece('pawn', 'orange', 4, 6, team1) + spawn_piece('rook', 'teal', 7, 3, team2) + +func checkmatet2(): + team2_king = spawn_piece('king', 'teal', 4, 0, team2) + spawn_piece('bishop', 'teal', 3, 0, team2) + spawn_piece('pawn', 'teal', 5, 1, team2) + spawn_piece('pawn', 'teal', 3, 1, team2) + spawn_piece('pawn', 'teal', 4, 1, team2) + spawn_piece('rook', 'orange', 7, 2, team1) + func _process(_delta): safely_handle_movement = false if Input.is_action_just_pressed("mouse2"): @@ -66,8 +79,25 @@ func _process(_delta): print("whoops") 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)) + 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)) func new_turn(): + check_for_promotion() var text_board = board_to_text_board(board) var captures = update_capture_tables(text_board) team1_capture = captures[0] @@ -75,10 +105,11 @@ func new_turn(): #print(captures) is_king_team1_checked(text_board, true, team2_capture) is_king_team2_checked(text_board, true, team1_capture) - check_for_promotion() + turn += 1 print("Turn: %s" % turn) save_turn() + print("-------------------") func save_turn(): var text_board = board_to_text_board(board) @@ -112,7 +143,7 @@ func board_to_text_board(boarde): text_board[i][k] = 0 return text_board -func update_capture_tables(text_board): +func update_capture_tables(text_board, setting=tile_setting.hide_tiles_and_cover_allies): var t1_capture = new_board() var t2_capture = new_board() var pieces = get_pieces(text_board) @@ -125,9 +156,8 @@ func update_capture_tables(text_board): for e in pieces: var coords = e[chess_enum.pos] var pattern = get_move_pattern(e, coords) - var captured = can_chess_move(pattern, coords, text_board, tile_setting.hide_tiles_and_cover_allies) + var captured = can_chess_move(pattern, coords, text_board, setting) if e[chess_enum.team] == team1: - #rint(captured) for c in captured: if c.size() == 3: if c[2] == "not attacking": @@ -141,15 +171,19 @@ func update_capture_tables(text_board): t2_capture[c[0]][c[1]] = 1 #print(team1_capture) #print(team2_capture) - #for i in 8: - # for k in 8: - # if t1_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(t2_capture, "red") + #color_tiles(t1_capture, "green") return [t1_capture, t2_capture] +func color_tiles(capture, color): + for i in 8: + for k in 8: + if capture[i][k] == 1: + var move_tile = movetile_scene.instance() + add_child(move_tile) + move_tile.set_color(color) + move_tile.position = in_square(Vector2(i * board_cell, k * board_cell)) + func check_for_promotion(): var pieces = get_tree().get_nodes_in_group("piece") for e in pieces: @@ -201,24 +235,22 @@ func enumerate_checkmate(text_board, which_king=1): var piece = every_move[0] #rint("piece at (%s,%s)" % [piece[0],piece[1]]) for move in every_move[1]: - #rint("can move to: (%s,%s)" % [move[0],move[1]]) + #print("can move to: (%s,%s)" % [move[0],move[1]]) var fake_board = pretend_text_board_movement(text_board.duplicate(true), piece, move) #print(text_board) #print(fake_board) - var fake_captures = update_capture_tables(fake_board) + var fake_captures = update_capture_tables(fake_board.duplicate(true)) + var t1_captures = fake_captures[0] + var t2_captures = fake_captures[1] if which_king == 1: - if is_king_team1_checked(fake_board, false, fake_captures[1]): + if is_king_team1_checked(fake_board, false, t2_captures): pass - #rint("King in check on this fake board!!") else: - #rint("King ***NOT*** in check! Not a ckeckmate!") checkmate = false else: # team2 - if is_king_team2_checked(fake_board, false, fake_captures[0]): + if is_king_team2_checked(fake_board, false, t1_captures): pass - #rint("King in check on this fake board!!") else: - #rint("King ***NOT*** in check! Not a ckeckmate!") checkmate = false return checkmate @@ -241,7 +273,7 @@ func team_every_legal_move(text_board, team): var coords = [i,k] var move_pattern #var name_of_this = match_piece_names_enums(piece_name) - move_pattern = get_move_pattern([piece_name, team2], 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)]) #rint(every_legal_move) return every_legal_move @@ -467,81 +499,81 @@ func can_chess_move(pattern, coords, text_board, create_tiles=tile_setting.show_ for e in pattern: match (e): move_1_down_pawn: - var test = make_tiles(coords, [0,1], 1, true, curr_team, {}, create_tiles) + var test = make_tiles(coords, [0,1], 1, true, curr_team, {}, create_tiles, text_board) if test: test[0].push_back("not attacking") can_move.append_array(test) move_2_down_pawn: - var test = make_tiles(coords, [0,1], 1, true, curr_team, {}, create_tiles) + var test = make_tiles(coords, [0,1], 1, true, curr_team, {}, create_tiles, text_board) if test: test[0].push_back("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) + 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") can_move.append_array(test2) attack_1_sw: - var test = make_tiles(coords, [-1,1], 1, false, curr_team, {"must_attack": true}, create_tiles) + 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") can_move.append_array(test) attack_1_se: - var test = make_tiles(coords, [1,1], 1, false, curr_team, {"must_attack": true}, create_tiles) + 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") 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)) + can_move.append_array(make_tiles(coords, [-1,1], 1, true, curr_team, {"en_passant_pawn": pawn_maybe}, create_tiles, text_board)) 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)) + can_move.append_array(make_tiles(coords, [1,1], 1, true, curr_team, {"en_passant_pawn": pawn_maybe}, create_tiles, text_board)) move_1_up_pawn: - var test = make_tiles(coords, [0,-1], 1, true, curr_team, {}, create_tiles) + var test = make_tiles(coords, [0,-1], 1, true, curr_team, {}, create_tiles, text_board) if test: test[0].push_back("not attacking") can_move.append_array(test) move_2_up_pawn: - var test = make_tiles(coords, [0,-1], 1, true, curr_team, {}, create_tiles) + var test = make_tiles(coords, [0,-1], 1, true, curr_team, {}, create_tiles, text_board) if test: test[0].push_back("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) + 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") can_move.append_array(test2) attack_1_nw: - var test = make_tiles(coords, [-1,-1], 1, false, curr_team, {"must_attack": true}, create_tiles) + 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") can_move.append_array(test) attack_1_ne: - var test = make_tiles(coords, [1,-1], 1, false, curr_team, {"must_attack": true}, create_tiles) + 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") 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)) + can_move.append_array(make_tiles(coords, [-1,-1], 1, true, curr_team, {"en_passant_pawn": pawn_maybe}, create_tiles, text_board)) 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)) + can_move.append_array(make_tiles(coords, [1,-1], 1, true, curr_team, {"en_passant_pawn": pawn_maybe}, create_tiles, text_board)) move_up_inf: - can_move.append_array(make_tiles(coords, [0,-1], BOARD_HEIGHT, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [0,-1], BOARD_HEIGHT, false, curr_team, {}, create_tiles, text_board)) move_down_inf: - can_move.append_array(make_tiles(coords, [0,1], BOARD_HEIGHT, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [0,1], BOARD_HEIGHT, false, curr_team, {}, create_tiles, text_board)) move_left_inf: - can_move.append_array(make_tiles(coords, [-1,0], BOARD_WIDTH, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [-1,0], BOARD_WIDTH, false, curr_team, {}, create_tiles, text_board)) move_right_inf: - can_move.append_array(make_tiles(coords, [1,0], BOARD_WIDTH, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [1,0], BOARD_WIDTH, false, curr_team, {}, create_tiles, text_board)) castling: if ! curr_piece[chess_enum.has_moved] and ! curr_piece[chess_enum.in_check]: var y = coords[1] @@ -569,7 +601,7 @@ 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)) + can_move.append_array(make_tiles(coords, [2,0], 1, false, curr_team, {"castling_rook": ele}, create_tiles, text_board)) else: var blocked = false var diff = king_x - rook_x @@ -586,43 +618,43 @@ 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)) + can_move.append_array(make_tiles(coords, [-2,0], 1, false, curr_team, {"castling_rook": ele}, create_tiles, text_board)) move_ne_inf: - can_move.append_array(make_tiles(coords, [1,-1], 8, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [1,-1], 8, false, curr_team, {}, create_tiles, text_board)) move_nw_inf: - can_move.append_array(make_tiles(coords, [-1,-1], 8, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [-1,-1], 8, false, curr_team, {}, create_tiles, text_board)) move_sw_inf: - can_move.append_array(make_tiles(coords, [-1,1], 8, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [-1,1], 8, false, curr_team, {}, create_tiles, text_board)) move_se_inf: - can_move.append_array(make_tiles(coords, [1,1], 8, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [1,1], 8, false, curr_team, {}, create_tiles, text_board)) knight: - can_move.append_array(make_tiles(coords, [1,2], 1, false, curr_team, {}, create_tiles)) - can_move.append_array(make_tiles(coords, [1,-2], 1, false, curr_team, {}, create_tiles)) - can_move.append_array(make_tiles(coords, [-1,-2], 1, false, curr_team, {}, create_tiles)) - can_move.append_array(make_tiles(coords, [-1,2], 1, false, curr_team, {}, create_tiles)) - can_move.append_array(make_tiles(coords, [2,1], 1, false, curr_team, {}, create_tiles)) - can_move.append_array(make_tiles(coords, [2,-1], 1, false, curr_team, {}, create_tiles)) - can_move.append_array(make_tiles(coords, [-2,-1], 1, false, curr_team, {}, create_tiles)) - can_move.append_array(make_tiles(coords, [-2,1], 1, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [1,2], 1, false, curr_team, {}, create_tiles, text_board)) + can_move.append_array(make_tiles(coords, [1,-2], 1, false, curr_team, {}, create_tiles, text_board)) + can_move.append_array(make_tiles(coords, [-1,-2], 1, false, curr_team, {}, create_tiles, text_board)) + can_move.append_array(make_tiles(coords, [-1,2], 1, false, curr_team, {}, create_tiles, text_board)) + can_move.append_array(make_tiles(coords, [2,1], 1, false, curr_team, {}, create_tiles, text_board)) + can_move.append_array(make_tiles(coords, [2,-1], 1, false, curr_team, {}, create_tiles, text_board)) + can_move.append_array(make_tiles(coords, [-2,-1], 1, false, curr_team, {}, create_tiles, text_board)) + can_move.append_array(make_tiles(coords, [-2,1], 1, false, curr_team, {}, create_tiles, text_board)) move_1_down: - can_move.append_array(make_tiles(coords, [0,1], 1, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [0,1], 1, false, curr_team, {}, create_tiles, text_board)) move_1_up: - can_move.append_array(make_tiles(coords, [0,-1], 1, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [0,-1], 1, false, curr_team, {}, create_tiles, text_board)) move_1_right: - can_move.append_array(make_tiles(coords, [1,0], 1, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [1,0], 1, false, curr_team, {}, create_tiles, text_board)) move_1_left: - can_move.append_array(make_tiles(coords, [-1,0], 1, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [-1,0], 1, false, curr_team, {}, create_tiles, text_board)) move_1_ne: - can_move.append_array(make_tiles(coords, [1,-1], 1, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [1,-1], 1, false, curr_team, {}, create_tiles, text_board)) move_1_nw: - can_move.append_array(make_tiles(coords, [-1,-1], 1, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [-1,-1], 1, false, curr_team, {}, create_tiles, text_board)) move_1_se: - can_move.append_array(make_tiles(coords, [1,1], 1, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [1,1], 1, false, curr_team, {}, create_tiles, text_board)) move_1_sw: - can_move.append_array(make_tiles(coords, [-1,1], 1, false, curr_team, {}, create_tiles)) + can_move.append_array(make_tiles(coords, [-1,1], 1, false, curr_team, {}, create_tiles, text_board)) return can_move func position_to_board_cell(vect2): @@ -706,7 +738,7 @@ func make_tiles(coords, pattern, go_range, cant_attack, curr_team, 'tile_is_en_passant': false, 'en_passant_pawn': null, 'castling_rook': null - }, create_tiles = 1): + }, create_tiles = tile_setting.show_tiles, text_board=[]): var x = coords[0] var y = coords[1] var pattern0 = pattern[0] @@ -721,24 +753,24 @@ func make_tiles(coords, pattern, go_range, cant_attack, curr_team, b += pattern1 # dont go out of bounds: not bigger than board size but not smaller than 0 if (x + a) <= BOARD_WIDTH and (y + b) <= BOARD_HEIGHT and (x + a) >= 0 and (y + b) >= 0 : - var check = board[x + a][y + b] + var check = text_board[x + a][y + b] if ! check and ! dict.get("must_attack"): - if create_tiles == 1: + if create_tiles == tile_setting.show_tiles: spawn_move_tile([x + a, y + b], dict.get("tile_is_en_passant"), dict.get("en_passant_pawn"), dict.get("castling_rook")) made_tile.push_back([x + a, y + b]) elif dict.get("must_attack"): # pawn setting - if check and check.get_team() != curr_team: - if create_tiles == 1: + if check and check[chess_enum.team] != curr_team: + if create_tiles == tile_setting.show_tiles: spawn_move_tile([x + a, y + b]) made_tile.push_back([x + a, y + b]) elif create_tiles == 0: made_tile.push_back([x + a, y + b]) - elif ! cant_attack and check.get_team() != curr_team: - if create_tiles == 1: + elif ! cant_attack and check[chess_enum.team] != curr_team: + if create_tiles == tile_setting.show_tiles: spawn_move_tile([x + a, y + b], dict.get("tile_is_en_passant"), dict.get("en_passant_pawn")) made_tile.push_back([x + a, y + b]) break # rules of chess say pieces cant go past another - elif ! create_tiles and ! cant_attack and check.get_team() == curr_team : + elif create_tiles == tile_setting.hide_tiles_and_cover_allies and ! cant_attack and check[chess_enum.team] == curr_team : # ^ in otherwords: we won't mark it on map but will 'mark' it for non-maps (like capture board) made_tile.push_back([x + a, y + b]) break # but still can't go past teammate diff --git a/MoveTile.gd b/MoveTile.gd index bb74eba..4ae1ea1 100644 --- a/MoveTile.gd +++ b/MoveTile.gd @@ -3,7 +3,8 @@ extends Node2D signal move_clicked const blue = Color(0.30, 0.71, 0.96, 0.54) -const red = Color(1.00, 0.00, 0.00, 0.54) +const red = Color(1.00, 0.00, 0.00, 0.15) +const green = Color(0.00, 1.00, 0.00, 0.15) export var en_passant_tile = false export var en_passant_kill_tile = false @@ -18,3 +19,5 @@ func set_color(color): $Rect.color = blue elif color == "red": $Rect.color = red + elif color == "green": + $Rect.color = green |