Genie Advanced Example

class AdvancedSample 

        prop name : string
        event foo ()
        construct ( name : string )
       = name
        def run () : void
       event_handler )
        def event_handler() 
                stdout.printf("Name: %s\n", name)

        //args : array of string = { "alpha", "beta", "gamma", "delta" }
        for s in args
                var sample = new AdvancedSample(s)

Compile and Run

$ valac -o advancedsample
$ ./advancedsample

Game Classic: Number Guessing

Requires at least Vala 0.7.5 (for stdin.read_line () support)

class NumberGuessing

        prop min : int
        prop max : int
        construct ( m : int, n : int)
                self.min = m
                self.max = n
        def start()
                try_count : int = 0
                number : int = Random.int_range (min, max)
                stdout.printf ("Welcome to Number Guessing!\n\n")
                stdout.printf ("I have thought up a number between %d and %d\n", min, max)
                stdout.printf ("which you have to guess now. Don't worry, I will\n")
                stdout.printf ("give you some hints.\n\n")
                while true
                        stdout.printf ("Try #%d\n", ++try_count)
                        stdout.printf ("Please enter a number between %d and %d: ", min, max)
                        input : int = int.parse (stdin.read_line ())
                        if number is input
                                stdout.printf ("Congratulations! You win.\n")
                                stdout.printf ("Wrong. The wanted number is %s than %d.\n",
                               number > input ? "greater" : "less", input)
        var game = new NumberGuessing(1, 100)

Compile and Run

$ valac
$ ./numberguessing

More Genie Examples

Projects/Genie/AdvancedSample (last edited 2014-03-17 21:49:03 by AlThomas)