This site has been retired. For up to date information, see handbook.gnome.org or gitlab.gnome.org.


[Home] [TitleIndex] [WordIndex

Vala Advanced Example

public class AdvancedSample : Object {
    public string name { get; set; }   // Property
    public signal void foo ();         // Signal

    public AdvancedSample (string name) {
        this.name = name;
    }

    public void run () {
        // Assigning anonymous function as signal handler
        this.foo.connect ((s) => {
            stdout.printf ("Lambda expression: Argument is %s!\n", this.name);
        });

        // Emitting the signal
        this.foo ();
    }
}

void main (string[] args) {
    foreach (string arg in args) {
        var sample = new AdvancedSample (arg);
        sample.run ();
    }
}

Compile and run

$ valac -o advancedsample AdvancedSample.vala
$ ./advancedsample

Game Classic: Number Guessing

public class NumberGuessing {
    private int min;
    private int max;

    public NumberGuessing (int min, int max) {
        this.min = min;
        this.max = max;
    }

    public void start () {
        int try_count = 0;
        int number = 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) {
            try_count++;

            stdout.printf ("Try #%d\n", try_count);
            stdout.printf ("Please enter a number between %d and %d: ", min, max);
            int input = int.parse (stdin.read_line ());

            if (number == input) {
                stdout.printf ("Congratulations! You win.\n");
                break;
            } else {
                var how = number > input ? "greater" : "less";
                stdout.printf ("Wrong. The wanted number is %s than %d.\n", how, input);
            }
        }
    }
}

void main (string[] args) {
    var game = new NumberGuessing (1, 100);
    game.start ();
}

Compile and run

$ valac number-guessing.vala
$ ./number-guessing


Vala/Examples


2024-10-23 11:37