-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
extends Node | ||
|
||
var data := {} | ||
var turns := [] | ||
const COLOURS = ["White", "Black"] | ||
|
||
func _ready(): | ||
self.read('test.pgn') | ||
|
||
func read(path): | ||
var file := File.new() | ||
var status = file.open(path, File.READ) | ||
|
||
if status: | ||
printerr("Cannot open file: ", path) | ||
var content := file.get_as_text() | ||
file.close() | ||
|
||
var rawCommentRE = RegEx.new() | ||
rawCommentRE.compile(";(?<comment>.*)") | ||
var tagRE = RegEx.new() | ||
tagRE.compile("\\[(?<key>[A-Za-z]+) *\"(?<value>[^\"]+)\"\\]") | ||
var moveRE = ("(?<Piece>[NKQRBP]?)"+ | ||
"(?<Disambiguation>(?:[a-h]|[1-8]|[a-h][1-8])(?=x?[a-h][1-8]))?"+ | ||
"(?<Capture>x)?"+ | ||
"(?<Location>[a-h][1-8])"+ | ||
"(?<Promotion>=[NKQRB])?"+ | ||
"(?<Check>[+#])?") | ||
var castleRE = "O-O(-O)?" | ||
var commentRE = "\\{(?<comment>[^}]+)\\}" | ||
var turnRE = RegEx.new() | ||
turnRE.compile("(?<turn>[0-9]+)\\.\\s*" + | ||
"("+moveRE.replace("?<","?<White")+"|(?<WhiteCastle>O-O(?:-O)?))\\s*" + | ||
"("+moveRE.replace("?<","?<Black")+"|(?<BlackCastle>O-O(?:-O)?))\\s*" + | ||
"(?:"+commentRE+")?" | ||
) | ||
|
||
content = rawCommentRE.sub(content, "{$comment}", true).strip_escapes() | ||
|
||
for tag in tagRE.search_all(content): | ||
data[tag.get_string("key")] = tag.get_string("value") | ||
content = content.replace(tag.get_string(), "") | ||
|
||
var currTurn | ||
if not "setup" in data: | ||
currTurn = Turn.new().standard_start() | ||
turns.push_back(currTurn) | ||
|
||
for turn in turnRE.search_all(content): | ||
currTurn = calc_turn(Turn.COLOUR.WHITE, currTurn.positions, turn) | ||
turns.push_back(currTurn) | ||
currTurn = calc_turn(Turn.COLOUR.BLACK, currTurn.positions, turn) | ||
turns.push_back(currTurn) | ||
if currTurn.turnNo > 2: | ||
break | ||
|
||
print(turns) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[gd_scene load_steps=2 format=2] | ||
|
||
[ext_resource path="res://FileParser.gd" type="Script" id=1] | ||
|
||
[node name="FileParser" type="Node"] | ||
script = ExtResource( 1 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
extends Resource | ||
class_name Turn | ||
|
||
enum COLOUR {WHITE=1, BLACK=-1} | ||
const COLOUR_NAMES = {COLOUR.WHITE:"White", COLOUR.BLACK:"Black"} | ||
const LET = {"a":1, "b":2, "c":3, "d":4, "e":5, "f":6, "g":7, "h":8} | ||
const NUM = {1:"a", 2:"b", 3:"c", 4:"d", 5:"e", 6:"f", 7:"g", 8:"h"} | ||
const KNIGHT_MOVES = [Vector2(1,2), | ||
Vector2(-1,2), | ||
Vector2(-1,-2), | ||
Vector2(1,-2), | ||
Vector2(2,1), | ||
Vector2(-2,1), | ||
Vector2(-2,-1), | ||
Vector2(2,-1)] | ||
|
||
var raw : String | ||
var piece : String | ||
var location : String | ||
var turnNo : int | ||
var player : int | ||
var comment : String | ||
var positions : Dictionary | ||
var capture : bool = false | ||
var check : int = 0 | ||
|
||
func standard_start(): | ||
var currTurn = self | ||
currTurn.turnNo = 0 | ||
currTurn.positions = {"WR1":Vector2(1,1),"WN1":Vector2(1,2),"WB1":Vector2(1,3),"WK":Vector2(1,4),"WQ":Vector2(1,5),"WB2":Vector2(1,6),"WN2":Vector2(1,7),"WR2":Vector2(1,8), | ||
"WP1":Vector2(2,1),"WP2":Vector2(2,2),"WP3":Vector2(2,3),"WP4":Vector2(2,4),"WP5":Vector2(2,5),"WP6":Vector2(2,6),"WP7":Vector2(2,7),"WP8":Vector2(2,8), | ||
"BR1":Vector2(8,1),"BN1":Vector2(8,2),"BB1":Vector2(8,3),"BQ":Vector2(8,4),"BK":Vector2(8,5),"BB2":Vector2(8,6),"BN2":Vector2(8,7),"BR2":Vector2(8,8), | ||
"BP1":Vector2(7,1),"BP2":Vector2(7,2),"BP3":Vector2(7,3),"BP4":Vector2(7,4),"BP5":Vector2(7,5),"BP6":Vector2(7,6),"BP7":Vector2(7,7),"BP8":Vector2(7,8)} | ||
currTurn.comment = "Start game" | ||
return currTurn | ||
|
||
func which_piece(colour, label, disamb, pos) -> String: | ||
var check : String | ||
if colour == COLOUR.WHITE: | ||
check = "W" | ||
else: | ||
check = "B" | ||
|
||
if not label: | ||
label = "P" | ||
check += label | ||
|
||
var possibles := [] | ||
for key in positions.keys(): | ||
if key.left(2) == check: | ||
possibles.push_back(key) | ||
|
||
if len(possibles) == 0: | ||
printerr("No possible piece found") | ||
elif len(possibles) == 1: | ||
return possibles[0] | ||
|
||
if disamb: # Have disambiguation | ||
for poss in possibles: | ||
if disamb in positions[poss]: | ||
return poss | ||
|
||
# Need to do this manually | ||
var grid_pos = to_grid(pos) | ||
|
||
for poss in possibles: | ||
var ref = abs(grid_pos - positions[poss]) | ||
if label == "Q": # Ambiguous | ||
if ref.x == 0 or ref.y == 0 or ref.x == ref.y: | ||
return poss | ||
elif label == "K": | ||
if ref.length_squared < 2: | ||
return poss | ||
elif label == "N": | ||
if ref in KNIGHT_MOVES: | ||
return poss | ||
elif label == "R": | ||
if ref.x == 0 or ref.y == 0: | ||
return poss | ||
elif label == "B": | ||
if ref.x == ref.y: | ||
return poss | ||
elif label == "P": | ||
if self.capture: | ||
if ref.y == colour and abs(ref.x) == 1: | ||
return poss | ||
else: | ||
if ref.y == colour: | ||
return poss | ||
return "Not found" | ||
|
||
func to_grid(pos: String) -> Vector2: | ||
return Vector2(LET[pos[0]], int(pos[1])) | ||
|
||
func from_grid(pos: Vector2) -> String: | ||
return NUM[pos[0]] + str(pos[1]) | ||
|
||
func move(piece, location): | ||
pass | ||
|
||
func calc_turn(colour: int, boardState: Dictionary, turn: RegExMatch): | ||
self.turnNo = int(turn.get_string("turn")) | ||
self.player = colour | ||
self.capture = turn.get_string(COLOUR_NAMES[colour]+"Capture") != "" | ||
self.check = "+#".find(turn.get_string(COLOUR_NAMES[colour]+"Check")) + 1 | ||
self.positions = boardState | ||
self.piece = self.which_piece(colour, turn.get_string(COLOUR_NAMES[colour]+"Piece"), | ||
turn.get_string(COLOUR_NAMES[colour]+"Disambiguation"), | ||
turn.get_string(COLOUR_NAMES[colour]+"Location")) | ||
self.positions = self.move(piece, location) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[gd_resource type="Environment" load_steps=2 format=2] | ||
|
||
[sub_resource type="ProceduralSky" id=1] | ||
|
||
[resource] | ||
background_mode = 2 | ||
background_sky = SubResource( 1 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[remap] | ||
|
||
importer="texture" | ||
type="StreamTexture" | ||
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" | ||
metadata={ | ||
"vram_texture": false | ||
} | ||
|
||
[deps] | ||
|
||
source_file="res://icon.png" | ||
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ] | ||
|
||
[params] | ||
|
||
compress/mode=0 | ||
compress/lossy_quality=0.7 | ||
compress/hdr_mode=0 | ||
compress/bptc_ldr=0 | ||
compress/normal_map=0 | ||
flags/repeat=0 | ||
flags/filter=true | ||
flags/mipmaps=false | ||
flags/anisotropic=false | ||
flags/srgb=2 | ||
process/fix_alpha_border=true | ||
process/premult_alpha=false | ||
process/HDR_as_SRGB=false | ||
process/invert_color=false | ||
stream=false | ||
size_limit=0 | ||
detect_3d=true | ||
svg/scale=1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
; Engine configuration file. | ||
; It's best edited using the editor UI and not directly, | ||
; since the parameters that go here are not all obvious. | ||
; | ||
; Format: | ||
; [section] ; section goes between [] | ||
; param=value ; assign values to parameters | ||
|
||
config_version=4 | ||
|
||
_global_script_classes=[ { | ||
"base": "Resource", | ||
"class": "Turn", | ||
"language": "GDScript", | ||
"path": "res://Turn.gd" | ||
} ] | ||
_global_script_class_icons={ | ||
"Turn": "" | ||
} | ||
|
||
[application] | ||
|
||
config/name="ChessRender" | ||
config/icon="res://icon.png" | ||
|
||
[rendering] | ||
|
||
environment/default_environment="res://default_env.tres" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[Event "F/S Return Match"] | ||
[Site "Belgrade, Serbia JUG"] | ||
[Date "1992.11.04"] | ||
[Round "29"] | ||
[White "Fischer, Robert J."] | ||
[Black "Spassky, Boris V."] | ||
[Result "1/2-1/2"] | ||
|
||
1. e4 d5 2. Nf3 Nc6 3. Bb5 a6 {This opening is called the Ruy Lopez.} | ||
4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6 8. c3 O-O 9. h3 Nb8 10. d4 Nbd7 ; End of line comment | ||
11. c4 c6 12. cxb5 axb5 13. Nc3 Bb7 14. Bg5 b4 15. Nb1 h6 16. Bh4 c5 17. dxe5 | ||
Nxe4 18. Bxe7 Qxe7 19. exd6 Qf6 20. Nbd2 NxD6 21. Nc4 Nxc4 22. Bxc4 Nb6 | ||
23. Ne5 Rae8 24. Bxf7+ Rxf7 25. Nxf7 Rxe1+ 26. Qxe1 Kxf7 27. Qe3 Qg5 28. Qxg5 | ||
hxg5 29. b3 Ke6 30. a3 Kd6 31. axb4 cxb4 32. Ra5 Nd5 33. f3 Bc8 34. Kf2 Bf5 | ||
35. Ra7 g6 36. Ra6+ Kc5 37. Ke1 Nf4 38. g3 Nxh3 39. Kd2 Kb5 40. Rd6 Kc5 41. | ||
Ra6 Nf2 42. g4 Bd3 43. Re6 1/2-1/2 |