Skip to content

Instantly share code, notes, and snippets.

@AngelOnFira
Created January 2, 2023 17:04
Show Gist options
  • Select an option

  • Save AngelOnFira/50983941febfc9cbd468604181912fd2 to your computer and use it in GitHub Desktop.

Select an option

Save AngelOnFira/50983941febfc9cbd468604181912fd2 to your computer and use it in GitHub Desktop.
const std = @import("std");
const GameState = struct {
year: u32,
trees: u32,
bank: Bank,
};
const Bank = struct {
money: u32,
seeds: u32,
bonemeal: u32,
};
const TurnState = struct {
actions_left: u32,
fn start_year(year: u32) TurnState {
return TurnState{
.actions_left = std.rand.DefaultPrng.init(year).random().int(u32) % (3 + (2 * year)) + 1,
};
}
};
const ScreenState = enum {
Main,
Plant,
Harvest,
fn print_menu(self: ScreenState) void {
switch (self) {
ScreenState.Main => {
std.debug.print("1. Plant a tree", .{});
std.debug.print("2. Harvest a tree", .{});
std.debug.print("3. Go to the bank", .{});
},
ScreenState.Plant => {
std.debug.print("How many trees would you like to plant? ", .{});
},
ScreenState.Harvest => {
std.debug.print("How many trees would you like to harvest? ", .{});
},
}
}
};
const State = struct {
game_state: GameState,
screen_state: ScreenState,
};
pub fn main() anyerror!void {
// Print out "welcome to the game"
std.debug.print("Welcome to the game", .{});
// Create a new bank
var bank = Bank{
.money = 100,
.seeds = 0,
.bonemeal = 0,
};
// Create a new game state
var game_state = GameState{
.year = 0,
.trees = 0,
.bank = bank,
};
// Create a new turn state
var turn_state = TurnState.start_year(game_state.year);
// Make the state
var state = State{
.game_state = game_state,
.screen_state = ScreenState.Main,
};
// Start the input loop
while (true) {
// Get user input
std.debug.print("What would you like to do? ", .{});
// Print the menu
state.screen_state.print_menu();
// Get user input
var buf: [10]u8 = undefined;
var input = 0;
if (try std.io.getStdIn().reader().readUntilDelimiterOrEof(buf[0..], '\n')) |user_input| {
input = std.fmt.parseInt(i64, user_input, 10);
} else |err| {
std.debug.print("Error: {s}\n", .{err});
}
// Handle the user input
handle_user_input(input, &state);
// Check if the user has any actions left
if (turn_state.actions_left == 0) {
// Start the next year
game_state.year += 1;
turn_state = TurnState.start_year(game_state.year);
}
}
}
const InputError = error{InvalidInput};
pub fn handle_user_input(input: []const u8, state: *State) InputError!void {
// Convert the input to an integer
const input_int = std.fmt.parseInt(u8, input, 10) catch {
return InputError.InvalidInput;
};
// Handle the input
switch (state.screen_state) {
ScreenState.Main => {
switch (input_int) {
1 => {
state.screen_state = ScreenState.Plant;
},
2 => {
state.screen_state = ScreenState.Harvest;
},
3 => {
state.screen_state = ScreenState.Bank;
},
else => {
std.debug.print("Invalid input", .{});
},
}
},
ScreenState.Plant => {
// Plant trees
state.game_state.trees += input_int;
},
ScreenState.Harvest => {
// Harvest trees
state.game_state.trees -= input_int;
},
}
}
// pub fn next_year(game_state: *GameState) void {}
test "basic test" {
try std.testing.expectEqual(10, 3 + 7);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment