From 30971c4ade40da8818b1bf46b0f78c863c21f5fe Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Mon, 3 Jun 2024 20:02:54 +0100 Subject: [PATCH] implemented simpleView type in PasswordView --- DiceView.swift | 173 +++++++++++++++++++++++---------------------- MyApp.swift | 89 +++++++++++++---------- PasswordView.swift | 170 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 310 insertions(+), 122 deletions(-) diff --git a/DiceView.swift b/DiceView.swift index bbaee45..667f256 100644 --- a/DiceView.swift +++ b/DiceView.swift @@ -2,97 +2,102 @@ import SwiftUI struct DiceView: View { @State var generated: [Int] = [0] - @State var displayDies: [Int] = [] - @State var displayMultiDieMode = "" - @State var multiDieMode = "plus" - @State var result = 0 - @State var resultDescription = "" - @State var history: [Int] = [] + @State var displayDies: [Int] = [] + @State var displayMultiDieMode = "" + @State var multiDieMode = "plus" + @State var result = 0 + @State var resultDescription = "" + @State var history: [Int] = [] let columns = [GridItem(.adaptive(minimum: 25, maximum: 75))] @State var dies: Double = 1 var body: some View { - VStack { - List { - Section("Number of die") { - HStack { - Text(String(Int(dies))) - Slider(value: $dies, in: 1...10, step: 1) - } - } - Section("Multi die mode") { - Picker(selection: $multiDieMode, label: Text("ioug")) { - Image(systemName: "plus").tag("plus") - Image(systemName: "multiply").tag("multiply") - }.pickerStyle(SegmentedPickerStyle()) - } - Section("Visual") { - HStack { - Spacer() - if displayDies.isEmpty { - Text("Results are visualised here when you press the generate button") - .font(.subheadline) - } else if displayDies.count != Int(dies) { - Text("Press Generate") - .font(.title) - } else { - LazyVGrid(columns: columns, spacing: 10) { - ForEach(0.. displayDies.count ? Int.random(in: 1...6) : displayDies[index])") - .resizable() - .scaledToFit() - .frame(width: 50, height: 50) - if index != displayDies.count-1 { - Image(systemName: displayMultiDieMode) - .resizable() - .scaledToFit() - .frame(width: 10, height: 10) - } - } - }.padding() - } - Spacer() - } - } - Button("Generate") { - generated = rngN6DieArr(dies: Int(dies)) - displayDies = generated - displayMultiDieMode = multiDieMode - result = arrCombine(arr: generated, combineMode: multiDieMode) - resultDescription = describeResult(inp: displayDies, combineMode: multiDieMode) - } - Text(String(result)) - .bold() - .font(.largeTitle) - Text(resultDescription) - } - } - } + VStack { + List { + Section("Number of die") { + HStack { + Text(String(Int(dies))) + Slider(value: $dies, in: 1...10, step: 1) + } + } + Section("Multi die mode") { + Picker(selection: $multiDieMode, label: Text("ioug")) { + Image(systemName: "plus").tag("plus") + Image(systemName: "multiply").tag("multiply") + }.pickerStyle(SegmentedPickerStyle()) + } + Section("Visual") { + HStack { + Spacer() + if displayDies.isEmpty { + Text("Results are visualised here when you press the generate button") + .font(.subheadline) + } else if displayDies.count != Int(dies) { + Text("Press Generate") + .font(.title) + } else { + LazyVGrid(columns: columns, spacing: 10) { + ForEach(0.. displayDies.count ? Int.random(in: 1...6) : displayDies[index])") + .resizable() + .scaledToFit() + .frame(width: 50, height: 50) + if index != displayDies.count-1 { + Image(systemName: displayMultiDieMode) + .resizable() + .scaledToFit() + .frame(width: 10, height: 10) + } + } + }.padding() + } + Spacer() + } + } + Button("Generate") { + generated = rngN6DieArr(dies: Int(dies)) + displayDies = generated + displayMultiDieMode = multiDieMode + result = arrCombine(arr: generated, combineMode: multiDieMode) + resultDescription = describeResult(inp: displayDies, combineMode: multiDieMode) + } + HStack { + Spacer() + Text(String(result)) + .bold() + .font(.largeTitle) + Spacer() + } + Text(resultDescription) + } + .listRowSpacing(5) + } + } } func describeResult(inp: [Int], combineMode: String) -> String { - var len = inp.count-1 - var result = "" - var symbol = "" - if inp.isEmpty { - return "0" - } else { - if combineMode == "plus" { - symbol = " + " - } else if combineMode == "multiply" { - symbol = " x " - } - for i in 0...len { - if i == len { - result += String(inp[i]) - } else { - result += String(inp[i]) + symbol - } - } - result += " = " + String(arrCombine(arr: inp, combineMode: combineMode)) - return result - } + let len = inp.count-1 + var result = "" + var symbol = "" + if inp.isEmpty { + return "0" + } else { + if combineMode == "plus" { + symbol = " + " + } else if combineMode == "multiply" { + symbol = " x " + } + for i in 0...len { + if i == len { + result += String(inp[i]) + } else { + result += String(inp[i]) + symbol + } + } + result += " = " + String(arrCombine(arr: inp, combineMode: combineMode)) + return result + } } #Preview { - DiceView() + DiceView() } diff --git a/MyApp.swift b/MyApp.swift index b371f8c..f991c88 100644 --- a/MyApp.swift +++ b/MyApp.swift @@ -11,39 +11,39 @@ struct RNGApp: App { // array combiner, adds up all ints in an array func totalIntArr(arr: [Int]) -> Int { - var total = 0 - for num in arr { - total += num - } - return total + var total = 0 + for num in arr { + total += num + } + return total } // array combiner, adds up all doubles in an array func totalDoubleArr(arr: [Double]) -> Double { - var total: Double = 0 - for num in arr { - total += num - } - return total + var total: Double = 0 + for num in arr { + total += num + } + return total } // array combiner, adds or multiplies all ints in an array func arrCombine(arr: [Int], combineMode: String) -> Int { - var output = 0 - if combineMode == "plus" { - for num in arr { - output += num - } - } else if combineMode == "multiply" { - output = 1 - for num in arr { - output *= num - } - } else { - print("invalid combineMode '\(combineMode)'. must be 'plus' or 'multiply', returning 0") - return 0 - } - return output + var output = 0 + if combineMode == "plus" { + for num in arr { + output += num + } + } else if combineMode == "multiply" { + output = 1 + for num in arr { + output *= num + } + } else { + print("invalid combineMode '\(combineMode)'. must be 'plus' or 'multiply', returning 0") + return 0 + } + return output } @@ -55,25 +55,40 @@ func rng(min: Int, max: Int, step: Int) -> Int { } func rng6Die() -> Int { - return Int.random(in: 1...6) + return Int.random(in: 1...6) } func rngCDie(min: Int, max: Int) -> Int { - return Int.random(in: min...max) + return Int.random(in: min...max) } func rngN6DieArr(dies: Int) -> [Int] { - var output: [Int] = [] - for _ in 1...dies { - output.append(rng6Die()) - } - return output + var output: [Int] = [] + for _ in 1...dies { + output.append(rng6Die()) + } + return output } func rngNCDie(dies: Int, min: Int, max: Int) -> [Int] { - var output: [Int] = [] - for _ in 1...dies { - output.append(rngCDie(min: min, max: max)) - } - return output + var output: [Int] = [] + for _ in 1...dies { + output.append(rngCDie(min: min, max: max)) + } + return output +} + +func rngInt(len: Int) -> Int { + var result = "" + for _ in 1...len { + result += String(Int.random(in: 0...9)) + } + let resultInt = Int(result) + return resultInt! +} + +func rngString(len: Int) -> String { + let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + return String((1.. String { + var result = "" + if type == "4Int" { + result = String(rngInt(len: 4)) + } else if type == "6Int" { + result = String(rngInt(len: 6)) + } else if type == "4String" { + result = rngString(len: 4) + } else if type == "6String" { + result = rngString(len: 6) + } else if min != nil && max != nil { + if type == "Int" { + result = String(rngInt(len: max! - min!) + min!) + } else if type == "String" { + if min == nil { + //if there no min, use max as length + result = rngString(len: max!) + } else { + print("when type is String, leave min empty and use max as a length.") + print("returning -1") + } + } + } else { + print("invalid input") + print("type = Int: give min and max") + print("type = String: min = nil, max = length") + print("Preset: (type = 4Int/String, 6Int/String) and leave min, max = nil") + print(" 4Int: 0000-9999; 4 digit") + print(" 6Int: 000000-999999; 6 digit") + print(" 4String: a-z/0-9: _ _ _ _; 4 digit alphanumeric") + print(" 6String: a-z/0-9: _ _ _ _ _ _; 6 digit alphanumeric") + print("returning -1") + result = "-1" + } + return result +} + #Preview { PasswordView() } +#Preview { + PassCodeWordHelpView() +}