A BingoMachine class using Swift

Setting up the project

Create a OSX command line application, using Swift as the language. Once the project is created, create a new Swift file (using File - New - File) and name it BingoMachine.swift

Creating the class and methods

  1. Inside the BingoMachine.swift file, under the import statement, add the following code to declare a new class:
    class BingoMachine {
    }
  2. Next add the following lines of code, inside the braces, to determine properties of the class - these will be the tumbler (which contains the Bingo machine's ball at the start of the game), and a board (which contains the called numbers and would allow us to check them). Add code as shown here:
    //declare a tumbler (keep private)
    private var tumbler: [Int]
    //declare the board (this will be public)    
    var calledNumbers: [Int]

    This declares two arrays of integers (but does not create them)

  3. In order for to be able to use the arrays, we need to create (instantiate) them, and we also need to populate the tumbler with numbers 1 to 90. Add the function shown below, inside the class, to do both these things:
    init(){
        //instantiate the tumbler and called numbers arrays
        tumbler = [Int]()
        calledNumbers = [Int]()
            
        //add balls to tumbler
        for i in 1...90{
        	tumbler.append(i)
        }
    }

    Whenever we create a new BingoMachine using BingoMachine() the init method will be called, instantiating the two arrays, and placing the numbers in the tumbler

  4. The next method will enable us to get a number from the tumbler at random. There is, unfortunately, a bit of converting between types of integer necessary. Add the code below to enable the instance of the bingo machine to return a number:
    func getNumber() -> Int{
            
        //to use the random function, which requires an unsigned 32 bit integer,
        //we need to convert the number of balls in the tumbled to this format      
        let positionUInt = UInt32(tumbler.count)
            
        //then convert the result of calling the random number function
        //back to Swift's 'Int' type (64 bit signed)
        let position = Int(arc4random_uniform(positionUInt))
            
        //get the number at the random position
        let number = tumbler[position]
            
        //remove it from the tumbler array
        tumbler.removeAtIndex(position)
            
        //place it in the called numbers array
        calledNumbers.append(number)
        //return the number
        return number        
    }
  5. There is a slight problem with our code in that, it would be possible to call the get number method even if the tumbler array was empty. In order to provide a way to check if numbers remain, we can add a method which simply checks there are more than 0 items in the array, and returns a true or false value. Add the function below to do that:
    func ballsRemaining() -> Bool{
            
        return tumbler.count > 0   
    }
  6. We should now be able to use the Bingo machine. Open the main.swift file, delete the existing code below the line starting import, and add the following code:
    //create a new instance of the BingoMachine class
    var bingoMachine = BingoMachine()
    //while there are balls remaining
    while bingoMachine.ballsRemaining(){
        //print out the number, followed by a comma and space
        print(bingoMachine.getNumber())
        print(", ")
    }

  7. Run the application, and you should see the numbers 1 to 90, printed out in the console, in a random order. Run it again, and the order should be different.

A method to check a number has been called

It's likely that we may want to check if a number has been called - we can do this by checking for its existence in the calledNumbers array. One way to do so would be to loop the the array and compare values, however swift provides a generic function to check if a sequence contains an value, the method is simply contains(). Add the function shown below to the BingoMachine class to do just that:

func checkNumberCalled(number: Int) -> Bool{
        
    return calledNumbers.contains(number)
}

To test this add the code below to the bottom of the while loop in main.swift:

let luckyNumber = 7;
if bingoMachine.checkNumberCalled(luckyNumber){
    print("Lucky number \(luckyNumber) found")
    break
}

Further tasks