impl some hands
This commit is contained in:
279
src/game/rule/operation/hand_test.go
Normal file
279
src/game/rule/operation/hand_test.go
Normal file
@@ -0,0 +1,279 @@
|
||||
package operation
|
||||
|
||||
import (
|
||||
"local/sandbox/cards/src/entity"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testHandCase struct {
|
||||
public []entity.Card
|
||||
private []entity.Card
|
||||
want int
|
||||
}
|
||||
|
||||
func TestStraight(t *testing.T) {
|
||||
cases := map[string]testHandCase{
|
||||
"no cards": {
|
||||
want: 0,
|
||||
},
|
||||
"straight big 3": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 11},
|
||||
entity.Card{Value: 12},
|
||||
entity.Card{Value: 13},
|
||||
},
|
||||
want: 13,
|
||||
},
|
||||
"straight sm 3": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 1},
|
||||
entity.Card{Value: 2},
|
||||
entity.Card{Value: 3},
|
||||
},
|
||||
want: 3,
|
||||
},
|
||||
"straight sm 2": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 1},
|
||||
entity.Card{Value: 2},
|
||||
},
|
||||
want: 2,
|
||||
},
|
||||
"not a straight": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 1},
|
||||
entity.Card{Value: 3},
|
||||
},
|
||||
want: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for name, d := range cases {
|
||||
testHand(t, name, straight, d.public, d.private, d.want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlush(t *testing.T) {
|
||||
cases := map[string]testHandCase{
|
||||
"no cards": {
|
||||
want: 0,
|
||||
},
|
||||
"not a flush": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 1, Suit: 0},
|
||||
entity.Card{Value: 2, Suit: 1},
|
||||
},
|
||||
want: 0,
|
||||
},
|
||||
"big, big, small, small flush": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 12},
|
||||
entity.Card{Value: 10},
|
||||
entity.Card{Value: 2},
|
||||
entity.Card{Value: 1},
|
||||
},
|
||||
want: 12100201,
|
||||
},
|
||||
"small, small flush": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 2},
|
||||
entity.Card{Value: 1},
|
||||
},
|
||||
want: 201,
|
||||
},
|
||||
"big, small flush": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 11},
|
||||
entity.Card{Value: 1},
|
||||
},
|
||||
want: 1101,
|
||||
},
|
||||
"big, big flush": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 11},
|
||||
entity.Card{Value: 12},
|
||||
},
|
||||
want: 1211,
|
||||
},
|
||||
}
|
||||
|
||||
for name, d := range cases {
|
||||
testHand(t, name, flush, d.public, d.private, d.want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFullHouse(t *testing.T) {
|
||||
cases := map[string]testHandCase{
|
||||
"no cards": {
|
||||
want: 0,
|
||||
},
|
||||
"10 > 11": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 11},
|
||||
entity.Card{Value: 11},
|
||||
entity.Card{Value: 10},
|
||||
entity.Card{Value: 10},
|
||||
entity.Card{Value: 10},
|
||||
},
|
||||
want: 1011,
|
||||
},
|
||||
"five of a kind": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
},
|
||||
want: 0,
|
||||
},
|
||||
"two pair": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 4},
|
||||
entity.Card{Value: 4},
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 6},
|
||||
},
|
||||
want: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for name, d := range cases {
|
||||
testHand(t, name, fullHouse, d.public, d.private, d.want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFourOfAKind(t *testing.T) {
|
||||
cases := map[string]testHandCase{
|
||||
"no cards": {
|
||||
want: 0,
|
||||
},
|
||||
"one card": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 5},
|
||||
},
|
||||
want: 0,
|
||||
},
|
||||
"two cards": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
},
|
||||
want: 0,
|
||||
},
|
||||
"three cards": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
},
|
||||
want: 0,
|
||||
},
|
||||
"four cards": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
},
|
||||
want: 500,
|
||||
},
|
||||
"five cards": {
|
||||
public: []entity.Card{
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
entity.Card{Value: 5},
|
||||
},
|
||||
want: 505,
|
||||
},
|
||||
}
|
||||
|
||||
for name, d := range cases {
|
||||
testHand(t, name, fourOfAKind, d.public, d.private, d.want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStraightFlush(t *testing.T) {
|
||||
cases := map[string]testHandCase{
|
||||
"no cards": {
|
||||
want: 0,
|
||||
},
|
||||
"one nonroyal cards": {
|
||||
public: []entity.Card{entity.Card{Value: 5}},
|
||||
want: 0,
|
||||
},
|
||||
"one royal cards": {
|
||||
public: []entity.Card{entity.Card{Value: 12}},
|
||||
want: 0,
|
||||
},
|
||||
"two nonroyal cards": {
|
||||
public: []entity.Card{entity.Card{Value: 5}, entity.Card{Value: 6}},
|
||||
want: 6,
|
||||
},
|
||||
"twe royal cards": {
|
||||
public: []entity.Card{entity.Card{Value: 12}, entity.Card{Value: 13}},
|
||||
want: 13,
|
||||
},
|
||||
"twe mix cards": {
|
||||
public: []entity.Card{entity.Card{Value: 11}, entity.Card{Value: 10}},
|
||||
want: 11,
|
||||
},
|
||||
}
|
||||
|
||||
for name, d := range cases {
|
||||
testHand(t, name, straightFlush, d.public, d.private, d.want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoyalFlush(t *testing.T) {
|
||||
cases := map[string]testHandCase{
|
||||
"no cards": {
|
||||
want: 0,
|
||||
},
|
||||
"one nonroyal cards": {
|
||||
public: []entity.Card{entity.Card{Value: 5}},
|
||||
want: 0,
|
||||
},
|
||||
"one royal cards": {
|
||||
public: []entity.Card{entity.Card{Value: 12}},
|
||||
want: 0,
|
||||
},
|
||||
"two nonroyal cards": {
|
||||
public: []entity.Card{entity.Card{Value: 5}, entity.Card{Value: 6}},
|
||||
want: 0,
|
||||
},
|
||||
"twe royal cards": {
|
||||
public: []entity.Card{entity.Card{Value: 12}, entity.Card{Value: 13}},
|
||||
want: 1,
|
||||
},
|
||||
"twe mix cards": {
|
||||
public: []entity.Card{entity.Card{Value: 11}, entity.Card{Value: 10}},
|
||||
want: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for name, d := range cases {
|
||||
testHand(t, name, royalFlush, d.public, d.private, d.want)
|
||||
}
|
||||
}
|
||||
|
||||
func testHand(t *testing.T, name string, foo Int, public []entity.Card, private []entity.Card, want int) {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
hand := entity.Hand{
|
||||
Public: public,
|
||||
Private: private,
|
||||
}
|
||||
game := &entity.Game{
|
||||
Players: []entity.Player{
|
||||
entity.Player{Hand: hand},
|
||||
},
|
||||
}
|
||||
got := foo(game, nil)
|
||||
if got != want {
|
||||
t.Fatal(want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user