Andrew Kelley 78d4fb20c4 inline parameters
This replaces the current generic syntax for functions and replaces
it with the concept of inline parameters.

This paves the way for the "all structs anonymous" proposal.

Closes #151.
2016-07-25 22:55:15 -07:00

40 lines
1.1 KiB
Zig

const std = @import("std");
const io = std.io;
const Rand = std.Rand;
const os = std.os;
pub fn main(args: [][]u8) -> %void {
%%io.stdout.printf("Welcome to the Guess Number Game in Zig.\n");
var seed : u32 = undefined;
const seed_bytes = (&u8)(&seed)[0...4];
%%os.get_random_bytes(seed_bytes);
var rand = Rand.init(seed);
const answer = rand.range_u64(0, 100) + 1;
while (true) {
%%io.stdout.printf("\nGuess a number between 1 and 100: ");
var line_buf : [20]u8 = undefined;
const line_len = io.stdin.read(line_buf) %% |err| {
%%io.stdout.printf("Unable to read from stdin.\n");
return err;
};
const guess = io.parse_unsigned(u8, line_buf[0...line_len - 1], 10) %% {
%%io.stdout.printf("Invalid number.\n");
continue;
};
if (guess > answer) {
%%io.stdout.printf("Guess lower.\n");
} else if (guess < answer) {
%%io.stdout.printf("Guess higher.\n");
} else {
%%io.stdout.printf("You win!\n");
return;
}
}
}