Fix(Tp5): Condition de victoire

pull/6/head
Louis DUFOUR 1 year ago
parent 524fb61e7c
commit 3b3bf57d03

@ -96,24 +96,25 @@ func olMain(){
} }
func main() { func main() {
/*guard let randomPlayer = RandomPlayer(withName: "Random Player 1", andId: .player1), guard let randomPlayer = RandomPlayer(withName: "Random Player 1", andId: .player1),
let humanPlayer = HumanPlayer(name: "Human Player 2", id: .player2, inputMethod: { validMoves in let humanPlayer = HumanPlayer(name: "Human Player 2", id: .player2, inputMethod: { validMoves in
return validMoves.first return validMoves.first
}) else { }) else {
fatalError("Failed to initialize players") fatalError("Failed to initialize players")
}*/ }
/*
guard let Humanplayer1 = HumanPlayer(name: "Player 1", id: .player1, inputMethod: { _ in nil }), guard let Humanplayer1 = HumanPlayer(name: "Player 1", id: .player1, inputMethod: { _ in nil }),
let Humanplayer2 = HumanPlayer(name: "Player 2", id: .player2, inputMethod: { _ in nil }) else { let Humanplayer2 = HumanPlayer(name: "Player 2", id: .player2, inputMethod: { _ in nil }) else {
fatalError("Failed to initialize players") fatalError("Failed to initialize players")
} } */
var game = Game( var game = Game(
rules: VerySimpleRules(), rules: VerySimpleRules(),
// player1: humanPlayer, player1: humanPlayer,
// player2: humanPlayer player2: randomPlayer
player1: Humanplayer1, // player1: Humanplayer1,
player2: Humanplayer2 // player2: Humanplayer2
) )
// Configuration des callbacks pour la gestion des événements de jeu // Configuration des callbacks pour la gestion des événements de jeu

@ -21,7 +21,7 @@ public struct VerySimpleRules: Rules {
let initialBoardConfiguration: [[Cell]] = [ let initialBoardConfiguration: [[Cell]] = [
// Ligne 1 - Joueur 1 // Ligne 1 - Joueur 1
[Cell(ofType: .jungle), Cell(ofType: .jungle, withPiece: Piece(withOwner: .player1, andAnimal: .lion)), [Cell(ofType: .jungle), Cell(ofType: .jungle, withPiece: Piece(withOwner: .player1, andAnimal: .lion)),
Cell(ofType: .den), Cell(ofType: .jungle, withPiece: Piece(withOwner: .player1, andAnimal: .tiger)), Cell(ofType: .den, ownedBy: Owner.player1), Cell(ofType: .jungle, withPiece: Piece(withOwner: .player1, andAnimal: .tiger)),
Cell(ofType: .jungle)], Cell(ofType: .jungle)],
// Ligne 2 - Joueur 1 // Ligne 2 - Joueur 1
@ -40,7 +40,7 @@ public struct VerySimpleRules: Rules {
// Ligne 5 - Joueur 2 // Ligne 5 - Joueur 2
[Cell(ofType: .jungle), Cell(ofType: .jungle, withPiece: Piece(withOwner: .player2, andAnimal: .tiger)), [Cell(ofType: .jungle), Cell(ofType: .jungle, withPiece: Piece(withOwner: .player2, andAnimal: .tiger)),
Cell(ofType: .den), Cell(ofType: .jungle, withPiece: Piece(withOwner: .player2, andAnimal: .lion)), Cell(ofType: .den, ownedBy: Owner.player2), Cell(ofType: .jungle, withPiece: Piece(withOwner: .player2, andAnimal: .lion)),
Cell(ofType: .jungle)] Cell(ofType: .jungle)]
] ]
if let board = Board(withGrid: initialBoardConfiguration) { if let board = Board(withGrid: initialBoardConfiguration) {
@ -134,8 +134,6 @@ public struct VerySimpleRules: Rules {
return false return false
} }
// Autres vérifications spécifiques à votre jeu...
return true return true
} }
@ -162,12 +160,12 @@ public struct VerySimpleRules: Rules {
guard let movingPiece = board.getCell(atRow: move.rowOrigin, atColumn: 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 // 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 let destinationPiece = board.getCell(atRow: move.rowDestination, atColumn: move.columnDestination)?.piece {
if destinationPiece.owner != movingPiece.owner { if destinationPiece.owner != movingPiece.owner {
// La pièce ennemie est capturée et doit être supprimée // La pièce ennemie est capturée et doit être supprimée
_ = board.removePiece(atRow: move.rowDestination, andColumn: move.columnDestination) _ = board.removePiece(atRow: move.rowDestination, andColumn: move.columnDestination)
} }
} }
@ -188,6 +186,7 @@ public struct VerySimpleRules: Rules {
historic.append(move) historic.append(move)
} }
public func hasPlayerNoPieces(board: Board, player: Owner) -> Bool { public func hasPlayerNoPieces(board: Board, player: Owner) -> Bool {
for row in board.grid { for row in board.grid {
for cell in row { for cell in row {
@ -202,23 +201,30 @@ public struct VerySimpleRules: Rules {
public func isGameOver(board: Board, lastMove: Move) -> (Bool, Result) { public func isGameOver(board: Board, lastMove: Move) -> (Bool, Result) {
let destinationCell = board.grid[lastMove.rowDestination][lastMove.columnDestination] 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
} }
// Vérifiez si la destination est la tanière de l'opposant // Vérifiez si la destination est la tanière de l'opposant
if destinationCell.cellType == .den && destinationCell.initialOwner == opponent { if destinationCell.cellType == .den && destinationCell.initialOwner == opponent {
return (true, .winner(lastMove.owner, .denReached)) return (true, .winner(lastMove.owner, .denReached))
} }
// Vérifie si la tanière de l'adversaire a été atteinte // Vérifie si la tanière de l'adversaire a été atteinte
if destinationCell.cellType == .den && destinationCell.initialOwner != lastMove.owner { if destinationCell.cellType == .den && destinationCell.initialOwner != lastMove.owner {
return (true, .winner(lastMove.owner, .denReached)) return (true, .winner(lastMove.owner, .denReached))
} }
// Vérifie si l'un des joueurs a atteint la tanière de l'adversaire
let player1ReachedOpponentDen = board.grid[0][4].piece?.owner == .player1
let player2ReachedOpponentDen = board.grid[4][0].piece?.owner == .player2
if player1ReachedOpponentDen || player2ReachedOpponentDen {
return (true, .winner(player1ReachedOpponentDen ? .player1 : .player2, .denReached))
}
// Vérifie si l'un des joueurs n'a plus de pièces // Vérifie si l'un des joueurs n'a plus de pièces
if hasPlayerNoPieces(board: board, player: opponent) { if hasPlayerNoPieces(board: board, player: opponent) {
return (true, .winner(lastMove.owner, .noMorePieces)) return (true, .winner(lastMove.owner, .noMorePieces))
@ -228,5 +234,7 @@ public struct VerySimpleRules: Rules {
return (false, .notFinished) return (false, .notFinished)
} }
} }

Loading…
Cancel
Save