diff --git a/Board Solver/Data Types/Board.swift b/Board Solver/Data Types/Board.swift index 43a034f..0f7d364 100644 --- a/Board Solver/Data Types/Board.swift +++ b/Board Solver/Data Types/Board.swift @@ -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 { @@ -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 diff --git a/Board Solver/Views/BufferView.swift b/Board Solver/Views/BufferView.swift index 9893524..1684a01 100644 --- a/Board Solver/Views/BufferView.swift +++ b/Board Solver/Views/BufferView.swift @@ -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) } } diff --git a/Board Solver/Views/FourInARowMenuView.swift b/Board Solver/Views/FourInARowMenuView.swift index fbf9504..afde28a 100644 --- a/Board Solver/Views/FourInARowMenuView.swift +++ b/Board Solver/Views/FourInARowMenuView.swift @@ -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() { @@ -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: { @@ -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: @@ -102,6 +104,13 @@ struct FourInARowMenuView: View }) } } + //add toggle here + + + Toggle("",isOn : $fastSolver ) + .toggleStyle(CustomToggleStyle()) + .padding() + Spacer() } } @@ -109,6 +118,36 @@ struct FourInARowMenuView: View } } +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() } diff --git a/Board Solver/Views/SolverView.swift b/Board Solver/Views/SolverView.swift index e7c8b80..10f634d 100644 --- a/Board Solver/Views/SolverView.swift +++ b/Board Solver/Views/SolverView.swift @@ -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 @@ -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 { @@ -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) diff --git a/Board Solver/Views/WordScrambleMenuView.swift b/Board Solver/Views/WordScrambleMenuView.swift index e033e0d..f7b2e2b 100644 --- a/Board Solver/Views/WordScrambleMenuView.swift +++ b/Board Solver/Views/WordScrambleMenuView.swift @@ -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 @@ -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) @@ -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: {