riscrivi la funzione adattandoti alla struct Piece. ‘’‘struct Piece { let type: String…
- User
riscrivi la funzione adattandoti alla struct Piece. ‘’‘struct Piece {
let type: String let color: String var position: (Int, Int)’‘’ inoltre board è di questo tipo [[Piece?]]
''' func possibleMoves(piece: Piece) -> [(Int, Int)] { var moves: [(Int, Int)] = [] switch piece.type { case "King": for i in -1...1 { for j in -1...1 { if !(i == 0 && j == 0) { let row = piece.position.0 + i let col = piece.position.1 + j if row >= 0 && row <= 7 && col >= 0 && col <= 7 { moves.append((row, col)) } } } } case "Queen": // mosse orizzontali e verticali for i in -7...7 where i != 0 { let row = piece.position.0 + i let col = piece.position.1 if row >= 0 && row <= 7 { moves.append((row, col)) } } for i in -7...7 where i != 0 { let row = piece.position.0 let col = piece.position.1 + i if col >= 0 && col <= 7 { moves.append((row, col)) } } // mosse diagonali for i in -7...7 where i != 0 { let row1 = piece.position.0 + i let col1 = piece.position.1 + i let row2 = piece.position.0 + i let col2 = piece.position.1 - i if row1 >= 0 && row1 <= 7 && col1 >= 0 && col1 <= 7 { moves.append((row1, col1)) } if row2 >= 0 && row2 <= 7 && col2 >= 0 && col2 <= 7 { moves.append((row2, col2)) } } case "Rook": for i in -7...7 where i != 0 { let row = piece.position.0 + i let col = piece.position.1 if row >= 0 && row <= 7 { moves.append((row, col)) } } for i in -7...7 where i != 0 { let row = piece.position.0 let col = piece.position.1 + i if col >= 0 && col <= 7 { moves.append((row, col)) } } case "Bishop": for i in -7...7 where i != 0 { let row1 = piece.position.0 + i let col1 = piece.position.1 + i let row2 = piece.position.0 + i let col2 = piece.position.1 - i if row1 >= 0 && row1 <= 7 && col1 >= 0 && col1 <= 7 { moves.append((row1, col1)) } if row2 >= 0 && row2 <= 7 && col2 >= 0 && col2 <= 7 { moves.append((row2, col2)) } } case "Knight": let offsets = [(-2, -1), (-2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2), (2, -1), (2, 1)] for offset in offsets { let row = piece.position.0 + offset.0 let col = piece.position.1 + offset.1 if row >= 0 && row <= 7 && col >= 0 && col <= 7 { moves.append((row, col)) } } case "Pawn": let direction = (piece.color == "White" ? 1 : -1) let row1 = piece.position.0 let col1 = piece.position.1 + direction let row2 = piece.position.0 let col2 = piece.position.1 + 2*direction if col1 >= 0 && col1 <= 7 && board[row1][col1] == nil { moves.append((row1, col1)) } if col2 >= 0 && col2 <= 7 && piece.moves == 0 && board[row1][col1] == nil && board[row1][col2] == nil { moves.append((row1, col2)) } let left = piece.position.0 - 1 let right = piece.position.0 + 1 if left >= 0 && col1 >= 0 && board[left][col1]?.color != piece.color { moves.append((left, col1)) } if right <= 7 && col1 >= 0 && board[right][col1]?.color != piece.color { moves.append((right, col1)) } default: break } return moves } '''
- Assistant
func possibleMoves(piece: Piece, board: [[Piece?]]) -> [(Int, Int)] { var moves: [(Int, Int)] = [] switch piece.type { case "King": for i in -1...1 { for j in -1...1 { if !(i == 0 && j == 0) { let row = piece.position.0 + i let col = piece.position.1 + j if row >= 0 && row <= 7 && col >= 0 && col <= 7 { moves.append((row, col)) } } } } case "Queen": // mosse orizzontali e verticali for i in -7...7 where i != 0 { let row = piece.position.0 + i let col = piece.position.1 if row >= 0 && row <= 7 { moves.append((row, col)) } } for i in -7...7 where i != 0 { let row = piece.position.0 let col = piece.position.1 + i if col >= 0 && col <= 7 { moves.append((row, col)) } } // mosse diagonali for i in -7...7 where i != 0 { let row1 = piece.position.0 + i let col1 = piece.position.1 + i let row2 = piece.position.0 + i let col2 = piece.position.1 - i if row1 >= 0 && row1 <= 7 && col1 >= 0 && col1 <= 7 { moves.append((row1, col1)) } if row2 >= 0 && row2 <= 7 && col2 >= 0 && col2 <= 7 { moves.append((row2, col2)) } } case "Rook": for i in -7...7 where i != 0 { let row = piece.position.0 + i let col = piece.position.1 if row >= 0 && row <= 7 { moves.append((row, col)) } } for i in -7...7 where i != 0 { let row = piece.position.0 let col = piece.position.1 + i if col >= 0 && col <= 7 { moves.append((row, col)) } } case "Bishop": for i in -7...7 where i != 0 { let row1 = piece.position.0 + i let col1 = piece.position.1 + i let row2 = piece.position.0 + i let col2 = piece.position.1 - i if row1 >= 0 && row1 <= 7 && col1 >= 0 && col1 <= 7 { moves.append((row1, col1)) } if row2 >= 0 && row2 <= 7 && col2 >= 0 && col2 <= 7 { moves.append((row2, col2)) } } case "Knight": let offsets = [(-2, -1), (-2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2), (2, -1), (2, 1)] for offset in offsets { let row = piece.position.0 + offset.0 let col = piece.position.1 + offset.1 if row >= 0 && row <= 7 && col >= 0 && col <= 7 { moves.append((row, col)) } } case "Pawn": let direction = (piece.color == "White" ? 1 : -1) let row1 = piece.position.0 let col1 = piece.position.1 + direction let row2 = piece.position.0 let col2 = piece.position.1 + 2*direction if col1 >= 0 && col1 <= 7 && board[row1][col1] == nil { moves.append((row1, col1)) } if col2 >= 0 && col2 <= 7 && piece.moves == 0 && board[row1][col1] == nil && board[row1][col2] == nil { moves.append((row1, col2)) } let left = piece.position.0 - 1 let right = piece.position.0 + 1 if left >= 0 && col1 >= 0 && board[left][col1]?.color != piece.color { moves.append((left, col1)) } if right <= 7 && col1 >= 0 && board[right][col1]?.color != piece.color { moves.append((right, col1)) } default: break } return moves }
- User
piece non ha alcun membro "moves"
- Assistant
Hai ragione, avevo inserito un membro non presente nella struct Piece. Quindi, ecco la funzione aggiornata senza tale membro:
func possibleMoves(piece: Piece, board: [[Piece?]]) -> [(Int, Int)] { var moves: [(Int, Int)] = [] switch piece.type { case "King": for i in -1...1 { for j in -1...1 { if !(i == 0 && j == 0) { let row = piece.position.0 + i let col = piece.position.1 + j if row >= 0 && row <= 7 && col >= 0 && col <= 7 { moves.append((row, col)) } } } } case "Queen": // mosse orizzontali e verticali for i in -7...7 where i != 0 { let row = piece.position.0 + i let col = piece.position.1 if row >= 0 && row <= 7 { moves.append((row, col)) } } for i in -7...7 where i != 0 { let row = piece.position.0 let col = piece.position.1 + i if col >= 0 && col <= 7 { moves.append((row, col)) } } // mosse diagonali for i in -7...7 where i != 0 { let row1 = piece.position.0 + i let col1 = piece.position.1 + i let row2 = piece.position.0 + i let col2 = piece.position.1 - i if row1 >= 0 && row1 <= 7 && col1 >= 0 && col1 <= 7 { moves.append((row1, col1)) } if row2 >= 0 && row2 <= 7 && col2 >= 0 && col2 <= 7 { moves.append((row2, col2)) } } case "Rook": for i in -7...7 where i != 0 { let row = piece.position.0 + i let col = piece.position.1 if row >= 0 && row <= 7 { moves.append((row, col)) } } for i in -7...7 where i != 0 { let row = piece.position.0 let col = piece.position.1 + i if col >= 0 && col <= 7 { moves.append((row, col)) } } case "Bishop": for i in -7...7 where i != 0 { let row1 = piece.position.0 + i let col1 = piece.position.1 + i let row2 = piece.position.0 + i let col2 = piece.position.1 - i if row1 >= 0 && row1 <= 7 && col1 >= 0 && col1 <= 7 { moves.append((row1, col1)) } if row2 >= 0 && row2 <= 7 && col2 >= 0 && col2 <= 7 { moves.append((row2, col2)) } } case "Knight": let offsets = [(-2, -1), (-2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2), (2, -1), (2, 1)] for offset in offsets { let row = piece.position.0 + offset.0 let col = piece.position.1 + offset.1 if row >= 0 && row <= 7 && col >= 0 && col <= 7 { moves.append((row, col)) } } case "Pawn": let direction = (piece.color == "White" ? 1 : -1) let row1 = piece.position.0 let col1 = piece.position.1 + direction let row2 = piece.position.0 let col2 = piece.position.1 + 2*direction if col1 >= 0 && col1 <= 7 && board[row1][col1] == nil { moves.append((row1, col1)) } if col2 >= 0 && col2 <= 7 && board[row1][col1] == nil && board[row1][col2] == nil { moves.append((row1, col2)) } let left = piece.position.0 - 1 let right = piece.position.0 + 1 if left >= 0 && col1 >= 0 && board[left][col1]?.color != piece.color { moves.append((left, col1)) } if right <= 7 && col1 >= 0 && board[right][col1]?.color != piece.color { moves.append((right, col1)) } default: break } return moves }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.