Coding score rules

Hi. I´m pretty new to coding and app developing. The last few week i have read about it and watched tutorials and i think i am starting to get a grasp of the basics, but seeing as i am new to this there are things i dont understand.


Lets say i am making a card type of game. A game where you get points by "linking" two cards. By linking i mean you throw a card on top of another card. How do i make a rule/statement saying if one links hearts with heart one gets 6 points, while if you link hearts with another type that is not hearts you only get 3 points. And another rule saying that if you link two cards with the same numbers you get six points as well.


I am not producing this card type of game, but the game that i am producing needs these kinds of codes.


If someone needs me to explain a bit better, i will try to do that!

Something like this, perhaps?


for i in 1..<array.count {
     let currentCard = array[i], previousCard = array[i - 1];
     if currentCard.suit == previousCard.suit {
          addPoints(6)
     } else if currentCard.value == previousCard.value {
          addPoints(3)
     }
}
    

I understand everything from line 3 and down (the if statement), but i don´t fully understand the first part. Care to explain?


class HeartsofTwo {
   
    let suit = "Hearts"
    let value = "2"
   
}
let heartsoftwo = HeartsofTwo()
heartsoftwo.suit
heartsoftwo.value

class HeartsofThree {
   
    let suit = "Hearts"
    let value = "3"
   
}
let heartsofthree = HeartsofThree()
heartsofthree.suit
heartsofthree.value

class SpadesofTwo {
   
    let suit = "Spades"
    let value = "2"
   
}
let spadesoftwo = SpadesofTwo()
spadesoftwo.suit
spadesoftwo.value

class SpadesofThree {
   
    let suit = "Spades"
    let value = "3"
   
}
let spadesofthree = SpadesofThree()
spadesofthree.suit
spadesofthree.value

class ClubsofTwo {
   
    let suit = "Clubs"
    let value = "2"
   
}
let clubsoftwo = ClubsofTwo()
clubsoftwo.suit
clubsoftwo.value

class ClubsofThree {
   
    let suit = "Clubs"
    let value = "3"
   
}
let clubsofthree = ClubsofThree()
clubsofthree.suit
clubsofthree.value

class DiamondsofTwo {
   
    let suit = "Diamonds"
    let value = "2"
   
}
let diamondsofTwo = DiamondsofTwo()
diamondsofTwo.suit
diamondsofTwo.value

class DiamondsofThree {
   
    let suit = "Diamonds"
    let value = "3"
   
}
let diamondsofthree = DiamondsofThree()
diamondsofthree.suit
diamondsofthree.value


This is how i plan to register all of the cards^^ Is this the best way or do you recommend an other way?


Is it someway possible to test if the code you wrote for me works in swift playground?


thanks for replying!

Doing all of the cards that way will take forever. You only had eight of the 52 cards in a deck. Besides, it's going to be incredibly difficult to do the kind of comparisons you're thinking about if every kind of card is a different class. You should probably do it like this, with just one Card class:


enum CardSuit: Int {
  case Diamonds = 0, Clubs, Hearts, Spades
}
enum CardValue: Int {
  case Two = 2, Three ,Four , Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace
}
class Card {
  var value: CardValue
  var suit: CardSuit

  init(value initialValue: CardValue, suit initialSuit: CardSuit) {
       value = initialValue
       suit = initialSuit
  }
}

//Now look at how easy it is to create individual cards
let twoOfHearts = Card(value: .Two, suit: .Hearts)
let threeOfHearts = Card(value: .Three, suit: .Hearts)
let twoOfSpades = Card(value: .Two, suit: .Spades)
let threeOfSpades = Card(value: .Three, suit: .Spades)
let twoOfClubs = Card(value: .Two, suit: .Clubs)
let threeOfClubs = Card(value: .Three, suit: .Clubs)
let twoOfDiamonds = Card(value: .Two, suit: .Diamonds)
let threeOfDiamonds = Card(value: .Three, suit: .Diamonds)

let array = [twoOfHeards, threeOfSpades, twoOfClubs, twoOfDiamonds]




Here's my earlier code:

for i in 1..<array.count {
     let currentCard = array[i], previousCard = array[i - 1];
     if currentCard.suit == previousCard.suit {
          addPoints(6)
     } else if currentCard.value == previousCard.value {
          addPoints(3)
     }
}


I'll start with line 1. The keyword "for" indicates a loop, and "i" is the number indicating the current iteration (pass through) the loop. The "i in 1..<array.count" means that the loop runs from i = 1 and stops just short of reaching the total number of cards in the array. Normally you'd start out with i = 0 because arrays' first objects are at index 0, but I'm starting at 1 here because you're comparing two cards in order and the first card has nothing above it to compare with. I end at the array's count minus one because, again, arrays start at 0. If you have an array with five cards, the first one will be at 0 and the last one will be at 4.


Now, for line 2. Not much going on there—all I do is grab the current card ( array[i] ) and the one right before it ( array[i - 1] ) so I can compare them more easily. I could have used two separate lines to do the same thing (which probably would have been less confusing for you—sorry 🙂)


let currentCard = array[i]
let previousCard = array[i - 1];


Does all of that make sense? It's perfectly fine to be 🙂 or 😐 or 😕 or even 😮 — these concepts are not exactly the most intuitive…

Much of it is making sense to me 🙂 but would you care to explain the logic of the array at line 27? Is the plan to "store" all the cards in that array?


And after writing evertyhing you have written in Playground I get 2 errors at the lines where it is written addPoints(6) and addPoints(3) saying "Use of unresolved identifier ´addPoints´ "


and do you know if there is anyway to test if the code worked in Playground. I mean, if I for example wrote heartsOfTwo + heartsOfThree i would get 6 points written on the right side. Something like that.


Thanks again for sharing your knowledge, very much appreciated 🙂

I use the array to simulate a deck or a hand of cards. Since the pattern-checking code with the loop used an array, I created one in line 27 to link the two pieces of code. That's all.


The reason you couldn't get addPoints() to work is that I just used a hypothetical example. You'd have to replace that with whatever class is responsible for keeping track of the points. You could always write the addPoints() function in the playground to test it 🙂


Now about doing something like heartsOfTwo + heartsOfThree to get 6 points, it's possible in Swift, but I'd recommend against trying to do it that way because it doesn't stand to reason that you'd be able to add cards like you add numbers. That would be really confusing to someone else or even to you if you were to look at the code again after a long time. Far better would be to just use a traditional function.

Now, after creating all of the cards in a deck and the array to simulate it, how would a class responsible for keeping track of the points look like?


class addPoints { }

Just like that? ^^


And how would the addPoints() function look like?

Accepted Answer

I'd do it like this:


class GameController {
     var points = 0

     func addPoints(newPoints:Int) {
          points += newPoints
     }
}

let controller = GameController()
controller.addPoints(6)

Ok, so after writing all of that the initial code you wrote would be changed to this?

for i in 1..<array.count {
    let currentCard = array [i], previousCard = array[i - 1];
    if currentCard.suit == previousCard.suit {
        GameController(6)
    } else if currentCard.value == previousCard.value {
        GameController(3)
    }
}


if so, it still says errors on the lines of GameController(), "Cannot convert value of type int to expected argument type ()"

No, like this:

let controller = GameController()

for i in 1..<array.count {
    let currentCard = array [i], previousCard = array[i - 1];
    if currentCard.suit == previousCard.suit {
        controller.addPoints(6)
    } else if currentCard.value == previousCard.value {
        controller.addPoints(3)
    }
}


Line 1 creates the game controller object, using the GameController() call; lines 6 and 8 call that controller object to add points to it.

Great, thanks 🙂

Glad to hear. You should probably read the iBooks guide about Swift for more information about where to go from here. Here's the link:

https://geo.itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11


Good luck with your future work! 🙂

Thanks! I¨ll do that 🙂

Coding score rules
 
 
Q