online multiplayer chess game (note server currently down)
CHECKS
bendn 2022-04-28
parent 20e43f0 · commit 5c19a5c
-rw-r--r--Grid.gd42
-rw-r--r--pieces/King.gd52
-rw-r--r--pieces/Knight.gd2
-rw-r--r--pieces/Piece.gd1
4 files changed, 87 insertions, 10 deletions
diff --git a/Grid.gd b/Grid.gd
index a8a5016..423a0b8 100644
--- a/Grid.gd
+++ b/Grid.gd
@@ -48,10 +48,10 @@ func check_in_check(prin = false): # 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_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
+ if spot.can_attack_piece(Globals.white_king if Globals.turn else Globals.black_king): # if it can take the king
if prin:
+ Globals.in_check = true # set in_check
+ Globals.checking_piece = spot # set checking_piece
print("check by " + spot.shortname) # print the check
return true # stop at the first check found
return false
@@ -155,19 +155,33 @@ func add_kings():
Globals.black_king = matrix[0][4] # set the black king
+const topper_header = "┏━━━┳━━━━┳━━━━┳━━━━┳━━━━┳━━━━┳━━━━┳━━━━┳━━━━┓"
+const middle_header = "┣━━━╋━━━━╋━━━━╋━━━━╋━━━━╋━━━━╋━━━━╋━━━━╋━━━━┫"
+const middish_heads = "┗━━━╋━━━━╋━━━━╋━━━━╋━━━━╋━━━━╋━━━━╋━━━━╋━━━━┫"
+const bottom_header = "┗━━━┻━━━━┻━━━━┻━━━━┻━━━━┻━━━━┻━━━━┻━━━━┻━━━━┛"
+const smaller_heads = " ┗━━━━┻━━━━┻━━━━┻━━━━┻━━━━┻━━━━┻━━━━┻━━━━┛"
+const letter_header = " ┃ a ┃ b ┃ c ┃ d ┃ e ┃ f ┃ g ┃ h ┃"
+const ender = " ┃ "
+
+
func print_matrix_pretty(mat = matrix): # print the matrix
for j in range(len(mat)): # for each row
var r = mat[j] # get the row
- var row = str(8 - j) + " " # init the string
+ if j == 0:
+ print(topper_header) # print the top border
+ else:
+ print(middle_header) # print the middle border
+ var row = "┃ " + str(8 - j) + " ┃ " # init the string
for i in range(8): # for each column
var c = r[i] # get the column
- var ender = " " if i < 7 else "" # set the end string
if c: # if there is a piece
row += c.shortname + ender # add the shortname
else: # if there is no piece
row += "00" + ender # add 00
print(row) # print the string
- print(" a b c d e f g h") # print the column names
+ print(middish_heads)
+ print(letter_header)
+ print(smaller_heads)
func check_for_circle(position: Vector2): # check for a circle, validating movement
@@ -189,8 +203,7 @@ func square_clicked(position: Vector2): # square clicked
last_clicked.take(matrix[position.y][position.x]) # eat
turn_over()
if check_for_circle(position): # see if theres a circle at the position
- last_clicked.moveto(position) # if there is, move there
- turn_over()
+ handle_move(position) # move
last_clicked.clear_clicked() # remove the circles
last_clicked = null # set it to null
elif last_clicked != spot: # we got a new piece (or pawn) clicked
@@ -200,6 +213,19 @@ func square_clicked(position: Vector2): # square clicked
spot.clicked() # tell the piece shit happeend
+func handle_move(position):
+ if last_clicked is King and last_clicked.can_castle:
+ for i in range(len(last_clicked.can_castle)):
+ var castle_data = last_clicked.can_castle[i]
+ if castle_data[0] == position:
+ last_clicked.castle(castle_data[0])
+ castle_data[1].moveto(castle_data[2])
+ turn_over()
+ return
+ last_clicked.moveto(position)
+ turn_over()
+
+
func turn_over():
Globals.add_turn()
Globals.turn = not Globals.turn
diff --git a/pieces/King.gd b/pieces/King.gd
index c63e6e4..465a8e0 100644
--- a/pieces/King.gd
+++ b/pieces/King.gd
@@ -1,6 +1,9 @@
extends Piece
class_name King, "res://assets/california/wK.png"
+var castle_check = true
+var can_castle = []
+
func get_moves():
var moves = []
@@ -9,12 +12,59 @@ func get_moves():
if is_on_board(spot):
if no_enemys and at_pos(spot):
continue
- if check_spots_check and Globals.in_check and checkcheck(spot):
+ if check_spots_check and checkcheck(spot):
continue
moves.append(spot)
+ if castle_check and !has_moved and !Globals.in_check: # make sure this is only called when clicking
+ var rooks = [pos_around(Vector2.RIGHT * 3), pos_around(Vector2.LEFT * 4)]
+ var rook_motion = [pos_around(Vector2.RIGHT), pos_around(Vector2.LEFT)]
+ var king_moveto_spots = [Vector2.RIGHT, Vector2.LEFT] # O-O and O-O-O respectivel
+ for i in range(len(rooks)):
+ var rook = at_pos(rooks[i])
+ if !rook is Rook:
+ continue
+ if rook.has_moved:
+ continue
+ var direction = king_moveto_spots[i]
+ var posx2 = pos_around(direction * 2)
+ var pos = pos_around(direction)
+ if at_pos(posx2) or at_pos(pos):
+ continue
+ if checkcheck(posx2) or checkcheck(pos):
+ continue
+ can_castle.append([posx2, rook, rook_motion[i]])
+ moves.append(posx2)
return moves
+func castle(position):
+ can_castle.clear()
+ moveto(position)
+
+
func _ready():
._ready()
shortname = "k" + team
+
+
+func can_move(): # checks if you can legally move
+ castle_check = false
+ if get_moves().size() != 0 or get_attacks().size() != 0:
+ castle_check = true
+ return true
+ castle_check = false
+ return false
+
+
+func get_attacks(): # @Override
+ castle_check = false
+ var moves = get_moves() # assumes the attacks are same as moves
+ var final = []
+ for i in moves:
+ if at_pos(i) != null:
+ if at_pos(i).white != white: # attack ze enemie
+ if check_spots_check and checkcheck(i):
+ continue
+ final.append(i)
+ castle_check = true
+ return final
diff --git a/pieces/Knight.gd b/pieces/Knight.gd
index e46651c..0f58fc5 100644
--- a/pieces/Knight.gd
+++ b/pieces/Knight.gd
@@ -26,4 +26,4 @@ func get_moves():
func _ready():
._ready()
- shortname = "k" + team
+ shortname = "n" + team
diff --git a/pieces/Piece.gd b/pieces/Piece.gd
index 6ea76c0..9570ac1 100644
--- a/pieces/Piece.gd
+++ b/pieces/Piece.gd
@@ -103,6 +103,7 @@ func at_pos(vector):
func can_move(): # checks if you can legally move
if get_moves().size() != 0 or get_attacks().size() != 0:
return true
+ return false
func get_moves(): # @Override