In: Computer Science
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{
}
}
}
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!