UT: Game.play(). Pass 80+%
continuous-integration/drone/push Build is passing Details

main
Alexis Drai 2 years ago
parent c50b11e9b5
commit e54877263d

@ -36,10 +36,10 @@ if let rules = BasicDefaultsNoDiag() {
print("game") print("game")
status = game.isOver status = game.isOver
print(game.boardString) // 1st turn print(game.displayBoard()) // 1st turn
while(!(status.isOver)) { while(!(status.isOver)) {
if game.play() { if game.play() {
print(game.boardString) print(game.displayBoard())
status = game.isOver status = game.isOver
} }
} }

@ -1,7 +1,7 @@
import Foundation import Foundation
public class Game { public class Game {
private let scanner: () -> Int private let scanner: () -> Int
private let displayBoard: () -> String public let displayBoard: () -> String
private var board: Board private var board: Board
private let rules: IRules private let rules: IRules
private let player1: Player private let player1: Player
@ -28,10 +28,6 @@ public class Game {
onGrid: board.grid) onGrid: board.grid)
} }
public var boardString: String {
return board.description
}
public var gameOverString: String { public var gameOverString: String {
var string = "Game over" var string = "Game over"

@ -3,4 +3,58 @@ import connect4_lib
final class GameTest: XCTestCase { 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) }
} }

Loading…
Cancel
Save