有没有办法让这个if语句缩短?还是更容易维护?

时间:2016-02-08 09:04:50

标签: ios iphone swift

我制作了一个(摇滚,纸张,剪刀)应用程序用于教育, 有没有办法让这个if语句缩短?

这是快速的代码段:

var playerSelection = "" // possible values are r,p,s
var cpuSelection    = "" // possible values are r,p,s
var resultString    = ""

// DECIDE PLAYER RESULT
if(playerSelection == "r")
{
    if(cpuSelection == "r")
    {
        resultString = "draw"
    }
    else if(cpuSelection == "p")
    {
        resultString = "lose"
    }
    else if(cpuSelection == "s")
    {
        resultString = "win"
    }
}
else if(playerSelection == "p")
{
    if(cpuSelection == "r")
    {
        resultString = "win"
    }
    else if(cpuSelection == "p")
    {
        resultString = "draw"
    }
    else if(cpuSelection == "s")
    {
        resultString = "lose"
    }
}
else if(playerSelection == "s")
{
    if(cpuSelection == "r")
    {
        resultString = "lose"
    }
    else if(cpuSelection == "p")
    {
        resultString = "win"
    }
    else if(cpuSelection == "s")
    {
        resultString = "draw"
    }
}

据我所知,这是教育学生如何制作它的最简单形式,你有什么想法吗?

非常感谢

5 个答案:

答案 0 :(得分:9)

怎么样:

if (playerSelection == "r" && cpuSelection == "r") || (playerSelection == "p" && cpuSelection == "p") || (playerSelection == "s" && cpuSelection == "s") {
    resultString = "draw"
} else if (playerSelection == "r" && cpuSelection == "p") || (playerSelection == "p" && cpuSelection == "s") || (playerSelection == "s" && cpuSelection == "r") {
    resultString = "lose"
} else {
    resultString = "win"
}

//编辑,由@Quince建议:

    if (playerSelection == cpuSelection) {
        resultString = "draw"
    } else if (playerSelection == "r" && cpuSelection == "p") || (playerSelection == "p" && cpuSelection == "s") || (playerSelection == "s" && cpuSelection == "r") {
        resultString = "lose"
    } else {
        resultString = "win"
    }

答案 1 :(得分:9)

let winArray = ["rs", "pr", "sp"]

func getResult(playerSelection:String, cpuSelection:String) -> String {
    if playerSelection == cpuSelection {
        return "draw"
    }
    if winArray.contains(playerSelection + cpuSelection) {
        return "win"
    } else {
        return "lose"
    }
}
resultString = getResult(playerSelection, cpuSelection)

使用枚举来保证安全:

enum GameResult {
    case Win
    case Draw
    case Lose
}

enum Selection {
    case Rock
    case Paper
    case Scissor
}

let winArray:[(Selection, Selection)] = [(.Rock, .Paper), (.Paper, .Scissor), (.Scissor, .Paper)]

func getResult(playerSelection:Selection, cpuSelection:Selection) -> GameResult {
    if playerSelection == cpuSelection {
        return .Draw
    }
    if winArray.contains({$0.0 == playerSelection && $0.1 == cpuSelection}) {
        return .Win
    } else {
        return .Lose
    }
}
let playerSelection:Selection = .Rock
let cpuSelection:Selection = .Scissor
let gameResult = getResult(playerSelection, cpuSelection: cpuSelection)

答案 2 :(得分:6)

您应该使用枚举来管理数据值。您有3种可能性,但您正在使用显式字符串文字。枚举也会促进转换案例陈述,在您的案例中更清晰。

由于你有一组静态的选项,你也并不总是需要测试它们中的每一个 - 如果前两个不是真的那么第三个必须是。所以你可以认为这是一场胜利并且可以测试失败或平局。

答案 3 :(得分:5)

尝试使用嵌套的三元运算符,使用下面的代码

在一行中检查
resultString = ( playerSelection == "r") ? (cpuSelection == "r" ? "draw" : (cpuSelection == "p") ? "lose" : "win") : ((( playerSelection == "p") ? (cpuSelection == "r" ? "win" : ((cpuSelection == "p") ? "draw" : "lose")):((cpuSelection == "r" ? "lose" : ((cpuSelection == "p") ? "win" : "draw")))))

你可以使用枚举和数组循环逻辑。

请检查以下代码。

enum GameResult : Int {
    case None = 0
    case Win = 1
    case Draw = 2
    case Lose = 3
}

enum Selection : Int{

    case Paper = 1
    case Rock = 2
    case Scissor = 3
}

var pSelect = Selection.Paper;
var cSelect = Selection.Rock;
var result = GameResult.None

var playerRotation = (pSelect.rawValue) % 3 + 1;

if(pSelect.rawValue == cSelect.rawValue){
    result = GameResult.Draw
}
else if(playerRotation == cSelect.rawValue){
    result = GameResult.Win
}
else{
    result = GameResult.Lose
}

答案 4 :(得分:1)

这个怎么样:

let scores = ["s", "r", "p"]
let playerScore = scores.indexOf(playerSelection)!
let cpuScore = scores.indexOf(cpuSelection)!
resultString = playerScore > cpuScore ? "win" : playerScore == cpuScore ? "draw" : "lose"

或者在这个特定的背景下(非常棘手):

resultString = playerSelection < cpuSelection ? "win" : playerSelection == cpuSelection ? "draw" : "lose"