~tieong/zig-guessing-game

ec23b345d2b1b351bfe73d0c9f2ec88d3006f72d — Thomas Ieong 11 months ago master
Init
4 files changed, 109 insertions(+), 0 deletions(-)

A .gitignore
A README.md
A build.zig
A src/main.zig
A  => .gitignore +0 -0
A  => README.md +20 -0
@@ 1,20 @@
# Zig guessing game

Guessing game to practice my zig.

The release has been built for linux x86_64

# Usage

The number of tries is set to 10 and secret number is somewhere
between 0 and 100.

```
./zig-guessing-game
```

# Build

```
zig build
```

A  => build.zig +34 -0
@@ 1,34 @@
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
    // Standard target options allows the person running `zig build` to choose
    // what target to build for. Here we do not override the defaults, which
    // means any target is allowed, and the default is native. Other options
    // for restricting supported target set are available.
    const target = b.standardTargetOptions(.{});

    // Standard release options allow the person running `zig build` to select
    // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
    const mode = b.standardReleaseOptions();

    const exe = b.addExecutable("zig-guessing-game", "src/main.zig");
    exe.setTarget(target);
    exe.setBuildMode(mode);
    exe.install();

    const run_cmd = exe.run();
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);

    const exe_tests = b.addTest("src/main.zig");
    exe_tests.setTarget(target);
    exe_tests.setBuildMode(mode);

    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&exe_tests.step);
}

A  => src/main.zig +55 -0
@@ 1,55 @@
const std = @import("std");
const os = @import("std").os;
const fmt = @import("std").fmt;
const mem = @import("std").mem;
const rand = @import("std").rand;

const stdout = std.io.getStdOut().writer();
const stdin = std.io.getStdIn().reader();

pub fn main() anyerror!void {
    var prng = rand.DefaultPrng.init(blk: {
        var seed: u64 = undefined;
        try os.getrandom(mem.asBytes(&seed));
        break :blk seed;
    });

    const secret_number: u8 = prng.random().uintAtMost(u8, 100);

    var buf: [255]u8 = undefined;

    var i: u8 = 0;
    var tries: u8 = 0;

    try stdout.print("Please input an integer \n", .{});

    while (i < 10) : ({
        i += 1;
        tries += 1;
    }) {
        var my_input = try stdin.readUntilDelimiter(buf[0..], '\n');

        var my_input_number = fmt.parseInt(u8, my_input, 10) catch |err| {
            if (err == fmt.ParseIntError.InvalidCharacter) {
                try stdout.print("'{s}' is not a valid input!\n", .{buf});
                try stdout.print("Please enter an integer!\n", .{});
                continue;
            }

            continue;
        };

        if (my_input_number == secret_number) {
            try stdout.print("WON, the solution was {d}\n", .{secret_number});
            try stdout.print("Number of tries : {d}\n", .{i});
            return;
        } else if (my_input_number < secret_number) {
            try stdout.print("Higher!\n", .{});
        } else if (my_input_number > secret_number) {
            try stdout.print("Lower!\n", .{});
        } else {}
    }

    try stdout.print("LOST\n", .{});
    try stdout.print("Number of tries : {d}\n", .{i});
}