我正在尝试使用Swift为iPhone创建一个单词jumbler游戏。我对编程和Swift比较陌生,但以前用Python创建过这个游戏。
这是我的伪代码:
//select RANDOM WORD from array
//determine the number of characters in the RANDOMLY SELECTED WORD
//randomly select a NUMBER within the boundries of the number of characters (i.e. if the word is "apple," select a number between 0 and 4)
//select the CHARACTER that corresponds to the randomly selected NUMBER
//add the CHARACTER to a new string (JUMBLE)
//remove the CHARCATER from the RANDOMLY SELECTED WORD
//Repeat until the RANDOM WORD is empty
到目前为止,这是我的代码:
导入UIKit
//this is my array/word bank
var words = ["Apple", "Orange", "Pear"]
//this selects a random word in the array
var selectedWord = words[Int(arc4random_uniform(UInt32(words.count)))]
//this counts the number of characters in the string
var length = (countElements(selectedWord))
//this randomly selects a position within the string
var position = Int(arc4random_uniform(UInt32(length)))
//this creates a new string consiting of the letter found in the position from the previous line of code
var subString = selectedWord[advance(selectedWord.startIndex, position)]
我的问题是我无法弄清楚如何从所选单词中删除所选字符。如果可以的话,我只需创建一个循环,然后重复上面的过程直到原始单词为空
答案 0 :(得分:5)
Swift 5或更高版本
extension RangeReplaceableCollection {
/// Returns a new collection containing this collection shuffled
var shuffled: Self {
var elements = self
return elements.shuffleInPlace()
}
/// Shuffles this collection in place
@discardableResult
mutating func shuffleInPlace() -> Self {
indices.forEach {
let subSequence = self[$0...$0]
let index = indices.randomElement()!
replaceSubrange($0...$0, with: self[index...index])
replaceSubrange(index...index, with: subSequence)
}
return self
}
func choose(_ n: Int) -> SubSequence { return shuffled.prefix(n) }
}
let words = ["python", "jumble", "easy", "difficult", "answer", "xylophone"]
let randomWordShuffled = words.randomElement()?.shuffled ?? "" // "jmeblu"
答案 1 :(得分:2)
您希望随机播放String
。
最简单的方法是:
var a = Array(selectedWord)
let shuffledString = String(a)
因此,如果您选择变异的随机播放算法:
extension Array {
mutating func shuffle() {
for i in 0..<(count - 1) {
let j = Int(arc4random_uniform(UInt32(count - i))) + i
guard i != j else { continue}
swap(&self[i], &self[j])
}
}
}
let selectedWord = "Apple"
var a = Array(selectedWord)
a.shuffle()
let shuffledWord = String(a)
// shuffledWord = “pAelp” or similar
答案 2 :(得分:2)
func scramble(word: String) -> String {
var chars = Array(word.characters)
var result = ""
while chars.count > 0 {
let index = Int(arc4random_uniform(UInt32(chars.count - 1)))
chars[index].writeTo(&result)
chars.removeAtIndex(index)
}
return result
}
Character
结构,通过上述代码中的word.characters
,具有writeTo
任意OutputStreamType
的功能,包括String
个对象。因此,在对结果字符串中随机选择Character
后,只需将其删除,然后继续循环,直到不再有字符为止。
答案 3 :(得分:1)
你可以使用许多不同的算法来改变一个单词,但这里有一个我用同样的方式编写的算法/使用与当前算法类似的代码,它重复选择并删除一个随机字符要添加到新字符串的原始字符串:
//this is my array/word bank
var words = ["Apple", "Orange", "Pear"]
//this selects a random word in the array
var selectedWord = words[Int(arc4random_uniform(UInt32(words.count)))]
// The string that will eventually hold the shuffled word
var shuffledWord:String = ""
// Loop until the "selectedWord" string is empty
while countElements(selectedWord) > 0 {
// Get the random index
var length = countElements(selectedWord)
var position = Int(arc4random_uniform(UInt32(length)))
// Get the character at that random index
var subString = selectedWord[advance(selectedWord.startIndex, position)]
// Add the character to the shuffledWord string
shuffledWord.append(subString)
// Remove the character from the original selectedWord string
selectedWord.removeAtIndex(advance(selectedWord.startIndex, position))
}
println("shuffled word: \(shuffledWord)")
答案 4 :(得分:0)
这是一个解决方案:
var myString = "abcdefg"
let shuffledString = String(Array(myString).shuffled())
说明: