Final cleanup

main
Mathieu GROUSSEAU 1 week ago
parent 2305ba43ad
commit baf8fb6d0c

@ -19,6 +19,6 @@ final class CustomTypesTests: XCTestCase {
let text: String = board.description
XCTAssertEqual(text, "⚪⚪⚪⚪\n⚪🔴🔴⚪\n⚪🟡🔴⚪\n⚪⚪⚪⚪\n")
XCTAssertEqual(text, " 1 2 3 4\n 1⚪⚪⚪⚪\n 2⚪🔴🔴⚪\n 3⚪🟡🔴⚪\n 4⚪⚪⚪⚪\n")
}
}

@ -57,21 +57,6 @@ public struct Board: Equatable {
}
}
@available(*, deprecated, renamed: "cells", message: "Use cells sequence instead")
public func forEach(_ consumer: (Piece?) -> Void) {
self.cells.forEach(consumer)
}
@available(*, deprecated, renamed: "cells", message: "Use cells sequence instead")
public mutating func forEach(mutating consumer: (Coords, inout Piece?) -> Void) {
for column in 0..<self.columns {
for row in 0..<self.rows {
let coords = Coords(column, row)
consumer(coords, &self[coords])
}
}
}
public func countPieces(filter: (Piece) -> Bool = { piece in true }) -> Int {
var count = 0

@ -101,21 +101,18 @@ public struct FourInARowRules: Rules {
internal static func rowOfLengthWin(board: Board, last_turn: Player?, minimum_aligned: Int, players: [Player]) -> GameState {
var occupied = 0
for column in 0..<board.columns {
for row in 0..<board.rows {
let current = Coords(column, row)
guard let piece = board[current] else { continue }
occupied += 1
for current in board.coords {
guard let piece = board[current] else { continue }
occupied += 1
// For each "axis" (described as one direction)
for dir: (Int, Int) in [(1, 0), (0, 1), (1, 1), (-1, 1)] {
let cells = Self.row(center: current, board: board, dir: dir)
// For each "axis" (described as one direction)
for dir: (Int, Int) in [(1, 0), (0, 1), (1, 1), (-1, 1)] {
let cells = Self.row(center: current, board: board, dir: dir)
if cells.count >= minimum_aligned {
let player = players.first(where: { $0.piece_type == piece.type })!
return GameState.Win(winner: player, board: board, cells: cells)
}
if cells.count >= minimum_aligned {
let player = players.first(where: { $0.piece_type == piece.type })!
return GameState.Win(winner: player, board: board, cells: cells)
}
}
}
@ -128,41 +125,6 @@ public struct FourInARowRules: Rules {
return .Playing(turn: players.first!)
}
}
/* public mutating func onMoveDone(move: Move, board: Board) -> Void {
// self.history.append(move)
switch move.action {
case .InsertOnSide(side: .Top, let offset):
let initCoords = board.getInsertionCoordinates(from: .Top, offset: offset)
let pieceCoords = switch board.fallCoordinates(
initialCoords: initCoords,
direction: .Bottom
) {
case .Occupied:
initCoords
case .Piece(_, let touched):
touched
default:
fatalError("Illegal move \(move.action)")
}
if Self.countMaxRow(center: pieceCoords, board: board) >= self.minAligned {
self.state = .Finished(winner: move.player)
} else if board.countPieces() == board.columns * board.rows {
self.state = .Finished(winner: nil)
} else {
let next: PieceType = switch move.player {
case .A: .B
case .B: .A
}
self.state = .Playing(turn: next)
}
default:
fatalError("Illegal move \(move.action)")
}
} */
@available(*, deprecated, message: "Old Rules design remnents")
internal static func countMaxRow(center: Coords, board: Board) -> Int {

@ -53,11 +53,9 @@ public struct TicTacToeRules: Rules {
public func validMoves(board: Board, for_player player: Player) -> [Move.Action] {
var moves: [Move.Action] = []
for col in 0..<board.columns {
for row in 0..<board.rows {
if board[col, row] == nil {
moves.append(.InsertAt(where: Coords(col, row)))
}
board.coords.forEach { coords in
if board[coords] == nil {
moves.append(.InsertAt(where: coords))
}
}
@ -67,24 +65,4 @@ public struct TicTacToeRules: Rules {
public func gameState(board: Board, last_turn: Player?) -> GameState {
FourInARowRules.rowOfLengthWin(board: board, last_turn: last_turn, minimum_aligned: self.minAligned, players: self.players)
}
/* public mutating func onMoveDone(move: Move, board: Board) {
self.history.append(move)
guard case .InsertAt(let coords) = move.action else {
fatalError("Illegal move \(move.action)")
}
if FourInARowRules.countMaxRow(center: coords, board: board) >= self.minAligned {
self.state = .Finished(winner: move.player)
} else if board.countPieces() == board.columns * board.rows {
self.state = .Finished(winner: nil)
} else {
let next: PieceType = switch move.player {
case .A: .B
case .B: .A
}
self.state = .Playing(turn: next)
}
} */
}

@ -9,7 +9,7 @@ final class HumanPlayerTests: XCTestCase {
XCTAssertEqual(p.piece_type, .B)
}
func testChoose() {
func testChoose() async {
let board = Board(columns: 3, rows: 3)!
let moves: [Move.Action] = [.InsertAt(where: Coords(1, 2)), .InsertOnSide(side: .Left, offset: 1)]
@ -22,6 +22,7 @@ final class HumanPlayerTests: XCTestCase {
return .InsertOnSide(side: .Bottom, offset: 99)
})
XCTAssertEqual(player.chooseMove(allowed_moves: moves, board: board), .InsertOnSide(side: .Bottom, offset: 99))
let result = await player.chooseMove(allowed_moves: moves, board: board)
XCTAssertEqual(result, .InsertOnSide(side: .Bottom, offset: 99))
}
}

@ -11,7 +11,7 @@ final class RandomPlayerTests: XCTestCase {
}
}
func testChoose() {
func testChoose() async {
let board = Board(columns: 3, rows: 3)!
let moves: [Move.Action] = [.InsertAt(where: Coords(1, 2)), .InsertOnSide(side: .Left, offset: 1)]
@ -19,7 +19,7 @@ final class RandomPlayerTests: XCTestCase {
// +5 test quality credits
for _ in 1...10 {
let choosen = player.chooseMove(allowed_moves: moves, board: board)
let choosen = await player.chooseMove(allowed_moves: moves, board: board)
XCTAssertNotNil(moves.firstIndex(of: choosen))
}

Loading…
Cancel
Save