Skip to content

Instantly share code, notes, and snippets.

@1010real
Created August 19, 2022 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1010real/1e2424dc61bb854f4ac8cfada352d1a4 to your computer and use it in GitHub Desktop.
Save 1010real/1e2424dc61bb854f4ac8cfada352d1a4 to your computer and use it in GitHub Desktop.
first try zig - number 5 game
const std = @import("std");
const time = std.time;
const prng = std.rand.DefaultPrng;
pub fn main() anyerror!void {
const stdin = std.io.getStdIn().reader();
const stdout = std.io.getStdOut().writer();
try stdout.print("Welcome, {s}!\n", .{"5 of Number Game!"});
var buf: [6]u8 = undefined;
var intall: i64 = undefined;
var int: [5]i64 = undefined;
var rand: [5]i64 = undefined;
var randall = @intCast(i64, @rem(prng.init(@intCast(u64, time.milliTimestamp())).random().int(u64), 100000));
// try stdout.print("Answer Number is {d}\n", .{randall});
rand[0] = try std.math.divFloor(i64, randall, 10000);
rand[1] = try std.math.divFloor(i64, @rem(randall, 10000), 1000);
rand[2] = try std.math.divFloor(i64, @rem(randall, 1000), 100);
rand[3] = try std.math.divFloor(i64, @rem(randall, 100), 10);
rand[4] = try std.math.divFloor(i64, @rem(randall, 10), 1);
while(true) {
if (try stdin.readUntilDelimiterOrEof(buf[0..], '\n')) |user_input| {
try stdout.print("Input Number is {s}\n", .{user_input});
intall = try std.fmt.parseInt(i64, user_input, 10);
int[0] = try std.math.divFloor(i64, intall, 10000);
int[1] = try std.math.divFloor(i64, @rem(intall, 10000), 1000);
int[2] = try std.math.divFloor(i64, @rem(intall, 1000), 100);
int[3] = try std.math.divFloor(i64, @rem(intall, 100), 10);
int[4] = try std.math.divFloor(i64, @rem(intall, 10), 1);
for (int) |val, index| {
try stdout.print("{d}st Input Number is {d} -> {b}\n", .{index+1, val, val == rand[index]});
}
}
if (int[0] == rand[0] and int[1] == rand[1] and int[2] == rand[2] and int[3] == rand[3] and int[4] == rand[4]) {
try stdout.print("Great! Answer Number is {d}! You Won!\n", .{randall});
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment