From baf8fb6d0cbe097ed23f9135381d757eceedcc9d Mon Sep 17 00:00:00 2001 From: Mathieu GROUSSEAU Date: Thu, 13 Feb 2025 21:03:17 +0100 Subject: [PATCH] Final cleanup --- .../Tests/CustomTypesTests/DisplayTests.swift | 2 +- Model/Sources/Model/Board.swift | 15 ----- .../Sources/Model/Rules/FourInARowRules.swift | 60 ++++--------------- .../Sources/Model/Rules/TicTacToeRules.swift | 28 +-------- .../ModelTests/Players/HumanPlayerTests.swift | 5 +- .../Players/RandomPlayerTests.swift | 4 +- 6 files changed, 20 insertions(+), 94 deletions(-) diff --git a/CustomTypes/Tests/CustomTypesTests/DisplayTests.swift b/CustomTypes/Tests/CustomTypesTests/DisplayTests.swift index 1faca77..38d6624 100644 --- a/CustomTypes/Tests/CustomTypesTests/DisplayTests.swift +++ b/CustomTypes/Tests/CustomTypesTests/DisplayTests.swift @@ -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") } } diff --git a/Model/Sources/Model/Board.swift b/Model/Sources/Model/Board.swift index 4d0b59d..1043dcb 100644 --- a/Model/Sources/Model/Board.swift +++ b/Model/Sources/Model/Board.swift @@ -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.. Bool = { piece in true }) -> Int { var count = 0 diff --git a/Model/Sources/Model/Rules/FourInARowRules.swift b/Model/Sources/Model/Rules/FourInARowRules.swift index a48e5ad..bbdb8f5 100644 --- a/Model/Sources/Model/Rules/FourInARowRules.swift +++ b/Model/Sources/Model/Rules/FourInARowRules.swift @@ -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..= 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 { diff --git a/Model/Sources/Model/Rules/TicTacToeRules.swift b/Model/Sources/Model/Rules/TicTacToeRules.swift index b577abb..e832f13 100644 --- a/Model/Sources/Model/Rules/TicTacToeRules.swift +++ b/Model/Sources/Model/Rules/TicTacToeRules.swift @@ -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.. 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) - } - } */ } diff --git a/Model/Tests/ModelTests/Players/HumanPlayerTests.swift b/Model/Tests/ModelTests/Players/HumanPlayerTests.swift index eef1a56..a07d921 100644 --- a/Model/Tests/ModelTests/Players/HumanPlayerTests.swift +++ b/Model/Tests/ModelTests/Players/HumanPlayerTests.swift @@ -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)) } } diff --git a/Model/Tests/ModelTests/Players/RandomPlayerTests.swift b/Model/Tests/ModelTests/Players/RandomPlayerTests.swift index 9562b25..64dcbe6 100644 --- a/Model/Tests/ModelTests/Players/RandomPlayerTests.swift +++ b/Model/Tests/ModelTests/Players/RandomPlayerTests.swift @@ -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)) }