Update(Tp3): Fix name method but still broken

pull/4/head
Louis DUFOUR 2 years ago
parent 979741d588
commit b78f8ef00b

@ -13,8 +13,8 @@ protocol Rules {
// pas forcément utile si j'utilise pas playedMove // pas forcément utile si j'utilise pas playedMove
var historic: [Move] { get set } var historic: [Move] { get set }
func static createBoard() -> Board static func createBoard() -> Board
func static checkBoard( b: Board) throws static func checkBoard( b: Board) throws
func getNextPlayer() -> Owner func getNextPlayer() -> Owner
// Donne tout les coups autoriser // Donne tout les coups autoriser
@ -28,5 +28,5 @@ protocol Rules {
func isGameOver( board: Board, lastMove: Move) -> (Bool, Result) func isGameOver( board: Board, lastMove: Move) -> (Bool, Result)
// permet de stocker le coût qui a été fait. (playedMove) // permet de stocker le coût qui a été fait. (playedMove)
func playedMove( move: Move, oldBoard: Board, newBoard: Board) func playedMove(move: Move, oldBoard: Board, newBoard: Board)
} }

@ -11,7 +11,7 @@ struct VerySimpleRules: Rules {
var occurences = [Board: Int]() var occurences = [Board: Int]()
var historic = [Move]() var historic = [Move]()
func createBoard() -> Board { static func createBoard() -> Board {
var cells = [[Cell]]() var cells = [[Cell]]()
// Configuration initiale du plateau // Configuration initiale du plateau
@ -28,7 +28,7 @@ struct VerySimpleRules: Rules {
if owner != .noOne && column < initialSetup.count { if owner != .noOne && column < initialSetup.count {
if let animal = initialSetup[column] { 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) 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 // Vérifier les dimensions du plateau
guard b.nbRows == 5, b.nbColumns == 5 else { guard b.nbRows == 5, b.nbColumns == 5 else {
throw InvalidBoardError.badDimensions(b.nbRows, b.nbColumns) throw InvalidBoardError.badDimensions(b.nbRows, b.nbColumns)
@ -70,18 +75,13 @@ struct VerySimpleRules: Rules {
} }
} }
func getNextPlayer() -> Owner { func getMoves(board: Board, owner: Owner) -> [Move] {
// Implémentation basée sur l'historique des coups
return historic.last?.owner == .player1 ? .player2 : .player1
}
func getMoves(_ board: Board, _ owner: Owner) -> [Move] {
var moves = [Move]() var moves = [Move]()
for row in 0..<board.nbRows { for row in 0..<board.nbRows {
for col in 0..<board.nbColumns { for col in 0..<board.nbColumns {
if let cell = board.grid[row][col], cell.piece?.owner == owner { 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))
} }
} }
} }
@ -89,8 +89,7 @@ struct VerySimpleRules: Rules {
return moves return moves
} }
func getMoves(board: Board, owner: Owner, row: Int, column: Int) -> [Move] {
func getMoves(_ board: Board, _ owner: Owner, _ row: Int, _ column: Int) -> [Move] {
var moves = [Move]() var moves = [Move]()
// Directions: haut, bas, gauche, droite // Directions: haut, bas, gauche, droite
@ -110,7 +109,7 @@ struct VerySimpleRules: Rules {
return moves return moves
} }
func isMoveValid(_ board: Board, _ move: Move) -> Bool { func isMoveValid(board: Board, move: Move) -> Bool {
// Vérifier si les coordonnées de destination sont valides // Vérifier si les coordonnées de destination sont valides
if move.rowDestination < 0 || move.rowDestination >= board.nbRows || if move.rowDestination < 0 || move.rowDestination >= board.nbRows ||
move.columnDestination < 0 || move.columnDestination >= board.nbColumns { move.columnDestination < 0 || move.columnDestination >= board.nbColumns {
@ -130,7 +129,11 @@ struct VerySimpleRules: Rules {
return true return true
} }
func isGameOver(_ board: Board, _ lastMove: Move) -> (Bool, Result) { 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 // Vérifier si la tannière de l'adversaire a été atteinte
let opponent = lastMove.owner == .player1 ? .player2 : .player1 let opponent = lastMove.owner == .player1 ? .player2 : .player1
if let destinationCell = board.grid[lastMove.rowDestination][lastMove.columnDestination], if let destinationCell = board.grid[lastMove.rowDestination][lastMove.columnDestination],
@ -148,7 +151,7 @@ struct VerySimpleRules: Rules {
} }
// Vérifier si l'adversaire ne peut plus bouger // Vérifier si l'adversaire ne peut plus bouger
let opponentMoves = getMoves(board, opponent) let opponentMoves = getMoves(board: board, owner: opponent)
if opponentMoves.isEmpty { if opponentMoves.isEmpty {
return (true, .winner(lastMove.owner, .noMovesLeft)) return (true, .winner(lastMove.owner, .noMovesLeft))
} }
@ -157,4 +160,8 @@ struct VerySimpleRules: Rules {
return (false, .notFinished) return (false, .notFinished)
} }
func playedMove(move: Move, oldBoard: Board, newBoard: Board) {
<#code#>
} }
}

Loading…
Cancel
Save