Post

Replies

Boosts

Views

Activity

Reply to Calling a property of a struct with a placeholder?
Claude, I used your "else" suggestion and cleaned it up a bit Ooper, I used the same code and I think I can make it work for currentBoost. I left out the skill struct by accident, but it works when I typed it out like this var currentTraining: WritableKeyPath<Horse, Float> = \.basicTraining //or \.rhythm or \.suppleness var currentBoost: WritableKeyPath<Skill, Float> = \.basicBoost This might clean up a lot of unnecessary code, but how can I change the \.basicTraining to \.rhythm in the variable? I tried this with no luck currentTraining = \.rhythm print(currentTraining) The console reads "Swift.WritableKeyPath<Tobiano.Horse, Swift.Float>"
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’21
Reply to Number variation, but with limits
You're right about the double random, I changed one to a mean. The problem is that it's not totally random. If two individuals have 100 the offspring will have a similar result. This is what I have so far with If statements. I thought there may be a shorter way of writing it, but for now it works         let sire = myStallions[sireIndex]         let dam = myMares[damIndex]         let statVariability = Int.random(in:-12...12)         let conformationVariability = Int.random(in:-1...1)         //conformation         var foalHead = ((dam.head + sire.head)/2)+conformationVariability         if foalHead > 4 {foalHead = 4}         if foalHead < 0 {foalHead = 0}                  var foalNeck = ((dam.neck + sire.neck)/2)+conformationVariability         if foalNeck > 4 {foalNeck = 4}         if foalNeck < 0 {foalNeck = 0}                var foalBack = ((dam.back + sire.back)/2)+conformationVariability         if foalBack > 4 {foalBack = 4}         if foalBack < 0 {foalBack = 0}         var foalLegs = ((dam.legs + sire.legs)/2)+conformationVariability         if foalLegs > 4 {foalLegs = 4}         if foalLegs < 0 {foalLegs = 0}         var foalHindquarters = ((dam.hindquarters + sire.hindquarters)/2)+conformationVariability         if foalHindquarters > 4 {foalHindquarters = 4}         if foalHindquarters < 0 {foalHindquarters = 0}         var foalSaddleSize = ((dam.saddleSize + sire.saddleSize)/2)+conformationVariability         if foalSaddleSize > 4 {foalSaddleSize = 4}         if foalSaddleSize < 0 {foalSaddleSize = 0}         //stats         var foalReactivity = ((sire.reactivity + dam.reactivity)/2)+statVariability         if foalReactivity > 100 {foalReactivity = 100}         if foalReactivity < 1 {foalReactivity = 1}                  if sire.health > 95 && dam.health > 95 {             generateFoalGenotype()             checkPhenotype()             myHorses.append(Horse(                 //Base stats                 name: "New Foal",                 gender: ["Mare", "Stallion"][Int.random(in:0...1)],                 reactivity: foalReactivity,                 //Conformation                 head: foalHead,                 neck: foalNeck,                 back: foalBack,                 legs: foalLegs,                 hindquarters: foalHindquarters,                 saddleSize: foalSaddleSize,
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to Number variation, but with limits
I'll use a clearer example so it's easier to understand. The numbers represent the value of some stats that are passed to offspring when two horses are bred together. The resultant foal will have an "average" of stats from the father and the mother, but there is some variability. The foal has a small chance to be better or worse than both parents, but in general it is pretty much somewhere between the stats of the parents. none can be below 1 or above 100, however
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to Number variation, but with limits
The variability should affect the lower bound if the range goes from -12...+12, so if the result is exactly in between 20 and 30 I may get 25, and then variability can change it to anywhere from 13-37. By using an if statement I would have to do something like this; var result = (firstNumber+secondNumber)/2 +variability if result > 100 { result = 100 } if result < 0 { result = 0 } I have a lot of numbers to handle and the code will be very long if I need an if statement for each factor, because then I need a variable outside of the code for a lot of the factors. It would be messy so I thought maybe there was a better way
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20
Reply to Can you append an array with an if statement?
Here is the code: import UIKit class HorseSalesViewController: UIViewController {. //Error: Class 'HorseSalesViewController' has no initializers  override func viewDidLoad() {         super.viewDidLoad() } //Genotype     let extGenotype = ["E", "e"][Int.random(in:0...1)]+["E", "e"][Int.random(in:0...1)]     let agoGenotype = ["A", "a"][Int.random(in:0...1)]+["A", "a"][Int.random(in:0...1)]     let greGenotype = ["G", "g"][Int.random(in:0...1)]+["G", "g"][Int.random(in:0...1)]     let creGenotype = ["Cr", "cr"][Int.random(in:0...1)]+["Cr", "cr"][Int.random(in:0...1)]     let dunGenotype = ["D", "nd2"][Int.random(in:0...1)]+["D", "nd2"][Int.random(in:0...1)]     let tobGenotype = ["To", "to"][Int.random(in:0...1)]+["To", "to"][Int.random(in:0...1)]     let sabGenotype =  ["SB1", "sb1"][Int.random(in:0...1)]+["SB1", "sb1"][Int.random(in:0...1)]     let roaGenotype = ["Rn", "rn"][Int.random(in:0...1)]+["Rn", "rn"][Int.random(in:0...1)] //Phenotype     var grePhenotype : UIImage //Error: 1. Stored property 'grePhenotype' without initial value prevents synthesized initializers     switch greGenotype {&#9;//Error: Expected declaration         case "Gg" : grePhenotype = UIImage(greyArray.randomElement()!)         case "gG" : grePhenotype = UIImage(greyArray.randomElement()!)         case "GG" : grePhenotype = UIImage(greyArray.randomElement()!)         case "gg" : grePhenotype =  : blankImage         default :  grePhenotype =  : blankImage     } @IBAction func buyHorseButton(_ sender: UIButton) {         myHorses.append(Horse( //Genotype             ext: extGenotype,             ago: agoGenotype,             gre: greGenotype,             cre: creGenotype,             dun: dunGenotype,             tob: tobGenotype,             sab: sabGenotype,             roa: roaGenotype,             //Phenotype             basePhenotype :  imageLiteral(resourceName: "Blank image.png"),             roanPhenotype :  imageLiteral(resourceName: "Blank image.png"),             greyPhenotype : grePhenotype,             tobianoPhenotype :  imageLiteral(resourceName: "Blank image.png"),             sabinoPhenotype :  imageLiteral(resourceName: "Blank image.png"),             whiteFacePhenotype :  imageLiteral(resourceName: "Blank image.png"),             whiteLegsPhenotype :  imageLiteral(resourceName: "Blank image.png")) } }     
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’20
Reply to Can you append an array with an if statement?
I have the IBAction in the section below viewDidLoad. When I move it into viewDidLoad, I get errors:         case "Gg" : grePhenotype = UIImage(greyArray.randomElement()!) //No exact matches in call to initialize         case "gG" : grePhenotype = UIImage(greyArray.randomElement()!) //No exact matches in call to initialize         case "GG" : grePhenotype = UIImage(greyArray.randomElement()!) //No exact matches in call to initialize         case "gg" : grePhenotype =  : blankImage //Expected expression in assignment         default :  grePhenotype =  : blankImage //Expected expression in assignment     }     @IBAction func buyHorseButton(_ sender: UIButton) { //Only instance methods can be declared @IBAction I had to change the code to match the rest of the app so I could test it. everything should work though
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’20
Reply to Can you append an array with an if statement?
Where are you putting the switch statement? I have it just above the IBAction that triggers the append but I get an error "expected declaration" at the start of the switch statement I reworded the code to make it easier to distinguish all the different genes that will be in play later.     override func viewDidLoad() {         super.viewDidLoad()          } //Genotype let greGenotype = ["G", "g"][Int.random(in:0...1)]+["G", "g"][Int.random(in:0...1)]     //Phenotype     var grePhenotype : UIImage     switch greGenotype {         case "Gg" : grePhenotype = UIImage(greyArray.randomElement()!)         case "gG" : grePhenotype = UIImage(greyArray.randomElement()!)         case "GG" : grePhenotype = UIImage(greyArray.randomElement()!)         case "gg" : grePhenotype =  : blankImage         default :  grePhenotype =  : blankImage     } IBAction myHorses.append(Horse( gre: greGenotype, greyPhenotype : grePhenotype,))
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’20