online multiplayer chess game (note server currently down)
-rw-r--r--Grid.gd7
-rw-r--r--pieces/Piece.gd21
2 files changed, 12 insertions, 16 deletions
diff --git a/Grid.gd b/Grid.gd
index fed1d80..6c50d7c 100644
--- a/Grid.gd
+++ b/Grid.gd
@@ -41,13 +41,12 @@ func check_in_check(): # check if in_check
for j in range(0, 8): # for each column
var spot = matrix[i][j] # get the square
if spot and spot.white != Globals.turn: # enemie
- if matrix[i][j].can_check_king(Globals.white_king if Globals.turn else Globals.black_king): # if it can take the king
+ if matrix[i][j].can_attack_piece(Globals.white_king if Globals.turn else Globals.black_king): # if it can take the king
Globals.in_check = true # set in_check
Globals.checking_piece = matrix[i][j] # set checking_piece
print("check by " + spot.shortname) # print the check
- return true
- return false # not in check
-
+ return true # stop at the first check found
+ return false
func _exit_tree():
Globals.grid = null # reset the globals grid when leaving tree
diff --git a/pieces/Piece.gd b/pieces/Piece.gd
index bc9068f..0e01389 100644
--- a/pieces/Piece.gd
+++ b/pieces/Piece.gd
@@ -112,10 +112,10 @@ func get_attacks(): # @Override
return final
-func can_check_king(king):
+func can_attack_piece(piece):
check_spots_check = false
- for attack in get_attacks():
- if at_pos(attack) == king:
+ for pos in get_attacks():
+ if at_pos(pos) == piece:
check_spots_check = true
return true
check_spots_check = true
@@ -140,16 +140,13 @@ func set_circle(positions: Array, type := "move"):
func checkcheck(pos): # moves to position, then checks if your king is in check
- if Globals.in_check: # if you are in check
- var mat = Globals.grid.matrix.duplicate(true) # make a copy of the matrix
- moveto(pos, false) # move to the position
- var retu = false # return true by default
- if Globals.grid.check_in_check(): # if you are still in check
- retu = true # return false, but fix the matrix first
- # Globals.grid.print_matrix_pretty(mat) # print the matrix
+ var mat = Globals.grid.matrix.duplicate(true) # make a copy of the matrix
+ moveto(pos, false) # move to the position
+ if Globals.grid.check_in_check(): # if you are still in check# Globals.grid.print_matrix_pretty(mat) # print the matrix
Globals.grid.matrix = mat # revert changes on the matrix
- return retu
- return false
+ return true # return in check
+ Globals.grid.matrix = mat # revert changes on the matrix
+ return false # return not in check
func is_on_board(vector: Vector2):