Question

In: Computer Science

Hi had a question I am trying to develop this iOS app in Xcode in Swift...

Hi had a question I am trying to develop this iOS app in Xcode in Swift language and getting hung up on this Rock Paper Scissors game on the bottom is my code when it comes to paper and rock rock wins when it comes to rock and scissors scissors wins other than that it works ok . please help thanks !

import UIKit

class ViewController: UIViewController {

  

  

  

@IBOutlet weak var leftblankview: UIImageView!

  

  

@IBOutlet weak var rightblankview: UIImageView!

  

  

@IBOutlet weak var leftscorelabel: UILabel!

  

  

  

@IBOutlet weak var rightscorelabel: UILabel!

  

  

var leftScore = 0

var rightScore = 0

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view.

  

  

}

  

@IBAction func buttonclick(_ sender: Any) {

  

  

  

let leftNumber = Int.random(in: 1...3)

  

let rightNumber = Int.random(in: 1...3)

  

leftblankview.image = UIImage(named: "symbol\(leftNumber)")

rightblankview.image = UIImage(named: "symbol\(rightNumber)")

if leftNumber > rightNumber {

leftScore += 1

  

leftscorelabel.text = String(leftScore)

  

}

else if leftNumber < rightNumber{

rightScore += 1

  

rightscorelabel.text = String(rightScore)

}

else{

}

}

}

Solutions

Expert Solution

Ans:

// In the above program's logic which you have used for making it work, the logic part seems to be incorrect

// This is happening since after looking at your code I realized that you must be considering the following assignments:

// 1 : Rock , 2 : Paper and 3 : Scissors

// but according to the fundamentals of how the game of a Rock, Paper and Scissors game works

// You must understand that the above code if Logically incorrect

// Since in a traditional game of Rock, Paper, Scissors, the following set of rules govern the game -

// Rock beats Scissors, Scissors Cut Paper, and Paper beats Rock

// i.e 1 > 3, 3 > 2 and 2 > 1

// But according to your logic part i.e :

@IBAction func buttonclick(_ sender: Any) {

let leftNumber = Int.random(in: 1...3)

let rightNumber = Int.random(in: 1...3)

leftblankview.image = UIImage(named: "symbol\(leftNumber)")

rightblankview.image = UIImage(named: "symbol\(rightNumber)")

if leftNumber > rightNumber {

leftScore += 1

leftscorelabel.text = String(leftScore)

}

else if leftNumber < rightNumber{

rightScore += 1  

rightscorelabel.text = String(rightScore)

}

else{

}

}

}

// the random number selected in leftNumber variable and rightNumber variable is set such that

// which ever number is the greater one, that person will score

// i.e 1 < 2 < 3 but you see whenever the random number 3 gets selected, it will always win by default ( since it is the greatest of the 3 numbers

// Hence the program gives chooses the winner Scissors (#3) no matter the other choice which is Rock (#2)

// Therefore the following logic changes can be made to correct your program -

// REQUIRED LOGIC AFTER CORRECTIONS -:

@IBAction func buttonclick(_ sender: Any) {

  
let leftNumber = Int.random(in: 1...3)

  

let rightNumber = Int.random(in: 1...3)

  

leftblankview.image = UIImage(named: "symbol\(leftNumber)")

rightblankview.image = UIImage(named: "symbol\(rightNumber)")

if leftNumber == 1 {// Means left chose Rock
  
if rightNumber == 2{// right chose Paper
rightScore += 1
rightscorelabel.text = String(rightScore)
}
  
else if rightNumber == 3{// right chose scissors
leftScore += 1
leftscorelabel.text = String(leftScore)
}
  
// otherwise it's a draw!
}

else if leftNumber == 2{// left chose Paper
  
if rightNumber == 1{// right chose Rock
leftScore += 1
leftscorelabel.text = String(leftScore)
}

else if rightNumber == 3{// Right chose scissors
rightScore += 1
rightscorelabel.text = String(rightScore)
}

// otherwise it's a draw!
}

else{// left chose scissors
  
if rightNumber == 2{// right chose Paper
leftScore += 1
leftscorelabel.text = String(leftScore)
}

else if rightNumber == 1{// right chose Rock
rightScore += 1
rightscorelabel.text = String(rightScore)
}

// otherwise it's a draw!
}

}

}

// The same program can also be simulated in a more systematic manner such as this -

// A MORE SYSTEMATIC SAMPLE PROGRAM FOR SIMULATING A ROCK,PAPER and SCISSORS GAME :-

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// when the person choses Rock ( presses on Rock Button )
  
@IBAction func onRockButtonPress(_ sender: Any) {
let opponentSelection: String = makeOpponentSelection()
var outcome: String = "..."
if opponentSelection == "ROCK" {
outcome = "OPPONENT CHOSE ROCK. TIE GAME."
} else if opponentSelection == "SCISSORS" {
outcome = "OPPONENT CHOSE SCISSORS. YOU WIN!"
} else if opponentSelection == "PAPER" {
outcome = "OPPONENT CHOSE PAPER. YOU LOSE!"
}
displayDialog(with: outcome)
}
  
// when the person choses Scissors ( presses on Scissors Button )
  
@IBAction func onScissorsButtonPress(_ sender: Any) {
let opponentSelection: String = makeOpponentSelection()
var outcome: String = "..."
if opponentSelection == "ROCK" {
outcome = "OPPONENT CHOSE ROCK. YOU LOSE!"
} else if opponentSelection == "SCISSORS" {
outcome = "OPPONENT CHOSE SCISSORS. TIE GAME."
} else if opponentSelection == "PAPER" {
outcome = "OPPONENT CHOSE PAPER. YOU WIN!"
}
displayDialog(with: outcome)
}
  
// when the person choses Paper ( presses on Paper Button )
@IBAction func onPaperButtonPress(_ sender: Any) {
let opponentSelection: String = makeOpponentSelection()
var outcome: String = "..."
if opponentSelection == "ROCK" {
outcome = "OPPONENT CHOSE ROCK. YOU WIN!"
} else if opponentSelection == "SCISSORS" {
outcome = "OPPONENT CHOSE SCISSORS. YOU LOSE!"
} else if opponentSelection == "PAPER" {
outcome = "OPPONENT CHOSE PAPER. TIE GAME."
}
displayDialog(with: outcome)
}
  
func displayDialog(with dialogMessage: String) {
// REFERENCE (UIAlertViewController): https://developer.apple.com/documentation/uikit/uialertcontroller
let alert = UIAlertController(title: "", message: dialogMessage, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in
NSLog("The \"OK\" alert occured.")
}))
self.present(alert, animated: true, completion: nil)
}
// function for simulating the random choice of the computer, whether it is ROCK,SCISSORS or PAPER
func makeOpponentSelection() -> String {
var option: [String] = []
option.append("ROCK")
option.append("SCISSORS")
option.append("PAPER")
let randomNumber = Int(arc4random_uniform(3))
return option[randomNumber]
}


}

// PLEASE DO LIKE AND UPVOTE IF THIS WAS HELPFUL!

// THANK YOU SO MUCH IN ADVANCE!


Related Solutions

Intro to App Development with Swift by Apple education, Apple Education ITEC245, iOS App Development Description:...
Intro to App Development with Swift by Apple education, Apple Education ITEC245, iOS App Development Description: Write a report with your self reflection and learning outcome on below topics: 1. Playground Basics with sample program , Naming and Identifiers with sample program , Strings with sample program , First App with sample program , Functions with sample program
Intro to App Development with Swift by Apple education, Apple Education ITEC245, iOS App Development Description:...
Intro to App Development with Swift by Apple education, Apple Education ITEC245, iOS App Development Description: Write a report with your self reflection and learning outcome on below topics: 1. Playground Basics with sample program , Naming and Identifiers with sample program , Strings with sample program , First App with sample program , Functions with sample program
Implement a calculator (not postfix notation) using Swift Programming Language(Swift 3, basic ios app). Please use...
Implement a calculator (not postfix notation) using Swift Programming Language(Swift 3, basic ios app). Please use MVC model(CalculatorBrain.swift can be the model and ViewController.swift can be the view). Also please post the sreenshot of the storyboard that you make. Requirements: Implement add subtract, multiply, divide, pi, sqrt, Euler’s natural number (e), co-sine and equals. Ensure you have the ability to handle multiple operations in sequence Implement the ability to enter floating point numbers into the display Add 4 more buttons...
hot use blutooth on your app stimulation to connect to a device. xcode swift. please inclide...
hot use blutooth on your app stimulation to connect to a device. xcode swift. please inclide a code if possible.
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
Hi, I had a quick question about hydrophobic and hydrophillic regions on a molecule. I am...
Hi, I had a quick question about hydrophobic and hydrophillic regions on a molecule. I am confused on how to know if a molecule, which has both hydrophillic and hydrophobic regions, will be soluble in water or not? Can someone please explain in clear and simple words, thank you so much! For example, in the molecule vitamin A, theres OH group which is hydrophillic but the rest of the molecule is hydrophobic, so how would I know if the whole...
Hi, I am having trouble with trying to calculate asymptotic time complexity for each of these...
Hi, I am having trouble with trying to calculate asymptotic time complexity for each of these functions. A step by step tutorial would be great. This is done in Python. Please not only the answer, but how you calculated it as well. Here is the code #Q2 # to denote ayymptotic time complexity use the following notation # O(l) O(m) O(a) O(b) # e.g. traversing through l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is O(l)...
Hi, I am trying to build Wien-Bridge oscillator in PSpice and I want to produce sine...
Hi, I am trying to build Wien-Bridge oscillator in PSpice and I want to produce sine wave. The circuit seems to be correct but when I do tansient analysis the output is not produced. What should I do?
Assembly Question: I am trying to annotate this code and am struggling to understand what it...
Assembly Question: I am trying to annotate this code and am struggling to understand what it is doing. Can someone please add comments? .data .star: .string "*" .line: .string "\n" .input: .string "%d" .count: .space 8 .text .global main printstars: push %rsi push %rdi _printstars: push %rdi mov $.star, %rdi xor %rax, %rax call printf pop %rdi dec %rdi cmp $0, %rdi jg _printstars mov $.line, %rdi xor %rax, %rax call printf pop %rdi pop %rsi ret printstarpyramid: mov %rdi,...
Hi, I am taking MGMT460 and I have a question on an assignment for Work rules...
Hi, I am taking MGMT460 and I have a question on an assignment for Work rules ch.6 : What style of leadership is encouraged at Google (phrase your answer in one of the four styles described in Organizational Design, pages 160 – 166)? Explain (provide at least 3 examples from Work Rules, chapter 6).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT