Skip to content

Commit

Permalink
feat: implement deck evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
PapePathe committed Oct 29, 2023
1 parent 3b93e46 commit d24db69
Show file tree
Hide file tree
Showing 11 changed files with 309 additions and 0 deletions.
1 change: 1 addition & 0 deletions gametake/gametake.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type GameTake interface {
GreaterThan(t GameTake) bool
EvaluateHand([5]cards.Card) GameTakeEntry
EvaluateCard(cards.Card) (int, bool)
EvaluateDeck([4]cards.Card) int
GetValue() int
Name() string
}
9 changes: 9 additions & 0 deletions gametake/gametake_cent.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ func (t Cent) GetValue() int {
return 5
}

func (t Cent) EvaluateDeck(cards [4]cards.Card) (result int) {
for _, c := range cards {
value, _ := t.EvaluateCard(c)
result += value
}

return result
}

func (t Cent) EvaluateHand(cards [5]cards.Card) (entry GameTakeEntry) {
entry.Flags = make(map[string]flag)
result, acesCount := 0, 0
Expand Down
37 changes: 37 additions & 0 deletions gametake/gametake_cent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,40 @@ func TestEvaluateCardCENT(t *testing.T) {
})
}
}

func TestCENTEvaluateDeck(t *testing.T) {
type TestCENTEvaluateDeckTable struct {
name string
deck [4]cards.Card
result int
}
testcases := []TestCENTEvaluateDeckTable{
TestCENTEvaluateDeckTable{
name: "With no cards",
deck: [4]cards.Card{},
result: 0,
},
TestCENTEvaluateDeckTable{
name: "With cards all worth zero points",
deck: [4]cards.Card{cards.SeptCarreau, cards.NeufCoeur, cards.HuitCarreau, cards.NeufTrefle},
result: 0,
},
TestCENTEvaluateDeckTable{
name: "With one ace and three tens",
deck: [4]cards.Card{cards.AsCarreau, cards.DixCarreau, cards.DixPique, cards.DixCoeur},
result: 41,
},
TestCENTEvaluateDeckTable{
name: "With one ten two kings and a seven",
deck: [4]cards.Card{cards.DixCarreau, cards.RoiCarreau, cards.RoiCoeur, cards.SeptCarreau},
result: 18,
},
}

for _, test := range testcases {
t.Run(test.name, func(t *testing.T) {
result := CENT.EvaluateDeck(test.deck)
assert.Equal(t, result, test.result)
})
}
}
9 changes: 9 additions & 0 deletions gametake/gametake_color.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ func (t ColorTake) GetValue() int {
return t.Value
}

func (t ColorTake) EvaluateDeck(cards [4]cards.Card) (result int) {
for _, c := range cards {
value, _ := t.EvaluateCard(c)
result += value
}

return result
}

func (t ColorTake) EvaluateHand(cards [5]cards.Card) (entry GameTakeEntry) {
t.parseCard(&entry, cards[0])
t.parseCard(&entry, cards[1])
Expand Down
42 changes: 42 additions & 0 deletions gametake/gametake_color_carreau_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,45 @@ func TestCARREAUGreaterThan(t *testing.T) {
})
}
}

func TestEvaluateDeckCARREAU(t *testing.T) {
type TestEvaluateDeckTableCARREAU struct {
name string
deck [4]cards.Card
result int
}
testcases := []TestEvaluateDeckTableCARREAU{
TestEvaluateDeckTableCARREAU{
name: "With no cards",
deck: [4]cards.Card{},
result: 0,
},
TestEvaluateDeckTableCARREAU{
name: "With a valet of other color and zero value cards",
deck: [4]cards.Card{cards.SeptCarreau, cards.ValetCoeur, cards.HuitCarreau, cards.SeptTrefle},
result: 2,
},
TestEvaluateDeckTableCARREAU{
name: "With two nines of other color an height and a seven",
deck: [4]cards.Card{cards.SeptCarreau, cards.NeufCoeur, cards.HuitCarreau, cards.NeufTrefle},
result: 0,
},
TestEvaluateDeckTableCARREAU{
name: "With one ace and three tens",
deck: [4]cards.Card{cards.AsCarreau, cards.DixCarreau, cards.DixPique, cards.DixCoeur},
result: 41,
},
TestEvaluateDeckTableCARREAU{
name: "With one ten two kings and a seven",
deck: [4]cards.Card{cards.DixCarreau, cards.RoiCarreau, cards.RoiCoeur, cards.SeptCarreau},
result: 18,
},
}

for _, test := range testcases {
t.Run(test.name, func(t *testing.T) {
result := CARREAU.EvaluateDeck(test.deck)
assert.Equal(t, result, test.result)
})
}
}
52 changes: 52 additions & 0 deletions gametake/gametake_color_coeur_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,55 @@ func TestNameCOEUR(t *testing.T) {
result := COEUR.Name()
assert.Equal(t, result, "Coeur")
}

func TestEvaluateDeckCOEUR(t *testing.T) {
type TestEvaluateDeckTableCOEUR struct {
name string
deck [4]cards.Card
result int
}
testcases := []TestEvaluateDeckTableCOEUR{
TestEvaluateDeckTableCOEUR{
name: "With no cards",
deck: [4]cards.Card{},
result: 0,
},
TestEvaluateDeckTableCOEUR{
name: "With a valet of other color and zero value cards",
deck: [4]cards.Card{cards.SeptCarreau, cards.ValetCarreau, cards.HuitCarreau, cards.SeptTrefle},
result: 2,
},
TestEvaluateDeckTableCOEUR{
name: "With a valet of same color and zero value cards",
deck: [4]cards.Card{cards.SeptCarreau, cards.ValetCoeur, cards.HuitCarreau, cards.SeptTrefle},
result: 20,
},
TestEvaluateDeckTableCOEUR{
name: "With two nines of other color an height and a seven",
deck: [4]cards.Card{cards.SeptCarreau, cards.NeufPique, cards.HuitCarreau, cards.NeufTrefle},
result: 0,
},
TestEvaluateDeckTableCOEUR{
name: "With one nine of same color and a nine of other color an height and a seven",
deck: [4]cards.Card{cards.SeptCarreau, cards.NeufCoeur, cards.HuitCarreau, cards.NeufTrefle},
result: 14,
},
TestEvaluateDeckTableCOEUR{
name: "With one ace and three tens",
deck: [4]cards.Card{cards.AsCarreau, cards.DixCarreau, cards.DixPique, cards.DixCoeur},
result: 41,
},
TestEvaluateDeckTableCOEUR{
name: "With one ten two kings and a seven",
deck: [4]cards.Card{cards.DixCarreau, cards.RoiCarreau, cards.RoiCoeur, cards.SeptCarreau},
result: 18,
},
}

for _, test := range testcases {
t.Run(test.name, func(t *testing.T) {
result := COEUR.EvaluateDeck(test.deck)
assert.Equal(t, result, test.result)
})
}
}
52 changes: 52 additions & 0 deletions gametake/gametake_color_pique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,55 @@ func TestNamePIQUE(t *testing.T) {
result := PIQUE.Name()
assert.Equal(t, result, "Pique")
}

func TestEvaluateDeckPIQUE(t *testing.T) {
type TestEvaluateDeckTablePIQUE struct {
name string
deck [4]cards.Card
result int
}
testcases := []TestEvaluateDeckTablePIQUE{
TestEvaluateDeckTablePIQUE{
name: "With no cards",
deck: [4]cards.Card{},
result: 0,
},
TestEvaluateDeckTablePIQUE{
name: "With a valet of other color and zero value cards",
deck: [4]cards.Card{cards.SeptCarreau, cards.ValetCarreau, cards.HuitCarreau, cards.SeptTrefle},
result: 2,
},
TestEvaluateDeckTablePIQUE{
name: "With a valet of same color and zero value cards",
deck: [4]cards.Card{cards.SeptCarreau, cards.ValetPique, cards.HuitCarreau, cards.SeptTrefle},
result: 20,
},
TestEvaluateDeckTablePIQUE{
name: "With two nines of other color an height and a seven",
deck: [4]cards.Card{cards.SeptCarreau, cards.NeufCoeur, cards.HuitCarreau, cards.NeufCarreau},
result: 0,
},
TestEvaluateDeckTablePIQUE{
name: "With one nine of same color and a nine of other color an height and a seven",
deck: [4]cards.Card{cards.SeptCarreau, cards.NeufPique, cards.HuitCarreau, cards.NeufCarreau},
result: 14,
},
TestEvaluateDeckTablePIQUE{
name: "With one ace and three tens",
deck: [4]cards.Card{cards.AsCarreau, cards.DixCarreau, cards.DixPique, cards.DixCoeur},
result: 41,
},
TestEvaluateDeckTablePIQUE{
name: "With one ten two kings and a seven",
deck: [4]cards.Card{cards.DixCarreau, cards.RoiCarreau, cards.RoiCoeur, cards.SeptCarreau},
result: 18,
},
}

for _, test := range testcases {
t.Run(test.name, func(t *testing.T) {
result := PIQUE.EvaluateDeck(test.deck)
assert.Equal(t, result, test.result)
})
}
}
52 changes: 52 additions & 0 deletions gametake/gametake_color_trefle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,55 @@ func TestNameTREFLE(t *testing.T) {
result := TREFLE.Name()
assert.Equal(t, result, "Trefle")
}

func TestEvaluateDeckTREFLE(t *testing.T) {
type TestEvaluateDeckTableTREFLE struct {
name string
deck [4]cards.Card
result int
}
testcases := []TestEvaluateDeckTableTREFLE{
TestEvaluateDeckTableTREFLE{
name: "With no cards",
deck: [4]cards.Card{},
result: 0,
},
TestEvaluateDeckTableTREFLE{
name: "With a valet of other color and zero value cards",
deck: [4]cards.Card{cards.SeptCarreau, cards.ValetCarreau, cards.HuitCarreau, cards.SeptTrefle},
result: 2,
},
TestEvaluateDeckTableTREFLE{
name: "With a valet of same color and zero value cards",
deck: [4]cards.Card{cards.SeptCarreau, cards.ValetTrefle, cards.HuitCarreau, cards.SeptTrefle},
result: 20,
},
TestEvaluateDeckTableTREFLE{
name: "With two nines of other color an height and a seven",
deck: [4]cards.Card{cards.SeptCarreau, cards.NeufPique, cards.HuitCarreau, cards.NeufCarreau},
result: 0,
},
TestEvaluateDeckTableTREFLE{
name: "With one nine of same color and a nine of other color an height and a seven",
deck: [4]cards.Card{cards.SeptCarreau, cards.NeufTrefle, cards.HuitCarreau, cards.NeufCarreau},
result: 14,
},
TestEvaluateDeckTableTREFLE{
name: "With one ace and three tens",
deck: [4]cards.Card{cards.AsCarreau, cards.DixCarreau, cards.DixPique, cards.DixCoeur},
result: 41,
},
TestEvaluateDeckTableTREFLE{
name: "With one ten two kings and a seven",
deck: [4]cards.Card{cards.DixCarreau, cards.RoiCarreau, cards.RoiCoeur, cards.SeptCarreau},
result: 18,
},
}

for _, test := range testcases {
t.Run(test.name, func(t *testing.T) {
result := TREFLE.EvaluateDeck(test.deck)
assert.Equal(t, result, test.result)
})
}
}
9 changes: 9 additions & 0 deletions gametake/gametake_tout.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ func (t Tout) GetValue() int {
return 6
}

func (t Tout) EvaluateDeck(cards [4]cards.Card) (result int) {
for _, c := range cards {
value, _ := t.EvaluateCard(c)
result += value
}

return result
}

func (t Tout) EvaluateHand(cards [5]cards.Card) (entry GameTakeEntry) {
result := 0
valetsCount := 0
Expand Down
42 changes: 42 additions & 0 deletions gametake/gametake_tout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,45 @@ func TestTOUTGreaterThan(t *testing.T) {
})
}
}

func TestTOUTEvaluateDeck(t *testing.T) {
type TestTOUTEvaluateDeckTable struct {
name string
deck [4]cards.Card
result int
}
testcases := []TestTOUTEvaluateDeckTable{
TestTOUTEvaluateDeckTable{
name: "With no cards",
deck: [4]cards.Card{},
result: 0,
},
TestTOUTEvaluateDeckTable{
name: "With a valet and zero value cards",
deck: [4]cards.Card{cards.SeptCarreau, cards.ValetCoeur, cards.HuitCarreau, cards.SeptTrefle},
result: 20,
},
TestTOUTEvaluateDeckTable{
name: "With two nines an height and a seven",
deck: [4]cards.Card{cards.SeptCarreau, cards.NeufCoeur, cards.HuitCarreau, cards.NeufTrefle},
result: 28,
},
TestTOUTEvaluateDeckTable{
name: "With one ace and three tens",
deck: [4]cards.Card{cards.AsCarreau, cards.DixCarreau, cards.DixPique, cards.DixCoeur},
result: 41,
},
TestTOUTEvaluateDeckTable{
name: "With one ten two kings and a seven",
deck: [4]cards.Card{cards.DixCarreau, cards.RoiCarreau, cards.RoiCoeur, cards.SeptCarreau},
result: 18,
},
}

for _, test := range testcases {
t.Run(test.name, func(t *testing.T) {
result := TOUT.EvaluateDeck(test.deck)
assert.Equal(t, result, test.result)
})
}
}
4 changes: 4 additions & 0 deletions gametake/pass.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func (t Passe) GetValue() int {
return 0
}

func (t Passe) EvaluateDeck(cards [4]cards.Card) int {
return 0
}

func (t Passe) EvaluateHand(cards [5]cards.Card) (entry GameTakeEntry) {
return entry
}
Expand Down

0 comments on commit d24db69

Please sign in to comment.