Created
August 19, 2022 11:42
-
-
Save 1010real/1e2424dc61bb854f4ac8cfada352d1a4 to your computer and use it in GitHub Desktop.
first try zig - number 5 game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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