online multiplayer chess game (note server currently down)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
extends Node

signal newmove(move)
signal newfen(fen)

var turn_moves: PoolStringArray = []
var turns_moves: PoolStringArray = []

var counter := 0


func _ready() -> void:
	Events.connect("turn_over", self, "_on_turn_over")


static func is_pawn(inode) -> bool:
	return inode is Pawn


static func is_king(inode) -> bool:
	return inode is King


func add_move(move) -> void:
	if turn_moves.size() == 0:
		turn_moves.append(str(Globals.fullmove) + ". " + move)
	else:
		turn_moves.append(move)
	emit_signal("newmove", move)


static func flip_int(i: int) -> int:
	return int(abs(7 - i))


func reset_vars() -> void:
	turn_moves.resize(0)
	turns_moves.resize(0)
	counter = 0


static func to_algebraic(real_position) -> String:
	return char(65 + (real_position.x)).to_lower() + str(8 - real_position.y)


static func from_algebraic(algebraic_position: String) -> Vector2:
	return Vector2(ord(algebraic_position[0]) - ord("a"), 8 - int(algebraic_position[1]))


static func get_node_name(node) -> Array:
	if is_pawn(node):
		return ["♙", "p"] if node.white else ["♟", "p"]
	elif node is King:
		return ["♔", "K"] if node.white else ["♚", "K"]
	elif node is Queen:
		return ["♕", "Q"] if node.white else ["♛", "Q"]
	elif node is Rook:
		return ["♖", "R"] if node.white else ["♜", "R"]
	elif node is Bishop:
		return ["♗", "B"] if node.white else ["♝", "B"]
	elif node is Knight:
		return ["♘", "N"] if node.white else ["♞", "N"]
	else:
		return ["", ""]


func internet_available() -> bool:
	var http = HTTPRequest.new()
	add_child(http)
	var httpurl = "https://1.1.1.1"
	var returnable = http.request(httpurl) == OK
	http.queue_free()
	return returnable


func walk_dir(path = "res://assets/pieces") -> PoolStringArray:  # walk the directory, finding the asset packs
	var folders: PoolStringArray = []  # init the folders
	var dir := Directory.new()  # init the directory
	if dir.open(path) == OK:  # open the directory
		dir.list_dir_begin(true)  # list the directory
		var file_name := dir.get_next()  # get the next file
		while file_name != "":  # while there is a file
			if dir.current_is_dir():  # if the current is a directory
				folders.append(file_name)  # add the folder
			file_name = dir.get_next()  # get the next file
	else:
		Log.err("An error occurred when trying to access the path " + path)  # print the error
	return folders  # return the folders


func format_seconds(time: float, use_milliseconds: bool = false) -> String:
	var format_string = "%02d:%04.1f" if use_milliseconds else "%02d:%02d"
	return format_string % [time / 60, fmod(time, 60)]


func _on_turn_over() -> void:
	var fen = fen()
	emit_signal("newfen", fen)
	counter += 1
	if counter >= 2:
		counter = 0
		turns_moves.append(turn_moves.join(" "))
		turn_moves.resize(0)


func fen() -> String:
	var pieces = ""
	for rank in range(8):
		var empty = 0
		for file in range(8):
			var spot = Globals.grid.matrix[rank][file]
			if spot == null:
				empty += 1
				if len(pieces) > 0 and str(empty - 1) == pieces[-1]:
					pieces[-1] = str(empty)
				else:
					pieces += str(empty)
			else:
				pieces += (spot.shortname[0].to_upper() if spot.white else spot.shortname[0].to_lower())
				empty = 0
		if rank != 7:
			pieces += "/"
	# handle castling checks
	var whitecastling = PoolStringArray(Globals.white_king.castleing(true)).join(" ")
	var blackcastling = PoolStringArray(Globals.black_king.castleing(true)).join(" ")
	var castlingrights = ""
	if blackcastling and whitecastling:
		castlingrights += "K" if "K" in whitecastling else ""
		castlingrights += "Q" if "Q" in whitecastling else ""
		castlingrights += "k" if "K" in blackcastling else ""
		castlingrights += "q" if "Q" in blackcastling else ""
	else:
		castlingrights = "-"

	var enpassants = ""
	for pawn in Globals.pawns:
		if pawn.twostepfirstmove and pawn.just_set:
			enpassants += to_algebraic(pawn.real_position + (Vector2.DOWN * pawn.whiteint))
	var fen = (
		"%s %s %s %s %s %s"
		% [
			pieces,
			"w" if Globals.team else "b",
			castlingrights,
			enpassants if enpassants else "-",
			Globals.halfmove,
			Globals.fullmove,
		]
	)  # pos  # turn  # castling  # enpassant  # halfmove  # fullmove
	return fen