|
|
|
@ -10,8 +10,8 @@ import Foundation
|
|
|
|
|
struct VerySimpleRules: Rules {
|
|
|
|
|
var occurences = [Board: Int]()
|
|
|
|
|
var historic = [Move]()
|
|
|
|
|
|
|
|
|
|
func createBoard() -> Board {
|
|
|
|
|
|
|
|
|
|
static func createBoard() -> Board {
|
|
|
|
|
var cells = [[Cell]]()
|
|
|
|
|
|
|
|
|
|
// Configuration initiale du plateau
|
|
|
|
@ -28,7 +28,7 @@ struct VerySimpleRules: Rules {
|
|
|
|
|
|
|
|
|
|
if owner != .noOne && column < initialSetup.count {
|
|
|
|
|
if let animal = initialSetup[column] {
|
|
|
|
|
piece = Piece(owner: owner, animal: animal)
|
|
|
|
|
piece = Piece(withOwner: owner, andAnimal: animal)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -37,10 +37,15 @@ struct VerySimpleRules: Rules {
|
|
|
|
|
cells.append(rowCells)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Board(grid: cells)
|
|
|
|
|
return Board(withGrid: cells)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func checkBoard(_ b: Board) throws {
|
|
|
|
|
|
|
|
|
|
func getNextPlayer() -> Owner {
|
|
|
|
|
// Implémentation basée sur l'historique des coups
|
|
|
|
|
return historic.last?.owner == .player1 ? .player2 : .player1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func checkBoard(b: Board) throws {
|
|
|
|
|
// Vérifier les dimensions du plateau
|
|
|
|
|
guard b.nbRows == 5, b.nbColumns == 5 else {
|
|
|
|
|
throw InvalidBoardError.badDimensions(b.nbRows, b.nbColumns)
|
|
|
|
@ -69,92 +74,94 @@ struct VerySimpleRules: Rules {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getNextPlayer() -> Owner {
|
|
|
|
|
// Implémentation basée sur l'historique des coups
|
|
|
|
|
return historic.last?.owner == .player1 ? .player2 : .player1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getMoves(_ board: Board, _ owner: Owner) -> [Move] {
|
|
|
|
|
|
|
|
|
|
func getMoves(board: Board, owner: Owner) -> [Move] {
|
|
|
|
|
var moves = [Move]()
|
|
|
|
|
|
|
|
|
|
for row in 0..<board.nbRows {
|
|
|
|
|
for col in 0..<board.nbColumns {
|
|
|
|
|
if let cell = board.grid[row][col], cell.piece?.owner == owner {
|
|
|
|
|
moves.append(contentsOf: getMoves(board, owner, row, col))
|
|
|
|
|
moves.append(contentsOf: getMoves(board: board, owner: owner, row: row, column: col))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return moves
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getMoves(_ board: Board, _ owner: Owner, _ row: Int, _ column: Int) -> [Move] {
|
|
|
|
|
var moves = [Move]()
|
|
|
|
|
|
|
|
|
|
// Directions: haut, bas, gauche, droite
|
|
|
|
|
let directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
|
|
|
|
|
|
|
|
|
|
for (dRow, dCol) in directions {
|
|
|
|
|
let newRow = row + dRow
|
|
|
|
|
let newCol = column + dCol
|
|
|
|
|
|
|
|
|
|
if newRow >= 0, newRow < board.nbRows, newCol >= 0, newCol < board.nbColumns,
|
|
|
|
|
let destinationCell = board.grid[newRow][newCol],
|
|
|
|
|
destinationCell.piece?.owner != owner {
|
|
|
|
|
moves.append(Move(owner: owner, rowOrigin: row, columnOrigin: column, rowDestination: newRow, columnDestination: newCol))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getMoves(board: Board, owner: Owner, row: Int, column: Int) -> [Move] {
|
|
|
|
|
var moves = [Move]()
|
|
|
|
|
|
|
|
|
|
// Directions: haut, bas, gauche, droite
|
|
|
|
|
let directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
|
|
|
|
|
|
|
|
|
|
for (dRow, dCol) in directions {
|
|
|
|
|
let newRow = row + dRow
|
|
|
|
|
let newCol = column + dCol
|
|
|
|
|
|
|
|
|
|
if newRow >= 0, newRow < board.nbRows, newCol >= 0, newCol < board.nbColumns,
|
|
|
|
|
let destinationCell = board.grid[newRow][newCol],
|
|
|
|
|
destinationCell.piece?.owner != owner {
|
|
|
|
|
moves.append(Move(owner: owner, rowOrigin: row, columnOrigin: column, rowDestination: newRow, columnDestination: newCol))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return moves
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isMoveValid(_ board: Board, _ move: Move) -> Bool {
|
|
|
|
|
// Vérifier si les coordonnées de destination sont valides
|
|
|
|
|
if move.rowDestination < 0 || move.rowDestination >= board.nbRows ||
|
|
|
|
|
move.columnDestination < 0 || move.columnDestination >= board.nbColumns {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return moves
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isMoveValid(board: Board, move: Move) -> Bool {
|
|
|
|
|
// Vérifier si les coordonnées de destination sont valides
|
|
|
|
|
if move.rowDestination < 0 || move.rowDestination >= board.nbRows ||
|
|
|
|
|
move.columnDestination < 0 || move.columnDestination >= board.nbColumns {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Vérifier si la case de destination est occupée par une pièce du même joueur
|
|
|
|
|
if let destinationCell = board.grid[move.rowDestination][move.columnDestination],
|
|
|
|
|
let piece = destinationCell.piece,
|
|
|
|
|
piece.owner == move.owner {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
// Vérifier si la case de destination est occupée par une pièce du même joueur
|
|
|
|
|
if let destinationCell = board.grid[move.rowDestination][move.columnDestination],
|
|
|
|
|
let piece = destinationCell.piece,
|
|
|
|
|
piece.owner == move.owner {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Autres vérifications spécifiques au jeu peuvent être ajoutées ici
|
|
|
|
|
// ...
|
|
|
|
|
// Autres vérifications spécifiques au jeu peuvent être ajoutées ici
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isGameOver(_ board: Board, _ lastMove: Move) -> (Bool, Result) {
|
|
|
|
|
// Vérifier si la tannière de l'adversaire a été atteinte
|
|
|
|
|
let opponent = lastMove.owner == .player1 ? .player2 : .player1
|
|
|
|
|
if let destinationCell = board.grid[lastMove.rowDestination][lastMove.columnDestination],
|
|
|
|
|
destinationCell.cellType == .den, destinationCell.initialOwner == opponent {
|
|
|
|
|
return (true, .winner(lastMove.owner, .denReached))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Vérifier si l'adversaire n'a plus de pièces
|
|
|
|
|
let hasOpponentPieces = board.grid.flatMap { $0 }.contains { cell in
|
|
|
|
|
guard let piece = cell.piece else { return false }
|
|
|
|
|
return piece.owner == opponent
|
|
|
|
|
}
|
|
|
|
|
if !hasOpponentPieces {
|
|
|
|
|
return (true, .winner(lastMove.owner, .noMorePieces))
|
|
|
|
|
}
|
|
|
|
|
func isMoveValid(board: Board, row: Int, column: Int, rowArrived: Int, columnArrived: Int) -> Bool {
|
|
|
|
|
<#code#>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isGameOver(board: Board, lastMove: Move) -> (Bool, Result) {
|
|
|
|
|
// Vérifier si la tannière de l'adversaire a été atteinte
|
|
|
|
|
let opponent = lastMove.owner == .player1 ? .player2 : .player1
|
|
|
|
|
if let destinationCell = board.grid[lastMove.rowDestination][lastMove.columnDestination],
|
|
|
|
|
destinationCell.cellType == .den, destinationCell.initialOwner == opponent {
|
|
|
|
|
return (true, .winner(lastMove.owner, .denReached))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Vérifier si l'adversaire ne peut plus bouger
|
|
|
|
|
let opponentMoves = getMoves(board, opponent)
|
|
|
|
|
if opponentMoves.isEmpty {
|
|
|
|
|
return (true, .winner(lastMove.owner, .noMovesLeft))
|
|
|
|
|
}
|
|
|
|
|
// Vérifier si l'adversaire n'a plus de pièces
|
|
|
|
|
let hasOpponentPieces = board.grid.flatMap { $0 }.contains { cell in
|
|
|
|
|
guard let piece = cell.piece else { return false }
|
|
|
|
|
return piece.owner == opponent
|
|
|
|
|
}
|
|
|
|
|
if !hasOpponentPieces {
|
|
|
|
|
return (true, .winner(lastMove.owner, .noMorePieces))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Si aucune condition de victoire n'est remplie, la partie n'est pas encore terminée
|
|
|
|
|
return (false, .notFinished)
|
|
|
|
|
// Vérifier si l'adversaire ne peut plus bouger
|
|
|
|
|
let opponentMoves = getMoves(board: board, owner: opponent)
|
|
|
|
|
if opponentMoves.isEmpty {
|
|
|
|
|
return (true, .winner(lastMove.owner, .noMovesLeft))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Si aucune condition de victoire n'est remplie, la partie n'est pas encore terminée
|
|
|
|
|
return (false, .notFinished)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func playedMove(move: Move, oldBoard: Board, newBoard: Board) {
|
|
|
|
|
<#code#>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|