Shortening the code - "switch" case

Hello everyone,
I’m writing a simple code for a board gaming purposes. It should draw two, three or four nations (depending on how many players are in) out of 16 possible ones. No repetitions.

Here is the key part:

					rand.Seed(time.Now().UnixNano())
		
					var a = rand.Intn(17-1)+1		

					switch a {				
						case 1: 
						fmt.Println("uranopolis")
						case 2: 
						fmt.Println("smart")
						case 3: 
						fmt.Println("sand runners")
						case 4: 
						fmt.Println("policja")
						case 5: 
						fmt.Println("dancer")
						case 6: 
						fmt.Println("nowy jork")
						case 7: 
						fmt.Println("missisipi")
						case 8: 
						fmt.Println("vegas")	
						case 9: 
						fmt.Println("neodżungla")
						case 10: 
						fmt.Println("troglodyci")
						case 11: 
						fmt.Println("szczury")
						case 12: 
						fmt.Println("iron gang")
						case 13: 
						fmt.Println("borgo")
						case 14: 
						fmt.Println("hegemonia")
						case 15: 
						fmt.Println("moloch")
						case 16: 
						fmt.Println("posterunek")	
					}

I used to have this block four times (because of the four draws). I’d like to shorten it; I’d like to have the above part of the code only once.
Is that possible to somehow change this part:

  			switch a {				

to something like this (I know, the syntax below is bad):

  			switch a, b, c, d {				

In other words: I would like the code to draw four players at once: player a, b, c ,d. Just from this “switch” block I’ve pasted here.
The code has also an input box (how many players are playing? [2-4]. Even if we choose 2 players, the code can draw 4 players - no matter, because the code will eventually print out as many outputs as many players we declared.

Is there a magical syntax that can help me? I think it’s messy to have 4x almost the same block of text in the code.

cheers!

Is this what you are getting at? Fallthrough for your cases with common code:

case 1: fallthrough
case 2: fallthrough
case 3:
         commonOperationForCases1Through3()
case 4: // etc.

Sorry, I skimmed the problem too coarsely and answered a different problem.

What you want is not related to the switch statement.

What you want is to create a single function that you call 4 times.

something like this?
https://play.golang.com/p/ynx7Uxq4ga2

package main

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

func main() {
	rand.Seed(time.Now().UnixNano())  // change the seed manually in "The Go Playground"
	players := getFirstX(rand.Intn(4) + 1)
	for i := range players {
		fmt.Println(players[i])
	}
}

func getFirstX(X int) []string {
	data := []string{"uranopolis", "smart", "sand runners", "policja", "dancer"}
	rand.Shuffle(len(data), func(i, j int) { data[i], data[j] = data[j], data[i] })
	return data[:X]
}

I’m not sure if anyone else’s response answered your question or not, but to me it looks like you want a for loop? Something like:

numberOfNations := askUserForNumberOfNations()

for i := 0; i < numberOfNations; i++ {
    nationCode := askUserForNationCode()
    a := nationCode
    switch a {
        case 1:
        // ...
    }
}

Maybe something like this:

package main

import "fmt"

var cities = map[int]string{
	0: "uranopolis", 1: "smart", 2: "sand runners", 3: "policja"}

func main() {
	fmt.Println(GetNation(0))
	fmt.Println(GetNation(1))
	fmt.Println(GetNation(2))
}

func GetNation(selectedNr int) string {
	if value, ok := cities[selectedNr]; ok {
		delete(cities, selectedNr)
		return value
	}
	return ""
}

Thanks for your replies. I think @NobbZ is right. I think I should create a funcion that draws 1 out of 16 possible nations and put this part of code inside the “general” funciton, where - accordingly to the input data - “drawing” function will be called as many times as needed.
I’m a very beginner so I’m eager to test all your suggestions. Thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.