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.
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:
// ...
}
}
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!