Redesign des colonnes

main
Mathis RIBEMONT 2 years ago
parent 8bd0b38862
commit 2c77207c43

@ -11,7 +11,7 @@ public struct Board: CustomStringConvertible {
var grid: [[Int?]]; var grid: [[Int?]];
let nbRows: Int; let nbRows: Int;
let nbColumns: Int; let nbColumns: Int;
private static let descriptionMapper: [Int?:String] = [nil:"-", 1:"O", 2:"X"]; private static let descriptionMapper: [Int?:String] = [nil:" ", 1:"O", 2:"X"];
public init?(nbR: Int = 6, nbC: Int = 7) { public init?(nbR: Int = 6, nbC: Int = 7) {
if nbR <= 0 || nbC <= 0 { if nbR <= 0 || nbC <= 0 {
@ -43,17 +43,30 @@ public struct Board: CustomStringConvertible {
var string = String() var string = String()
for row in grid { for row in grid {
string.append("| ")
for cell in row { for cell in row {
string.append("\(String(describing: Board.descriptionMapper[cell] ?? "-")) ") string.append("\(String(describing: Board.descriptionMapper[cell] ?? " "))")
string.append(" | ")
} }
string.append("\n") string.append("\n")
} }
string.append("|")
for i in 0...grid.count{
string.append("---|")
}
string.append("\n")
string.append("|")
for i in 0...grid.count{
string.append(" \(i) |")
}
return string return string
} }
private func isFull() -> Bool { private func isFull() -> Bool {
for column in 0..<nbColumns{ for column in 0...nbColumns{
if !isColumnFull(column: column) { if !isColumnFull(column: column) {
return false return false
} }

@ -12,6 +12,7 @@ public struct Game {
private var players: [Player] private var players: [Player]
private var numero: Int = 0 private var numero: Int = 0
private var afficheur: Afficheur private var afficheur: Afficheur
private var winner: Player?
public init?(withBoard board: Board, playedBy players: [Player], writeOn afficheur: Afficheur) { public init?(withBoard board: Board, playedBy players: [Player], writeOn afficheur: Afficheur) {
self.board = board self.board = board
@ -60,4 +61,8 @@ public struct Game {
public mutating func joueurSuivant() { public mutating func joueurSuivant() {
numero = (numero + 1) % players.count numero = (numero + 1) % players.count
} }
public func getWinner() -> Player? {
return winner;
}
} }

@ -12,6 +12,7 @@ public class Human : Player {
public var board: Board public var board: Board
private var lecteur: Lecteur private var lecteur: Lecteur
// au lieu du protocol Lecteur, prendre une fonction qui sera appelé à chaque fois si on ne veux qu'une seul méthode (c'est une closure, si on la stock, on doit mettre @escaping en paramètre). On peut stocker une méthode optionnel
public init(named name: String, playedOn board: Board, readOn lecteur: Lecteur){ public init(named name: String, playedOn board: Board, readOn lecteur: Lecteur){
self.name = name self.name = name
self.board = board self.board = board

@ -7,17 +7,17 @@
import Foundation import Foundation
class IA: Player { public class IA: Player {
var name: String public var name: String
var board: Board public var board: Board
init(named name: String, playedOn board: Board){ public init(named name: String, playedOn board: Board){
self.name = name self.name = name
self.board = board self.board = board
} }
func playInColumn() -> Int { public func playInColumn() -> Int {
return Int.random(in: 0..<(board.nbColumns)) return Int.random(in: 0..<(board.nbColumns))
} }

@ -7,6 +7,11 @@
import Foundation import Foundation
// en static, le nombre de ligne, de colonne, et de case à aligner
// méthode createBoard, ou alors permettre de vérifier si la board est valid
// isValid(column) , IsGameOver(column),
// Pour avoir une collection de règles, créer une règle qui étend ce protocol, et qui contiendra une liste de rules, qui elles vont faire la logique. Ca permettra de juger les priorités des règles.
public protocol Rule { public protocol Rule {
func executeRule() -> Bool func executeRule(onBoard board: Board, lastPieceX x: Int) -> Bool
} }

@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1410"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "EC62D5D02976881000CAB764"
BuildableName = "puissance4"
BlueprintName = "puissance4"
ReferencedContainer = "container:puissance4.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES"
viewDebuggingEnabled = "No">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "EC62D5D02976881000CAB764"
BuildableName = "puissance4"
BlueprintName = "puissance4"
ReferencedContainer = "container:puissance4.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "EC62D5D02976881000CAB764"
BuildableName = "puissance4"
BlueprintName = "puissance4"
ReferencedContainer = "container:puissance4.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

@ -10,5 +10,13 @@
<integer>1</integer> <integer>1</integer>
</dict> </dict>
</dict> </dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>EC62D5D02976881000CAB764</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict> </dict>
</plist> </plist>

@ -27,11 +27,15 @@ let afficheur = Afficheur()
var b = Board() var b = Board()
if let board = b { if let board = b {
let player1 = Human(named: "Bingeamain", playedOn: board, readOn: lecteur) let player1 = Human(named: "Bingeamain", playedOn: board, readOn: lecteur)
let player2 = Human(named: "illeauhanne", playedOn: board, readOn: lecteur) //let player2 = Human(named: "illeauhanne", playedOn: board, readOn: lecteur)
let player2 = IA(named: "Géraimiedepain", playedOn: board)
var game = Game(withBoard: board, playedBy: [player1, player2], writeOn: afficheur) var game = Game(withBoard: board, playedBy: [player1, player2], writeOn: afficheur)
while true { var winner : Player?
while game?.getWinner() == nil {
game?.tour() game?.tour()
} }
afficheur.afficherLigne(message: "Le gagnant est \(winner?.name)")
} }

Loading…
Cancel
Save