Update(Tp3): Accessibilty and Add class

pull/4/head
Louis DUFOUR 1 year ago
parent e4e06c168f
commit cdd4d02e18

@ -193,7 +193,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 13.1;
MACOSX_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
@ -247,7 +247,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 13.1;
MACOSX_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = macosx;

@ -7,7 +7,6 @@
import Foundation
public enum BoardResult : Equatable {
case ok
case failed(reason: BoardFailingReason)

@ -7,6 +7,6 @@
import Foundation
enum GameError: Error {
public enum GameError: Error {
case invalidMove
}

@ -7,7 +7,7 @@
import Foundation
enum InvalidBoardError: Error {
public enum InvalidBoardError: Error {
case badDimensions(Int, Int)
case badCellType(CellType, Int, Int)
case multipleOccurencesOfSamePiece(Piece)

@ -7,7 +7,7 @@
import Foundation
enum Result {
public enum Result {
case notFinished
case even
case winner(Owner, WinningReason)

@ -7,7 +7,7 @@
import Foundation
enum WinningReason {
public enum WinningReason {
case denReached
case noMorePieces
case noMovesLeft

@ -0,0 +1,21 @@
//
// File.swift
//
//
// Created by Louis Dufour on 29/01/2024.
//
import Foundation
public class HumanPlayer: Player {
private var inputMethod: ((Board, VerySimpleRules) -> Move?)
public init?(name: String, id: Owner, inputMethod: @escaping (Board, VerySimpleRules) -> Move?) {
self.inputMethod = inputMethod
super.init(name: name, id: id)
}
public override func chooseMove(board: Board, rules: VerySimpleRules) -> Move? {
return inputMethod(board, rules) // Utilise la méthode de saisie injectée pour obtenir le mouvement
}
}

@ -7,7 +7,7 @@
import Foundation
protocol Rules {
public protocol Rules {
var occurences: [Board: Int] { get set }
// pas forcément utile si j'utilise pas playedMove

@ -8,9 +8,9 @@
import Foundation
public struct Move {
let owner: Owner
let rowOrigin: Int
let columnOrigin: Int
let rowDestination: Int
let columnDestination: Int
public let owner: Owner
public let rowOrigin: Int
public let columnOrigin: Int
public let rowDestination: Int
public let columnDestination: Int
}

@ -0,0 +1,26 @@
//
// File.swift
//
//
// Created by Louis Dufour on 29/01/2024.
//
import Foundation
public class Player {
public let id: Owner
public let name: String
// Initialiseur de la classe Player.
public init?(name: String, id: Owner) {
self.name = name
self.id = id
}
// Méthode chooseMove qui doit être surchargée dans les classes dérivées.
public func chooseMove(board: Board, rules: VerySimpleRules) -> Move? {
// Dans la classe de base, cette méthode ne fait rien.
// Les classes dérivées implémenteront leur propre logique.
return nil
}
}

@ -0,0 +1,16 @@
//
// File.swift
//
//
// Created by Louis Dufour on 29/01/2024.
//
import Foundation
public class RandomPlayer: Player {
public override func chooseMove(board: Board, rules: VerySimpleRules) -> Move? {
let validMoves = rules.getMoves(board: board, owner: self.id)
return validMoves.randomElement() // Sélectionne un mouvement aléatoire parmi les mouvements valides
}
}

@ -7,12 +7,12 @@
import Foundation
struct VerySimpleRules: Rules {
public struct VerySimpleRules: Rules {
var occurences = [Board: Int]()
var historic = [Move]()
public var occurences = [Board: Int]()
public var historic = [Move]()
static func createBoard() -> Board {
public static func createBoard() -> Board {
var cells = [[Cell]]()
// Configuration initiale du plateau
@ -39,12 +39,12 @@ struct VerySimpleRules: Rules {
return Board(withGrid: cells)!
}
func getNextPlayer() -> Owner {
public 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 {
public 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)
@ -73,7 +73,7 @@ struct VerySimpleRules: Rules {
}
}
func getMoves(board: Board, owner: Owner) -> [Move] {
public func getMoves(board: Board, owner: Owner) -> [Move] {
var moves = [Move]()
for row in 0..<board.nbRows {
@ -88,7 +88,7 @@ struct VerySimpleRules: Rules {
return moves
}
func getMoves(board: Board, owner: Owner, row: Int, column: Int) -> [Move] {
public func getMoves(board: Board, owner: Owner, row: Int, column: Int) -> [Move] {
var moves = [Move]()
// Directions: haut, bas, gauche, droite
@ -110,7 +110,7 @@ struct VerySimpleRules: Rules {
return moves
}
func isMoveValid(board: Board, move: Move) -> Bool {
public 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 {
@ -129,7 +129,7 @@ struct VerySimpleRules: Rules {
return true
}
func isMoveValid(board: Board, row: Int, column: Int, rowArrived: Int, columnArrived: Int) -> Bool {
public func isMoveValid(board: Board, row: Int, column: Int, rowArrived: Int, columnArrived: Int) -> Bool {
let move = Move(owner: board.grid[row][column].piece?.owner ?? .noOne,
rowOrigin: row,
columnOrigin: column,
@ -139,7 +139,7 @@ struct VerySimpleRules: Rules {
return isMoveValid(board: board, move: move)
}
func isGameOver(board: Board, lastMove: Move) -> (Bool, Result) {
public func isGameOver(board: Board, lastMove: Move) -> (Bool, Result) {
// Vérifier si la tannière de l'adversaire a été atteinte
var opponent: Owner
if lastMove.owner == .player1 {
@ -171,15 +171,12 @@ struct VerySimpleRules: Rules {
return (false, .notFinished)
}
func playedMove(move: Move, oldBoard: Board, newBoard: Board) {
public func playedMove(move: Move, oldBoard: Board, newBoard: Board) {
// 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