Groovy web console

subscribe to the feed Subscribe
to this
site
Roguelike (via #groovywebconsole)
tweet this snippet Tweet
this
script

Roguelike

Published 1 month ago by Jeremy
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
package game
class MyGame {

    static main(args) {
        MyGame myGame = new MyGame()
        println("Player gets a red potion and drinks it!")
        println("Player has been effected! ${ myGame.getPotionEffect('red') }")
        println("Player gets a black potion and drinks it!")
        println("Player has been effected! ${ myGame.getPotionEffect('black') }")
        println("Player gets a red potion and drinks it!")
        println("Player has been effected! ${ myGame.getPotionEffect('red') }")
    }


    def availableColors  = [ 'pink', 'red', 'black' ]
    def availableEffects = [ 'heal', 'poison', 'fly' ]

    Map discoveredPotions = [:]

    String getPotionEffect(def color) {
        if (!discoveredPotions.containsKey(color)) {
            // Randomly assign color and effect
            def effect = availableEffects.get(new Random().nextInt(availableEffects.size()))
            // Add to discoveredPotions
            discoveredPotions.put(color, effect)
            // Remove color and effect from lists (since they have been discovered)
            availableColors .remove(color)
            availableEffects.remove(effect)
        }

        return discoveredPotions.get(color)
    }

}