Minifying Code – The “Switch” Case – Getting Help

Greetings to everyone,
I am writing a simple code for board gaming purposes. It should attract two, three or four nations (depending on how many players) out of 16 possible nations. No repetition.

Here is the main 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 (due to four draws). I would like to shorten it; I want to have the above piece of code only once.
Is it possible to somehow replace this part:

  			switch a {				

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

  			switch a, b, c, d {				

In other words: I want the code to draw four players at once: players A, B, C, D. Just from this “switch” block I pasted here.
The code also contains an input box (how many players are playing? [2-4], Even if we choose 2 players, the code can draw 4 players – no problem, because the code will eventually print as many outputs as we have declared.

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

encourage!


Leave a Comment