0

This probably has an easy answer but I can't find exactly what I've done so I'm just going to post the whole spiel with comments. This is the main file for a card game I'm making using a multi-dimensional array for the card deck.

class GameViewController: UIViewController
{

var royal = false
var HandMe: [[String]] = [[" ", " ", " ", " ", " "]]
var turn = "Computer"
var turns = 1
var Pot: [[String]] = [[" ", " ", " ", " ", " "]]
var Cards = [
    ["Queen", "Spades", "2", "yes", "Queen_Spades.jpg"],
    ["King", "Spades", "3", "yes", "King_Spades.jpg"],
    ["Jack", "Spades", "1", "yes", "Jack_Spades.jpg"],
    ["Ace", "Spades", "4", "yes", "Ace_Spades.jpg"],
    ["2", "Spades", "1", "no", "2_Spades.jpg"],
    ["3", "Spades", "1", "no", "3_Spades.jpg"],
    ["4", "Spades", "1", "no", "4_Spades.jpg"],
    ["5", "Spades", "1", "no", "5_Spades.jpg"],
    ["6", "Spades", "1", "no", "6_Spades.jpg"],
    ["7", "Spades", "1", "no", "7_Spades.jpg"],
    ["8", "Spades", "1", "no", "8_Spades.jpg"],
    ["9", "Spades", "1", "no", "9_Spades.jpg"],
    ["10", "Spades", "1", "no", "10_Spades.jpg"],
    ["Queen", "Hearts", "2", "yes", "Queen_Hearts.jpg"],
    ["King", "Hearts", "3", "yes", "King_Hearts.jpg"],
    ["Jack", "Hearts", "1", "yes", "Jack_Hearts.jpg"],
    ["Ace", "Hearts", "4", "yes", "Ace_Hearts.jpg"],
    ["2", "Hearts", "1", "no", "2_Hearts.jpg"],
    ["3", "Hearts", "1", "no", "3_Hearts.jpg"],
    ["4", "Hearts", "1", "no", "4_Hearts.jpg"],
    ["5", "Hearts", "1", "no", "5_Hearts.jpg"],
    ["6", "Hearts", "1", "no", "6_Hearts.jpg"],
    ["7", "Hearts", "1", "no", "7_Hearts.jpg"],
    ["8", "Hearts", "1", "no", "8_Hearts.jpg"],
    ["9", "Hearts", "1", "no", "9_Hearts.jpg"],
    ["10", "Hearts", "1", "no", "10_Hearts.jpg"],
    ["Queen", "Diamonds", "2", "yes", "Queen_Diamonds.jpg"],
    ["King", "Diamonds", "3", "yes", "King_Diamonds.jpg"],
    ["Jack", "Diamonds", "1", "yes", "Jack_Diamonds.jpg"],
    ["Ace", "Diamonds", "4", "yes", "Ace_Diamonds.jpg"],
    ["2", "Diamonds", "1", "no", "2_Diamonds.jpg"],
    ["3", "Diamonds", "1", "no", "3_Diamonds.jpg"],
    ["4", "Diamonds", "1", "no", "4_Diamonds.jpg"],
    ["5", "Diamonds", "1", "no", "5_Diamonds.jpg"],
    ["6", "Diamonds", "1", "no", "6_Diamonds.jpg"],
    ["7", "Diamonds", "1", "no", "7_Diamonds.jpg"],
    ["8", "Diamonds", "1", "no", "8_Diamonds.jpg"],
    ["9", "Diamonds", "1", "no", "9_Diamonds.jpg"],
    ["10", "Diamonds", "1", "no", "10_Diamonds.jpg"],
    ["Queen", "Clubs", "2", "yes", "Queen_Clubs.jpg"],
    ["King", "Clubs", "3", "yes", "King_Clubs.jpg"],
    ["Jack", "Clubs", "1", "yes", "Jack_Clubs.jpg"],
    ["Ace", "Clubs", "4", "yes", "Ace_Clubs.jpg"],
    ["2", "Clubs", "1", "no", "2_Clubs.jpg"],
    ["3", "Clubs", "1", "no", "3_Clubs.jpg"],
    ["4", "Clubs", "1", "no", "4_Clubs.jpg"],
    ["5", "Clubs", "1", "no", "5_Clubs.jpg"],
    ["6", "Clubs", "1", "no", "6_Clubs.jpg"],
    ["7", "Clubs", "1", "no", "7_Clubs.jpg"],
    ["8", "Clubs", "1", "no", "8_Clubs.jpg"],
    ["9", "Clubs", "1", "no", "9_Clubs.jpg"],
    ["10", "Clubs", "1", "no", "10_Clubs.jpg"]
    ]
    var last: UInt32 = UInt32(Cards.endIndex - 1)//instance member 'Cards' cannot be used on type 'GameViewController'
    var random = Int(arc4random_uniform(last) + 1)//instance member 'last' cannot be used on type 'GameViewController'
    var TopCard  = UIButton(type: .Custom)
    let image = UIImage(named: Pot[Int(last)][4])//instance member 'Pot' cannot be used on type 'GameViewController'
    TopCard.setImage(image, forState: .Normal)
janusfidel
  • 8,036
  • 4
  • 30
  • 53
  • 1
    The code using the properties has to be inside a method – dan Apr 20 '17 at 16:25
  • 1
    Or a computed property. – JAL Apr 20 '17 at 16:48
  • @JAL isn't a computed property basically a method? – mfaani Apr 20 '17 at 16:53
  • See http://stackoverflow.com/questions/25854300/how-to-initialize-properties-that-depend-on-each-other and many of your *variables* are actually constants. – vadian Apr 20 '17 at 16:55
  • You might want to take a more type safe approach to dealing with cards. It'll make working with suits/ranks much easier: https://repl.it/HS1g/2 – Alexander Apr 20 '17 at 17:35
  • @Honey Yes, yes it basically is – just a bit of syntactic sugar (but *calling* it a method would be quite confusing). – Hamish Apr 20 '17 at 18:00

2 Answers2

0

Code that uses previously defined properties must be inside a method, or inside the initializer, or inside a computed property.

class GameViewController: UIViewController {
  var royal = false
  var HandMe: [[String]] = [[" ", " ", " ", " ", " "]]
  var turn = "Computer"
  var turns = 1
  var Pot: [[String]] = [[" ", " ", " ", " ", " "]]
  var Cards = //huge array
  var last: UInt32
  var random: Int
  var TopCard = UIButton(type: .Custom)
  let image: UIImage 

  init() {
    last = UInt32(Cards.endIndex - 1)
    random = Int(arc4random_uniform(last) + 1)
    image = UIImage(named: Pot[Int(last)][4]) 
    TopCard.setImage(image, forState: .Normal)
  }
}
Pedro Castilho
  • 10,174
  • 2
  • 28
  • 39
0

Turns out I had declared all of this mess before the form even loaded. Thats why I had a million errors! Thanks for the answers!