How to resolve the algorithm Rock-paper-scissors step by step in the Swift programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Rock-paper-scissors step by step in the Swift programming language

Table of Contents

Problem Statement

Implement the classic children's game Rock-paper-scissors, as well as a simple predictive   AI   (artificial intelligence)   player. Rock Paper Scissors is a two player game. Each player chooses one of rock, paper or scissors, without knowing the other player's choice. The winner is decided by a set of rules:

If both players choose the same thing, there is no winner for that round. For this task, the computer will be one of the players. The operator will select Rock, Paper or Scissors and the computer will keep a record of the choice frequency, and use that information to make a weighted random choice in an attempt to defeat its opponent.

Support additional choices   additional weapons.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Rock-paper-scissors step by step in the Swift programming language

Source code in the swift programming language

enum Choice: CaseIterable {
  case rock
  case paper
  case scissors
  case lizard
  case spock
}

extension Choice {
  var weaknesses: Set<Choice> {
    switch self {
      case .rock:
        return [.paper, .spock]
      case .paper:
        return [.scissors, .lizard]
      case .scissors:
        return [.rock, .spock]
      case .lizard:
        return [.rock, .scissors]
      case .spock:
        return [.paper, .lizard]
    }
  }
}

struct Game {
  private(set) var history: [(Choice, Choice)] = []
  private(set) var p1Score: Int = 0
  private(set) var p2Score: Int = 0

  mutating func play(_ p1Choice: Choice, against p2Choice: Choice) {
    history.append((p1Choice, p2Choice))
    if p2Choice.weaknesses.contains(p1Choice) {
      p1Score += 1
    } else if p1Choice.weaknesses.contains(p2Choice) {
      p2Score += 1
    }
  }
}

func aiChoice(for game: Game) -> Choice {
  if let weightedWeekness = game.history.flatMap({ $0.0.weaknesses }).randomElement() {
    return weightedWeekness
  } else {
    // If history is empty, return random Choice
    return Choice.allCases.randomElement()!
  }
}

var game = Game()
print("Type your choice to play a round, or 'q' to quit")
loop: while true {
  let choice: Choice
  switch readLine().map({ $0.lowercased() }) {
    case "r", "rock":
      choice = .rock
    case "p", "paper":
      choice = .paper
    case "scissors":
      choice = .scissors
    case "l", "lizard":
      choice = .lizard
    case "spock":
      choice = .spock
    case "q", "quit", "exit":
      break loop
    case "s":
      print("Do you mean Spock, or scissors?")
      continue
    default: 
      print("Unknown choice. Type 'q' to quit")
      continue
  }
  let p2Choice = aiChoice(for: game)
  print("You played \(choice) against \(p2Choice)")
  game.play(choice, against: p2Choice)
  print("Current score: \(game.p1Score) : \(game.p2Score)")
}


  

You may also check:How to resolve the algorithm Factors of an integer step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the APL programming language
You may also check:How to resolve the algorithm Shortest common supersequence step by step in the Racket programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the Java programming language
You may also check:How to resolve the algorithm Motzkin numbers step by step in the jq programming language