Update(Tp3): VerySimpleRules compile

pull/4/head
Louis DUFOUR 2 years ago
parent b78f8ef00b
commit 1fb9d2da9b

@ -8,6 +8,7 @@
import Foundation import Foundation
struct VerySimpleRules: Rules { struct VerySimpleRules: Rules {
var occurences = [Board: Int]() var occurences = [Board: Int]()
var historic = [Move]() var historic = [Move]()
@ -31,13 +32,11 @@ struct VerySimpleRules: Rules {
piece = Piece(withOwner: owner, andAnimal: animal) piece = Piece(withOwner: owner, andAnimal: animal)
} }
} }
rowCells.append(Cell(ofType: cellType, ownedBy: owner, withPiece: piece))
rowCells.append(Cell(cellType: cellType, initialOwner: owner, piece: piece))
} }
cells.append(rowCells) cells.append(rowCells)
} }
return Board(withGrid: cells)!
return Board(withGrid: cells)
} }
func getNextPlayer() -> Owner { func getNextPlayer() -> Owner {
@ -69,7 +68,6 @@ struct VerySimpleRules: Rules {
if piece.owner != .player1 && piece.owner != .player2 { if piece.owner != .player1 && piece.owner != .player2 {
throw InvalidBoardError.pieceWithNoOwner(piece) 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 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 { let cell = board.grid[row][col]
if cell.piece?.owner == owner {
moves.append(contentsOf: getMoves(board: board, owner: owner, row: row, column: col)) moves.append(contentsOf: getMoves(board: board, owner: owner, row: row, column: col))
} }
} }
@ -99,15 +98,17 @@ struct VerySimpleRules: Rules {
let newRow = row + dRow let newRow = row + dRow
let newCol = column + dCol let newCol = column + dCol
if newRow >= 0, newRow < board.nbRows, newCol >= 0, newCol < board.nbColumns, if newRow >= 0, newRow < board.nbRows, newCol >= 0, newCol < board.nbColumns {
let destinationCell = board.grid[newRow][newCol], let destinationCell = board.grid[newRow][newCol]
destinationCell.piece?.owner != owner { if 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
}
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
@ -117,9 +118,8 @@ struct VerySimpleRules: Rules {
} }
// Vérifier si la case de destination est occupée par une pièce du même joueur // 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 destinationCell = board.grid[move.rowDestination][move.columnDestination]
let piece = destinationCell.piece, if let piece = destinationCell.piece, piece.owner == move.owner {
piece.owner == move.owner {
return false return false
} }
@ -130,14 +130,25 @@ struct VerySimpleRules: Rules {
} }
func isMoveValid(board: Board, row: Int, column: Int, rowArrived: Int, columnArrived: Int) -> Bool { 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) { 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 var opponent: Owner
if let destinationCell = board.grid[lastMove.rowDestination][lastMove.columnDestination], if lastMove.owner == .player1 {
destinationCell.cellType == .den, destinationCell.initialOwner == opponent { 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)) return (true, .winner(lastMove.owner, .denReached))
} }
@ -161,7 +172,14 @@ struct VerySimpleRules: Rules {
} }
func playedMove(move: Move, oldBoard: Board, newBoard: Board) { 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
} }
} }

Loading…
Cancel
Save