Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
oerc0122 committed Mar 6, 2021
1 parent b043b1d commit 3b7a464
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 42 deletions.
Binary file modified BoardTile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 5 additions & 10 deletions FileParser.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extends Node

var path := ""
var data := {}
var turns := []
const COLOURS = ["White", "Black"]
Expand All @@ -8,11 +9,12 @@ func _ready():
self.read('test.pgn')

func read(path):
self.path = path
var file := File.new()
var status = file.open(path, File.READ)
var status = file.open(self.path, File.READ)

if status:
printerr("Cannot open file: ", path)
printerr("Cannot open file: ", self.path)
var content := file.get_as_text()
file.close()

Expand Down Expand Up @@ -43,7 +45,7 @@ func read(path):

var currTurn
if not "setup" in data:
currTurn = Turn.new().standard_start()
currTurn = Turn.new()
currTurn.raw = "0. Start game"
turns.push_back(currTurn)

Expand All @@ -52,12 +54,5 @@ func read(path):
turns.push_back(currTurn)
currTurn = Turn.new(Turn.COLOUR.BLACK, currTurn.positions, turn, currTurn.promotions)
turns.push_back(currTurn)
if currTurn.turnNo > 2:
break

for turn in turns:
print(turn.raw)
print(turn.positions)

print(turns)

28 changes: 25 additions & 3 deletions MainGame.gd
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
extends Node

var piece = load("res://Piece.tscn")
const TILE_OFFSET := Vector2(32,32)

var turnID = 0
var nTurns = 0
signal turn_updated

func _ready():
load_turn(0)
nTurns = len($FileParser.turns)

func _input(event: InputEvent) -> void:
if event.is_action_pressed('ui_right'):
self.next_turn()
if event.is_action_pressed("ui_left"):
self.prev_turn()

func next_turn():
turnID = clamp(turnID+1, 0, nTurns-1)
load_turn(turnID)

func prev_turn():
turnID = clamp(turnID-1, 0, nTurns-1)
load_turn(turnID)

func load_turn(turn: int):
self.turnID = turn
var currTurn = $FileParser.turns[turn]
for child in $Board.get_children(): # Clear old children
child.queue_free()
for pos in currTurn.positions:
var currPiece = piece.instance()
var loc = $Board.map_to_world(currTurn.positions[pos])
currPiece.frame = "KQBNRP".find(pos[1])
currPiece.name = pos
if pos[0] == "B":
currPiece.frame+=6
currPiece.position = loc
add_child(currPiece)
currPiece.position = loc + TILE_OFFSET
$Board.add_child(currPiece)
emit_signal("turn_updated", self.turnID)
7 changes: 4 additions & 3 deletions MainGame.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ script = ExtResource( 3 )

[node name="Board" type="TileMap" parent="."]
tile_set = SubResource( 1 )
cell_quadrant_size = 64
cell_y_sort = true
format = 1
tile_data = PoolIntArray( -524287, 0, 0, -524286, 1, 0, -524285, 0, 0, -524284, 1, 0, -524283, 0, 0, -524282, 1, 0, -524281, 0, 0, -524280, 1, 0, -458751, 1, 0, -458750, 0, 0, -458749, 1, 0, -458748, 0, 0, -458747, 1, 0, -458746, 0, 0, -458745, 1, 0, -458744, 0, 0, -393215, 0, 0, -393214, 1, 0, -393213, 0, 0, -393212, 1, 0, -393211, 0, 0, -393210, 1, 0, -393209, 0, 0, -393208, 1, 0, -327679, 1, 0, -327678, 0, 0, -327677, 1, 0, -327676, 0, 0, -327675, 1, 0, -327674, 0, 0, -327673, 1, 0, -327672, 0, 0, -262143, 0, 0, -262142, 1, 0, -262141, 0, 0, -262140, 1, 0, -262139, 0, 0, -262138, 1, 0, -262137, 0, 0, -262136, 1, 0, -196607, 1, 0, -196606, 0, 0, -196605, 1, 0, -196604, 0, 0, -196603, 1, 0, -196602, 0, 0, -196601, 1, 0, -196600, 0, 0, -131071, 0, 0, -131070, 1, 0, -131069, 0, 0, -131068, 1, 0, -131067, 0, 0, -131066, 1, 0, -131065, 0, 0, -131064, 1, 0, -65535, 1, 0, -65534, 0, 0, -65533, 1, 0, -65532, 0, 0, -65531, 1, 0, -65530, 0, 0, -65529, 1, 0, -65528, 0, 0 )
tile_data = PoolIntArray( 65537, 1, 0, 65538, 0, 0, 65539, 1, 0, 65540, 0, 0, 65541, 1, 0, 65542, 0, 0, 65543, 1, 0, 65544, 0, 0, 131073, 0, 0, 131074, 1, 0, 131075, 0, 0, 131076, 1, 0, 131077, 0, 0, 131078, 1, 0, 131079, 0, 0, 131080, 1, 0, 196609, 1, 0, 196610, 0, 0, 196611, 1, 0, 196612, 0, 0, 196613, 1, 0, 196614, 0, 0, 196615, 1, 0, 196616, 0, 0, 262145, 0, 0, 262146, 1, 0, 262147, 0, 0, 262148, 1, 0, 262149, 0, 0, 262150, 1, 0, 262151, 0, 0, 262152, 1, 0, 327681, 1, 0, 327682, 0, 0, 327683, 1, 0, 327684, 0, 0, 327685, 1, 0, 327686, 0, 0, 327687, 1, 0, 327688, 0, 0, 393217, 0, 0, 393218, 1, 0, 393219, 0, 0, 393220, 1, 0, 393221, 0, 0, 393222, 1, 0, 393223, 0, 0, 393224, 1, 0, 458753, 1, 0, 458754, 0, 0, 458755, 1, 0, 458756, 0, 0, 458757, 1, 0, 458758, 0, 0, 458759, 1, 0, 458760, 0, 0, 524289, 0, 0, 524290, 1, 0, 524291, 0, 0, 524292, 1, 0, 524293, 0, 0, 524294, 1, 0, 524295, 0, 0, 524296, 1, 0 )

[node name="FileParser" parent="." instance=ExtResource( 1 )]

[node name="Camera2D" type="Camera2D" parent="."]
position = Vector2( 322.424, -255 )
position = Vector2( 320, 320 )
current = true
zoom = Vector2( 0.9, 0.9 )
1 change: 1 addition & 0 deletions Piece.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[ext_resource path="res://ChessSprite.png" type="Texture" id=1]

[node name="Piece" type="Sprite"]
scale = Vector2( 0.3, 0.3 )
texture = ExtResource( 1 )
vframes = 2
hframes = 6
Expand Down
62 changes: 41 additions & 21 deletions Turn.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ const KNIGHT_MOVES = [Vector2(1,2),
Vector2(-2,1),
Vector2(-2,-1),
Vector2(2,-1)]
const STARTING_BOARD_STATE = {"WR1":Vector2(1,1),"WN1":Vector2(2,1),"WB1":Vector2(3,1),"WK":Vector2(4,1),"WQ":Vector2(5,1),"WB2":Vector2(6,1),"WN2":Vector2(7,1),"WR2":Vector2(8,1),
const STARTING_BOARD_STATE = {"WR1":Vector2(1,1),"WN1":Vector2(2,1),"WB1":Vector2(3,1),"WQ":Vector2(4,1),"WK":Vector2(5,1),"WB2":Vector2(6,1),"WN2":Vector2(7,1),"WR2":Vector2(8,1),
"WP1":Vector2(1,2),"WP2":Vector2(2,2),"WP3":Vector2(3,2),"WP4":Vector2(4,2),"WP5":Vector2(5,2),"WP6":Vector2(6,2),"WP7":Vector2(7,2),"WP8":Vector2(8,2),
"BR1":Vector2(1,8),"BN1":Vector2(2,8),"BB1":Vector2(3,8),"BQ":Vector2(4,8),"BK":Vector2(5,8),"BB2":Vector2(6,8),"BN2":Vector2(7,8),"BR2":Vector2(8,8),
"BP1":Vector2(1,7),"BP2":Vector2(2,7),"BP3":Vector2(3,7),"BP4":Vector2(4,7),"BP5":Vector2(5,7),"BP6":Vector2(6,7),"BP7":Vector2(7,7),"BP8":Vector2(8,7)}


var raw : String
var ID : String
var piece : String
var location : String
var turnNo : int
Expand All @@ -35,29 +36,31 @@ var promotions : int = 0

func _init(colour: int = COLOUR.WHITE, boardState: Dictionary = STARTING_BOARD_STATE, turn: RegExMatch = null, nPromotions: int = 0, comment = null):
self.player = colour
self.positions = boardState
self.positions = boardState.duplicate()
if turn:
self.raw = turn.get_string()
self.turnNo = int(turn.get_string("turn"))
self.capture = turn.get_string(self.get_colour()+"Capture") != ""
self.check = "+#".find(turn.get_string(self.get_colour()+"Check")) + 1
self.piece = self.which_piece(colour, turn.get_string(self.get_colour()+"Piece"),
turn.get_string(self.get_colour()+"Disambiguation"),
turn.get_string(self.get_colour()+"Location"))
self.location = turn.get_string(self.get_colour()+"Location")
self.move(self.piece, self.location)
var castle = turn.get_string(self.get_colour()+"Castle")
if castle:
self.castling(castle.count("O")==2)
self.ID = self.get_colour() + " : " + castle
else:
self.piece = self.which_piece(colour, turn.get_string(self.get_colour()+"Piece"),
turn.get_string(self.get_colour()+"Disambiguation"),
turn.get_string(self.get_colour()+"Location"))
self.location = turn.get_string(self.get_colour()+"Location")
self.move(self.piece, self.location)
self.ID = self.get_colour() + " : " + self.piece[1] + self.from_grid(boardState[self.piece]) + " -> " + self.location
self.comment = turn.get_string("Comment")
else:
self.turnNo = 0
self.ID = "Board start"

if comment:
self.comment += "; "+ comment

func standard_start():
var currTurn = self
currTurn.turnNo = 0
return currTurn

func which_piece(colour, label, disamb, pos) -> String:
var check : String
match colour:
Expand All @@ -79,12 +82,13 @@ func which_piece(colour, label, disamb, pos) -> String:

if len(possibles) == 0:
printerr("No possible piece found")
return "Not found"
elif len(possibles) == 1:
return possibles[0]

if disamb: # Have disambiguation
for poss in possibles:
if disamb in positions[poss]:
if disamb in self.from_grid(positions[poss]):
return poss

# Ambiguous: need to do this manually
Expand Down Expand Up @@ -112,7 +116,7 @@ func which_piece(colour, label, disamb, pos) -> String:
"P":
if ((colour == COLOUR.WHITE and positions[poss].y == 2) or # Initial move of two
(colour == COLOUR.BLACK and positions[poss].y == 7)):
if raw.y in [colour, 2*colour]:
if ref.x == 0 and raw.y in [colour, 2*colour]:
return poss
# En passant?
if self.capture:
Expand All @@ -123,33 +127,49 @@ func which_piece(colour, label, disamb, pos) -> String:
if self.capture:
if raw.y == colour and ref.x == 1:
return poss
if raw.y == colour:
if ref.x == 0 and raw.y == colour:
return poss
print(piece, colour, label)
printerr(piece, " ", get_colour(), " ", label, " ", pos, " ", to_grid(pos))
printerr(positions)
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])
return NUM[int(pos.x)] + str(pos.y)

func move(piece, location):
if self.promote:
self.positions.erase(piece)
piece = self.get_colour() + self.promote + char(65+self.promotions)
self.promotions += 1


self.positions[piece] = self.to_grid(location)

if self.capture:
if self.captureLocation != null:
location = self.captureLocation

for capturedPiece in self.positions:
if self.positions[capturedPiece] == location:
if self.positions[capturedPiece] == self.to_grid(location):
self.positions.erase(capturedPiece)

self.positions[piece] = self.to_grid(location)

func castling(kingside: bool):
if kingside:
if self.player == COLOUR.WHITE:
self.move("WK", "g1")
self.move("WR2", "f1")
else:
self.move("BK", "g8")
self.move("BR2", "f8")
else:
if self.player == COLOUR.WHITE:
self.move("WK", "c1")
self.move("WR2", "d1")
else:
self.move("BK", "c8")
self.move("BR2", "d8")

func get_colour() -> String:
return COLOUR_NAMES[self.player]
36 changes: 36 additions & 0 deletions UI.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
extends Control

onready var game = $ViewportContainer/Viewport/MainGame
onready var parser = $ViewportContainer/Viewport/MainGame/FileParser
onready var turnsList = $PanelContainer/VBoxContainer/Turns/VBoxContainer
var lastTurn = 0

func _ready() -> void:
$PanelContainer/VBoxContainer/GameDetails.text = parser.path+"\n"
for data in parser.data:
$PanelContainer/VBoxContainer/GameDetails.text += data+" : "+parser.data[data]+"\n"

var i = 0
for turn in parser.turns:
var button = Button.new()
button.text = turn.ID
button.name = "Button"+str(i)
button.connect('pressed', self, 'set_turn', [i])
turnsList.add_child(button)
i += 1
game.load_turn(0)

func update_turn(newTurn):
turnsList.get_child(lastTurn).disabled = false
turnsList.get_child(newTurn).disabled = true
self.lastTurn = newTurn
$PanelContainer/VBoxContainer/HBoxContainer/TurnNo.text = str(newTurn)

func set_turn(turn: int):
self.game.load_turn(turn)

func _on_prevTurn_button_up() -> void:
self.game.prev_turn()

func _on_nextTurn_button_up() -> void:
self.game.next_turn()
91 changes: 91 additions & 0 deletions UI.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
[gd_scene load_steps=3 format=2]

[ext_resource path="res://MainGame.tscn" type="PackedScene" id=1]
[ext_resource path="res://UI.gd" type="Script" id=2]

[node name="UI" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
script = ExtResource( 2 )
__meta__ = {
"_edit_use_anchors_": false
}

[node name="ViewportContainer" type="ViewportContainer" parent="."]
anchor_bottom = 1.0
margin_right = 880.0
stretch = true
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Viewport" type="Viewport" parent="ViewportContainer"]
size = Vector2( 880, 600 )
handle_input_locally = false
render_target_update_mode = 3

[node name="MainGame" parent="ViewportContainer/Viewport" instance=ExtResource( 1 )]

[node name="PanelContainer" type="PanelContainer" parent="."]
anchor_left = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = -143.0
__meta__ = {
"_edit_use_anchors_": false
}

[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer"]
margin_left = 7.0
margin_top = 7.0
margin_right = 136.0
margin_bottom = 593.0

[node name="GameDetails" type="Label" parent="PanelContainer/VBoxContainer"]
margin_right = 129.0
margin_bottom = 14.0

[node name="HBoxContainer" type="HBoxContainer" parent="PanelContainer/VBoxContainer"]
margin_top = 18.0
margin_right = 129.0
margin_bottom = 32.0

[node name="TurnLabel" type="Label" parent="PanelContainer/VBoxContainer/HBoxContainer"]
margin_right = 36.0
margin_bottom = 14.0
text = "Turn: "

[node name="TurnNo" type="Label" parent="PanelContainer/VBoxContainer/HBoxContainer"]
margin_left = 40.0
margin_right = 40.0
margin_bottom = 14.0

[node name="Turns" type="ScrollContainer" parent="PanelContainer/VBoxContainer"]
margin_top = 36.0
margin_right = 129.0
margin_bottom = 336.0
rect_min_size = Vector2( 129, 300 )
scroll_horizontal_enabled = false

[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer/VBoxContainer/Turns"]

[node name="VBoxContainer2" type="HBoxContainer" parent="PanelContainer/VBoxContainer"]
margin_top = 340.0
margin_right = 129.0
margin_bottom = 360.0
alignment = 1

[node name="prevTurn" type="Button" parent="PanelContainer/VBoxContainer/VBoxContainer2"]
margin_left = 37.0
margin_right = 62.0
margin_bottom = 20.0
text = "<-"

[node name="nextTurn" type="Button" parent="PanelContainer/VBoxContainer/VBoxContainer2"]
margin_left = 66.0
margin_right = 91.0
margin_bottom = 20.0
text = "->"
[connection signal="turn_updated" from="ViewportContainer/Viewport/MainGame" to="." method="update_turn"]
[connection signal="button_up" from="PanelContainer/VBoxContainer/VBoxContainer2/prevTurn" to="." method="_on_prevTurn_button_up"]
[connection signal="button_up" from="PanelContainer/VBoxContainer/VBoxContainer2/nextTurn" to="." method="_on_nextTurn_button_up"]
10 changes: 5 additions & 5 deletions test.pgn
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
[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
1. e4 e5 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
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
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
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

0 comments on commit 3b7a464

Please sign in to comment.