-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d6a9ae
commit ff47367
Showing
3 changed files
with
133 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// GameLogicModel.swift | ||
// TicTacToe | ||
// | ||
// Created by Ashborn on 18/03/23. | ||
// | ||
|
||
import Foundation | ||
|
||
struct GameLogicModel { | ||
private(set) var moves: [VersusAIModel.Move?] | ||
private var aiVM: VersusAIViewModel | ||
private var humanMoves: [VersusAIModel.Move] | ||
private var computerMoves: [VersusAIModel.Move] | ||
|
||
init(currentMove: [VersusAIModel.Move?]) { | ||
moves = currentMove | ||
aiVM = VersusAIViewModel() | ||
|
||
// Get human positions to block | ||
humanMoves = moves.compactMap { $0 }.filter { $0.player == .human } | ||
|
||
// Get computer positions to win | ||
computerMoves = moves.compactMap { $0 }.filter { $0.player == .computer } | ||
} | ||
|
||
func pickRandomSquare() -> Int { | ||
var randomPosition = Int.random(in: 0..<9) | ||
|
||
// If square is occupied, pick any other | ||
while aiVM.isSquareOccupied(in: moves, forIndex: randomPosition) { | ||
randomPosition = Int.random(in: 0..<9) | ||
} | ||
|
||
return randomPosition | ||
} | ||
|
||
func pickMiddleSquare() -> Int { | ||
let centerSquare = 4 | ||
if !aiVM.isSquareOccupied(in: moves, forIndex: centerSquare) { | ||
return centerSquare | ||
} | ||
|
||
return -1 | ||
} | ||
|
||
func pickCornerSquare() -> Int { | ||
let corners: [Int] = [0, 2, 6, 8] | ||
for corner in corners { | ||
if !aiVM.isSquareOccupied(in: moves, forIndex: corner) { | ||
return corner | ||
} | ||
} | ||
|
||
return -1 | ||
} | ||
|
||
func blockWin(patterns: Set<Set<Int>>) -> Int { | ||
let humanPositions = Set(humanMoves.map { $0.boardIndex }) | ||
|
||
for pattern in patterns { | ||
let blockPositions = pattern.subtracting(humanPositions) | ||
|
||
if blockPositions.count == 1 { | ||
let isBlockable = !aiVM.isSquareOccupied(in: moves, forIndex: blockPositions.first!) | ||
if isBlockable { return blockPositions.first! } | ||
} | ||
} | ||
|
||
return -1 | ||
} | ||
|
||
func winGame(patterns: Set<Set<Int>>) -> Int { | ||
let computerPositions = Set(computerMoves.map { $0.boardIndex }) | ||
|
||
for pattern in patterns { | ||
let winPositions = pattern.subtracting(computerPositions) | ||
|
||
if winPositions.count == 1 { | ||
let isAvailable = !aiVM.isSquareOccupied(in: moves, forIndex: winPositions.first!) | ||
if isAvailable { return winPositions.first! } | ||
} | ||
} | ||
|
||
return -1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters