added FEN string support

This commit is contained in:
2ManyProjects 2025-02-09 11:36:04 -06:00
parent 8c9b1093e7
commit 2a48655766
3 changed files with 111 additions and 11 deletions

View file

@ -21,6 +21,7 @@ var currentlyMovingPiece = null
var p1Points: int = 0 var p1Points: int = 0
var p2Points: int = 0 var p2Points: int = 0
var Turn: int = 0 var Turn: int = 0
@onready var turnIndicator: ColorRect = $TurnIndicator @onready var turnIndicator: ColorRect = $TurnIndicator
@onready var p1String: RichTextLabel = $Player1Points @onready var p1String: RichTextLabel = $Player1Points
@onready var p2String: RichTextLabel = $Player2Points @onready var p2String: RichTextLabel = $Player2Points
@ -35,6 +36,7 @@ var Turn: int = 0
@export var tileYSize: int = 50 @export var tileYSize: int = 50
@export var windowXSize: int = 1280 @export var windowXSize: int = 1280
@export var windowYSize: int = 720 @export var windowYSize: int = 720
@export var FEN: String = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
@onready var boardContainer: FlowContainer = $Flow @onready var boardContainer: FlowContainer = $Flow
@onready var stateMachine: StateMachine = $StateMachine @onready var stateMachine: StateMachine = $StateMachine
@ -79,21 +81,53 @@ func initializeCardPreview() -> void:
add_child(cardPreview) add_child(cardPreview)
# func initializeBoard() -> void:
# board = []
# for i in range(boardXSize):
# var row = []
# for j in range(boardYSize):
# row.append(null)
# board.append(row)
# createBoard()
# setupPieces()
# if !boardContainer.has_user_signal("tile_pressed"):
# boardContainer.add_user_signal("tile_pressed")
# if !boardContainer.has_user_signal("send_location"):
# boardContainer.add_user_signal("send_location")
func initializeBoard() -> void: func initializeBoard() -> void:
# Parse FEN to get board dimensions
# rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
var fen_parts = FEN.split(" ")
var rows = fen_parts[0].split("/")
boardYSize = rows.size()
boardXSize = 0
# Calculate width from first row by counting both pieces and numbers
for c in rows[0]:
if c.is_valid_int():
boardXSize += int(c) # Add the number of empty squares
else:
boardXSize += 1
# Calculate board width by counting pieces and empty squares in first row
# Initialize board array
board = [] board = []
for i in range(boardXSize): for i in range(boardYSize):
var row = [] var row = []
for j in range(boardYSize): for j in range(boardXSize):
row.append(null) row.append(null)
board.append(row) board.append(row)
createBoard() createBoard()
setupPieces() setupPiecesFromFEN()
if !boardContainer.has_user_signal("tile_pressed"): if !boardContainer.has_user_signal("tile_pressed"):
boardContainer.add_user_signal("tile_pressed") boardContainer.add_user_signal("tile_pressed")
if !boardContainer.has_user_signal("send_location"): if !boardContainer.has_user_signal("send_location"):
boardContainer.add_user_signal("send_location") boardContainer.add_user_signal("send_location")
func setupUI() -> void: func setupUI() -> void:
p1String.text = "0" p1String.text = "0"
p2String.text = "0" p2String.text = "0"
@ -229,10 +263,49 @@ func setupPieces() -> void:
for x in range(boardXSize): for x in range(boardXSize):
placePiece(str(x) + "-6", "Pawn", 0) placePiece(str(x) + "-6", "Pawn", 0)
func setupPiecesFromFEN() -> void:
var fen_parts = FEN.split(" ")
var rows = fen_parts[0].split("/")
# Iterate through rows in reverse to place black pieces at top
for y in range(rows.size()):
var x = 0
# Convert y coordinate to flip the board (7-y puts white at bottom)
var board_y = (boardYSize - 1) - y # For an 8x8 board
for c in rows[y]:
if c.is_valid_int():
# Skip empty squares
x += int(c)
else:
var piece_info = getFENPieceInfo(c)
placePiece(str(x) + "-" + str(board_y), piece_info.name, piece_info.color)
x += 1
func getFENPieceInfo(fen_char: String) -> Dictionary:
var piece_info = {
"name": "",
"color": 0 # Black
}
# If uppercase, it's white
if fen_char.to_upper() == fen_char:
piece_info.color = 1 # White
# Map FEN characters to piece names
match fen_char.to_upper():
"P": piece_info.name = "Pawn"
"R": piece_info.name = "Rook"
"N": piece_info.name = "Knight"
"B": piece_info.name = "Bishop"
"Q": piece_info.name = "Queen"
"K": piece_info.name = "King"
return piece_info
func placePiece(position: String, pieceName: String, color: int) -> void: func placePiece(position: String, pieceName: String, color: int) -> void:
var piece = summonPiece(pieceName, color) var piece = summonPiece(pieceName, color)
# boardContainer.get_node(position).add_child(piece)
var container = boardContainer.get_node(position) as PieceContainer var container = boardContainer.get_node(position) as PieceContainer
await container.set_piece(piece, false) await container.set_piece(piece, false)
container.remove_piece(true) container.remove_piece(true)
@ -241,6 +314,18 @@ func placePiece(position: String, pieceName: String, color: int) -> void:
var coords = position.split("-") var coords = position.split("-")
board[int(coords[1])][int(coords[0])] = piece board[int(coords[1])][int(coords[0])] = piece
# func placePiece(position: String, pieceName: String, color: int) -> void:
# var piece = summonPiece(pieceName, color)
# # boardContainer.get_node(position).add_child(piece)
# var container = boardContainer.get_node(position) as PieceContainer
# await container.set_piece(piece, false)
# container.remove_piece(true)
# container.set_piece(piece, false)
# var coords = position.split("-")
# board[int(coords[1])][int(coords[0])] = piece
func summonPiece(pieceName: String, color: int) -> Node: func summonPiece(pieceName: String, color: int) -> Node:
var piece var piece
match pieceName: match pieceName:
@ -309,7 +394,7 @@ func isValidMove(location: String) -> bool:
func resetBoard() -> void: func resetBoard() -> void:
clearSelection() clearSelection()
clearBoard() clearBoard()
setupPieces() setupPiecesFromFEN()
Turn = 0 Turn = 0
currentPlayer = WHITE currentPlayer = WHITE
p1Points = 0 p1Points = 0

View file

@ -81,11 +81,19 @@ func place_random_game_tiles(num_tiles: int = 8) -> void:
push_error("TileManager not initialized with board_flow") push_error("TileManager not initialized with board_flow")
return return
var fen_parts = game.FEN.split(" ")
var rows = fen_parts[0].split("/")
var available_positions = [] var available_positions = []
for x in range(game.boardXSize):
for y in range(game.boardYSize): # Find empty rows from FEN
if y > 1 && y < 6: # Don't place on starting rows for y in range(rows.size()):
available_positions.append(str(x) + "-" + str(y)) var row = rows[y]
# If row is all empty squares (like "8" in standard chess)
if row.is_valid_int() && int(row) == game.boardXSize:
var board_y = (game.boardYSize - 1) - y # Flip y coordinate to match board orientation
# Add all positions in this empty row
for x in range(game.boardXSize):
available_positions.append(str(x) + "-" + str(board_y))
available_positions.shuffle() available_positions.shuffle()

View file

@ -27,6 +27,13 @@ offset_bottom = -5.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
script = ExtResource("1_fkb2r") script = ExtResource("1_fkb2r")
boardXSize = null
boardYSize = null
tileXSize = null
tileYSize = null
windowXSize = null
windowYSize = null
FEN = null
[node name="Flow" type="FlowContainer" parent="."] [node name="Flow" type="FlowContainer" parent="."]
layout_mode = 1 layout_mode = 1