~reesmichael1/gridz

0d8271b0775417cc7658a7c367a516c8b601fc55 — Michael Rees 5 months ago add02c3
Ask players to discard generators when max reached
2 files changed, 41 insertions(+), 6 deletions(-)

M src/game.zig
M src/player.zig
M src/game.zig => src/game.zig +3 -1
@@ 17,7 17,9 @@ const Market = @import("resource_market.zig").Market;
const Player = player_mod.Player;
const Resource = @import("resource.zig").Resource;

fn getCitiesToShow(comptime T: type, count: T) []const u8 {
/// If we're talking about multiple cities, then return the string "cities."
/// Otherwise, return the string "city."
pub fn getCitiesToShow(comptime T: type, count: T) []const u8 {
    if (count == 1) {
        return "city";
    }

M src/player.zig => src/player.zig +38 -5
@@ 4,6 4,8 @@ const Allocator = std.mem.Allocator;

const constants = @import("constants.zig");
const gen_mod = @import("generator.zig");
const input = @import("input.zig");
const getCitiesToShow = @import("game.zig").getCitiesToShow;

const City = @import("city.zig").City;
const Generator = gen_mod.Generator;


@@ 64,15 66,46 @@ pub const Player = struct {
        defer gens.deinit();

        if (self.generators.len == constants.max_gens) {
            unreachable; // TODO: ask the player to remove a generator
        }
            const stdout = std.io.getStdOut().outStream();
            try stdout.print("You have reached the maximum number of generators.\n", .{});
            try stdout.print("You currently own these generators:\n\n", .{});
            for (self.generators) |generator| {
                try stdout.print("Generator {}: uses {} {} to power {} {}\n", .{
                    generator.index,
                    generator.resource_count,
                    generator.resource,
                    generator.can_power,
                    getCitiesToShow(usize, generator.can_power),
                });
            }

        for (self.generators) |generator| {
            try gens.append(generator);
            try stdout.print("\n", .{});

            while (true) {
                const remove_ix = try input.getNumberFromUser(u8, "Please enter the index of the generator you will discard: ", .{});

                for (self.generators) |generator| {
                    if (generator.index != remove_ix) {
                        try gens.append(generator);
                    }
                }

                if (gens.items.len == constants.max_gens) {
                    try stdout.print("You do not own generator {}.\n", .{remove_ix});
                    gens.deinit();
                    gens = std.ArrayList(Generator).init(self.allocator);
                } else {
                    break;
                }
            }
        } else {
            for (self.generators) |generator| {
                try gens.append(generator);
            }
        }

        try gens.append(gen);
        self.generators = try std.mem.dupe(self.allocator, Generator, gens.items);
        self.generators = gens.toOwnedSlice();
        std.sort.sort(Generator, self.generators, gen_mod.genComp);
    }