mirror of
https://github.com/neon443/RNG_Swift.git
synced 2026-03-11 06:49:12 +00:00
Rewrote basically half the app
+ reamde + noDebug.xcscheme + ui improvements +max die is 100+overflow protextion +fix giant padding in diceview +rewrite rng2() +ui friedlification +rewrite arrCombine
This commit is contained in:
@@ -5,36 +5,17 @@
|
||||
// Created by Nihaal on 09/10/2024.
|
||||
//
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//fix date created
|
||||
//TODO: readd ios 16 support
|
||||
//TODO: try to use inject
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct NumberView: View {
|
||||
@State var low: String = "1"
|
||||
@State var high: String = "10"
|
||||
@State var exclude: String = "none"
|
||||
@State var generated = "5"
|
||||
@State var generated = ""
|
||||
@State var prevGen = ""
|
||||
@State var generatedInt: Int = 0
|
||||
@State var prevGenInt: Int = 0
|
||||
@State var dispResult = 5
|
||||
@State var dispResult = 0
|
||||
var body: some View {
|
||||
List {
|
||||
HStack {
|
||||
@@ -65,30 +46,18 @@ struct NumberView: View {
|
||||
}
|
||||
Section("Description") {
|
||||
Text(describeInputs(min: low, max: high, exclude: exclude))
|
||||
.font(.headline)
|
||||
.fontWeight(.heavy)
|
||||
.frame(alignment: .center)
|
||||
}
|
||||
}
|
||||
Text(String(dispResult))
|
||||
.font(.system(size: 75, weight: .bold))
|
||||
Text(generated)
|
||||
.font(.system(size: 50, weight: .bold))
|
||||
.foregroundColor(.gray)
|
||||
.frame(height: 50)
|
||||
.frame(height: 40)
|
||||
Button {
|
||||
prevGen = generated
|
||||
generated = rng2(min: low, max: high, exclude: exclude)
|
||||
prevGenInt = Int(prevGen)!
|
||||
generatedInt = Int(generated)!
|
||||
if prevGenInt < generatedInt {
|
||||
for _ in prevGenInt...generatedInt {
|
||||
dispResult += 1
|
||||
//sleep( UInt32( 1 / (generatedInt - prevGenInt) ) )
|
||||
}
|
||||
} else if prevGenInt > generatedInt {
|
||||
for _ in generatedInt...prevGenInt {
|
||||
dispResult -= 1
|
||||
//sleep( UInt32( 1 / (prevGenInt - generatedInt) ) )
|
||||
}
|
||||
} else if prevGenInt == generatedInt {
|
||||
dispResult = generatedInt
|
||||
}
|
||||
generated = rng3(min: low, max: high, exclude: exclude)
|
||||
} label: {
|
||||
Text("Generate")
|
||||
.padding(.horizontal)
|
||||
@@ -96,26 +65,26 @@ struct NumberView: View {
|
||||
}
|
||||
.buttonStyle(BorderedProminentButtonStyle())
|
||||
.cornerRadius(15)
|
||||
.padding(.bottom)
|
||||
.padding(.vertical)
|
||||
}
|
||||
}
|
||||
|
||||
func describeInputs(min: String, max: String, exclude: String) -> String {
|
||||
let validExcludes = ["start", "end", "both", "none"]
|
||||
let validExcludes = ["start", "none", "end", "both"]
|
||||
guard validExcludes.contains(exclude) else {
|
||||
print("invalid exclude: " + exclude)
|
||||
return "invalid exclude: " + exclude
|
||||
}
|
||||
|
||||
var result = "Any number from "
|
||||
result += min != "" ? min : "_"
|
||||
result += min
|
||||
result += " to "
|
||||
result += max != "" ? max : "_"
|
||||
result += max
|
||||
|
||||
if min == "" || max == "" {
|
||||
return "Enter values above."
|
||||
} else {
|
||||
switch exclude {
|
||||
}
|
||||
switch exclude {
|
||||
case "start":
|
||||
result += ", excluding \(min)"
|
||||
case "end":
|
||||
@@ -126,48 +95,31 @@ func describeInputs(min: String, max: String, exclude: String) -> String {
|
||||
break
|
||||
default:
|
||||
result = "invalid 'exclude'"
|
||||
}
|
||||
}
|
||||
result += "."
|
||||
return result
|
||||
return result + "."
|
||||
}
|
||||
|
||||
enum rng2Errors: Error {
|
||||
case h
|
||||
}
|
||||
|
||||
func rng2(min: String, max: String, exclude: String) -> String {
|
||||
guard min != "" || max != "" else {
|
||||
return "Enter values above."
|
||||
func rng3(min: String, max: String, exclude: String) -> String {
|
||||
var newMin = Int(min)!
|
||||
var newMax = Int(max)!
|
||||
guard newMin <= newMax else {
|
||||
return "Invalid inputs"
|
||||
}
|
||||
guard var minInt = Int(min) else {
|
||||
print("invalid min")
|
||||
return "-1"
|
||||
guard newMin != newMax else {
|
||||
return "No numbers"
|
||||
}
|
||||
guard var maxInt = Int(max), maxInt >= minInt else {
|
||||
print("invalid max or is less than min")
|
||||
return "-1"
|
||||
guard newMin + 1 != newMax else {
|
||||
return "No numbers"
|
||||
}
|
||||
guard exclude != "both", min != max else {
|
||||
print("excluding both but min == max")
|
||||
return "No possible numbers, both bounds equal."
|
||||
}
|
||||
let minIntPlus1 = minInt + 1
|
||||
guard exclude != "both", minIntPlus1 != maxInt else {
|
||||
print("excluding both but min +1 == max")
|
||||
return "No possible numbers."
|
||||
}
|
||||
var generated = 0
|
||||
if exclude == "both" {
|
||||
maxInt -= 1
|
||||
minInt += 1
|
||||
newMax -= 1
|
||||
newMin += 1
|
||||
} else if exclude == "start" {
|
||||
minInt += 1
|
||||
newMin += 1
|
||||
} else if exclude == "end" {
|
||||
maxInt -= 1
|
||||
newMax -= 1
|
||||
}
|
||||
generated = rng(min: minInt, max: maxInt)
|
||||
return String(generated)
|
||||
return String(rng(min: newMin, max: newMax))
|
||||
}
|
||||
|
||||
#Preview {
|
||||
|
||||
Reference in New Issue
Block a user