Skip to content

Instantly share code, notes, and snippets.

@cakper
Last active September 15, 2017 11:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cakper/11fa2c597f976a8052a5 to your computer and use it in GitHub Desktop.
Save cakper/11fa2c597f976a8052a5 to your computer and use it in GitHub Desktop.
Swift
protocol Board {
func isValidMove(move: (x: Int, y: Int)) -> Bool
}
class MoveGenerator {
let board: BoardProtocol
func forPosition(position: (x: Int, y: Int)) -> [(x: Int, y: Int)] {
let move = (x: position.x, y: position.y)
if self.board.isValidMove(move) {
return [move]
}
return []
}
init(board: Board) {
self.board = board
}
}
class FullBoard: BoardProtocol {
func isValidMove(move: (x: Int, y: Int)) -> Bool {
return false
}
}
class EmptyBoard: BoardProtocol {
func isValidMove(move: (x: Int, y: Int)) -> Bool {
return true
}
}
class MoveGeneratorTests: XCTestCase {
func testGeneratesEmptyList() {
let generator = MoveGenerator(board: FullBoard())
XCTAssertEqual(generator.forPosition((x: 0, y: 0)).count, 0, "Expected zero moves for full board")
}
func testGeneratesAllAvialable() {
let generator = MoveGenerator(board: EmptyBoard())
XCTAssertEqual(generator.forPosition((x: 0, y: 0)).count, 1, "Expected one move for empty board")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment