Golang中Blackjack AI遇到MoveHit类型不匹配错误该如何解决
Golang中Blackjack AI遇到MoveHit类型不匹配错误该如何解决 我刚刚完成了Blackjack AI的“加倍”视频。
就我所见,我在humanAI上实现的Play方法与视频中的相同。
func (ai humanAI) Play(hand []deck.Card, dealer deck.Card) Move {
for {
fmt.Println("Player:", hand)
fmt.Println("Dealer:", dealer)
fmt.Println("What will you do? (h)it, (s)tand, (d)ouble")
var input string
fmt.Scanf("%s\n", &input)
switch input {
case "h":
return MoveHit // <- 注意这里
case "s":
return MoveStand // <- 这里
case "d":
return MoveDouble // <- 还有这里
default:
fmt.Println("Invalid option:", input)
}
}
}
在game.go的Game.Play方法中,当我赋值err := move(g)时,遇到了move(g) (no value) used as value的错误。
move(g) (no value) used as value
对上述错误进行一些研究表明,我需要从ai.Play返回一些内容。但我已经在其中返回了Move。
但在这些地方:
case "h":
return MoveHit
case "s":
return MoveStand
case "d":
return MoveDouble
我遇到了:
cannot use MoveHit (value of type func(g *Game) error) as Move value in return statement
请指出我做错了什么?
这里是完整的ai.go和game.go的链接:https://github.com/santosh/gophercises/tree/7249c69822e759138f7059905ca2c25b4c5a3541/blackjack
更多关于Golang中Blackjack AI遇到MoveHit类型不匹配错误该如何解决的实战教程也可以访问 https://www.itying.com/category-94-b0.html
我不知道gophercises,我只是指出你代码中的不一致之处。类型不匹配。
更多关于Golang中Blackjack AI遇到MoveHit类型不匹配错误该如何解决的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Move 被定义为一个接收 Game 参数且不返回任何内容的函数,但你返回的函数确实会返回一个 error。你需要更改你的类型定义或函数实现。
在“翻倍”练习中,引入了返回错误。我正尝试在不复制粘贴的情况下,尽可能地匹配它们。
根据你的代码和错误信息,问题在于类型定义不匹配。Move 类型被定义为函数类型,但你的 humanAI.Play 方法试图返回函数值而不是调用结果。
首先,让我们检查 Move 类型的定义(根据你的代码库):
type Move func(g *Game) error
这意味着 MoveHit、MoveStand、MoveDouble 实际上是函数,而不是简单的枚举值。你需要返回这些函数,而不是调用它们。
修正后的 humanAI.Play 方法应该是:
func (ai humanAI) Play(hand []deck.Card, dealer deck.Card) Move {
for {
fmt.Println("Player:", hand)
fmt.Println("Dealer:", dealer)
fmt.Println("What will you do? (h)it, (s)tand, (d)ouble")
var input string
fmt.Scanf("%s\n", &input)
switch input {
case "h":
return MoveHit // 返回函数本身,不是调用结果
case "s":
return MoveStand // 返回函数本身
case "d":
return MoveDouble // 返回函数本身
default:
fmt.Println("Invalid option:", input)
}
}
}
在 game.go 的 Game.Play 方法中,你应该这样调用:
func (g *Game) Play(ai AI) error {
// ... 其他代码 ...
move := ai.Play(g.player, g.dealer[0])
err := move(g) // 这里调用返回的 Move 函数
// ... 其他代码 ...
}
让我展示一个完整的示例来说明类型定义和用法:
// 在 game.go 或相关文件中
type Move func(g *Game) error
// 预定义的 Move 函数
var (
MoveHit = func(g *Game) error {
// 实现 hit 逻辑
g.player = append(g.player, g.deck[0])
g.deck = g.deck[1:]
return nil
}
MoveStand = func(g *Game) error {
// 实现 stand 逻辑
return nil
}
MoveDouble = func(g *Game) error {
// 实现 double 逻辑
g.player = append(g.player, g.deck[0])
g.deck = g.deck[1:]
return nil
}
)
// AI 接口定义
type AI interface {
Play(hand []deck.Card, dealer deck.Card) Move
}
// humanAI 实现
type humanAI struct{}
func (ai humanAI) Play(hand []deck.Card, dealer deck.Card) Move {
for {
fmt.Println("Player:", hand)
fmt.Println("Dealer:", dealer)
fmt.Println("What will you do? (h)it, (s)tand, (d)ouble")
var input string
fmt.Scanf("%s\n", &input)
switch input {
case "h":
return MoveHit
case "s":
return MoveStand
case "d":
return MoveDouble
default:
fmt.Println("Invalid option:", input)
}
}
}
关键点:
Move是func(g *Game) error类型MoveHit、MoveStand、MoveDouble是这个类型的函数值ai.Play()返回一个Move(函数)- 在
game.go中,你需要调用返回的函数:move(g)
这样就能解决类型不匹配的问题。你的代码结构是正确的,只是需要确保理解 Move 是函数类型,而不是简单的枚举或值类型。

