在Swift中拖拽一个字符串

我正在尝试使用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 (ie 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)] 

我的问题是我无法弄清楚如何从所选单词中删除所选字符。 如果可以,我只是创建一个循环,我重复上面的过程,直到原始单词为空

斯威夫特4

 extension Array { var shuffled: Array { var array = self indices.dropLast().forEach { guard case let index = Int(arc4random_uniform(UInt32(count - $0))) + $0, index != $0 else { return } array.swapAt($0, index) } return array } var chooseOne: Element { return self[Int(arc4random_uniform(UInt32(count)))] } } extension String { var jumble: String { return String(Array(self).shuffled) } } 

Swift 3.1

 extension Array { var shuffled: Array { var array = self indices.dropLast().forEach { guard case let index = Int(arc4random_uniform(UInt32(count - $0))) + $0, index != $0 else { return } swap(&array[$0], &array[index]) } return array } var chooseOne: Element { return self[Int(arc4random_uniform(UInt32(count)))] } } extension String { var jumble: String { return String(Array(characters).shuffled) } } 

 let myWords = ["python", "jumble", "easy", "difficult", "answer", "xylophone"] let myWordJumble = myWords.chooseOne.jumble // "ysae" 

您想要随机播放String的内容。

最简单的方法是:

  1. 将字符串转换为数组: var a = Array(selectedWord)
  2. 使用此答案中的信息来混洗该数组
  3. 将数组转换回字符串: let shuffledString = String(a)

所以,如果你选择变异的shuffle算法:

 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 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结构,通过上面代码中的writeTo ,具有writeTo任何OutputStreamType的function,包括String对象。 因此,在为结果字符串写入随机选择的Character ,只需将其删除,然后继续循环,直到不再有字符为止。

你可以使用许多不同的算法来改变一个单词,但这里有一个我用同样的方式编写的/使用与当前算法类似的代码,它重复选择并删除原始字符串中的随机字符以添加到新字符串:

 //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)")