Skip to content
Snippets Groups Projects
Commit b24b286b authored by Alan's avatar Alan
Browse files

Added King, Knight, Queen implementations

parent 5f65f108
No related branches found
No related tags found
1 merge request!1Refactoring game.gd to split up move validation to individual pieces, added...
......@@ -25,8 +25,8 @@ dock_5="Inspector,Node,History"
[EditorNode]
open_scenes=PackedStringArray("res://scenes/game.tscn", "res://scenes/explosion.tscn", "res://scenes/Pawn.tscn", "res://scenes/Rook.tscn", "res://scenes/Bishop.tscn")
current_scene="res://scenes/Bishop.tscn"
open_scenes=PackedStringArray("res://scenes/game.tscn", "res://scenes/explosion.tscn", "res://scenes/Pawn.tscn", "res://scenes/Rook.tscn", "res://scenes/Bishop.tscn", "res://scenes/Queen.tscn", "res://scenes/King.tscn", "res://scenes/Knight.tscn")
current_scene="res://scenes/Knight.tscn"
center_split_offset=-313
selected_default_debugger_tab_idx=0
selected_main_editor_idx=2
......@@ -34,8 +34,8 @@ selected_bottom_panel_item=0
[ScriptEditor]
open_scripts=["res://scripts/Bishop.gd", "res://scripts/game.gd", "res://scripts/Pawn.gd", "res://scripts/Piece.gd", "res://scripts/Rook.gd"]
selected_script="res://scripts/Bishop.gd"
open_scripts=["res://scripts/Bishop.gd", "res://scripts/game.gd", "res://scripts/King.gd", "res://scripts/Knight.gd", "res://scripts/Pawn.gd", "res://scripts/Piece.gd", "res://scripts/Queen.gd", "res://scripts/Rook.gd"]
selected_script="res://scripts/Knight.gd"
open_help=[]
script_split_offset=70
list_split_offset=0
......
......@@ -15,3 +15,9 @@ res://scenes/Rook.tscn
res://scripts/Piece.gd
res://scripts/Bishop.gd
res://scenes/Bishop.tscn
res://scripts/Queen.gd
res://scenes/Queen.tscn
res://scripts/King.gd
res://scenes/King.tscn
res://scripts/Knight.gd
res://scenes/Knight.tscn
......@@ -175,4 +175,4 @@ Anim={
"zfar": 4000.01,
"znear": 0.05
}
selected_nodes=Array[NodePath]([NodePath("/root/@EditorNode@16886/@Panel@13/@VBoxContainer@14/DockHSplitLeftL/DockHSplitLeftR/DockHSplitMain/@VBoxContainer@25/DockVSplitCenter/@VSplitContainer@52/@VBoxContainer@53/@PanelContainer@98/MainScreen/@CanvasItemEditor@9272/@VSplitContainer@9094/@HSplitContainer@9096/@HSplitContainer@9098/@Control@9099/@SubViewportContainer@9100/@SubViewport@9101/Node2D/Sprite2D")])
selected_nodes=Array[NodePath]([NodePath("/root/@EditorNode@16886/@Panel@13/@VBoxContainer@14/DockHSplitLeftL/DockHSplitLeftR/DockHSplitMain/@VBoxContainer@25/DockVSplitCenter/@VSplitContainer@52/@VBoxContainer@53/@PanelContainer@98/MainScreen/@CanvasItemEditor@9272/@VSplitContainer@9094/@HSplitContainer@9096/@HSplitContainer@9098/@Control@9099/@SubViewportContainer@9100/@SubViewport@9101/Node2D")])
......@@ -13,8 +13,8 @@ last_selected_language="GDScript"
[recent_files]
scripts=["res://scripts/Bishop.gd", "res://scripts/Rook.gd", "res://scripts/Pawn.gd", "res://scripts/Piece.gd", "@GDScript", "int", "res://background.gd", "res://chess_board.tscn::GDScript_fv0wj", "res://tile_container.gd", "res://chess_board.gd"]
scenes=["res://scenes/Bishop.tscn", "res://scenes/Rook.tscn", "res://scenes/Pawn.tscn", "res://scenes/explosion.tscn", "res://scenes/game.tscn", "res://scenes/chess_board.tscn", "res://chess_board.tscn"]
scripts=["res://scripts/Knight.gd", "res://scripts/King.gd", "res://scripts/Queen.gd", "res://scripts/Bishop.gd", "res://scripts/Rook.gd", "res://scripts/Pawn.gd", "res://scripts/Piece.gd", "@GDScript", "int", "res://background.gd"]
scenes=["res://scenes/Knight.tscn", "res://scenes/King.tscn", "res://scenes/Queen.tscn", "res://scenes/Bishop.tscn", "res://scenes/Rook.tscn", "res://scenes/Pawn.tscn", "res://scenes/explosion.tscn", "res://scenes/game.tscn", "res://scenes/chess_board.tscn", "res://chess_board.tscn"]
[color_picker]
......
......@@ -3,11 +3,11 @@
state={
"bookmarks": PackedInt32Array(),
"breakpoints": PackedInt32Array(),
"column": 31,
"column": 20,
"folded_lines": Array[int]([]),
"h_scroll_position": 0,
"row": 122,
"scroll_position": 95.0,
"row": 121,
"scroll_position": 111.0,
"selection": false,
"syntax_highlighter": "GDScript"
}
......@@ -59,7 +59,7 @@ state={
state={
"bookmarks": PackedInt32Array(),
"breakpoints": PackedInt32Array(),
"column": 19,
"column": 3,
"folded_lines": Array[int]([]),
"h_scroll_position": 0,
"row": 80,
......@@ -67,3 +67,45 @@ state={
"selection": false,
"syntax_highlighter": "GDScript"
}
[res://scripts/Queen.gd]
state={
"bookmarks": PackedInt32Array(),
"breakpoints": PackedInt32Array(),
"column": 0,
"folded_lines": Array[int]([]),
"h_scroll_position": 0,
"row": 0,
"scroll_position": 0.0,
"selection": false,
"syntax_highlighter": "GDScript"
}
[res://scripts/King.gd]
state={
"bookmarks": PackedInt32Array(),
"breakpoints": PackedInt32Array(),
"column": 0,
"folded_lines": Array[int]([]),
"h_scroll_position": 0,
"row": 0,
"scroll_position": 0.0,
"selection": false,
"syntax_highlighter": "GDScript"
}
[res://scripts/Knight.gd]
state={
"bookmarks": PackedInt32Array(),
"breakpoints": PackedInt32Array(),
"column": 40,
"folded_lines": Array[int]([]),
"h_scroll_position": 0,
"row": 11,
"scroll_position": 3.0,
"selection": false,
"syntax_highlighter": "GDScript"
}
......@@ -12,6 +12,18 @@ list=Array[Dictionary]([{
"path": "res://scripts/board.gd"
}, {
"base": &"Piece",
"class": &"King",
"icon": "",
"language": &"GDScript",
"path": "res://scripts/King.gd"
}, {
"base": &"Piece",
"class": &"Knight",
"icon": "",
"language": &"GDScript",
"path": "res://scripts/Knight.gd"
}, {
"base": &"Piece",
"class": &"Pawn",
"icon": "",
"language": &"GDScript",
......@@ -24,6 +36,12 @@ list=Array[Dictionary]([{
"path": "res://scripts/Piece.gd"
}, {
"base": &"Piece",
"class": &"Queen",
"icon": "",
"language": &"GDScript",
"path": "res://scripts/Queen.gd"
}, {
"base": &"Piece",
"class": &"Rook",
"icon": "",
"language": &"GDScript",
......
No preview for this file type
[gd_scene load_steps=3 format=3 uid="uid://dp63wl0rqfl2e"]
[ext_resource type="Script" path="res://scripts/King.gd" id="1_ohp73"]
[ext_resource type="Texture2D" uid="uid://cdogixbq8ay3t" path="res://assets/Resized Chess Piece Assets/White_king.png" id="2_n8anr"]
[node name="Node2D" type="Node2D"]
script = ExtResource("1_ohp73")
[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("2_n8anr")
[gd_scene load_steps=3 format=3 uid="uid://chdgfilxbspil"]
[ext_resource type="Script" path="res://scripts/Knight.gd" id="1_1c76x"]
[ext_resource type="Texture2D" uid="uid://mf4a3jclbwih" path="res://assets/Resized Chess Piece Assets/White_knight.png" id="2_22smc"]
[node name="Node2D" type="Node2D"]
script = ExtResource("1_1c76x")
[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("2_22smc")
[gd_scene load_steps=3 format=3 uid="uid://de2b4xoiogohv"]
[ext_resource type="Script" path="res://scripts/Queen.gd" id="1_bpbtf"]
[ext_resource type="Texture2D" uid="uid://cb3vq168iyomb" path="res://assets/Resized Chess Piece Assets/White_Queen.png" id="2_h1wfq"]
[node name="Node2D" type="Node2D"]
script = ExtResource("1_bpbtf")
[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("2_h1wfq")
......@@ -78,5 +78,5 @@ func get_valid_moves(board_state: Array, pos: Vector2) -> Array[Vector2]:
valid_moves.append(Vector2(x, y - 1))
if y < 7 and (board_state[x][y + 1] == null or not board_state[x][y + 1].is_white):
valid_moves.append(Vector2(x, y + 1))
return valid_moves
extends Piece
class_name King
enum Upgrade {
# Add any specific upgrades for King here
}
func _init(is_white: bool, position: Vector2):
super._init(is_white, position)
func get_valid_moves(board_state: Array, pos: Vector2) -> Array[Vector2]:
var valid_moves: Array[Vector2] = []
var x = pos.x
var y = pos.y
# TODO: STILL NEEDS TO IMPLEMENT CASTLING
# TODO: STILL NEEDS TO IMPLEMENT CHECKING FOR CHECK
# Check moves in all directions (one square)
var directions = [
Vector2(1, 0), Vector2(-1, 0), Vector2(0, 1), Vector2(0, -1), # Cardinal directions
Vector2(1, 1), Vector2(-1, -1), Vector2(1, -1), Vector2(-1, 1) # Diagonal directions
]
for direction in directions:
var new_x = x + direction.x
var new_y = y + direction.y
if new_x >= 0 and new_x < 8 and new_y >= 0 and new_y < 8:
if board_state[new_x][new_y] == null or board_state[new_x][new_y].is_white != is_white:
valid_moves.append(Vector2(new_x, new_y))
return valid_moves
extends Piece
class_name Knight
enum Upgrade {
LONGER_L_MOVE,
}
func _init(is_white: bool, position: Vector2):
super._init(is_white, position)
# Testing upgrades
#upgrades.append(Upgrade.LONGER_L_MOVE)
func get_valid_moves(board_state: Array, pos: Vector2) -> Array[Vector2]:
var valid_moves: Array[Vector2] = []
var x = pos.x
var y = pos.y
# Define all possible moves for a knight
var moves = [
Vector2(x + 2, y + 1), Vector2(x + 2, y - 1),
Vector2(x - 2, y + 1), Vector2(x - 2, y - 1),
Vector2(x + 1, y + 2), Vector2(x + 1, y - 2),
Vector2(x - 1, y + 2), Vector2(x - 1, y - 2)
]
# Check for LONGER_L_MOVE upgrade
if Upgrade.LONGER_L_MOVE in upgrades:
moves += [
Vector2(x + 3, y + 1), Vector2(x + 3, y - 1),
Vector2(x - 3, y + 1), Vector2(x - 3, y - 1),
Vector2(x + 1, y + 3), Vector2(x + 1, y - 3),
Vector2(x - 1, y + 3), Vector2(x - 1, y - 3)
]
for move in moves:
if move.x >= 0 and move.x < 8 and move.y >= 0 and move.y < 8:
if board_state[move.x][move.y] == null or board_state[move.x][move.y].is_white != is_white:
valid_moves.append(move)
return valid_moves
extends Piece
class_name Queen
enum Upgrade {
# Add any specific upgrades for Queen here
}
func _init(is_white: bool, position: Vector2):
super._init(is_white, position)
func get_valid_moves(board_state: Array, pos: Vector2) -> Array[Vector2]:
var valid_moves: Array[Vector2] = []
var x = pos.x
var y = pos.y
# Check moves in the right direction
for i in range(x + 1, 8):
if board_state[i][y] == null:
valid_moves.append(Vector2(i, y))
elif not board_state[i][y].is_white:
valid_moves.append(Vector2(i, y))
break
else:
break
# Check moves in the left direction
for i in range(x - 1, -1, -1):
if board_state[i][y] == null:
valid_moves.append(Vector2(i, y))
elif not board_state[i][y].is_white:
valid_moves.append(Vector2(i, y))
break
else:
break
# Check moves upwards
for i in range(y - 1, -1, -1):
if board_state[x][i] == null:
valid_moves.append(Vector2(x, i))
elif not board_state[x][i].is_white:
valid_moves.append(Vector2(x, i))
break
else:
break
# Check moves downwards
for i in range(y + 1, 8):
if board_state[x][i] == null:
valid_moves.append(Vector2(x, i))
elif not board_state[x][i].is_white:
valid_moves.append(Vector2(x, i))
break
else:
break
# Check moves in the top-right diagonal
for i in range(1, 8):
if x + i < 8 and y - i >= 0:
if board_state[x + i][y - i] == null:
valid_moves.append(Vector2(x + i, y - i))
elif not board_state[x + i][y - i].is_white:
valid_moves.append(Vector2(x + i, y - i))
break
else:
break
else:
break
# Check moves in the top-left diagonal
for i in range(1, 8):
if x - i >= 0 and y - i >= 0:
if board_state[x - i][y - i] == null:
valid_moves.append(Vector2(x - i, y - i))
elif not board_state[x - i][y - i].is_white:
valid_moves.append(Vector2(x - i, y - i))
break
else:
break
else:
break
# Check moves in the bottom-right diagonal
for i in range(1, 8):
if x + i < 8 and y + i < 8:
if board_state[x + i][y + i] == null:
valid_moves.append(Vector2(x + i, y + i))
elif not board_state[x + i][y + i].is_white:
valid_moves.append(Vector2(x + i, y + i))
break
else:
break
else:
break
# Check moves in the bottom-left diagonal
for i in range(1, 8):
if x - i >= 0 and y + i < 8:
if board_state[x - i][y + i] == null:
valid_moves.append(Vector2(x - i, y + i))
elif not board_state[x - i][y + i].is_white:
valid_moves.append(Vector2(x - i, y + i))
break
else:
break
else:
break
return valid_moves
......@@ -45,12 +45,16 @@ func _ready() -> void:
#hard-coded setup of pieces
board[0][7] = Rook.new(true, Vector2(0, 7))
board[1][7] = Bishop.new(true, Vector2(1, 7))
board[3][7] = Pawn.new(true, Vector2(3, 7))
board[2][7] = King.new(true, Vector2(2, 7))
board[3][6] = Queen.new(true, Vector2(3, 6))
board[4][7] = Pawn.new(true, Vector2(4, 7))
board[5][7] = Knight.new(true, Vector2(5, 7))
board[6][7] = Pawn.new(true, Vector2(6, 7))
board[7][7] = Rook.new(true, Vector2(7, 7))
board[0][4] = Rook.new(false, Vector2(0, 4))
board[1][5] = Bishop.new(false, Vector2(1, 5))
board[3][2] = Queen.new(false, Vector2(3, 2))
board[3][5] = Pawn.new(false, Vector2(3, 5))
board[4][4] = Pawn.new(false, Vector2(4, 4))
board[3][4] = Pawn.new(false, Vector2(3, 4))
......@@ -115,6 +119,13 @@ func draw_pieces():
piece_scene = preload("res://scenes/Rook.tscn")
elif board[x][y] is Bishop:
piece_scene = preload("res://scenes/Bishop.tscn")
elif board[x][y] is Queen:
piece_scene = preload("res://scenes/Queen.tscn")
elif board[x][y] is King:
piece_scene = preload("res://scenes/King.tscn")
elif board[x][y] is Knight:
piece_scene = preload("res://scenes/Knight.tscn")
# Add other piece types here (e.g., Knight, Bishop, Queen, King)
if piece_scene:
......@@ -311,7 +322,6 @@ func unselect_piece():
func highlight_tiles():
# Clear the previously selected valid moves
remove_highlight()
print("selected_piece_position: ", selected_piece_position)
valid_moves = selected_piece_value.get_valid_moves(board, selected_piece_position)
for child in tile_container.get_children():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment