Last active
September 15, 2017 11:49
-
-
Save cakper/11fa2c597f976a8052a5 to your computer and use it in GitHub Desktop.
Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Board { | |
func isValidMove(move: (x: Int, y: Int)) -> Bool | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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