Skip to content

Commit

Permalink
calculate option added for math training
Browse files Browse the repository at this point in the history
  • Loading branch information
as27 committed Apr 10, 2020
1 parent 14f7930 commit feb1816
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
34 changes: 31 additions & 3 deletions cards.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
package main

import (
"fmt"
"math/rand"
"time"

"github.com/google/uuid"
)

func createCalc(i int, max int) string {
a := rand.Intn(max)
s := ""
switch {
case a > i:
// minus
b := a - i
s = fmt.Sprintf("%d-%d", a, b)
case a <= i:
// plus
b := i - a
s = fmt.Sprintf("%d+%d", a, b)
}
return s
}

// CardGame defines all availiable cards inside the game.
// This function is called to generate a stack for the game.
// Exept for the wish cards, every card needs a value and a color.
func CardGame() *CardStack {
var cs CardStack
colors := []string{"c1", "c2", "c3", "c4"}
values := []string{"1", "1", "2", "2", "3", "3", "4", "4", "5", "5"}
numbers := []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}

for _, c := range colors {
for _, v := range values {
for _, n := range numbers {
var label string
if *flagCalc != 0 {
label = createCalc(n, *flagCalc)
} else {
label = fmt.Sprint(n)
}
newCard := Card{
ID: uuid.New().String(),
Color: c,
Value: v,
Value: fmt.Sprint(n),
Label: label,
SkipPlayers: 0,
TakeN: 0,
WishColor: false,
Expand All @@ -31,6 +56,7 @@ func CardGame() *CardStack {
ID: uuid.New().String(),
Color: c,
Value: "->",
Label: "->",
SkipPlayers: 1,
}
cs.push(newCard)
Expand All @@ -39,6 +65,7 @@ func CardGame() *CardStack {
ID: uuid.New().String(),
Color: c,
Value: "+2",
Label: "+2",
TakeN: 2,
}
cs.push(newCard)
Expand All @@ -58,6 +85,7 @@ type Card struct {
ID string `json:"id"`
Color string `json:"color"`
Value string `json:"value"` // number or name e.g. 1, K, J
Label string `json:"label"` // some tasks for kids like 1+1
SkipPlayers int `json:"skip_players"`
TakeN int `json:"take_n"` // next player has to take this nr of cards
WishColor bool `json:"wish_color"` // defines a wish card
Expand Down
4 changes: 3 additions & 1 deletion cards_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "testing"
import (
"testing"
)

func TestCard_Check(t *testing.T) {
type args struct {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

var (
flagPort = flag.String("p", ":2704", "Port für den Server")
flagCalc = flag.Int("c", 0, "Erstellt für die Ablage eine Rechenaufgabe mit dem Zahlenraum der über dieses Flag definiert wird.")
)

func main() {
Expand Down
3 changes: 3 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func (s *server) playerState(id string) (PlayerState, bool) {
}
}
ps.HeapHead = s.game.HeapHead
// Just show the label for the HeapHead
// so 9 - 3 is shown instead of the value
ps.HeapHead.Value = ps.HeapHead.Label
return ps, found
}

Expand Down

0 comments on commit feb1816

Please sign in to comment.