diff --git a/Connect4/connect4_cli/connect4_cli/main.swift b/Connect4/connect4_cli/connect4_cli/main.swift index 5904ca9..5c7ac29 100644 --- a/Connect4/connect4_cli/connect4_cli/main.swift +++ b/Connect4/connect4_cli/connect4_cli/main.swift @@ -12,40 +12,30 @@ public func scan() -> Int { } var status: (isOver: Bool, result: Result) -print("nothing yet") if let rules = BasicDefaultsNoDiag() { - print("rules") if let board = Board() { - print("board") - if let human = Human(withId: 1, + if let p1 = Human(withId: 1, withName: "Geraldine Humanman", usingScanner: scan) { - print("human") - if let bot = Bot(withId: 2, + if let p2 = Bot(withId: 2, withName: "Botty McBotFace") { - print("bot") if let game = Game(withScanner : scan, withBoard: board, withRules: rules, - withPlayer1: human, - withPlayer2: bot) { - print("game") + withPlayer1: p1, + withPlayer2: p2) { - status = game.isOver - print(game.displayBoard()) // 1st turn - while(!(status.isOver)) { + print(game.boardString) // 1st turn + while(!(game.isOver)) { if game.play() { - print(game.displayBoard()) - status = game.isOver + print(game.boardString) } } - print(game.gameOverString) - } } } diff --git a/Connect4/connect4_lib/Sources/connect4_lib/games/Game.swift b/Connect4/connect4_lib/Sources/connect4_lib/games/Game.swift index 9c5151d..ec722d0 100644 --- a/Connect4/connect4_lib/Sources/connect4_lib/games/Game.swift +++ b/Connect4/connect4_lib/Sources/connect4_lib/games/Game.swift @@ -1,7 +1,6 @@ import Foundation public class Game { private let scanner: () -> Int - public let displayBoard: () -> String private var board: Board private let rules: IRules private let player1: Player @@ -13,9 +12,6 @@ public class Game { withPlayer1 player1: Player, withPlayer2 player2: Player) { self.scanner = scanner - self.displayBoard = { () -> String in - return board.description - } guard(rules.isValid(board)) else { return nil } self.board = board self.rules = rules @@ -23,15 +19,20 @@ public class Game { self.player2 = player2 } - public var isOver: (isOver: Bool, result: Result) { + public var isOver: Bool { return rules.isGameOver(byPlayer: getCurrentPlayerId(), - onGrid: board.grid) + onGrid: board.grid).isOver + } + + public var boardString: String { + return board.description } public var gameOverString: String { var string = "Game over" - switch(isOver.result) { + switch(rules.isGameOver(byPlayer: getCurrentPlayerId(), + onGrid: board.grid).result) { case .won(let playerId, let victoryTiles): string.append("\nPlayer \(playerId) won!\n") string.append(board.displayVictory(fromTiles: victoryTiles)) diff --git a/Connect4/connect4_lib/Sources/connect4_lib/players/Bot.swift b/Connect4/connect4_lib/Sources/connect4_lib/players/Bot.swift index 3f8dfea..a3113b0 100644 --- a/Connect4/connect4_lib/Sources/connect4_lib/players/Bot.swift +++ b/Connect4/connect4_lib/Sources/connect4_lib/players/Bot.swift @@ -14,7 +14,7 @@ public class Bot: Player { public override func chooseColumn(inBoard board: Board, withRules rules: IRules) -> Int? { - print("(^°w°)") + print("(\(id)°w°)") return Int.random(in: 0..