From 5e7f978e745499be263966669214c17884d9566b Mon Sep 17 00:00:00 2001 From: geemili Date: Sun, 21 Apr 2024 01:56:45 -0600 Subject: [PATCH] feat: detect when a player has won the game --- src/main.zig | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 3 deletions(-) diff --git a/src/main.zig b/src/main.zig index b12f0ea..da7a7db 100644 --- a/src/main.zig +++ b/src/main.zig @@ -28,6 +28,9 @@ const MENU_ITEMS = [_][]const u8{ "Quit", }; +var win_count: u32 = 0; +var win_triggered: bool = false; + pub fn init(context: *seizer.Context) !void { gpa = context.gpa; prng = std.rand.DefaultPrng.init(@bitCast(std.time.timestamp())); @@ -83,7 +86,7 @@ pub fn init(context: *seizer.Context) !void { } fn resetGame() !void { - draw_pile.shrinkRetainingCapacity(0); + draw_pile.deinit(gpa); draw_pile_exhausted = false; drawn_cards.shrinkRetainingCapacity(0); for (&stacks) |*stack| { @@ -110,6 +113,8 @@ fn resetGame() !void { try stack.append(gpa, drawn_card); } } + + win_triggered = false; } fn destroy(window: *seizer.Window) void { @@ -131,6 +136,11 @@ fn destroy(window: *seizer.Window) void { } fn render(window: *seizer.Window) !void { + if (haveWon() and !win_triggered) { + win_count += 1; + win_triggered = true; + } + gl.clearColor(0.2, 0.4, 0.2, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); @@ -146,12 +156,38 @@ fn render(window: *seizer.Window) !void { card_tilemap.?.tilesheet.tile_size[1] * scale, }; const margin = card_tilemap.?.margin * scale; + const marginf: f32 = @floatFromInt(card_tilemap.?.margin * scale); const hand_offset = [2]u32{ card_tilemap.?.hand_offset[0] * scale, card_tilemap.?.hand_offset[1] * scale, }; + const hand_offsetf = [2]f32{ + @floatFromInt(hand_offset[0]), + @floatFromInt(hand_offset[1]), + }; const tile_sizef = [2]f32{ @floatFromInt(tile_size[0]), @floatFromInt(tile_size[1]) }; + const space_taken_by_board = [2]f32{ + @floatFromInt(2 * margin + 8 * (margin + tile_size[0])), + @floatFromInt(2 * margin + 5 * (margin + tile_size[1])), + }; + const board_offset = [2]f32{ + (canvas.window_size[0] - space_taken_by_board[0]) / 2, + (canvas.window_size[1] - space_taken_by_board[1]) / 2, + }; + + { + _ = canvas.printText( + .{ + canvas.window_size[0] - board_offset[0] - marginf - hand_offsetf[0], + board_offset[1] + marginf + hand_offsetf[1], + }, + "Win Count: {}", + .{win_count}, + .{ .@"align" = .right, .scale = scalef }, + ); + } + for (&stacks, 0..) |*stack, i| { const deck_is_selected = selected_deck != null and selected_deck.? == stack; const deck_is_hovered = hovered_deck != null and hovered_deck.? == stack; @@ -163,9 +199,11 @@ fn render(window: *seizer.Window) !void { [4]u8{ 0xFF, 0xFF, 0xFF, 0xFF }; var pos = [2]f32{ - @floatFromInt(margin + ((tile_size[0] + margin) * (i + 1))), - @floatFromInt(margin + margin + tile_size[1]), + @floor(@as(f32, @floatFromInt(margin + ((tile_size[0] + margin) * (i + 1))))), + @floor(@as(f32, @floatFromInt(margin + margin + tile_size[1]))), }; + pos[0] += board_offset[0]; + pos[1] += board_offset[1]; canvas.rect(pos, tile_sizef, .{ .color = deck_color }); for (stack.items, 0..) |card, j| { @@ -187,6 +225,26 @@ fn render(window: *seizer.Window) !void { } } + if (haveWon()) { + const offset = + [2]f32{ + 2 * marginf + tile_sizef[0], + 2 * marginf + tile_sizef[1], + }; + _ = canvas.writeText( + .{ + board_offset[0] + offset[0] + (space_taken_by_board[0] - offset[0]) / 2, + board_offset[1] + offset[1] + (space_taken_by_board[1] - offset[0]) / 2, + }, + "You Win!", + .{ + .baseline = .middle, + .@"align" = .center, + .scale = 2 * scalef, + }, + ); + } + for (&foundations, 0..) |*foundation, i| { const is_hovered = hovered_deck != null and hovered_deck.? == foundation; const color = if (is_hovered) [4]u8{ 0xA0, 0xFF, 0xA0, 0xFF } else [4]u8{ 0xFF, 0xFF, 0xFF, 0xFF }; @@ -195,6 +253,8 @@ fn render(window: *seizer.Window) !void { @floatFromInt(margin), @floatFromInt(margin + ((margin + tile_size[1]) * (i + 1))), }; + pos[0] += board_offset[0]; + pos[1] += board_offset[1]; canvas.rect(pos, tile_sizef, .{ .color = color }); for (foundation.items) |card| { @@ -215,6 +275,8 @@ fn render(window: *seizer.Window) !void { @floatFromInt(margin + margin + tile_size[0]), @floatFromInt(margin), }; + pos[0] += board_offset[0]; + pos[1] += board_offset[1]; for (drawn_cards.items, 0..) |card, j| { const is_selected = deck_is_selected and j >= selected_card; const is_hovered = deck_is_hovered and j >= hovered_card; @@ -239,6 +301,8 @@ fn render(window: *seizer.Window) !void { const color = if (is_hovered) [4]u8{ 0xA0, 0xFF, 0xA0, 0xFF } else [4]u8{ 0xFF, 0xFF, 0xFF, 0xFF }; var pos = [2]f32{ @floatFromInt(margin), @floatFromInt(margin) }; + pos[0] += board_offset[0]; + pos[1] += board_offset[1]; canvas.rect(pos, tile_sizef, .{ .color = color }); for (draw_pile.items) |card| { @@ -273,6 +337,15 @@ fn render(window: *seizer.Window) !void { canvas.end(); } +pub fn haveWon() bool { + if (draw_pile.items.len > 0) return false; + if (drawn_cards.items.len > 0) return false; + for (stacks) |stack| { + if (stack.items.len > 0) return false; + } + return true; +} + pub fn makeStandardDeck(allocator: std.mem.Allocator) ![]Card { var deck = try allocator.alloc(Card, 52); errdefer allocator.free(deck);