Basic stdio Wordle game
This commit is contained in:
85
src/main.zig
85
src/main.zig
@@ -1,5 +1,86 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const Wordle = @import("wordle.zig");
|
||||||
|
const Dict = @import("dict.zig");
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() !void {
|
||||||
std.debug.print("Hello, {s}!\n", .{"World"});
|
// acquire stdout writer
|
||||||
|
var stdoutBuf: [1024]u8 = undefined;
|
||||||
|
var stdoutFile = std.fs.File.stdout().writer(&stdoutBuf);
|
||||||
|
if (stdoutFile.err) |err| return err;
|
||||||
|
const stdout = &stdoutFile.interface;
|
||||||
|
|
||||||
|
// acquire stdin reader
|
||||||
|
var stdinBuf: [1024]u8 = undefined;
|
||||||
|
var stdinFile = std.fs.File.stdin().reader(&stdinBuf);
|
||||||
|
if (stdinFile.err) |err| return err;
|
||||||
|
const stdin = &stdinFile.interface;
|
||||||
|
|
||||||
|
// select a random dictionary word as the solution
|
||||||
|
const dict = Dict.getDictionary();
|
||||||
|
const solutionIdx = std.crypto.random.intRangeAtMost(usize, 0, dict.len);
|
||||||
|
|
||||||
|
// create a game with the chosen solution
|
||||||
|
var game: Wordle.Game = .{ .solution = dict[solutionIdx] };
|
||||||
|
|
||||||
|
// play game until over
|
||||||
|
while (!game.isOver()) {
|
||||||
|
// display guess prompt
|
||||||
|
try stdout.writeAll("Guess: ");
|
||||||
|
try stdout.flush();
|
||||||
|
|
||||||
|
// read next guess
|
||||||
|
const input = try stdin.takeDelimiter('\n') orelse continue;
|
||||||
|
|
||||||
|
// assert that input is correct size
|
||||||
|
if (input.len != Wordle.WordLen) {
|
||||||
|
try stdout.writeAll("Guess is wrong length.\n");
|
||||||
|
try stdout.flush();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// convert to word
|
||||||
|
var guess: Wordle.Word = undefined;
|
||||||
|
@memcpy(&guess, input);
|
||||||
|
|
||||||
|
// test if guess is valid
|
||||||
|
if (!Dict.isInDictionary(guess)) {
|
||||||
|
try stdout.writeAll("Guess is not in dictionary.\n");
|
||||||
|
try stdout.flush();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// display clues
|
||||||
|
const clues = Wordle.checkGuess(guess, game.solution);
|
||||||
|
try writeClues(stdout, clues);
|
||||||
|
|
||||||
|
// add guess
|
||||||
|
try game.addGuess(guess);
|
||||||
|
}
|
||||||
|
|
||||||
|
// display message win/lose
|
||||||
|
if (game.isWon()) {
|
||||||
|
try stdout.writeAll("You win!\n");
|
||||||
|
} else {
|
||||||
|
try stdout.writeAll("You lose :(\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// display spoilered guesses
|
||||||
|
for (game.guesses()) |guess| {
|
||||||
|
try writeClues(stdout, Wordle.checkGuess(guess, game.solution));
|
||||||
|
}
|
||||||
|
|
||||||
|
// flush stdout
|
||||||
|
try stdout.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn writeClues(out: *std.io.Writer, clues: Wordle.Clues) !void {
|
||||||
|
for (clues) |clue| {
|
||||||
|
try out.writeAll(switch (clue) {
|
||||||
|
Wordle.Clue.Gray => "⬛",
|
||||||
|
Wordle.Clue.Yellow => "🟨",
|
||||||
|
Wordle.Clue.Green => "🟩",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try out.writeAll("\n");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user