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 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 { public func countPieces(filter: (Piece) -> Bool = { piece in true }) -> Int {
var count = 0 var count = 0

@ -101,9 +101,7 @@ public struct FourInARowRules: Rules {
internal static func rowOfLengthWin(board: Board, last_turn: Player?, minimum_aligned: Int, players: [Player]) -> GameState { internal static func rowOfLengthWin(board: Board, last_turn: Player?, minimum_aligned: Int, players: [Player]) -> GameState {
var occupied = 0 var occupied = 0
for column in 0..<board.columns { for current in board.coords {
for row in 0..<board.rows {
let current = Coords(column, row)
guard let piece = board[current] else { continue } guard let piece = board[current] else { continue }
occupied += 1 occupied += 1
@ -118,7 +116,6 @@ public struct FourInARowRules: Rules {
} }
} }
} }
}
if occupied == board.columns * board.rows { if occupied == board.columns * board.rows {
return .Draw return .Draw
@ -129,41 +126,6 @@ public struct FourInARowRules: Rules {
} }
} }
/* 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") @available(*, deprecated, message: "Old Rules design remnents")
internal static func countMaxRow(center: Coords, board: Board) -> Int { internal static func countMaxRow(center: Coords, board: Board) -> Int {
var maxLength = 0 var maxLength = 0

@ -53,11 +53,9 @@ public struct TicTacToeRules: Rules {
public func validMoves(board: Board, for_player player: Player) -> [Move.Action] { public func validMoves(board: Board, for_player player: Player) -> [Move.Action] {
var moves: [Move.Action] = [] var moves: [Move.Action] = []
for col in 0..<board.columns { board.coords.forEach { coords in
for row in 0..<board.rows { if board[coords] == nil {
if board[col, row] == nil { moves.append(.InsertAt(where: coords))
moves.append(.InsertAt(where: Coords(col, row)))
}
} }
} }
@ -67,24 +65,4 @@ public struct TicTacToeRules: Rules {
public func gameState(board: Board, last_turn: Player?) -> GameState { public func gameState(board: Board, last_turn: Player?) -> GameState {
FourInARowRules.rowOfLengthWin(board: board, last_turn: last_turn, minimum_aligned: self.minAligned, players: self.players) 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) XCTAssertEqual(p.piece_type, .B)
} }
func testChoose() { func testChoose() async {
let board = Board(columns: 3, rows: 3)! let board = Board(columns: 3, rows: 3)!
let moves: [Move.Action] = [.InsertAt(where: Coords(1, 2)), .InsertOnSide(side: .Left, offset: 1)] 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) 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 board = Board(columns: 3, rows: 3)!
let moves: [Move.Action] = [.InsertAt(where: Coords(1, 2)), .InsertOnSide(side: .Left, offset: 1)] 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 // +5 test quality credits
for _ in 1...10 { 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)) XCTAssertNotNil(moves.firstIndex(of: choosen))
} }

Loading…
Cancel
Save