online multiplayer chess game (note server currently down)
-rw-r--r--Globals.gd17
-rw-r--r--Grid.gd29
-rw-r--r--pieces/Piece.gd6
3 files changed, 44 insertions, 8 deletions
diff --git a/Globals.gd b/Globals.gd
index e5c0dce..09b9c2c 100644
--- a/Globals.gd
+++ b/Globals.gd
@@ -1,10 +1,25 @@
extends Node
var grid: Grid = null
-var turns := 0
+var white_turns := 0
+var black_turns := 0
var in_check = false
var checking_piece: Piece = null
var white_king: King
var black_king: King
var turn := true # true for white, false for black
# true cuz white goes first
+
+
+func turns(winner):
+ if winner == "white":
+ return white_turns
+ elif winner == "black":
+ return black_turns
+
+
+func add_turn():
+ if turn:
+ white_turns += 1
+ else:
+ black_turns += 1
diff --git a/Grid.gd b/Grid.gd
index bd793b8..a8a5016 100644
--- a/Grid.gd
+++ b/Grid.gd
@@ -23,7 +23,7 @@ var last_clicked
onready var piece_sets = walk_dir()
-func _ready(): # TODO: add checkmates
+func _ready(): # TODO: add draws
Globals.grid = self # tell the globals that this is the grid
init_board() # create the tile squares
init_matrix() # create the pieces
@@ -33,10 +33,17 @@ func _ready(): # TODO: add checkmates
func _on_turn_over():
Globals.checking_piece = null # reset checking_piece
Globals.in_check = false # reset in_check
- check_in_check() # check if in_check
+ check_in_check(true) # check if in_check
+ if Globals.in_check and is_check_mate():
+ var winner = "black" if Globals.turn else "white"
+ print(winner, " won the game in ", Globals.turns(winner), " turns!")
+ print("the board:")
+ print_matrix_pretty()
+ yield(get_tree().create_timer(5), "timeout")
+ get_tree().reload_current_scene()
-func check_in_check(): # check if in_check
+func check_in_check(prin = false): # check if in_check
for i in range(0, 8): # for each row
for j in range(0, 8): # for each column
var spot = matrix[i][j] # get the square
@@ -44,11 +51,22 @@ func check_in_check(): # check if in_check
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
+ if prin:
+ print("check by " + spot.shortname) # print the check
return true # stop at the first check found
return false
+func is_check_mate():
+ for i in range(0, 8): # for each row
+ for j in range(0, 8): # for each column
+ var spot = matrix[i][j] # get the square
+ if spot and spot.white == Globals.turn: # fren
+ if spot.can_move():
+ return false
+ return true
+
+
func _exit_tree():
Globals.grid = null # reset the globals grid when leaving tree
@@ -168,7 +186,6 @@ func square_clicked(position: Vector2): # square clicked
if !last_clicked: # last clicked is null, so this is pointless
return
if check_for_frame(position): # takeable
- print("EATING " + last_clicked.shortname + " AT " + str(position)) # print the move
last_clicked.take(matrix[position.y][position.x]) # eat
turn_over()
if check_for_circle(position): # see if theres a circle at the position
@@ -184,8 +201,8 @@ func square_clicked(position: Vector2): # square clicked
func turn_over():
+ Globals.add_turn()
Globals.turn = not Globals.turn
- Globals.turns += 1
Events.emit_signal("turn_over")
diff --git a/pieces/Piece.gd b/pieces/Piece.gd
index 6c7721b..6ea76c0 100644
--- a/pieces/Piece.gd
+++ b/pieces/Piece.gd
@@ -100,6 +100,11 @@ func at_pos(vector):
return Globals.grid.matrix[vector.y][vector.x]
+func can_move(): # checks if you can legally move
+ if get_moves().size() != 0 or get_attacks().size() != 0:
+ return true
+
+
func get_moves(): # @Override
pass
@@ -140,7 +145,6 @@ func set_circle(positions: Array, type := "move"):
for pos in positions:
var spot = at_pos(pos) # get the piece at the position
if type == "move":
- print(shortname, " can move to ", pos)
create_move_circles(pos) # create the move circle
elif type == "take":
create_take_circles(spot) # if the king is in check, return true