在字符串中替换重复的子字符串只会替换其中一个。

huangapple go评论84阅读模式
英文:

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 (
&quot;fmt&quot;
&quot;strings&quot;
)
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(&quot;Welcome to hangman&quot;)
fmt.Println(&quot;Player 1, choose a word!&quot;)
fmt.Scan(&amp;word)
for i := 1; i &lt;= len(word); i++ {
unknown_word = append(unknown_word, &quot;_&quot;)
}
unknown_word_string = strings.Join(unknown_word, &quot;&quot;)
for {
fmt.Println(&quot;Player 2, guess a letter or a word&quot;)
fmt.Println(unknown_word_string)
fmt.Scan(&amp;input)
if guesses == 6 {
fmt.Println(&quot;Player 2 lost! Player 1&#39;s word was: &quot;, word)
break
} else if unknown_word_string == input {
fmt.Println(&quot;Player 1 lost! Player 2 guessed the word by guessing the letter!&quot;)
}
if strings.Contains(word, input) &amp;&amp; word != input {
index = strings.Index(word, input)
iterate()
fmt.Println(&quot;You guessed a letter!&quot;)
} else if word == input {
fmt.Println(&quot;Player 1 lost! Player 2 guessed the word by guessing the whole word!&quot;)
break
} else {
fmt.Println(&quot;Nothing found&quot;)
guesses++
}
}
}
func iterate() {
change_unknown_word_string = []byte(unknown_word_string)
for i := 0; i &lt; 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 runes (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(&quot;Player 1 lost! Player 2 guessed the word by guessing the letter!&quot;)
}

It should be if unknown_word_string == word and be located immediately after the call to iterate().

huangapple
  • 本文由 发表于 2023年1月6日 23:42:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75033047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定