🚧 👔 WIP Players, Rules, Games, Boards

main
Alexis Drai 2 years ago
parent e635bf5311
commit e519b07cf5

@ -16,3 +16,14 @@ if var b = Board(withRows: 3, andWithCols: 3) {
status = b.insertChip(from: 1, atCol: 1) status = b.insertChip(from: 1, atCol: 1)
print(b) print(b)
} }
public func scan() -> Int {
// board.dispokayBoard()
// print("player \(rules.getNextPlayer()), choose a column between ")
var res: Int? = nil
while(res == nil) {
let str = readLine()
res = Int(str ?? "")
}
return res!
}

@ -0,0 +1,27 @@
import Foundation
class Game {
private let scanner: () -> Int
private let displayBoard: () -> String
private let board: Board
private let rules: IRules
private let player1: Player
private let player2: Player
init(withScanner scanner: @escaping () -> Int,
withBoard board: Board,
withRules rules: IRules,
withPlayer1 player1: Player,
withPlayer2 player2: Player) {
self.scanner = scanner
self.displayBoard = { () -> String in
return board.description
}
// TODO check that board is valid using the rules
self.board = board
self.rules = rules
self.player1 = player1
self.player2 = player2
}
}

@ -1,7 +1,12 @@
import Foundation import Foundation
class Bot: Player { class Bot: Player {
override func playBetween(min: Int, andMaxIncl maxIncl: Int) -> Int {
return Int.random(in: min..<maxIncl + 1) // TODO implement
// board est passé par copie, donc constant et immuable (?)
// var b = board nous donne une copie modifiable, avec laquele le bot peut faire des essais
override func chooseColumn(inBoard board: Board, withRules rules: IRules) -> Int? {
return Int.random(in: 0..<board.nbCols)
} }
} }

@ -1,4 +1,14 @@
import Foundation import Foundation
class Human: Player { class Human: Player {
private let scanner: () -> Int?
init(withId id: Int, withName name: String, usingScanner scanner: @escaping () -> Int?){
self.scanner = scanner
super.init(withId: id, withName: name)
}
override func chooseColumn(inBoard board: Board, withRules rules: IRules) -> Int? {
return scanner()
}
} }

@ -2,17 +2,17 @@ import Foundation
class Player { class Player {
let id: Int private let id: Int
private let name: String
init(withId id: Int){ init(withId id: Int, withName name: String){
self.id = id self.id = id
self.name = name
} }
///
/// should be considered abstract and overridden by descendants /// should be considered abstract and overridden by descendants
internal func playBetween(min: Int, andMaxIncl maxIncl: Int) -> Int { func chooseColumn(inBoard board: Board, withRules rules: IRules) -> Int? {
return -1 return nil
} }
} }

@ -1,4 +0,0 @@
import Foundation
class SmarterBot: Bot {
}

@ -31,7 +31,7 @@ public struct BasicDefaultsNoDiag : IRules {
} }
public func isGameOver(byPlayer playerId: Int, onGrid grid: [[Int?]]) public func isGameOver(byPlayer playerId: Int, onGrid grid: [[Int?]])
-> (isOver: Bool, hasWinner: Bool, victoryTiles: [(Int, Int)]?) { -> (isOver: Bool, result: Result) {
// first check if board is full // first check if board is full
var isFull = true var isFull = true
@ -72,7 +72,7 @@ public struct BasicDefaultsNoDiag : IRules {
// row i, origin is (i, j), goes to the right // row i, origin is (i, j), goes to the right
victoryTiles[x] = (i, j + x) victoryTiles[x] = (i, j + x)
} }
return (isOver: true, hasWinner: true, victoryTiles) return (isOver: true, Result.won(playerId, victoryTiles))
} }
} }
@ -96,13 +96,16 @@ public struct BasicDefaultsNoDiag : IRules {
// column j, origin is (i, j), goes down // column j, origin is (i, j), goes down
victoryTiles[x] = (i + x, j) victoryTiles[x] = (i + x, j)
} }
return (isOver: true, hasWinner: true, victoryTiles) return (isOver: true, Result.won(playerId, victoryTiles))
} }
} }
} }
} }
if(isFull) {
return (isFull, hasWinner: false, nil); return (isOver: true, Result.deadlocked);
} else {
return (isOver: false, Result.notOver)
}
} }
private func checkAligned(byPlayer playerId: Int, private func checkAligned(byPlayer playerId: Int,

@ -1,10 +1,25 @@
import Foundation import Foundation
protocol IRules { public protocol IRules {
var minNbRows: Int { get } var minNbRows: Int { get }
var maxNbRows: Int { get } var maxNbRows: Int { get }
var minNbCols: Int { get } var minNbCols: Int { get }
var maxNbCols: Int { get } var maxNbCols: Int { get }
var nbChipsToAlign: Int { get } var nbChipsToAlign: Int { get }
func isGameOver(byPlayer playerId: Int, onGrid grid: [[Int?]]) -> (isOver: Bool, hasWinner: Bool, victoryTiles: [(Int, Int)]?) func isGameOver(byPlayer playerId: Int, onGrid grid: [[Int?]]) -> (isOver: Bool, result: Result)
// TODO plug in the EnumResult
// and
// isGameOver(c) -> (Bool, EnumResult)
// getNextPlayer(c) -> Int
// isValid(c) -> Bool
}
public enum Result {
case notOver
case deadlocked
// playerId, victoryTiles
case won(Int, [(Int, Int)]?)
} }

Loading…
Cancel
Save