online multiplayer chess game (note server currently down)
finalize 3fold repetition
bendn 2022-04-30
parent 4746a14 · commit 38d11fb
-rw-r--r--.gitignore1
-rw-r--r--Events.gd1
-rw-r--r--Grid.gd28
-rw-r--r--pieces/King.gd41
-rw-r--r--pieces/Pawn.gd34
5 files changed, 59 insertions, 46 deletions
diff --git a/.gitignore b/.gitignore
index ef7a920..56ac009 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
.import/
logs/
*.sh
+.vscode/
diff --git a/Events.gd b/Events.gd
index 608efaa..2d3b863 100644
--- a/Events.gd
+++ b/Events.gd
@@ -2,3 +2,4 @@ extends Node
# warning-ignore-all:unused_signal
signal turn_over
+signal just_before_turn_over # called just before turn over
diff --git a/Grid.gd b/Grid.gd
index 64e8988..139e34d 100644
--- a/Grid.gd
+++ b/Grid.gd
@@ -7,10 +7,6 @@ export(Color) var board_color1 = Color(0.870588, 0.890196, 0.901961)
export(Color) var board_color2 = Color(0.54902, 0.635294, 0.678431)
export(Color) var overlay_color = Color(0.2, 0.345098, 0.188235, 0.592157)
-onready var background = $Background
-
-onready var ASSETS_PATH = "res://assets/" + PIECE_SET + "/"
-
const Piece = preload("res://Piece.tscn")
const Square = preload("res://Square.tscn")
@@ -21,29 +17,26 @@ const default_metadata = {
"bccl": false, # black can castle left
"bccr": false, # black can castle right
"turn": true, # true = white, false = black
- "wccp": [], # white can enpassant
- "bccp": [], # black can enpassant
+ "wcep": [], # white can enpassant
+ "bcep": [], # black can enpassant
}
var matrix = []
var background_matrix = []
var history_matrixes: Dictionary = {}
+
var last_clicked
+onready var background = $Background
+onready var ASSETS_PATH = "res://assets/" + PIECE_SET + "/"
onready var piece_sets = walk_dir()
-func _ready(): # TODO: fill in wccp and bccp
+func _ready():
Globals.grid = self # tell the globals that this is the grid
init_board() # create the tile squares
init_matrix() # create the pieces
Events.connect("turn_over", self, "_on_turn_over") # listen for turn_over events
- # visualize the board
- # for y in range(8):
- # var strin = ""
- # for x in range(8):
- # strin += background_matrix[x][y].get_string() + " "
- # print(strin)
func threefoldrepetition():
@@ -56,28 +49,34 @@ func threefoldrepetition():
func mat2str(mat = matrix):
var string = ""
for y in range(8):
+ string += "\n"
for x in range(8):
var spot = mat[y][x]
if spot:
string += spot.mininame
else:
string += "*"
+ string += "\n"
for i in mat[8].keys(): # store the metadata
var thing = mat[8][i]
string += i + "=" + str(thing)
+ if default_metadata[i] != thing:
+ string += "!"
+ string += "\n"
return string
func _on_turn_over():
var matstr = mat2str()
+ # print(matstr)
if !history_matrixes.has(matstr):
history_matrixes[matstr] = 1
else:
history_matrixes[matstr] += 1
- matrix[8].turn = Globals.turn
Globals.checking_piece = null # reset checking_piece
Globals.in_check = false # reset in_check
matrix[8] = default_metadata.duplicate() # add the metadata to the matrix
+ matrix[8].turn = Globals.turn
check_in_check(true) # check if in_check
if !can_move():
print("what")
@@ -302,6 +301,7 @@ func handle_move(position):
func turn_over():
+ Events.emit_signal("just_before_turn_over")
Globals.add_turn()
Globals.turn = not Globals.turn
Events.emit_signal("turn_over")
diff --git a/pieces/King.gd b/pieces/King.gd
index 356c8b5..2a96c70 100644
--- a/pieces/King.gd
+++ b/pieces/King.gd
@@ -20,31 +20,24 @@ func get_moves():
return moves
-func moveto(position, real = true, take = false):
- if real: # assign metadata for threefold repetition draw check
- castleing()
- if can_castle.size() > 0:
- for i in can_castle:
- if i[3] == "O-O-O":
- if white:
- Globals.grid.matrix[8].wccl = true
- else:
- Globals.grid.matrix[8].bccl = true
+func just_before_over(): # assign metadata for threefold repetition draw check
+ castleing()
+ if can_castle.size() > 0:
+ for i in can_castle:
+ if i[3] == "O-O-O":
+ if white:
+ Globals.grid.matrix[8].wccl = true
else:
- if white:
- Globals.grid.matrix[8].wccr = true
- else:
- Globals.grid.matrix[8].wccr = true
- else:
- if white:
- Globals.grid.matrix[8].wccl = false
- Globals.grid.matrix[8].wccr = false
+ Globals.grid.matrix[8].bccl = true
else:
- Globals.grid.matrix[8].bccl = false
- Globals.grid.matrix[8].bccr = false
- if Input.is_action_pressed("ui_down") and real:
- breakpoint
- .moveto(position, real, take)
+ if white:
+ Globals.grid.matrix[8].wccr = true
+ else:
+ Globals.grid.matrix[8].bccr = true
+
+
+func _ready():
+ Events.connect("just_before_turn_over", self, "just_before_over")
func castleing():
@@ -53,6 +46,8 @@ func castleing():
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)):
+ if !is_on_board(rooks[i]):
+ continue
var rook = at_pos(rooks[i])
if !rook is Rook:
continue
diff --git a/pieces/Pawn.gd b/pieces/Pawn.gd
index e6deb56..5a2d36b 100644
--- a/pieces/Pawn.gd
+++ b/pieces/Pawn.gd
@@ -64,11 +64,11 @@ func get_attacks():
continue
if at_pos(point) != null and at_pos(point).white != white:
moves.append(point)
- moves.append_array(en_passant("attacks"))
+ en_passant()
return moves
-func en_passant(type: String = "moves"): # in passing
+func en_passant(turncheck = true): # in passing
var passants = [pos_around(Vector2.LEFT), pos_around(Vector2.RIGHT)]
var moves = []
for i in passants:
@@ -81,19 +81,35 @@ func en_passant(type: String = "moves"): # in passing
continue
if !Utils.is_pawn(spot):
continue
+ if turncheck and white != Globals.turn:
+ continue
if !spot.twostepfirstmove:
continue
if check_spots_check and checkcheck(i):
continue
- if type == "moves":
- var position = i + (Vector2.UP * whiteint)
- if !at_pos(position):
- moves.append(position)
- elif type == "attacks":
- enpassant.append([i + (Vector2.UP * whiteint), spot])
+ var position = i + (Vector2.UP * whiteint)
+ if !at_pos(position):
+ moves.append(position)
+ enpassant.append([position, spot])
return moves
+func _just_before_turn_over():
+ var had_a_enpassant = len(enpassant) > 0
+ enpassant.clear()
+ if !had_a_enpassant: # scuffed method to check if enpassant is possible
+ en_passant(false)
+ var temporary = []
+ for i in enpassant:
+ temporary.append(i[0])
+ if !temporary:
+ return
+ if white:
+ Globals.grid.matrix[8].wcep.append_array(temporary)
+ else:
+ Globals.grid.matrix[8].bcep.append_array(temporary)
+
+
func _ready():
Events.connect("turn_over", self, "_on_turn_over")
- ._ready()
+ Events.connect("just_before_turn_over", self, "_just_before_turn_over")