Skip to content
69 changes: 41 additions & 28 deletions Board Solver/Data Types/Board.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Board

// Takes the detected board and the player's color and then generates a solution. Returns the solution
// as a [[Int]], whereby the solution placement cell equals 3.
public static func startSolving(board: [[Int]], playerColor: Int) -> [[Int]]
public static func startSolving(board: [[Int]], playerColor: Int, fastSolver : Bool) -> [[Int]]
{
// Check for winner
if checkForWinner(board: board) != nil {
Expand All @@ -119,42 +119,55 @@ class Board
}
}
}
var col = 0;
if(!fastSolver){
let solveString = getSolverString(board: board2, playerColor: playerColor)
if(solveString == ""){
board2[5][3] = 3
return board2
}
let pos = Position()
let solver = Solver()
let _ = pos.play(seq: solveString)
let colarray = solver.scoreAllMoves(P: pos)
col = colarray.firstIndex(of: colarray.max()!)!
if colarray[col] == -999999{
return board2
}

}
else{
//tell the AI which color it should play as
let oppositePiece = 1 - playerColor;
print("playing for ")
if(numPieces % 2 == 0){
//the aiPiece is the piece the ai will try to make win
print(playerColor)
setPlayerandAI(playerPiece: oppositePiece, aiPiece: playerColor)
}
else{
print(oppositePiece)
setPlayerandAI(playerPiece: playerColor, aiPiece: oppositePiece)
}



//reverse the board because the FastSolver views it as reversed
let reverse = Array(board2.reversed())
let bestMove = getBestMove(board: reverse, piece: 0)
col = bestMove!//store the best move
}

// *******************************************************************************
// Current solver is below this line
// DEPRACATED
// let solveString = getSolverString(board: board2, playerColor: playerColor)
// if(solveString == ""){
// board2[5][3] = 3
// return board2
// }
// let pos = Position()
// let solver = Solver()
// let _ = pos.play(seq: solveString)
// let colarray = solver.scoreAllMoves(P: pos)
// let col = colarray.firstIndex(of: colarray.max()!)!
// if colarray[col] == -999999{
// return board2
// }

// *******************************************************************************

// NOTES:
// RED is 0, YELLOW is 1, EMPTY is 2

//tell the AI which color it should play as
let oppositePiece = 1 - playerColor;
if(numPieces % 2 == 0){
//the aiPiece is the piece the ai will try to make win
setPlayerandAI(playerPiece: oppositePiece, aiPiece: playerColor)
}
else{
setPlayerandAI(playerPiece: playerColor, aiPiece: oppositePiece)
}

//reverse the board because the FastSolver views it as reversed
let reverse = Array(board2.reversed())
let bestMove = getBestMove(board: reverse, piece: 0)
let col = bestMove!//store the best move


// Here should have col variable that is the column we want to play in
// place in first open column
Expand Down
3 changes: 2 additions & 1 deletion Board Solver/Views/BufferView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ struct BufferView: View {
@State var sResultsBoard = Array(repeating: Array(repeating: Character("*"), count: 7), count: 6)
@Binding var g: String
@Binding var letters: String
@Binding var fastSolver: Bool

var body: some View {
SolverView(Classifier: ImageClassifier(), FBoardView: BoardView(board: $board), playerColor: playerColor, FBoard: $board, SBoard: $sBoard, FResultsBoard: fResultsBoard, SResultsBoard: sResultsBoard, game: $g, letters: $letters)
SolverView(Classifier: ImageClassifier(), FBoardView: BoardView(board: $board), playerColor: playerColor, FBoard: $board, SBoard: $sBoard, FResultsBoard: fResultsBoard, SResultsBoard: sResultsBoard, game: $g, letters: $letters, fastSolver : $fastSolver)
}
}
43 changes: 41 additions & 2 deletions Board Solver/Views/FourInARowMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct FourInARowMenuView: View
@State var red: Int = 0
@State var game = "four"
@State var letters: String = ""
@State var fastSolver = true

// Disable animation transitions
init()
{
Expand Down Expand Up @@ -65,7 +67,7 @@ struct FourInARowMenuView: View
{
NavigationLink
{
BufferView(playerColor: $yellow, g: $game, letters: $letters)
BufferView(playerColor: $yellow, g: $game, letters: $letters, fastSolver: $fastSolver)
.navigationBarTitle("")
.navigationBarHidden(true)
} label: {
Expand All @@ -84,7 +86,7 @@ struct FourInARowMenuView: View

NavigationLink
{
BufferView(playerColor: $red, g: $game, letters: $letters)
BufferView(playerColor: $red, g: $game, letters: $letters, fastSolver: $fastSolver)
.navigationBarTitle("")
.navigationBarHidden(true)
} label:
Expand All @@ -102,13 +104,50 @@ struct FourInARowMenuView: View
})
}
}
//add toggle here


Toggle("",isOn : $fastSolver )
.toggleStyle(CustomToggleStyle())
.padding()

Spacer()
}
}
}.navigationViewStyle(StackNavigationViewStyle())
}
}

struct CustomToggleStyle: ToggleStyle {
func makeBody(configuration: Configuration) -> some View {
VStack{
Text("Click to Change Solver Type")
.font(.custom("PatrickHandSC-Regular", size: 30))
.foregroundStyle(.black)
.offset(y: 20)

Button(action: {
configuration.isOn.toggle()
},label: {
Rectangle()
.fill(configuration.isOn ? .orange : .blue)
.frame(width: 300, height: 75)
.cornerRadius(15)
.overlay(Group{
Text(configuration.isOn ? "Fast Solver" : "Accurate Solver")
.font(.custom("PatrickHandSC-Regular", size: 40))
.foregroundStyle(configuration.isOn ? .black : .white)
RoundedRectangle(cornerRadius: 15)
.stroke(Color.black, lineWidth: 5)
})
}


)
}
}
}

#Preview {
FourInARowMenuView()
}
51 changes: 38 additions & 13 deletions Board Solver/Views/SolverView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct SolverView: View
@State var canSolve: Bool = false
@Binding var game: String
@Binding var letters: String
@Binding var fastSolver: Bool
@State private var alreadyWon = false

var body: some View
Expand Down Expand Up @@ -120,7 +121,7 @@ struct SolverView: View
{
isSolving = true
withAnimation(){
FResultsBoard = Board.startSolving(board: FBoard, playerColor: playerColor)
FResultsBoard = Board.startSolving(board: FBoard, playerColor: playerColor, fastSolver: $fastSolver.wrappedValue)
if(FResultsBoard == FBoardView.board){
alreadyWon = true
} else {
Expand Down Expand Up @@ -177,19 +178,43 @@ struct SolverView: View
}
.padding(.bottom, 50)

ProgressView("Scanning")
.progressViewStyle(CircularProgressViewStyle())
.background(.white)
.foregroundColor(.black)
.opacity(isScanning ? 0.9 : 0)
.frame(width: 150, height: 150)

ProgressView("Solving")
.progressViewStyle(CircularProgressViewStyle())
.background(.white)
.foregroundColor(.black)
.opacity(isSolving ? 0.9 : 0)
.frame(width: 150, height: 150)
ZStack {
Color.white
.cornerRadius(10)
.shadow(radius: 5)

VStack(spacing: 1) {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
Text("Scanning")
.font(.custom("PatrickHandSC-Regular", size: 20))
.foregroundColor(.black)
}
}
.frame(width: 100, height: 70)
.opacity(isScanning ? 0.8 : 0)
.scaleEffect(1.5)


ZStack {
Color.white
.cornerRadius(10)
.shadow(radius: 5)

VStack(spacing: 1) {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
Text("Solving")
.font(.custom("PatrickHandSC-Regular", size: 20))
.foregroundColor(.black)
}
}
.frame(width: 100, height: 70)
.opacity(isSolving ? 0.8 : 0)
.scaleEffect(1.5)



}
.ignoresSafeArea(.all)
Expand Down
35 changes: 18 additions & 17 deletions Board Solver/Views/WordScrambleMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct WordScrambleMenuView: View {
@State var pc = -1
@State var game = "scramble"
@State private var letters: String = ""
@State var fastSolver = true

let maxLetters = 7

Expand Down Expand Up @@ -90,22 +91,22 @@ struct WordScrambleMenuView: View {
}
.padding(.vertical, 20)
.padding(.horizontal, 30)
.overlay(
// Hidden TextField to capture input
TextField("", text: $letters)
.foregroundColor(.clear) // Make text invisible
.accentColor(.clear) // Hide cursor
.keyboardType(.alphabet)
.disableAutocorrection(true)
.autocapitalization(.none)
.onChange(of: letters) {
// Limit input to max number of letters
if letters.count > maxLetters {
letters = String(letters.prefix(maxLetters))
}
}
.padding()
)
// .overlay(
// // Hidden TextField to capture input
// TextField("", text: $letters)
// .foregroundColor(.clear) // Make text invisible
// .accentColor(.clear) // Hide cursor
// .keyboardType(.alphabet)
// .disableAutocorrection(true)
// .autocapitalization(.none)
// .onChange(of: letters) {
// // Limit input to max number of letters
// if letters.count > maxLetters {
// letters = String(letters.prefix(maxLetters))
// }
// }
// .padding()
// )
.onTapGesture {
// Bring up the keyboard when tapping the letter area
UIApplication.shared.sendAction(#selector(UIResponder.becomeFirstResponder), to: nil, from: nil, for: nil)
Expand All @@ -114,7 +115,7 @@ struct WordScrambleMenuView: View {
Spacer()
NavigationLink
{
BufferView(playerColor: $pc, g: $game, letters: $letters)
BufferView(playerColor: $pc, g: $game, letters: $letters, fastSolver: $fastSolver)
.navigationBarTitle("")
.navigationBarHidden(true)
} label: {
Expand Down