feat - add display and scan method in CLT
continuous-integration/drone/push Build is failing Details

main
DJYohann 2 years ago
parent 3dc3aba52e
commit 95d95f6b60

@ -9,6 +9,10 @@
/* Begin PBXBuildFile section */
1E037925297971D7003044F5 /* Model in Frameworks */ = {isa = PBXBuildFile; productRef = 1E037924297971D7003044F5 /* Model */; };
1E3E843529775FEB0087C5C2 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E3E843429775FEB0087C5C2 /* main.swift */; };
1E680E5D299939AA00DB3727 /* Displayable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E680E5C299939AA00DB3727 /* Displayable.swift */; };
1E680E5F29993A1B00DB3727 /* Readable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E680E5E29993A1B00DB3727 /* Readable.swift */; };
1E680E6129993A7900DB3727 /* ConsoleDisplay.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E680E6029993A7900DB3727 /* ConsoleDisplay.swift */; };
1E680E6329993B2400DB3727 /* ConsoleRead.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E680E6229993B2400DB3727 /* ConsoleRead.swift */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
@ -26,6 +30,10 @@
/* Begin PBXFileReference section */
1E3E843129775FEB0087C5C2 /* CLT */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = CLT; sourceTree = BUILT_PRODUCTS_DIR; };
1E3E843429775FEB0087C5C2 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
1E680E5C299939AA00DB3727 /* Displayable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Displayable.swift; sourceTree = "<group>"; };
1E680E5E29993A1B00DB3727 /* Readable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Readable.swift; sourceTree = "<group>"; };
1E680E6029993A7900DB3727 /* ConsoleDisplay.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConsoleDisplay.swift; sourceTree = "<group>"; };
1E680E6229993B2400DB3727 /* ConsoleRead.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConsoleRead.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -43,6 +51,7 @@
1E3E842829775FEB0087C5C2 = {
isa = PBXGroup;
children = (
1E680E5B2999399500DB3727 /* IO */,
1E3E843329775FEB0087C5C2 /* CLT */,
1E3E843229775FEB0087C5C2 /* Products */,
1E3E843B2977619C0087C5C2 /* Frameworks */,
@ -72,6 +81,17 @@
name = Frameworks;
sourceTree = "<group>";
};
1E680E5B2999399500DB3727 /* IO */ = {
isa = PBXGroup;
children = (
1E680E5C299939AA00DB3727 /* Displayable.swift */,
1E680E5E29993A1B00DB3727 /* Readable.swift */,
1E680E6029993A7900DB3727 /* ConsoleDisplay.swift */,
1E680E6229993B2400DB3727 /* ConsoleRead.swift */,
);
path = IO;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@ -133,7 +153,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1E680E6129993A7900DB3727 /* ConsoleDisplay.swift in Sources */,
1E3E843529775FEB0087C5C2 /* main.swift in Sources */,
1E680E6329993B2400DB3727 /* ConsoleRead.swift in Sources */,
1E680E5F29993A1B00DB3727 /* Readable.swift in Sources */,
1E680E5D299939AA00DB3727 /* Displayable.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

@ -16,8 +16,6 @@ func insertPiece(id : Int, column : Int, _ board : inout Board) {
print("Unknown")
case .negativeOrOutOfBound:
print("Row or column must be posittive")
case .alreadyTake:
print("Column already take")
}
default:
print("Rien")
@ -46,3 +44,10 @@ if var board = Board(withNbRows: 6, withNbColumns: 7) {
print(board)
}
var grid : [[Int?]] = [[1,2,nil],[nil,nil,nil],[nil,nil,nil]]
var b = Board(withGrid: grid)
var displayer = ConsoleDisplay()
displayer.displayBoard(board: b!)

@ -0,0 +1,8 @@
import Foundation
import Model
public class ConsoleDisplay : Displayable {
public func displayBoard(board: Board) {
print(board)
}
}

@ -0,0 +1,13 @@
import Foundation
public class ConsoleRead : Readable {
public func readInt() -> Int {
print("Choose column :")
if let input = readLine() {
if let number = Int(input) {
return number
}
}
return 0
}
}

@ -0,0 +1,6 @@
import Foundation
import Model
public protocol Displayable {
func displayBoard(board: Board)
}

@ -0,0 +1,5 @@
import Foundation
public protocol Readable {
func readInt() -> Int
}

@ -100,7 +100,7 @@ public struct Board : CustomStringConvertible {
return .failed(reason: .negativeOrOutOfBound)
}
guard grid[row][column] == nil else{
return .failed(reason: .alreadyTake)
return .failed(reason: .columnFull)
}
grid[row][column] = id
return .ok
@ -121,7 +121,7 @@ public struct Board : CustomStringConvertible {
return insertPiece(id: id, row: row, column: column)
}
}
return .failed(reason: .alreadyTake)
return .failed(reason: .columnFull)
}
/// Remove a piece from the grid

@ -15,7 +15,6 @@ public enum BoardResult : Equatable {
public enum FailedResult {
case unknown
case negativeOrOutOfBound
case alreadyTake
case columnFull
case boardFull
}

@ -20,8 +20,6 @@ public class Game {
///
public func insertPiece(player: Player) {
var col = player.chooseColumn()
board?.insertPiece(id: player.id, column: col)
}
}

@ -11,13 +11,65 @@ public class ClassicRules : Rules {
Board(withNbRows: Self.nbRows, withNbColumns: Self.nbColumns) ?? nil
}
/// Check if game is over
///
public func isGameOver() -> (Bool, Int, GameResult) {
return (true, 2, .lose);
/// - Returns : true if game is over else false, coordinates of last piece, game result
public func isGameOver() -> (Bool, (Int, Int), GameResult) {
return (true, (1,2), .lose);
}
/// Returns id of next palyer to play
///
/// The player who must play is the one who has the fewest tokens
///
/// - Parameter board : board game
///
/// - Returns: id of next palyer to play
public func getNextPlayer(board: Board) -> Int {
return 9;
var countP1 = 0
var countP2 = 0
for row in 0..<board.nbRows {
for col in 0..<board.nbColumns {
if board.grid[row][col] == 1 {
countP1 = countP1 + 1
}
if board.grid[row][col] == 2 {
countP2 = countP2 + 2
}
}
}
return countP1 > countP2 ? 2 : 1
}
/// Check if 4 pieces were aligned in vertical
///
/// - Parameter board : board to examine
/// - Parameter coord : coordinates of last point
///
/// - Returns : true if 4 pieces were aligned in vertical else false
private func checkVertical(board: Board, coord: (Int, Int)) -> Bool {
return true
}
/// Check if 4 pieces were aligned in horizontal
///
/// - Parameter board : board to examine
/// - Parameter coord : coordinates of last point
///
/// - Returns : true if 4 pieces were aligned in horizontal else false
private func checkHorizontal(board: Board, coord: (Int, Int)) -> Bool {
return true
}
/// Check if 4 pieces were aligned in diagonal
///
/// - Parameter board : board to examine
/// - Parameter coord : coordinates of last point
///
/// - Returns : true if 4 pieces were aligned in diagonal else false
private func checkDiagonal(board: Board, coord: (Int, Int)) -> Bool {
return true
}
}

@ -19,10 +19,10 @@ public protocol Rules {
/// Create board
func createBoard() -> Board?
/// Defines if game is over
/// Check if game is over
///
/// - Returns
func isGameOver() -> (Bool, Int, GameResult)
/// - Returns : true if game is over else false, coordinates of last piece, game result
func isGameOver() -> (Bool, (Int, Int), GameResult)
/// Returns id of next palyer to play
///

@ -62,24 +62,22 @@ final class BoardTest: XCTestCase {
}
func testInsertPiece() {
func expect(grid : [[Int?]], idPlayer: Int, column: Int, boardResult : BoardResult, notNil: Bool) {
func expect(grid : [[Int?]], idPlayer: Int, column: Int, boardResult : BoardResult) {
var board = Board(withGrid: grid)
if !notNil {
XCTAssertNil(board)
return
}
XCTAssertNotNil(board)
XCTAssertEqual(board?.insertPiece(id: idPlayer, column: column), boardResult)
}
let grid : [[Int?]] = Array.init(repeating: Array.init(repeating: nil, count: 3), count: 3)
let grid : [[Int?]] = [[1,2,nil],[nil,nil,nil],[nil,nil,nil]]
let gridColumnFull : [[Int?]] = [[1,2,nil],[1,nil,nil],[1,nil,nil]]
expect(grid: grid, idPlayer: 2, column: 0, boardResult: .ok)
expect(grid: grid, idPlayer: 2, column: 2, boardResult: .ok)
expect(grid: grid, idPlayer: 2, column: -2, boardResult: .failed(reason: .negativeOrOutOfBound))
expect(grid: grid, idPlayer: 2, column: 7, boardResult: .failed(reason: .negativeOrOutOfBound))
expect(grid: grid, idPlayer: 2, column: 0, boardResult: .ok, notNil: true)
expect(grid: grid, idPlayer: 2, column: 0, boardResult: .ok, notNil: true)
expect(grid: grid, idPlayer: 2, column: 0, boardResult: .ok, notNil: true)
//expect(grid: grid, idPlayer: 2, column: 0, boardResult: .failed(reason: <#T##FailedResult#>), notNil: true)
expect(grid: gridColumnFull, idPlayer: 2, column: 0, boardResult: .failed(reason: .columnFull))
}

@ -3,15 +3,7 @@ import Model
final class AITests: XCTestCase {
func testInit() {
func expect(nickname: String) {
let ai = AI()
XCTAssertEqual(ai.nickname, nickname)
}
expect(nickname: "yobreuil")
expect(nickname: "cebouhou")
expect(nickname: "macheval")
XCTAssertEqual(ai.nickname, "AI")
}
}

@ -9,4 +9,5 @@ final class ClassicRulesTests: XCTestCase {
XCTAssertEqual(3, ClassicRules.nbTrials)
}
func
}

Loading…
Cancel
Save