|
|
@ -88,72 +88,60 @@ public struct VerySimpleRules: Rules {
|
|
|
|
|
|
|
|
|
|
|
|
public func getMoves(board: Board, owner: Owner) -> [Move] {
|
|
|
|
public func getMoves(board: Board, owner: Owner) -> [Move] {
|
|
|
|
var moves = [Move]()
|
|
|
|
var moves = [Move]()
|
|
|
|
|
|
|
|
for row in 0..<board.grid.count {
|
|
|
|
for row in 0..<board.nbRows {
|
|
|
|
for column in 0..<board.grid[row].count {
|
|
|
|
for col in 0..<board.nbColumns {
|
|
|
|
if let piece = board.grid[row][column].piece, piece.owner == owner {
|
|
|
|
let cell = board.grid[row][col]
|
|
|
|
moves.append(contentsOf: getMoves(board: board, owner: owner, row: row, column: column))
|
|
|
|
if cell.piece?.owner == owner {
|
|
|
|
|
|
|
|
moves.append(contentsOf: getMoves(board: board, owner: owner, row: row, column: col))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return moves
|
|
|
|
return moves
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public func getMoves(board: Board, owner: Owner, row: Int, column: Int) -> [Move] {
|
|
|
|
public func getMoves(board: Board, owner: Owner, row: Int, column: Int) -> [Move] {
|
|
|
|
var moves = [Move]()
|
|
|
|
var moves = [Move]()
|
|
|
|
|
|
|
|
let directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] // Haut, Bas, Gauche, Droite
|
|
|
|
// Directions: haut, bas, gauche, droite
|
|
|
|
|
|
|
|
let directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (dRow, dCol) in directions {
|
|
|
|
for (dRow, dCol) in directions {
|
|
|
|
let newRow = row + dRow
|
|
|
|
let newRow = row + dRow
|
|
|
|
let newCol = column + dCol
|
|
|
|
let newCol = column + dCol
|
|
|
|
|
|
|
|
if newRow >= 0, newRow < board.grid.count, newCol >= 0, newCol < board.grid[0].count {
|
|
|
|
if newRow >= 0, newRow < board.nbRows, newCol >= 0, newCol < board.nbColumns {
|
|
|
|
|
|
|
|
let destinationCell = board.grid[newRow][newCol]
|
|
|
|
let destinationCell = board.grid[newRow][newCol]
|
|
|
|
if destinationCell.piece?.owner != owner {
|
|
|
|
// Autoriser le mouvement si la case de destination est vide ou contient une pièce ennemie
|
|
|
|
|
|
|
|
if destinationCell.piece == nil || destinationCell.piece?.owner != owner {
|
|
|
|
moves.append(Move(owner: owner, rowOrigin: row, columnOrigin: column, rowDestination: newRow, columnDestination: newCol))
|
|
|
|
moves.append(Move(owner: owner, rowOrigin: row, columnOrigin: column, rowDestination: newRow, columnDestination: newCol))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return moves
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return moves
|
|
|
|
return moves
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public func isMoveValid(board: Board, move: Move) -> Bool {
|
|
|
|
public func isMoveValid(board: Board, move: Move) -> Bool {
|
|
|
|
// Vérifier si les coordonnées de destination sont valides
|
|
|
|
// Assurez-vous que le mouvement est d'une case dans toutes les directions
|
|
|
|
if move.rowDestination < 0 || move.rowDestination >= board.nbRows ||
|
|
|
|
let deltaRow = abs(move.rowDestination - move.rowOrigin)
|
|
|
|
move.columnDestination < 0 || move.columnDestination >= board.nbColumns {
|
|
|
|
let deltaColumn = abs(move.columnDestination - move.columnOrigin)
|
|
|
|
|
|
|
|
if deltaRow > 1 || deltaColumn > 1 || (deltaRow == 0 && deltaColumn == 0) {
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Obtenir la cellule de destination
|
|
|
|
// Obtenez la cellule de destination
|
|
|
|
let destinationCell = board.grid[move.rowDestination][move.columnDestination]
|
|
|
|
let destinationCell = board.grid[move.rowDestination][move.columnDestination]
|
|
|
|
|
|
|
|
|
|
|
|
// Si la case de destination est une case jungle, vérifier les conditions spécifiques
|
|
|
|
// Vérifiez si la case de destination est occupée par une pièce alliée
|
|
|
|
if destinationCell.cellType == .jungle {
|
|
|
|
|
|
|
|
// Vérifier si la case de destination est occupée par une pièce du même joueur
|
|
|
|
|
|
|
|
if let piece = destinationCell.piece, piece.owner == move.owner {
|
|
|
|
if let piece = destinationCell.piece, piece.owner == move.owner {
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Pour les autres types de cases, vérifier si la case de destination est occupée par une pièce du même joueur
|
|
|
|
|
|
|
|
if let piece = destinationCell.piece, piece.owner == move.owner {
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Autres vérifications spécifiques à votre jeu (par exemple, les règles de déplacement basées sur le type de pièce)
|
|
|
|
// Autres vérifications spécifiques à votre jeu...
|
|
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public func isMoveValid(board: Board, row: Int, column: Int, rowArrived: Int, columnArrived: Int) -> Bool {
|
|
|
|
public func isMoveValid(board: Board, row: Int, column: Int, rowArrived: Int, columnArrived: Int) -> Bool {
|
|
|
|
// Création d'un objet Move à partir des coordonnées fournies
|
|
|
|
// Création d'un objet Move à partir des coordonnées fournies
|
|
|
|
let move = Move(owner: board.getCell(atRow: row, column: column)?.piece?.owner ?? .noOne,
|
|
|
|
let move = Move(owner: board.getCell(atRow: row, atColumn: column)?.piece?.owner ?? .noOne,
|
|
|
|
rowOrigin: row,
|
|
|
|
rowOrigin: row,
|
|
|
|
columnOrigin: column,
|
|
|
|
columnOrigin: column,
|
|
|
|
rowDestination: rowArrived,
|
|
|
|
rowDestination: rowArrived,
|
|
|
@ -171,21 +159,28 @@ public struct VerySimpleRules: Rules {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Obtenez la pièce à déplacer
|
|
|
|
// Obtenez la pièce à déplacer
|
|
|
|
guard let piece = board.getCell(atRow: move.rowOrigin, column: move.columnOrigin)?.piece else {
|
|
|
|
guard let movingPiece = board.getCell(atRow: move.rowOrigin, atColumn: move.columnOrigin)?.piece else {
|
|
|
|
throw GameError.invalidMove // Ou une erreur plus spécifique
|
|
|
|
throw GameError.invalidMove // Ou une erreur plus spécifique
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Vérifiez s'il y a une pièce ennemie à la position de destination et la supprimez si c'est le cas
|
|
|
|
|
|
|
|
if let destinationCell = board.getCell(atRow: move.rowDestination, atColumn: move.columnDestination), let destinationPiece = destinationCell.piece {
|
|
|
|
|
|
|
|
if destinationPiece.owner != movingPiece.owner {
|
|
|
|
|
|
|
|
// La pièce ennemie est capturée et doit être supprimée
|
|
|
|
|
|
|
|
_ = board.removePiece(atRow: move.rowDestination, andColumn: move.columnDestination)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Supprimez la pièce de sa position d'origine
|
|
|
|
// Supprimez la pièce de sa position d'origine
|
|
|
|
_ = board.removePiece(atRow: move.rowOrigin, andColumn: move.columnOrigin)
|
|
|
|
_ = board.removePiece(atRow: move.rowOrigin, andColumn: move.columnOrigin)
|
|
|
|
|
|
|
|
|
|
|
|
// Placez la pièce à sa nouvelle position
|
|
|
|
// Placez la pièce à sa nouvelle position
|
|
|
|
let insertionResult = board.insert(piece: piece, atRow: move.rowDestination, andColumn: move.columnDestination)
|
|
|
|
let insertionResult = board.insert(piece: movingPiece, atRow: move.rowDestination, andColumn: move.columnDestination)
|
|
|
|
switch insertionResult {
|
|
|
|
switch insertionResult {
|
|
|
|
case .ok:
|
|
|
|
case .ok:
|
|
|
|
print("Mouvement appliqué avec succès.")
|
|
|
|
print("Mouvement appliqué avec succès.")
|
|
|
|
case .failed(let reason):
|
|
|
|
case .failed(let reason):
|
|
|
|
print("Échec de l'application du mouvement : \(reason).")
|
|
|
|
print("Échec de l'application du mouvement : \(reason).")
|
|
|
|
// Vous pouvez convertir ceci en une erreur spécifique si nécessaire
|
|
|
|
|
|
|
|
throw GameError.invalidMove
|
|
|
|
throw GameError.invalidMove
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -193,36 +188,44 @@ public struct VerySimpleRules: Rules {
|
|
|
|
historic.append(move)
|
|
|
|
historic.append(move)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public func hasPlayerNoPieces(board: Board, player: Owner) -> Bool {
|
|
|
|
|
|
|
|
for row in board.grid {
|
|
|
|
|
|
|
|
for cell in row {
|
|
|
|
|
|
|
|
if let piece = cell.piece, piece.owner == player {
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public func isGameOver(board: Board, lastMove: Move) -> (Bool, Result) {
|
|
|
|
public func isGameOver(board: Board, lastMove: Move) -> (Bool, Result) {
|
|
|
|
// Vérifier si la tannière de l'adversaire a été atteinte
|
|
|
|
let destinationCell = board.grid[lastMove.rowDestination][lastMove.columnDestination]
|
|
|
|
var opponent: Owner
|
|
|
|
var opponent:Owner
|
|
|
|
if lastMove.owner == .player1 {
|
|
|
|
if lastMove.owner == .player1 {
|
|
|
|
opponent = .player2
|
|
|
|
opponent = .player2
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
opponent = .player1
|
|
|
|
opponent = .player1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let destinationCell = board.grid[lastMove.rowDestination][lastMove.columnDestination]
|
|
|
|
|
|
|
|
if destinationCell.cellType == .den, destinationCell.initialOwner == opponent {
|
|
|
|
// Vérifiez si la destination est la tanière de l'opposant
|
|
|
|
|
|
|
|
if destinationCell.cellType == .den && destinationCell.initialOwner == opponent {
|
|
|
|
return (true, .winner(lastMove.owner, .denReached))
|
|
|
|
return (true, .winner(lastMove.owner, .denReached))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Vérifier si l'adversaire n'a plus de pièces
|
|
|
|
// Vérifie si la tanière de l'adversaire a été atteinte
|
|
|
|
let hasOpponentPieces = board.grid.flatMap { $0 }.contains { cell in
|
|
|
|
if destinationCell.cellType == .den && destinationCell.initialOwner != lastMove.owner {
|
|
|
|
guard let piece = cell.piece else { return false }
|
|
|
|
return (true, .winner(lastMove.owner, .denReached))
|
|
|
|
return piece.owner == opponent
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasOpponentPieces {
|
|
|
|
|
|
|
|
return (true, .winner(lastMove.owner, .noMorePieces))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Vérifier si l'adversaire ne peut plus bouger
|
|
|
|
// Vérifie si l'un des joueurs n'a plus de pièces
|
|
|
|
let opponentMoves = getMoves(board: board, owner: opponent)
|
|
|
|
if hasPlayerNoPieces(board: board, player: opponent) {
|
|
|
|
if opponentMoves.isEmpty {
|
|
|
|
return (true, .winner(lastMove.owner, .noMorePieces))
|
|
|
|
return (true, .winner(lastMove.owner, .noMovesLeft))
|
|
|
|
} else if hasPlayerNoPieces(board: board, player: lastMove.owner) {
|
|
|
|
|
|
|
|
return (true, .winner(opponent, .noMorePieces))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Si aucune condition de victoire n'est remplie, la partie n'est pas encore terminée
|
|
|
|
|
|
|
|
return (false, .notFinished)
|
|
|
|
return (false, .notFinished)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|