Piece bag WIP (Errors / No random)

This commit is contained in:
Dusk 2022-06-30 17:38:20 +02:00
parent b64efefea7
commit d77df73f61
16 changed files with 58 additions and 10 deletions

44
src/bag.zig Normal file
View File

@ -0,0 +1,44 @@
const Piece = @import("piece.zig");
const Self = @This();
contents: [14]?Piece.Type,
pub fn init() Self {
var ret = Self{
.contents = undefined,
};
// We must fill all of the array
for (ret.contents[0..6]) |_, i| {
ret.contents[i] = @intToEnum(Piece.Type, i);
}
// Then we would shuffle
for (ret.contents[7..13]) |_, i| {
ret.contents[i + 7] = @intToEnum(Piece.Type, i);
}
// Then we would shuffle
return ret;
}
pub fn pop(self: *Self) Piece.Type {
var ret = Piece.Type.i;
if (self.contents[0]) |head| {
ret = head;
for (self.contents) |_, i| {
if (self.contents[i + 1]) |next| {
self.contents[i] = next;
self.contents[i + 1] = null;
} else break;
}
if (self.contents[7] == null) {
for (self.contents[7..13]) |_, i| {
self.contents[i + 7] = @intToEnum(Piece.Type, i);
}
}
// Then we would shuffle
}
return ret;
}

View File

@ -3,6 +3,7 @@ const SDL = @import("sdl2");
const Grid = @import("grid.zig"); const Grid = @import("grid.zig");
const Piece = @import("piece.zig"); const Piece = @import("piece.zig");
const Bag = @import("bag.zig");
const movement = @import("movement.zig"); const movement = @import("movement.zig");
@ -13,7 +14,8 @@ grid_cell_size: i32,
grid_pos_x: i32, grid_pos_x: i32,
grid_pos_y: i32, grid_pos_y: i32,
bag: [14]Piece.Type, bag: Bag,
held: ?Piece, held: ?Piece,
piece: ?Piece, piece: ?Piece,
renderer: *SDL.SDL_Renderer, renderer: *SDL.SDL_Renderer,
@ -23,17 +25,19 @@ left_action: bool = false,
down_action: bool = false, down_action: bool = false,
pub fn init(renderer: *SDL.SDL_Renderer) Self { pub fn init(renderer: *SDL.SDL_Renderer) Self {
return Self{ var ret = Self {
.grid = Grid.init(), .grid = Grid.init(),
.grid_cell_size = 16, .grid_cell_size = 16,
.grid_pos_x = 16, .grid_pos_x = 16,
.grid_pos_y = 16, .grid_pos_y = 16,
.bag = undefined, .bag = Bag.init(),
.piece = Piece.init(Piece.Type.t), .piece = undefined,
.held = null, .held = null,
.renderer = renderer, .renderer = renderer,
}; };
ret.piece = Piece.init(ret.bag.pop());
return ret;
} }
fn render(self: Self) void { fn render(self: Self) void {
self.grid.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y); self.grid.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y);
@ -77,7 +81,7 @@ pub fn tick(self: *Self) void {
if (self.piece) |safe_piece| { if (self.piece) |safe_piece| {
if (safe_piece.dropped) { if (safe_piece.dropped) {
self.grid = movement.drop(self.grid, safe_piece); self.grid = movement.drop(self.grid, safe_piece);
self.piece = Piece.init(Piece.Type.t); self.piece = Piece.init(self.bag.pop());
} }
} }

View File

@ -27,7 +27,6 @@ dropped: bool = false,
color: SDL.SDL_Color, color: SDL.SDL_Color,
pub fn init(piece_type: Type) Self { pub fn init(piece_type: Type) Self {
return Self{ return Self{
.row = 0, .row = 0,

View File

@ -1,7 +1,7 @@
1271 36569768 1656334617470040282 fdaa7b4da8476360daa4621804b40f8f /home/strange/Documentos/Projectes/Programació/usg/src/main.zig 1271 36569768 1656334617470040282 fdaa7b4da8476360daa4621804b40f8f /home/strange/Documentos/Projectes/Programació/usg/src/main.zig
1271 36569768 1656334617470040282 fdaa7b4da8476360daa4621804b40f8f /home/strange/Documentos/Projectes/Programació/usg/src/main.zig 1271 36569768 1656334617470040282 fdaa7b4da8476360daa4621804b40f8f /home/strange/Documentos/Projectes/Programació/usg/src/main.zig
135300 36570392 1655299923670057346 b4c2d851673df5aab566633b89bdaa84 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl.zig 135300 36570392 1655299923670057346 b4c2d851673df5aab566633b89bdaa84 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl.zig
32 36968726 1656434162539985452 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s 32 36968726 1656603169529970766 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s
5621 36968525 1655270162000000000 66001e1a55f68e4681c1f32a69223647 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/std.zig 5621 36968525 1655270162000000000 66001e1a55f68e4681c1f32a69223647 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/std.zig
5621 36968525 1655270162000000000 66001e1a55f68e4681c1f32a69223647 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/std.zig 5621 36968525 1655270162000000000 66001e1a55f68e4681c1f32a69223647 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/std.zig
29901 36968437 1655270162000000000 5c441d88acbf5e7fe2a26137df2b5376 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/builtin.zig 29901 36968437 1655270162000000000 5c441d88acbf5e7fe2a26137df2b5376 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/builtin.zig
@ -21,14 +21,15 @@
121502 36968568 1655270162000000000 4bad05f05e4a10364e515a13d2fa2b3f /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/os/linux/io_uring.zig 121502 36968568 1655270162000000000 4bad05f05e4a10364e515a13d2fa2b3f /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/os/linux/io_uring.zig
135300 36570392 1655299923670057346 b4c2d851673df5aab566633b89bdaa84 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl.zig 135300 36570392 1655299923670057346 b4c2d851673df5aab566633b89bdaa84 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl.zig
1640 36570393 1655299923670057346 c3d50ea43c9e10688a521a9ac927ded4 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl_image.zig 1640 36570393 1655299923670057346 c3d50ea43c9e10688a521a9ac927ded4 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl_image.zig
32 36968726 1656434162539985452 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s 32 36968726 1656603169529970766 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s
7338 36968289 1655270162000000000 ee4fd6c02929ff52d99a8faa2539dce8 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/zig.zig 7338 36968289 1655270162000000000 ee4fd6c02929ff52d99a8faa2539dce8 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/zig.zig
21121 36968483 1655270162000000000 669c911cf26ce07c5f5516d896b30e33 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/zig/c_translation.zig 21121 36968483 1655270162000000000 669c911cf26ce07c5f5516d896b30e33 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/zig/c_translation.zig
113573 36968636 1655270162000000000 2c3980264c753ba754322c7c8c4f2634 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/mem.zig 113573 36968636 1655270162000000000 2c3980264c753ba754322c7c8c4f2634 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/mem.zig
2326 36570413 1656433976089986564 b5923a8a7d90c6a495a5914d992e6f25 /home/strange/Documentos/Projectes/Programació/usg/src/game.zig 2399 36570413 1656603168279970768 d4d51b79488448e23f39e4e84787d496 /home/strange/Documentos/Projectes/Programació/usg/src/game.zig
1359 36569821 1656418680670025505 5643391f858689faa3fe97fa8d814454 /home/strange/Documentos/Projectes/Programació/usg/src/grid.zig 1359 36569821 1656418680670025505 5643391f858689faa3fe97fa8d814454 /home/strange/Documentos/Projectes/Programació/usg/src/grid.zig
342 36569823 1656079597800036836 fcaa0d5efa9177d38e199b7727f8f020 /home/strange/Documentos/Projectes/Programació/usg/src/cell.zig 342 36569823 1656079597800036836 fcaa0d5efa9177d38e199b7727f8f020 /home/strange/Documentos/Projectes/Programació/usg/src/cell.zig
3497 36569819 1656433455109989671 609c6515376d60dd45ea803bc24f2389 /home/strange/Documentos/Projectes/Programació/usg/src/piece.zig 1044 36570092 1656602936009971256 dd01945b52fd457a1747c416231ca4d8 /home/strange/Documentos/Projectes/Programació/usg/src/bag.zig
3496 36570196 1656599489839978486 b265adb85e30c82ae22f0bb2d5a8dd49 /home/strange/Documentos/Projectes/Programació/usg/src/piece.zig
45023 36968356 1655270162000000000 c30e605ecc099ad6f5498460429756fa /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/elf.zig 45023 36968356 1655270162000000000 c30e605ecc099ad6f5498460429756fa /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/elf.zig
5432 36968458 1655270162000000000 624956409eee1e93746e77415e9cdca9 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/atomic.zig 5432 36968458 1655270162000000000 624956409eee1e93746e77415e9cdca9 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/atomic.zig
25568 36968501 1655270162000000000 f15ad6de023e2f7205ca6550cee7becd /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/atomic/Atomic.zig 25568 36968501 1655270162000000000 f15ad6de023e2f7205ca6550cee7becd /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/atomic/Atomic.zig

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.