去比较两个结构是否大于

我正在尝试比较两张扑克牌结构,看看哪张扑克牌比另一张更好。我在网上找到的所有用于比较围棋结构的东西都是为了比较是否相等,但在这种情况下,我想说黑桃 A 比梅花 7 更有价值。


鉴于 Go 没有提供开箱即用的 Java 比较器接口,我想创建自己的函数来使用循环进行卡片比较,但我收到了错误消息:


# cards/card

card/Card.go:12:19: cannot use Ranks (type [13]string) as type []string in argument to indexOfSlice

card/Card.go:14:26: cannot use Ranks (type [13]string) as type []string in argument to indexOfSlice

card/Card.go:15:20: cannot use Suits (type [4]string) as type []string in argument to indexOfSlice

这是我的卡包:


package Card


var Suits = [4]string {"hearts", "spades", "diamonds", "clubs"}

var Ranks = [13]string {"2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "ace"}


type Card struct {

        Value string

        Suit string

}


// if the index in the slices is greater for a than for b, then a must be greater value

func CardIsGreater(a Card, b Card) bool {

        if indexOfSlice(a.Value, Ranks) > indexOfSlice(b.Value, Ranks) {

                return true;

        } else if indexOfSlice(a.Value, Ranks) == indexOfSlice(b.Value, Ranks) {

                if indexOfSlice(a.Suit, Suits) > indexOfSlice(b.Suit, Suits) {

                        return true;

                }

        } else {

                return false;

        }

        // lets ignore for a second that invalid ranks or suits will break this comparator

        return false;

}


// finds the index of a suit or value in a slice

func indexOfSlice(element string, slice []string) int {

        for i, _ := range slice {

                if slice[i] == element {

                        return i;

                }

        }

        return -1;

}

这是我的主要包:


package main


import (

    "fmt"

    "cards/card"

)



在这种情况下,如何让 indexOfSlice 辅助函数接受一个字符串切片作为其参数之一?如果我从谷歌搜索的首页阅读随机教程,它看起来应该可以工作:https ://nanxiao.gitbooks.io/golang-101-hacks/content/posts/pass-slice-as-a-函数参数.html


这个堆栈溢出答案解释说,Go 基本上是在强制执行类型安全,因为类型 []string 可以通过许多事情来满足:https ://stackoverflow.com/a/44606795/7255394 。如果是这种情况,那么我该如何解决这个问题?


此外,这甚至是比较结构的正确方法吗?在将 Ace of Spaces 与 Ace of Hearts 进行比较的情况下,需要在 Suits And Ranks 切片 (!!) 上循环 4 次才能实际进行比较。


三国纷争
浏览 123回答 2
2回答

慕村9548890

Suits并且Ranks不是切片。它们是数组。数组是固定大小的类型,与切片不同。您不能将数组发送到需要切片的函数。因此,将它们声明为没有大小,它们将成为切片:var Suits=[]string{...} var Ranks=[]string{...}或者,如果要将它们保留为数组,则必须将切片传递给需要切片的函数:indexOfSlice(str,Suits[:])关于更好的方法的问题:您可以使用地图:var Suits=map[string]int{"hearts":0, "spades":1, "diamonds":2, "clubs":3}然后:rankSuit=Suits[str]

元芳怎么了

我会抽象你的类型,并为它们添加一些实用功能。Card为,Suit和,创建自定义类型Rank允许您将比较和有效性函数附加到它们中的每一个。如果您想要一个与ComparableJava 类似的界面,那么您也可以自己创建一个非常小的界面。为您的类型实现必要的功能以满足接口将很简单CompareTo,不过,我认为我提供的解决方案对类型的封装更好一些并且更容易测试。我已经添加了在两张牌相等的情况下比较Suit的逻辑,但我不完全确定这背后的原因,因为通常打出相同等级的牌被认为是相等的牌。CardRank祝你好运!卡片/card.gofunc New(s Suit, r Rank) (*Card, error) {&nbsp; &nbsp; if !s.Valid() {&nbsp; &nbsp; &nbsp; &nbsp; return nil, errors.New("invalid suit")&nbsp; &nbsp; }&nbsp; &nbsp; if !r.Valid() {&nbsp; &nbsp; &nbsp; &nbsp; return nil, errors.New("invalid rank")&nbsp; &nbsp; }&nbsp; &nbsp; return &Card{suit: s, rank: r}, nil}type Card struct {&nbsp; &nbsp; suit Suit&nbsp; &nbsp; rank Rank}func (c *Card) Cmp(c2 *Card) int {&nbsp; &nbsp; rankCmp := c.rank.Cmp(c2.rank)&nbsp; &nbsp; if rankCmp != 0 {&nbsp; &nbsp; &nbsp; &nbsp; return rankCmp&nbsp; &nbsp; }&nbsp; &nbsp; if c.suit < c2.suit {&nbsp; &nbsp; &nbsp; &nbsp; return -1&nbsp; &nbsp; }&nbsp; &nbsp; if c.suit > c2.suit {&nbsp; &nbsp; &nbsp; &nbsp; return 1&nbsp; &nbsp; }&nbsp; &nbsp; return 0}卡片/rank.gotype Rank stringvar ranks = []Rank{"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}func (r Rank) Cmp(r2 Rank) int {&nbsp; &nbsp; for _, rank := range ranks {&nbsp; &nbsp; &nbsp; &nbsp; if rank == r {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if rank == r2 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return 0}func (r Rank) Valid() bool {&nbsp; &nbsp; for _, rank := range ranks {&nbsp; &nbsp; &nbsp; &nbsp; if r == rank {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false}卡/suit.gotype Suit intconst (&nbsp; &nbsp; unknown&nbsp; Suit = 1&nbsp; &nbsp; Hearts&nbsp; &nbsp;Suit = 2&nbsp; &nbsp; Spades&nbsp; &nbsp;Suit = 3&nbsp; &nbsp; Diamonds Suit = 4&nbsp; &nbsp; Clubs&nbsp; &nbsp; Suit = 5&nbsp; &nbsp; sentinel Suit = 6)func (s Suit) Valid() bool {&nbsp; &nbsp; return s > unknown && s < sentinel}main.gofunc main() {&nbsp; &nbsp; c1, err := cards.New(Hearts, "2")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; c2, err := New(cards.Diamonds, "A")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; c3, err := New(cards.Spades, "A")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("c1 - c2: %d\n", c1.Cmp(c2))&nbsp; &nbsp; fmt.Printf("c2 - c3: %d\n", c2.Cmp(c3))&nbsp; &nbsp; fmt.Printf("c1 - c3: %d\n", c1.Cmp(c3))}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go