|
|
|
@ -8,6 +8,7 @@
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
struct VerySimpleRules: Rules {
|
|
|
|
|
|
|
|
|
|
var occurences = [Board: Int]()
|
|
|
|
|
var historic = [Move]()
|
|
|
|
|
|
|
|
|
@ -31,13 +32,11 @@ struct VerySimpleRules: Rules {
|
|
|
|
|
piece = Piece(withOwner: owner, andAnimal: animal)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rowCells.append(Cell(cellType: cellType, initialOwner: owner, piece: piece))
|
|
|
|
|
rowCells.append(Cell(ofType: cellType, ownedBy: owner, withPiece: piece))
|
|
|
|
|
}
|
|
|
|
|
cells.append(rowCells)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Board(withGrid: cells)
|
|
|
|
|
return Board(withGrid: cells)!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getNextPlayer() -> Owner {
|
|
|
|
@ -69,7 +68,6 @@ struct VerySimpleRules: Rules {
|
|
|
|
|
if piece.owner != .player1 && piece.owner != .player2 {
|
|
|
|
|
throw InvalidBoardError.pieceWithNoOwner(piece)
|
|
|
|
|
}
|
|
|
|
|
// Autres vérifications de pièces peuvent être ajoutées ici
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -80,7 +78,8 @@ struct VerySimpleRules: Rules {
|
|
|
|
|
|
|
|
|
|
for row in 0..<board.nbRows {
|
|
|
|
|
for col in 0..<board.nbColumns {
|
|
|
|
|
if let cell = board.grid[row][col], cell.piece?.owner == owner {
|
|
|
|
|
let cell = board.grid[row][col]
|
|
|
|
|
if cell.piece?.owner == owner {
|
|
|
|
|
moves.append(contentsOf: getMoves(board: board, owner: owner, row: row, column: col))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -99,27 +98,28 @@ struct VerySimpleRules: Rules {
|
|
|
|
|
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))
|
|
|
|
|
if newRow >= 0, newRow < board.nbRows, newCol >= 0, newCol < board.nbColumns {
|
|
|
|
|
let destinationCell = board.grid[newRow][newCol]
|
|
|
|
|
if destinationCell.piece?.owner != owner {
|
|
|
|
|
moves.append(Move(owner: owner, rowOrigin: row, columnOrigin: column, rowDestination: newRow, columnDestination: newCol))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return moves
|
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
|
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 {
|
|
|
|
|
let destinationCell = board.grid[move.rowDestination][move.columnDestination]
|
|
|
|
|
if let piece = destinationCell.piece, piece.owner == move.owner {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -130,14 +130,25 @@ struct VerySimpleRules: Rules {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isMoveValid(board: Board, row: Int, column: Int, rowArrived: Int, columnArrived: Int) -> Bool {
|
|
|
|
|
<#code#>
|
|
|
|
|
let move = Move(owner: board.grid[row][column].piece?.owner ?? .noOne,
|
|
|
|
|
rowOrigin: row,
|
|
|
|
|
columnOrigin: column,
|
|
|
|
|
rowDestination: rowArrived,
|
|
|
|
|
columnDestination: columnArrived)
|
|
|
|
|
|
|
|
|
|
return isMoveValid(board: board, move: move)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
var opponent: Owner
|
|
|
|
|
if lastMove.owner == .player1 {
|
|
|
|
|
opponent = .player2
|
|
|
|
|
} else {
|
|
|
|
|
opponent = .player1
|
|
|
|
|
}
|
|
|
|
|
let destinationCell = board.grid[lastMove.rowDestination][lastMove.columnDestination]
|
|
|
|
|
if destinationCell.cellType == .den, destinationCell.initialOwner == opponent {
|
|
|
|
|
return (true, .winner(lastMove.owner, .denReached))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -151,7 +162,7 @@ struct VerySimpleRules: Rules {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Vérifier si l'adversaire ne peut plus bouger
|
|
|
|
|
let opponentMoves = getMoves(board: board, owner: opponent)
|
|
|
|
|
let opponentMoves = getMoves(board: board, owner: opponent)
|
|
|
|
|
if opponentMoves.isEmpty {
|
|
|
|
|
return (true, .winner(lastMove.owner, .noMovesLeft))
|
|
|
|
|
}
|
|
|
|
@ -161,7 +172,14 @@ struct VerySimpleRules: Rules {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func playedMove(move: Move, oldBoard: Board, newBoard: Board) {
|
|
|
|
|
<#code#>
|
|
|
|
|
// ToDo
|
|
|
|
|
// Ajouter le mouvement à l'historique
|
|
|
|
|
// historic.append(move)
|
|
|
|
|
|
|
|
|
|
// Mise à jour du comptage des occurrences pour le nouveau plateau
|
|
|
|
|
// occurences[newBoard, default: 0] += 1
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|