added FEN string support
This commit is contained in:
parent
8c9b1093e7
commit
2a48655766
3 changed files with 111 additions and 11 deletions
|
|
@ -21,6 +21,7 @@ var currentlyMovingPiece = null
|
|||
var p1Points: int = 0
|
||||
var p2Points: int = 0
|
||||
var Turn: int = 0
|
||||
|
||||
@onready var turnIndicator: ColorRect = $TurnIndicator
|
||||
@onready var p1String: RichTextLabel = $Player1Points
|
||||
@onready var p2String: RichTextLabel = $Player2Points
|
||||
|
|
@ -35,6 +36,7 @@ var Turn: int = 0
|
|||
@export var tileYSize: int = 50
|
||||
@export var windowXSize: int = 1280
|
||||
@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 stateMachine: StateMachine = $StateMachine
|
||||
|
|
@ -79,21 +81,53 @@ func initializeCardPreview() -> void:
|
|||
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:
|
||||
# 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 = []
|
||||
for i in range(boardXSize):
|
||||
for i in range(boardYSize):
|
||||
var row = []
|
||||
for j in range(boardYSize):
|
||||
for j in range(boardXSize):
|
||||
row.append(null)
|
||||
board.append(row)
|
||||
|
||||
createBoard()
|
||||
setupPieces()
|
||||
setupPiecesFromFEN()
|
||||
|
||||
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 setupUI() -> void:
|
||||
p1String.text = "0"
|
||||
p2String.text = "0"
|
||||
|
|
@ -229,10 +263,49 @@ func setupPieces() -> void:
|
|||
for x in range(boardXSize):
|
||||
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:
|
||||
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)
|
||||
|
|
@ -241,6 +314,18 @@ func placePiece(position: String, pieceName: String, color: int) -> void:
|
|||
var coords = position.split("-")
|
||||
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:
|
||||
var piece
|
||||
match pieceName:
|
||||
|
|
@ -309,7 +394,7 @@ func isValidMove(location: String) -> bool:
|
|||
func resetBoard() -> void:
|
||||
clearSelection()
|
||||
clearBoard()
|
||||
setupPieces()
|
||||
setupPiecesFromFEN()
|
||||
Turn = 0
|
||||
currentPlayer = WHITE
|
||||
p1Points = 0
|
||||
|
|
|
|||
|
|
@ -81,11 +81,19 @@ func place_random_game_tiles(num_tiles: int = 8) -> void:
|
|||
push_error("TileManager not initialized with board_flow")
|
||||
return
|
||||
|
||||
var fen_parts = game.FEN.split(" ")
|
||||
var rows = fen_parts[0].split("/")
|
||||
var available_positions = []
|
||||
for x in range(game.boardXSize):
|
||||
for y in range(game.boardYSize):
|
||||
if y > 1 && y < 6: # Don't place on starting rows
|
||||
available_positions.append(str(x) + "-" + str(y))
|
||||
|
||||
# Find empty rows from FEN
|
||||
for y in range(rows.size()):
|
||||
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()
|
||||
|
||||
|
|
|
|||
|
|
@ -27,6 +27,13 @@ offset_bottom = -5.0
|
|||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_fkb2r")
|
||||
boardXSize = null
|
||||
boardYSize = null
|
||||
tileXSize = null
|
||||
tileYSize = null
|
||||
windowXSize = null
|
||||
windowYSize = null
|
||||
FEN = null
|
||||
|
||||
[node name="Flow" type="FlowContainer" parent="."]
|
||||
layout_mode = 1
|
||||
|
|
|
|||
Loading…
Reference in a new issue