Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
FredericXS committed Mar 19, 2023
1 parent 4723242 commit 2e302eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 11 additions & 0 deletions TicTacToe/Model/GameLogicModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ struct GameLogicModel {
return -1
}

func pickSecondSquare() -> Int {
let secondSquare = 1 // 1 is the second square in array [0, 1, 2...]
if !aiVM.isSquareOccupied(in: moves, forIndex: secondSquare) {
if moves.contains(where: { $0?.boardIndex == 4 && $0?.player == .computer }) {
return secondSquare
}
}

return -1
}

func pickCornerSquare() -> Int {
let corners: [Int] = [0, 2, 6, 8]
for corner in corners {
Expand Down
4 changes: 2 additions & 2 deletions TicTacToe/ViewModel/VersusAIViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ final class VersusAIViewModel: ObservableObject {
if (middleSquare != -1) { return middleSquare }

// If AI can't pick middle square because is yours, then pick a square above the middle
let secondSquare = 1 // 1 is the second square in array [0, 1, 2...]
if moves.contains(where: { $0?.boardIndex == 4 && $0?.player == .computer }) { return secondSquare }
let secondSquare = logicModel.pickMiddleSquare()
if (secondSquare != -1) { return secondSquare }

// If AI can't pick middle square because is not yours, then pick a corner square
let cornerSquare = logicModel.pickCornerSquare()
Expand Down

0 comments on commit 2e302eb

Please sign in to comment.