【问题标题】:Given a list of values, how can I pick the value greater than the previous one?给定一个值列表,我如何选择大于前一个值的值?
【发布时间】:2020-03-25 07:47:04
【问题描述】:

这就是问题所在。有一个机器人会玩石头,纸,剪刀。机器人应该选择会击败其前一个动作的动作。例如:第一轮:机器人挑纸,所以第二轮机器人应该挑剪刀。等等……

到目前为止我的代码是这样的:使第一步随机移动的方法。

let randomizer = GKARC4RandomSource()
// TODO: - REFACTOR
func botRandomChoice() -> Symbol {
    let botSymbol = randomizer.nextInt(upperBound: 3) // or 2
    if botSymbol == 0 {
        print("RandomSymbol is rock")
        return .rock
    } else if botSymbol == 1 {
        print("Random Symbol is paper")
        return .paper
    } else {
        print("Random Symbol is scissor")
        return .scissor
    }
}

型号:

struct Game {
    var symbol: Symbol
    var state: GameState
}

enum Symbol: String {
    case rock = "????"
    case paper = "????"
    case scissor = "✂️"

    func outcome(botChoice: Symbol) -> GameState {
        if self == botChoice {
            return .draw
        }
        switch self {
        case .rock:
            return botChoice == .scissor ? .win : .lose
        case .paper:
            return botChoice == .rock ? .win : .lose
        case .scissor:
            return botChoice == .paper ? .win : .lose
        }
    }
}

到目前为止,我尝试了什么? 我试图想出最好的方法来做到这一点。我正在考虑一个看起来像这样的 if 或 switch 语句:

var moveNumber = 1
var symbol: Symbol
if moveNumber == 1 {
// It is the first move so pick a random choice.
let boySymbol = randomizer.nextInt(upperBound: 3)
}
if moveNumber == 2 {
// Look at previous move
let previous = botSymbol
// Use the move that would beat it.
if previous == .rock {
// play paper

我认为有更好的方法来解决或编码这个问题,但我迷路了。我什至在考虑一个链表?也许是一本字典?

【问题讨论】:

    标签: ios swift algorithm


    【解决方案1】:

    我将首先向定义规则的枚举添加一个方法,该方法可用于选择“更大”的值。此方法返回击败self的符号

    func beatenBy() -> Symbol {
        switch self {
        case .rock:
            return .paper
        case .paper:
            return .scissor
        case .scissor:
            return .rock
        }
    }
    

    现在我们也可以使用它来简化outcome 方法为

    func outcome(other: Symbol) -> GameState {
        if self == other {
            return .draw
        }
        return self.beatenBy() == other ? .lose : .win
    }
    

    我不太确定你想怎么玩游戏(用代码),所以这里有一个简单的例子,说明第一轮并使用beatenBy为第二轮做准备

    var myChoice: Symbol!
    var botChoice = botRandomChoice()
    
    myChoice = .paper
    
    let result = myChoice.outcome(other: botChoice)
    
    //set next bot choice based on winning choice
    botChoice = result == .win ? myChoice.beatenBy() : botChoice.beatenBy()
    

    【讨论】:

      【解决方案2】:

      鉴于您已经开始创建 Game 结构,我将使用它来包含所有组件以及跟踪状态。我还将介绍一个Outcome 枚举,以允许您返回.win.lose。将Symbol 的所有比较逻辑移到枚举中可以整理代码:使用nextMove 方法返回下一步,并使用`play(against:)' 方法来“玩”一个回合。

      struct Game {
         enum Outcome {
            case win
            case lose
         }
      
         enum Symbol: String, CaseIterable {
            case rock = "?"
            case paper = "?"
            case scissor = "✂️"
      
            func play(against other: Symbol) -> Outcome {
               switch self {
               case .paper: return other == .scissor ? .lose : .win
               case .rock: return other == .paper ? .lose : .win
               case .scissor: return other == .rock ? .lose : .win
               }
            }
      
            func nextMove() -> Symbol {
               switch self {
               case .paper: return .scissor
               case .rock: return .paper
               case .scissor: return .rock
               }
            }
         }
      
         var moves: Int = 0
         var move : Symbol = Symbol.allCases[Int.random(in: 0...2)] //random starting symbol
      
         mutating func playARound(with other: Symbol) -> Outcome {
            move = move.nextMove()
            moves += 1
            return move.play(against: other)
         }
      }
      

      然后你可以实例化一个游戏,并为你想玩的每一轮使用它的.playARound(with:)方法

      var game = Game()
      let result = game.playARound(with: .rock)
      

      【讨论】:

      • :@Joakim Danielson 提出了一个非常相似的解决方案,并且首先得到了它,所以给他点赞吧:-)
      • 他已经解释了平局,我忘记了!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 2016-02-13
      • 2015-12-03
      • 2012-05-29
      相关资源
      最近更新 更多