summaryrefslogtreecommitdiff
path: root/extra/cluedo.go
blob: 1ef11ccb56ef37298a03c1b42582da0e4f59b9ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package extra

import (
	"math/rand"
	"strings"
)

var (
	rooms   = []string{"HALL", "LOUNGE", "DINING ROOM", "KITCHEN", "BALLROOM", "CONSERVATORY", "BILLIARD ROOM", "LIBRARY", "STUDY"}
	weapons = []string{"CANDLESTICK", "DAGGER", "LEAD PIPE", "REVOLVER", "ROPE", "SPANNER"}
	people  = []string{"Miss Scarlett", "Colonel Mustard", "Mrs. White", "Reverend Green", "Mrs. Peacock", "Professor Plum"}
)

type MurderTrifecta struct {
	Murderer string
	Weapon   string
	Room     string
}

type CluedoRoundInfo struct {
	Answer       MurderTrifecta
	PlayersCards map[string][]string
}

func (c *CluedoRoundInfo) GetPlayerCards(player string) string {
	// maybe format it a little
	return "cards of " + player + "are " + strings.Join(c.PlayersCards[player], ",")
}

func CluedoPrepCards(playerOrder []string) *CluedoRoundInfo {
	res := &CluedoRoundInfo{}
	// Select murder components
	trifecta := MurderTrifecta{
		Murderer: people[rand.Intn(len(people))],
		Weapon:   weapons[rand.Intn(len(weapons))],
		Room:     rooms[rand.Intn(len(rooms))],
	}
	// Collect non-murder cards
	var notInvolved []string
	for _, room := range rooms {
		if room != trifecta.Room {
			notInvolved = append(notInvolved, room)
		}
	}
	for _, weapon := range weapons {
		if weapon != trifecta.Weapon {
			notInvolved = append(notInvolved, weapon)
		}
	}
	for _, person := range people {
		if person != trifecta.Murderer {
			notInvolved = append(notInvolved, person)
		}
	}
	// Shuffle and distribute cards
	rand.Shuffle(len(notInvolved), func(i, j int) {
		notInvolved[i], notInvolved[j] = notInvolved[j], notInvolved[i]
	})
	players := map[string][]string{}
	cardsPerPlayer := len(notInvolved) / len(playerOrder)
	// playerOrder := []string{"{{user}}", "{{char}}", "{{char2}}"}
	for i, player := range playerOrder {
		start := i * cardsPerPlayer
		end := (i + 1) * cardsPerPlayer
		if end > len(notInvolved) {
			end = len(notInvolved)
		}
		players[player] = notInvolved[start:end]
	}
	res.Answer = trifecta
	res.PlayersCards = players
	return res
}