...

Source file src/gitlab.com/tslocum/joker/cards.go

Documentation: gitlab.com/tslocum/joker

     1  package joker
     2  
     3  import (
     4  	"sort"
     5  	"strings"
     6  )
     7  
     8  // Cards is a slice of Cards.
     9  type Cards []Card
    10  
    11  // StandardCards is a slice of standard cards.
    12  var StandardCards = Cards{
    13  	Card{FaceAce, SuitHearts},
    14  	Card{Face2, SuitHearts},
    15  	Card{Face3, SuitHearts},
    16  	Card{Face4, SuitHearts},
    17  	Card{Face5, SuitHearts},
    18  	Card{Face6, SuitHearts},
    19  	Card{Face7, SuitHearts},
    20  	Card{Face8, SuitHearts},
    21  	Card{Face9, SuitHearts},
    22  	Card{Face10, SuitHearts},
    23  	Card{FaceJack, SuitHearts},
    24  	Card{FaceQueen, SuitHearts},
    25  	Card{FaceKing, SuitHearts},
    26  	Card{FaceAce, SuitDiamonds},
    27  	Card{Face2, SuitDiamonds},
    28  	Card{Face3, SuitDiamonds},
    29  	Card{Face4, SuitDiamonds},
    30  	Card{Face5, SuitDiamonds},
    31  	Card{Face6, SuitDiamonds},
    32  	Card{Face7, SuitDiamonds},
    33  	Card{Face8, SuitDiamonds},
    34  	Card{Face9, SuitDiamonds},
    35  	Card{Face10, SuitDiamonds},
    36  	Card{FaceJack, SuitDiamonds},
    37  	Card{FaceQueen, SuitDiamonds},
    38  	Card{FaceKing, SuitDiamonds},
    39  	Card{FaceAce, SuitClubs},
    40  	Card{Face2, SuitClubs},
    41  	Card{Face3, SuitClubs},
    42  	Card{Face4, SuitClubs},
    43  	Card{Face5, SuitClubs},
    44  	Card{Face6, SuitClubs},
    45  	Card{Face7, SuitClubs},
    46  	Card{Face8, SuitClubs},
    47  	Card{Face9, SuitClubs},
    48  	Card{Face10, SuitClubs},
    49  	Card{FaceJack, SuitClubs},
    50  	Card{FaceQueen, SuitClubs},
    51  	Card{FaceKing, SuitClubs},
    52  	Card{FaceAce, SuitSpades},
    53  	Card{Face2, SuitSpades},
    54  	Card{Face3, SuitSpades},
    55  	Card{Face4, SuitSpades},
    56  	Card{Face5, SuitSpades},
    57  	Card{Face6, SuitSpades},
    58  	Card{Face7, SuitSpades},
    59  	Card{Face8, SuitSpades},
    60  	Card{Face9, SuitSpades},
    61  	Card{Face10, SuitSpades},
    62  	Card{FaceJack, SuitSpades},
    63  	Card{FaceQueen, SuitSpades},
    64  	Card{FaceKing, SuitSpades},
    65  }
    66  
    67  func (c Cards) Len() int {
    68  	return len(c)
    69  }
    70  
    71  func (c Cards) Less(i, j int) bool {
    72  	return c[i].Value() < c[j].Value()
    73  }
    74  
    75  func (c Cards) Swap(i, j int) {
    76  	c[i], c[j] = c[j], c[i]
    77  }
    78  
    79  func (c Cards) String() string {
    80  	var s strings.Builder
    81  	for i := range c {
    82  		if i > 0 {
    83  			s.WriteRune(',')
    84  		}
    85  		s.WriteString(c[i].String())
    86  	}
    87  	return s.String()
    88  }
    89  
    90  // Copy returns a copy of the supplied cards.
    91  func (c Cards) Copy() Cards {
    92  	cc := make(Cards, len(c))
    93  	copy(cc, c)
    94  	return cc
    95  }
    96  
    97  // Remove returns the supplied cards with the specified card removed one time.
    98  func (c Cards) Remove(card Card) Cards {
    99  	cc := c.Copy()
   100  	for i, searchCard := range c {
   101  		if searchCard == card {
   102  			return append(cc[:i], cc[i+1:]...)
   103  		}
   104  	}
   105  	return cc
   106  }
   107  
   108  // RemoveIndex returns the supplied cards excluding the card at the specified index.
   109  func (c Cards) RemoveIndex(i int) Cards {
   110  	cc := c.Copy()
   111  	return append(cc[:i], cc[i+1:]...)
   112  }
   113  
   114  // Contains returns whether the supplied cards contain the specified card.
   115  func (c Cards) Contains(card Card) bool {
   116  	for _, compcard := range c {
   117  		if compcard.Equal(card) {
   118  			return true
   119  		}
   120  	}
   121  
   122  	return false
   123  }
   124  
   125  // Count returns the number of occurrences of the specified card.
   126  func (c Cards) Count(card Card) int {
   127  	var n int
   128  	for _, compcard := range c {
   129  		if compcard.Equal(card) {
   130  			n++
   131  		}
   132  	}
   133  
   134  	return n
   135  }
   136  
   137  // Equal returns whether the supplied cards are equal to another set of cards.
   138  func (c Cards) Equal(cards Cards) bool {
   139  	if len(c) != len(cards) {
   140  		return false
   141  	}
   142  
   143  	for _, compcard := range c {
   144  		if c.Count(compcard) != cards.Count(compcard) {
   145  			return false
   146  		}
   147  	}
   148  
   149  	return true
   150  }
   151  
   152  // Sorted returns the supplied cards in order.
   153  func (c Cards) Sorted() Cards {
   154  	cc := c.Copy()
   155  	sort.Sort(cc)
   156  	return cc
   157  }
   158  
   159  // Reversed returns the supplied cards in reverse order.
   160  func (c Cards) Reversed() Cards {
   161  	l := len(c)
   162  	cc := make(Cards, l)
   163  	for i := 0; i < l; i++ {
   164  		cc[i] = c[l-i-1]
   165  	}
   166  	return cc
   167  }
   168  
   169  // Permutations returns all permutations of the supplied cards.
   170  func (c Cards) Permutations() []Cards {
   171  	var permute func(Cards, int)
   172  	var res []Cards
   173  	permute = func(c Cards, n int) {
   174  		if n == 1 {
   175  			res = append(res, c.Copy())
   176  		} else {
   177  			for i := 0; i < n; i++ {
   178  				permute(c, n-1)
   179  				if n%2 == 1 {
   180  					tmp := c[i]
   181  					c[i] = c[n-1]
   182  					c[n-1] = tmp
   183  				} else {
   184  					tmp := c[0]
   185  					c[0] = c[n-1]
   186  					c[n-1] = tmp
   187  				}
   188  			}
   189  		}
   190  	}
   191  	permute(c, len(c))
   192  	return res
   193  }
   194  
   195  // Low returns the lowest valued card.
   196  func (c Cards) Low() Card {
   197  	if len(c) == 0 {
   198  		return Card{}
   199  	}
   200  
   201  	l := c[0]
   202  	for _, comp := range c[1:] {
   203  		if comp.Value() < l.Value() {
   204  			l = comp
   205  		}
   206  	}
   207  	return l
   208  }
   209  
   210  // High returns the highest valued card.
   211  func (c Cards) High() Card {
   212  	if len(c) == 0 {
   213  		return Card{}
   214  	}
   215  
   216  	h := c[0]
   217  	for _, comp := range c[1:] {
   218  		if comp.Value() > h.Value() {
   219  			h = comp
   220  		}
   221  	}
   222  	return h
   223  }
   224  

View as plain text