Advertisement
cwchen

[Go] break with labels demo.

Sep 16th, 2017
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.67 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "math/rand"
  6.     "time"
  7. )
  8.  
  9. func main() {
  10.     // Set a random object
  11.     r := rand.New(rand.NewSource(time.Now().UnixNano()))
  12.    
  13. // The initial state of our runner.
  14. miles := 0
  15.  
  16. GAME_OVER:
  17.     for {
  18.         // Get a new game state for each round.
  19.         state := r.Intn(10)
  20.  
  21.         // Update the state of our runner.
  22.         switch state {
  23.         case 7:
  24.             fmt.Println("Jump!")
  25.             miles += 3
  26.         case 0:
  27.             fmt.Println("You FAIL")
  28.             break GAME_OVER
  29.         case 6:
  30.             fmt.Println("Missed!")
  31.             miles -= 2
  32.         default:
  33.             fmt.Println("Walk")
  34.             miles += 1
  35.         }
  36.  
  37.         // End the game if you win.
  38.         if miles >= 10 {
  39.             fmt.Println("You WIN")
  40.             break GAME_OVER
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement