diff --git a/Connect4/connect4_cli/connect4_cli/main.swift b/Connect4/connect4_cli/connect4_cli/main.swift index 45874ee..5904ca9 100644 --- a/Connect4/connect4_cli/connect4_cli/main.swift +++ b/Connect4/connect4_cli/connect4_cli/main.swift @@ -36,10 +36,10 @@ if let rules = BasicDefaultsNoDiag() { print("game") status = game.isOver - print(game.boardString) // 1st turn + print(game.displayBoard()) // 1st turn while(!(status.isOver)) { if game.play() { - print(game.boardString) + print(game.displayBoard()) status = game.isOver } } diff --git a/Connect4/connect4_lib/Sources/connect4_lib/games/Game.swift b/Connect4/connect4_lib/Sources/connect4_lib/games/Game.swift index b27c70a..9c5151d 100644 --- a/Connect4/connect4_lib/Sources/connect4_lib/games/Game.swift +++ b/Connect4/connect4_lib/Sources/connect4_lib/games/Game.swift @@ -1,7 +1,7 @@ import Foundation public class Game { private let scanner: () -> Int - private let displayBoard: () -> String + public let displayBoard: () -> String private var board: Board private let rules: IRules private let player1: Player @@ -28,10 +28,6 @@ public class Game { onGrid: board.grid) } - public var boardString: String { - return board.description - } - public var gameOverString: String { var string = "Game over" diff --git a/Connect4/connect4_lib/Tests/connect4_libTests/GameTest.swift b/Connect4/connect4_lib/Tests/connect4_libTests/GameTest.swift index 6c53eb9..f60ed2c 100644 --- a/Connect4/connect4_lib/Tests/connect4_libTests/GameTest.swift +++ b/Connect4/connect4_lib/Tests/connect4_libTests/GameTest.swift @@ -2,5 +2,59 @@ import XCTest import connect4_lib final class GameTest: XCTestCase { + + func scan() -> Int { + return 0 + } + + func testPlay() throws { + + let rules = BasicDefaultsNoDiag() + + func expect(withGrid orig: [[Int?]], + choice1: @escaping () -> Int, + choice2: @escaping () -> Int, + shouldWork: Bool) { + let p1 = Human(withId: 1, withName: "bot1", usingScanner: choice1) + let p2 = Human(withId: 2, withName: "bot2", usingScanner: choice2) + let board = Board(withGrid: orig) + let game = Game(withScanner: scan, withBoard: board!, withRules: rules!, withPlayer1: p1!, withPlayer2: p2!) + XCTAssertEqual(shouldWork, game!.play()) + } + + expect(withGrid: [[nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil]], choice1: { return 0 }, choice2: scan, shouldWork: true) + + expect(withGrid: [[nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [ 1 , nil, nil, nil, nil, nil, nil]], choice1: scan, choice2: { return 3 }, shouldWork: true) + + expect(withGrid: [[nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [ 1 , nil, nil, 2 , nil, nil, nil]], choice1: { return -1 }, choice2: scan, shouldWork: false) + + expect(withGrid: [[nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [ 1 , nil, nil, 2 , nil, nil, nil]], choice1: { return 0 }, choice2: scan, shouldWork: true) + + expect(withGrid: [[nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [nil, nil, nil, nil, nil, nil, nil], + [ 1 , nil, nil, nil, nil, nil, nil], + [ 1 , nil, nil, 2 , nil, nil, nil]], choice1: scan, choice2: { return 7 }, shouldWork: false) } }