initial commit

main
2wenty1ne 2024-10-14 00:03:51 +02:00
commit 750970b73f
1 changed files with 44 additions and 0 deletions

44
Zahlenraten.zig 100644
View File

@ -0,0 +1,44 @@
const std = @import("std");
pub fn main() !void {
const rand = std.crypto.random;
const reader = std.io.getStdIn().reader();
const writer = std.io.getStdOut().writer();
try writer.print("Welcome to my number guessing game! \n", .{});
try writer.print("Enter an upper limit\n", .{});
const upperLimit = oneIntInput(writer, reader);
try writer.print("Your upper limit is {d}! \n", .{upperLimit});
for (0..10) |i| {
const randomInt = rand.intRangeLessThan(u16, 0, 3);
try writer.print("Random number {d}: {d} \n\n", .{i, randomInt});
}
}
pub fn oneIntInput(writer: anytype, reader: anytype) !i64 {
var buffer: [256]u8 = undefined;
while (true) {
try writer.print("> ", .{});
const amountInputBytes = try reader.read(&buffer);
const input_slice: []u8 = buffer[0..(amountInputBytes - 1)];
const result: i64 = std.fmt.parseInt(i64, input_slice, 10) catch |err| {
switch (err) {
error.InvalidCharacter => {
try writer.print("Error: Invalid character, try again! \n", .{});
},
error.Overflow => {
try writer.print("Error: Too big for an i64, try again! \n", .{});
},
}
try writer.print("\n", .{});
continue;
};
return result;
}
}