parent
fd81d09b61
commit
082ee3e222
@ -0,0 +1,97 @@
|
|||||||
|
//
|
||||||
|
// Connect4Rules.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Adam BONAFOS on 14/01/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
public struct Connect4Rules : Rules {
|
||||||
|
let piecesToAlign: Int
|
||||||
|
|
||||||
|
init(piecesToAlign: Int) {
|
||||||
|
self.piecesToAlign = piecesToAlign
|
||||||
|
}
|
||||||
|
|
||||||
|
func add(board: inout Board, position pos: (Int, Int), playerId: PlayerId) -> Bool {
|
||||||
|
var currentRowNb = 0;
|
||||||
|
for var row in board.grid {
|
||||||
|
if row[pos.1] != PlayerId.empty && currentRowNb != 0 {
|
||||||
|
row[currentRowNb - 1] = playerId
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
currentRowNb += 1
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func isGameOver(board: Board) -> (Bool, PlayerId) {
|
||||||
|
// Vérification générale avec le nombre de pièces à aligner
|
||||||
|
let maxRow = board.rowNb
|
||||||
|
let maxCol = board.columnNb
|
||||||
|
|
||||||
|
// Vérification horizontale
|
||||||
|
for row in 0..<maxRow {
|
||||||
|
for col in 0..<(maxCol - piecesToAlign + 1) {
|
||||||
|
if checkLine(board: board, start: (row, col), direction: (0, 1)) {
|
||||||
|
return (true, board[row, col])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vérification verticale
|
||||||
|
for col in 0..<maxCol {
|
||||||
|
for row in 0..<(maxRow - piecesToAlign + 1) {
|
||||||
|
if checkLine(board: board, start: (row, col), direction: (1, 0)) {
|
||||||
|
return (true, board[row, col])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vérification diagonale ascendante (\)
|
||||||
|
for row in (piecesToAlign - 1)..<maxRow {
|
||||||
|
for col in 0..<(maxCol - piecesToAlign + 1) {
|
||||||
|
if checkLine(board: board, start: (row, col), direction: (-1, 1)) {
|
||||||
|
return (true, board[row, col])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vérification diagonale descendante (/)
|
||||||
|
for row in 0..<(maxRow - piecesToAlign + 1) {
|
||||||
|
for col in 0..<(maxCol - piecesToAlign + 1) {
|
||||||
|
if checkLine(board: board, start: (row, col), direction: (1, 1)) {
|
||||||
|
return (true, board[row, col])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vérification du match nul (plateau plein)
|
||||||
|
for row in 0..<maxRow {
|
||||||
|
for col in 0..<maxCol {
|
||||||
|
if board[row, col] == .empty {
|
||||||
|
return (false, PlayerId.empty)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (true, PlayerId.empty) // Match nul si toutes les cases sont pleines
|
||||||
|
}
|
||||||
|
|
||||||
|
private func checkLine(board: Board, start: (Int, Int), direction: (Int, Int)) -> Bool {
|
||||||
|
var currentPos = start
|
||||||
|
let player = board[start.0, start.1]
|
||||||
|
guard player != .empty else { return false }
|
||||||
|
|
||||||
|
for _ in 1..<piecesToAlign {
|
||||||
|
currentPos = (currentPos.0 + direction.0, currentPos.1 + direction.1)
|
||||||
|
if currentPos.0 < 0 || currentPos.0 >= board.rowNb ||
|
||||||
|
currentPos.1 < 0 || currentPos.1 >= board.columnNb ||
|
||||||
|
board[currentPos.0, currentPos.1] != player {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Adam BONAFOS on 14/01/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
enum PlayerId {
|
||||||
|
case empty
|
||||||
|
case player1
|
||||||
|
case player2
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Adam BONAFOS on 14/01/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
protocol Rules {
|
||||||
|
func add(board: inout Board, position pos: (Int, Int), playerId: PlayerId) -> Bool
|
||||||
|
func isGameOver(board: Board) -> (Bool, PlayerId)
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Adam BONAFOS on 14/01/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
public struct TicTacToeRules: Rules {
|
||||||
|
func add(board: inout Board, position pos: (Int, Int), playerId: PlayerId) -> Bool {
|
||||||
|
if board[pos.0, pos.1] == .empty {
|
||||||
|
board[pos.0, pos.1] = playerId
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func isGameOver(board: Board) -> (Bool, PlayerId) {
|
||||||
|
<#code#>
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue