Ios 以迅捷的速度移动字符串
我正在尝试使用Swift为iPhone创建一个单词混乱游戏。我对编程和Swift相对来说比较陌生,但之前已经用Python创建了这个游戏 这是我的伪代码:Ios 以迅捷的速度移动字符串,ios,swift,Ios,Swift,我正在尝试使用Swift为iPhone创建一个单词混乱游戏。我对编程和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 charac
//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)]
我的问题是,我不知道如何从所选单词中删除所选字符。如果可以的话,我只需要创建一个循环,重复上面的过程,直到原始单词为空,但这里有一个我用与当前算法相同的方式编写的代码,它重复地从原始字符串中选择并删除一个随机字符以添加到新字符串中:
//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)")
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) }
}
您想洗牌
字符串的内容
最简单的方法是:
将字符串转换为数组: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
扩展数组{
变异func shuffle(){
因为我在0。。
在上述代码中,通过word.characters
的Character
结构具有writeTo
任何OutputStreamType
的功能,包括String
对象。因此,在将随机选择的字符
写入结果字符串后,只需通过其索引将其删除,并让循环继续继续使用,直到不再剩下字符。这里有一个解决方案:
var myString = "abcdefg"
let shuffledString = String(Array(myString).shuffled())
说明:
将myString转换为字符数组
洗牌
将数组转换回字符串
您想在哪里删除字符?请在代码中指出这一点好吗?因此,基本上您只是在尝试混合字符串中的字符?确切地说,我正在尝试混合随机选择的字符串中的字符。我最初的想法是创建一个循环,从字符串中随机选择字符,直到字符串为e空的,但我愿意用另一种方式来做。嗨,我在一个应用程序中使用了相同的代码,但在xCode 7.1中它抛出并错误地说“不支持将一个位置与其自身交换。你看到修复方法了吗?是的,他们在最近的版本中添加了一个稍微迂腐的断言。我添加了一个应该避免击中它的防护。谢谢-我喜欢它,但这有一个错误,它总是保持最后一个数字不变。要修复,请删除“-1”在chars.count.arc4random_之后,统一已返回一个小于上限的数字,因此不需要减去一
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
}
var myString = "abcdefg"
let shuffledString = String(Array(myString).shuffled())