|
|
@ -31,11 +31,7 @@ enum PlayerType: String, CaseIterable {
|
|
|
|
case Human = "Player"
|
|
|
|
case Human = "Player"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func createPlayer(type: PlayerType, playing pieces: PieceType, named name: String) -> Player {
|
|
|
|
func playerPrompt(allowed_moves: [Move.Action], board: Board) -> Move.Action {
|
|
|
|
switch type {
|
|
|
|
|
|
|
|
case .Human:
|
|
|
|
|
|
|
|
HumanPlayer(name: name, piece_type: pieces, callback: {
|
|
|
|
|
|
|
|
allowed_moves, board in
|
|
|
|
|
|
|
|
for (i, action) in allowed_moves.enumerated() {
|
|
|
|
for (i, action) in allowed_moves.enumerated() {
|
|
|
|
let text = switch action {
|
|
|
|
let text = switch action {
|
|
|
|
case .InsertAt(let at): "Place piece at \(at.col):\(at.row)"
|
|
|
|
case .InsertAt(let at): "Place piece at \(at.col):\(at.row)"
|
|
|
@ -51,7 +47,12 @@ func createPlayer(type: PlayerType, playing pieces: PieceType, named name: Strin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return allowed_moves[index - 1]
|
|
|
|
return allowed_moves[index - 1]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func createPlayer(type: PlayerType, playing pieces: PieceType, named name: String) -> Player {
|
|
|
|
|
|
|
|
switch type {
|
|
|
|
|
|
|
|
case .Human:
|
|
|
|
|
|
|
|
HumanPlayer(name: name, piece_type: pieces, callback: playerPrompt)
|
|
|
|
case .AIRandom:
|
|
|
|
case .AIRandom:
|
|
|
|
RandomPlayer(name: name, piece_type: pieces)
|
|
|
|
RandomPlayer(name: name, piece_type: pieces)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -68,7 +69,7 @@ let players: [Player] = PieceType.allCases.map { type in
|
|
|
|
return createPlayer(type: ptype, playing: type, named: playerName)
|
|
|
|
return createPlayer(type: ptype, playing: type, named: playerName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let rules: Rules = switch gameType {
|
|
|
|
let rules: any Rules = switch gameType {
|
|
|
|
case .FourInARow: FourInARowRules(players: players)!
|
|
|
|
case .FourInARow: FourInARowRules(players: players)!
|
|
|
|
case .TicTacToe: TicTacToeRules(players: players)!
|
|
|
|
case .TicTacToe: TicTacToeRules(players: players)!
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -104,3 +105,9 @@ encoder.userInfo[.gameDecodingContext] = CodableContext()
|
|
|
|
let data = try encoder.encode(CodableGameWrapper(of: game))
|
|
|
|
let data = try encoder.encode(CodableGameWrapper(of: game))
|
|
|
|
|
|
|
|
|
|
|
|
print(String(data: data, encoding: .utf8)!)
|
|
|
|
print(String(data: data, encoding: .utf8)!)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var decoder = JSONDecoder()
|
|
|
|
|
|
|
|
decoder.userInfo[.gameDecodingContext] = CodableContext(creatingCallbacks: { name, type in playerPrompt })
|
|
|
|
|
|
|
|
let game2 = (try decoder.decode(CodableGameWrapper.self, from: data)).game
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert(game == game2, "Games are not equals!")
|
|
|
|