Simple Game

Build a text-based game with Ifa-Lang.

Game: Number Guessing

A classic number guessing game demonstrating loops, conditionals, and I/O.

Domains Used

Code Example

// Number Guessing Game (English syntax)
Fmt.println("=== Number Guessing Game ===");
Fmt.println("I'm thinking of a number between 1 and 100...");

let secret = Rand.random(1, 100);
let attempts = 0;
let won = false;

while !won {
    let input = Fmt.input("Your guess: ");
    let guess = String.to_int(input);
    attempts = attempts + 1;
    
    if guess < secret {
        Fmt.println("Too low! Try again.");
    } else if guess > secret {
        Fmt.println("Too high! Try again.");
    } else {
        won = true;
        Fmt.println("Correct! You got it in " + attempts + " attempts!");
    }
}

if attempts <= 5 {
    Fmt.println("Amazing! You're a master guesser!");
} else if attempts <= 10 {
    Fmt.println("Good job!");
} else {
    Fmt.println("Keep practicing!");
}

Game: Text Adventure

// Mini Text Adventure (English syntax)
let room = "start";
let inventory = [];

while true {
    match room {
        "start" => {
            Fmt.println("You are in a dark room. Exits: NORTH, EAST");
            let cmd = Fmt.input("> ");
            if cmd == "north" { room = "hall"; }
            if cmd == "east" { room = "garden"; }
        }
        "hall" => {
            Fmt.println("A grand hall with a KEY on the floor. Exits: SOUTH");
            let cmd = Fmt.input("> ");
            if cmd == "take key" { 
                List.push(inventory, "key"); 
                Fmt.println("Key taken!");
            }
            if cmd == "south" { room = "start"; }
        }
        "garden" => {
            Fmt.println("A beautiful garden with a locked GATE. Exits: WEST");
            let cmd = Fmt.input("> ");
            if cmd == "open gate" {
                if List.contains(inventory, "key") {
                    Fmt.println("You escape! YOU WIN!");
                    break;
                } else {
                    Fmt.println("It's locked.");
                }
            }
            if cmd == "west" { room = "start"; }
        }
    }
}