英文:
Replacing duplicate substrings in a string replaces only one of them
问题
我正在为您翻译以下代码:
package main
import (
"fmt"
"strings"
)
var input string
var word string
var unknown_word []string
var unknown_word_string string
var index int
var guesses int
var change_unknown_word_string []byte
func main() {
fmt.Println("欢迎来到猜词游戏")
fmt.Println("玩家1,请选择一个单词!")
fmt.Scan(&word)
for i := 1; i <= len(word); i++ {
unknown_word = append(unknown_word, "_")
}
unknown_word_string = strings.Join(unknown_word, "")
for {
fmt.Println("玩家2,请猜一个字母或一个单词")
fmt.Println(unknown_word_string)
fmt.Scan(&input)
if guesses == 6 {
fmt.Println("玩家2输了!玩家1的单词是:", word)
break
} else if unknown_word_string == input {
fmt.Println("玩家1输了!玩家2通过猜测字母猜到了单词!")
}
if strings.Contains(word, input) && word != input {
index = strings.Index(word, input)
iterate()
fmt.Println("你猜对了一个字母!")
} else if word == input {
fmt.Println("玩家1输了!玩家2通过猜测整个单词猜到了单词!")
break
} else {
fmt.Println("没有找到匹配的内容")
guesses++
}
}
}
func iterate() {
change_unknown_word_string = []byte(unknown_word_string)
for i := 0; i < len(change_unknown_word_string); i++ {
if change_unknown_word_string[i] == change_unknown_word_string[index] {
change_unknown_word_string[i] = []byte(input)[0]
}
}
unknown_word_string = string(change_unknown_word_string)
}
这段代码是一个简单的猜词游戏,当玩家猜对一个字母时,程序会将对应位置的 "_" 替换为正确的字母。然而,如果单词中有重复的字母,只有一个会被替换。您创建了一个名为 iterate
的函数来遍历所有索引,但它没有起作用。
英文:
I'm making a simple hangman game in Go, but I have come across an error, the unknow_string string (will be shown in the code) has _ characters so the other player guessing the letters can see the length of the word, whenever the player enters a correct letter, I want it to replace the nth (n depending on which index the letter is found in the word) _ character with the letter, which has been successful, but with one problem. If the word has two duplicate letters, only one of them is replaced.
I created a separate function called iterate (function because I want to avoid nested code), to iterate over all indexes. but it didn't work, here's the code:
package main
import (
"fmt"
"strings"
)
var input string
var word string
var unknown_word []string
var unknown_word_string string
var index int
var guesses int
var change_unknown_word_string []byte
func main() {
fmt.Println("Welcome to hangman")
fmt.Println("Player 1, choose a word!")
fmt.Scan(&word)
for i := 1; i <= len(word); i++ {
unknown_word = append(unknown_word, "_")
}
unknown_word_string = strings.Join(unknown_word, "")
for {
fmt.Println("Player 2, guess a letter or a word")
fmt.Println(unknown_word_string)
fmt.Scan(&input)
if guesses == 6 {
fmt.Println("Player 2 lost! Player 1's word was: ", word)
break
} else if unknown_word_string == input {
fmt.Println("Player 1 lost! Player 2 guessed the word by guessing the letter!")
}
if strings.Contains(word, input) && word != input {
index = strings.Index(word, input)
iterate()
fmt.Println("You guessed a letter!")
} else if word == input {
fmt.Println("Player 1 lost! Player 2 guessed the word by guessing the whole word!")
break
} else {
fmt.Println("Nothing found")
guesses++
}
}
}
func iterate() {
change_unknown_word_string = []byte(unknown_word_string)
for i := 0; i < len(change_unknown_word_string); i++ {
if change_unknown_word_string[i] == change_unknown_word_string[index] {
change_unknown_word_string[i] = []byte(input)[0]
}
}
unknown_word_string = string(change_unknown_word_string)
}
答案1
得分: 0
比较if change_unknown_word_string[i] == change_unknown_word_string[index]
没有意义,因为unknown_word_string
在这些位置上显然包含_
。
你应该在循环中比较word[i] == input[0]
。
但请注意,将string
转换为byte
数组不符合Unicode规范。最好使用rune
(Unicode码点)来处理,这样你就不仅限于Latin1字符。
func iterate() {
needle := []rune(input)[0]
haystack := []rune(word)
buf := []rune(unknown_word_string)
for i := range haystack {
if haystack[i] == needle {
buf[i] = needle
}
}
unknown_word_string = string(buf)
}
额外提示:这个比较是错误的
if unknown_word_string == input {
fmt.Println("Player 1 lost! Player 2 guessed the word by guessing the letter!")
}
应该是if unknown_word_string == word
,并且应该紧跟在对iterate()
的调用之后。
英文:
The comparison if change_unknown_word_string[i] == change_unknown_word_string[index]
makes no sense, as unknown_word_string
obviously contains _
at those positions.
You should be comparing word[i] == input[0]
in the loop instead.
But note that converting a string
to a byte
array is not Unicode-friendly. It's better to work with rune
s (Unicode code points) instead, this way you're not limited to latin1 characters.
func iterate() {
needle := []rune(input)[0]
haystack := []rune(word)
buf := []rune(unknown_word_string)
for i := range haystack {
if haystack[i] == needle {
buf[i] = needle
}
}
unknown_word_string = string(buf)
}
Bonus note: this comparison is wrong
if unknown_word_string == input {
fmt.Println("Player 1 lost! Player 2 guessed the word by guessing the letter!")
}
It should be if unknown_word_string == word
and be located immediately after the call to iterate()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论