Question

hello need help with making a simple game application in Xcode with using the swift language...

hello need help with making a simple game application in Xcode with using the swift language . any help would appreciate it . thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the solution:

Rock, Paper, Scissor game to play with computer that will show the your score, computer score and draw score how many times.

create a view on the IOS Main.storyboard:

In a view controller file:

import UIKit
import GameplayKit

class RPSVC: UIViewController {
    //declare the variable
    var drawScoreInt : Int = 0
    var userScoreInt : Int = 0
    var computerScoreInt : Int = 0
    var choice : String! //choice string will have the choice
    let randomChoice = GKRandomDistribution(lowestValue: 0, highestValue: 2) //random number
    //create an button and label
    @IBOutlet weak var rockBtn: UIButton!
    @IBOutlet weak var paperBtn: UIButton!
    @IBOutlet weak var scissorsBtn: UIButton!
    @IBOutlet weak var computerChoice: UILabel!
    @IBOutlet weak var statusLabel: UILabel!
    @IBOutlet weak var userChoice: UILabel!
    @IBOutlet weak var userScore: UILabel!
    @IBOutlet weak var drawScore: UILabel!
    @IBOutlet weak var computerScore: UILabel!
  
  
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    //button press event of rock
    @IBAction func rockBtnPressed(_ sender: Any) {
        choice = "??"
        getResult()
    }
    //button press event of paper
    @IBAction func paperBtnPressed(_ sender: Any) {
        choice = "✋?"
        getResult()
    }
    //button press event of rock
    @IBAction func scissors(_ sender: Any) {
        choice = "✌?"
        getResult()
      
    }
    //find the result
    func getResult() {
        paperBtn.isHidden = true
        scissorsBtn.isHidden = true
        rockBtn.isHidden = true
        userChoice.isHidden = false
        userChoice.text = choice
        computerChoice.text = randomSign() //get the random selection
        statusLabel.text = calculateResult(user: choice, computer: computerChoice.text!) //call the function to calculate the result
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.7, execute: {
            self.resetItems() //reset the all items
        })
    }
    //check for the which sgn has selected
    func randomSign() -> String { //return a String
        let sign = randomChoice.nextInt()
        if sign == 0 { // return rock sign
            let rock : String = "??"
            return rock
        } else if sign == 1 { //return paper
            let paper : String = "✋?"
            return paper
        } else { //return scissor
            let scissors : String = "✌?"
            return scissors
        }
    }
    //function will calculate the result and return as a string
    func calculateResult(user: String , computer: String) -> String {
        if user == computer { //check for the user selection and computer selection is same
            drawScoreInt = drawScoreInt + 1 //add the draw sore
            updateScores() //update score
            return "Draw!" //return the draw string
        } else if (user == "??" && computer == "✋?") || (user == "✋?" && computer == "✌?") || (user == "✌?" && computer == "??") { //check for the comuter win
            UIView.animate(withDuration: 0.2, animations: { () -> Void in //animation 2 secnds
                self.view.backgroundColor = #colorLiteral(red: 0.9058823529, green: 0.2980392157, blue: 0.2352941176, alpha: 1) //background color to red to 2 seconds
            })
            whiteLabels() //call the function
            computerScoreInt = computerScoreInt + 1 //add the computer sore
            updateScores()
            return "You Lose" //return the string
        } else {
            UIView.animate(withDuration: 0.2, animations: { () -> Void in
                self.view.backgroundColor = #colorLiteral(red: 0.1529411765, green: 0.6823529412, blue: 0.3764705882, alpha: 1) //background color to green to 2 seconds
            })
            whiteLabels() // call the function
            userScoreInt = userScoreInt + 1 //add the user sore
            updateScores()
            return "You win!" //return the string
        }
    }
  
    func whiteLabels() {
        UIView.animate(withDuration: 0.2, animations: { () -> Void in
            //set the all label text to white
            self.userScore.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
            self.computerScore.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
            self.drawScore.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
            self.statusLabel.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        })
    }
  
    func resetItems() { //reset the items of the UIView
        UIView.animate(withDuration: 0.2, animations: { () -> Void in
            self.view.backgroundColor = #colorLiteral(red: 0.9960784314, green: 0.8039215686, blue: 0.2470588235, alpha: 1) //yellow
            self.statusLabel.textColor = #colorLiteral(red: 0.1706000417, green: 0.1706000417, blue: 0.1706000417, alpha: 1) //transparent
            self.userScore.textColor = #colorLiteral(red: 0.1706000417, green: 0.1706000417, blue: 0.1706000417, alpha: 1) //transparent
            self.computerScore.textColor = #colorLiteral(red: 0.1706000417, green: 0.1706000417, blue: 0.1706000417, alpha: 1) //transparent
            self.drawScore.textColor = #colorLiteral(red: 0.1706000417, green: 0.1706000417, blue: 0.1706000417, alpha: 1)//transparent
        })
        paperBtn.isHidden = false //hide the button
        scissorsBtn.isHidden = false //hide the button
        rockBtn.isHidden = false //hide the button
        userChoice.isHidden = true
        computerChoice.text = "?"
        statusLabel.text = "Rock , Paper , Scissors?"
    }
  
    func updateScores() { //display the score to the user, computer and draw score
        userScore.text = "You: \(Int(userScoreInt))"
        drawScore.text = "Draw: \(Int(drawScoreInt))"
        computerScore.text = "Computer: \(Int(computerScoreInt))"
    }
  
    override var prefersStatusBarHidden: Bool {
        return true
    }
}

sample output:

Add a comment
Know the answer?
Add Answer to:
hello need help with making a simple game application in Xcode with using the swift language...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT