Bag preview rendering, bag randomization

This commit is contained in:
Dusk 2022-07-01 01:49:22 +02:00
parent d77df73f61
commit 9e7244833e
56 changed files with 71 additions and 61 deletions

View File

@ -1,43 +1,56 @@
const std = @import("std");
const Piece = @import("piece.zig");
const Self = @This();
contents: [14]?Piece.Type,
prng: std.rand.DefaultPrng,
pub fn init() Self {
var ret = Self{
.contents = undefined,
.prng = std.rand.DefaultPrng.init(@intCast(u64, std.time.milliTimestamp())),
};
// We must fill all of the array
for (ret.contents[0..6]) |_, i| {
for (ret.contents[0..7]) |_, i| {
std.debug.print("Bag: {any}\n", .{ret.contents});
ret.contents[i] = @intToEnum(Piece.Type, i);
}
// Then we would shuffle
// Then we shuffle
ret.prng.random().shuffle(?Piece.Type, ret.contents[0..7]);
for (ret.contents[7..13]) |_, i| {
for (ret.contents[7..14]) |_, i| {
std.debug.print("Bag: {any}\n", .{ret.contents});
ret.contents[i + 7] = @intToEnum(Piece.Type, i);
}
// Then we would shuffle
// Then we shuffle
ret.prng.random().shuffle(?Piece.Type, ret.contents[7..14]);
return ret;
}
pub fn pop(self: *Self) Piece.Type {
var ret = Piece.Type.i;
var ret = Piece.Type.i; // TODO
if (self.contents[0]) |head| {
ret = head;
for (self.contents) |_, i| {
//std.debug.print("Bag: {any}\n", .{self.contents});
if (i + 1 >= self.contents.len) {
break;
}
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| {
for (self.contents[7..14]) |_, i| {
self.contents[i + 7] = @intToEnum(Piece.Type, i);
}
self.prng.random().shuffle(?Piece.Type, self.contents[7..14]);
}
// Then we would shuffle
}
return ret;

View File

@ -5,6 +5,7 @@ const Grid = @import("grid.zig");
const Piece = @import("piece.zig");
const Bag = @import("bag.zig");
const preview = @import("preview.zig");
const movement = @import("movement.zig");
const Self = @This();
@ -44,10 +45,12 @@ fn render(self: Self) void {
if (self.piece) |safe_piece| {
safe_piece.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y);
}
preview.render(self.renderer, self.bag, self.grid_cell_size, self.grid_pos_x + self.grid_cell_size * (Grid.ncolumns + 1), self.grid_pos_y);
}
pub fn tick(self: *Self) void {
if (self.piece) |safe_piece| {
std.debug.print("Piece: Col: {} Row: {}\n", .{ safe_piece.col, safe_piece.row });
//std.debug.print("Piece: Col: {} Row: {}\n", .{ safe_piece.col, safe_piece.row });
//std.debug.print("Bag: {any}\n", .{self.bag.contents});
var key_state = SDL.SDL_GetKeyboardState(null);

View File

@ -4,66 +4,34 @@ const Piece = @import("piece.zig");
pub fn moveRight(grid: Grid, piece: Piece) Piece {
var new_piece = piece;
new_piece.col += 1;
for (new_piece.structure) |_, y| {
for (new_piece.structure[y]) |_, x| {
if (new_piece.structure[y][x] and
@intCast(i32, x) + new_piece.col > Grid.ncolumns - 1)
{
if (checkCollision(grid, new_piece)) {
return piece;
}
if (new_piece.structure[y][x] and
!grid.cells[@intCast(usize, new_piece.row + @intCast(i32, y))][@intCast(usize, new_piece.col + @intCast(i32, x))].free)
{
return piece;
}
}
}
} else {
return new_piece;
}
}
pub fn moveLeft(grid: Grid, piece: Piece) Piece {
var new_piece = piece;
new_piece.col -= 1;
for (new_piece.structure) |_, y| {
for (new_piece.structure[y]) |_, x| {
if (new_piece.structure[y][x] and
@intCast(i32, x) + new_piece.col < 0)
{
if (checkCollision(grid, new_piece)) {
return piece;
}
if (new_piece.structure[y][x] and
!grid.cells[@intCast(usize, new_piece.row + @intCast(i32, y))][@intCast(usize, new_piece.col + @intCast(i32, x))].free)
{
return piece;
}
}
}
} else {
return new_piece;
}
}
pub fn moveDown(grid: Grid, piece: Piece) Piece {
var new_piece = piece;
new_piece.row += 1;
for (new_piece.structure) |_, y| {
for (new_piece.structure[y]) |_, x| {
if (new_piece.structure[y][x] and
@intCast(i32, y) + new_piece.row > Grid.nrows - 1)
{
if (checkCollision(grid, new_piece)) {
new_piece.row -= 1;
new_piece.dropped = true;
return new_piece;
}
if (new_piece.structure[y][x] and
!grid.cells[@intCast(usize, new_piece.row + @intCast(i32, y))][@intCast(usize, new_piece.col + @intCast(i32, x))].free)
{
new_piece.row -= 1;
new_piece.dropped = true;
} else {
return new_piece;
}
}
}
return new_piece;
}
pub fn drop(grid: Grid, piece: Piece) Grid {
var new_grid = grid;
@ -79,3 +47,14 @@ pub fn drop(grid: Grid, piece: Piece) Grid {
return new_grid;
}
fn checkCollision(grid: Grid, piece: Piece) bool {
for (piece.structure) |_, y| {
for (piece.structure[y]) |_, x| {
if (piece.structure[y][x] and ((@intCast(i32, x) + piece.col > Grid.ncolumns - 1) or (@intCast(i32, x) + piece.col < 0) or (@intCast(i32, y) + piece.row > Grid.nrows - 1) or (!grid.cells[@intCast(usize, piece.row + @intCast(i32, y))][@intCast(usize, piece.col + @intCast(i32, x))].free))) {
return true;
}
}
}
return false;
}

12
src/preview.zig Normal file
View File

@ -0,0 +1,12 @@
const SDL = @import("sdl2");
const Bag = @import("bag.zig");
const Piece = @import("piece.zig");
pub fn render(renderer: *SDL.SDL_Renderer, bag: Bag, cell_size: i32, pos_x: i32, pos_y: i32) void {
for (bag.contents[0..5]) |_, i| {
if (bag.contents[i]) |piece_type| {
Piece.init(piece_type).render(renderer, cell_size, pos_x, pos_y + @intCast(i32, i) * cell_size * 4);
}
}
}

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
135300 36570392 1655299923670057346 b4c2d851673df5aab566633b89bdaa84 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl.zig
32 36968726 1656603169529970766 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s
32 36968726 1656632837179949947 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
29901 36968437 1655270162000000000 5c441d88acbf5e7fe2a26137df2b5376 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/builtin.zig
@ -21,15 +21,17 @@
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
1640 36570393 1655299923670057346 c3d50ea43c9e10688a521a9ac927ded4 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl_image.zig
32 36968726 1656603169529970766 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s
32 36968726 1656632837179949947 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
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
2399 36570413 1656603168279970768 d4d51b79488448e23f39e4e84787d496 /home/strange/Documentos/Projectes/Programació/usg/src/game.zig
2649 36570091 1656629353399957256 523c3a7aeb39f3ee57d067e56613429e /home/strange/Documentos/Projectes/Programació/usg/src/game.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
1044 36570092 1656602936009971256 dd01945b52fd457a1747c416231ca4d8 /home/strange/Documentos/Projectes/Programació/usg/src/bag.zig
1614 36573582 1656632835659949950 ac671c1b9b9d285f3bf62118c3faafb9 /home/strange/Documentos/Projectes/Programació/usg/src/bag.zig
3496 36570196 1656599489839978486 b265adb85e30c82ae22f0bb2d5a8dd49 /home/strange/Documentos/Projectes/Programació/usg/src/piece.zig
16192 36968506 1655270162000000000 744a2bb13e662f350a196780c9fbcad2 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/rand.zig
3105 36968346 1655270162000000000 412e5ff0bcf84571f173caa8acab5393 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/rand/Xoshiro256.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
25568 36968501 1655270162000000000 f15ad6de023e2f7205ca6550cee7becd /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/atomic/Atomic.zig
@ -38,7 +40,7 @@
9352 36968413 1655270162000000000 8c526bbafa435d2af666b490a1e838ad /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/Thread/Mutex.zig
3400 36968452 1655270162000000000 5d1d518c32f4fd700943417f56120125 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/io/writer.zig
41490 36968415 1655270162000000000 f3d5b2d6057676d8866cf9291ef27dee /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/Thread/Futex.zig
2668 36570091 1656434160779985462 f3c4e20f05debba69b5c21f77e7e35f9 /home/strange/Documentos/Projectes/Programació/usg/src/movement.zig
1819 36570413 1656628035579960021 a4d209e3de383732caf3ac9ebb5ae6fe /home/strange/Documentos/Projectes/Programació/usg/src/movement.zig
102705 36968384 1655270162000000000 4896646ce9abce74b86bcbbc275a18c1 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/fmt.zig
31107 36968497 1655270162000000000 efe5272d00145b6eca03d42fcf5fcd30 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/mem/Allocator.zig
87065 36968504 1655270162000000000 64f7d52fa9f0953194c996773f4ed130 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/hash_map.zig
@ -57,12 +59,13 @@
7395 36968424 1655270162000000000 fe0e04bcd58863e97993f2137b09a3ca /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/dwarf/AT.zig
50955 36968340 1655270162000000000 89afffa3bc72846bab2c1ed13b0672f4 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/fs/path.zig
7889 36968577 1655270162000000000 562fc4eee397a5037d16dffd5e048cd4 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/os/linux/errno/generic.zig
12529 36968527 1655270162000000000 2e8a443dd5c55df11dc0ea7ce5b52bf5 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/time.zig
663 36569831 1656078583710029346 74acba9445f0ebf3147733e83d02a6d1 /home/strange/Documentos/Projectes/Programació/usg/src/color.zig
412 36573579 1656629341849957280 ed9e5a84332a53905bd9184ccb8d55ab /home/strange/Documentos/Projectes/Programació/usg/src/preview.zig
932 36968507 1655270162000000000 b4c3b5276113dacf836baae9d9f94c34 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/event.zig
68808 36968298 1655270162000000000 1428e5a8bcb96a47d038d40214331d68 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/event/loop.zig
32411 36968509 1655270162000000000 a25cf125aa2af22a7263d8104b549cb9 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/unicode.zig
44495 36968491 1655270162000000000 bfcc36624c8f3cb045fc58558f6aa79d /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/process.zig
12529 36968527 1655270162000000000 2e8a443dd5c55df11dc0ea7ce5b52bf5 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/time.zig
76200 36968550 1655270162000000000 35261e8277652ce34bf8d0d6761b4bab /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/os/linux/syscalls.zig
1174 36968430 1655270162000000000 1a71f479a0d0d95f1b0d960381e246ee /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/hash.zig
10122 36968518 1655270162000000000 6372ee29c64a8e176138e879d43eb625 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/hash/wyhash.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.

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.

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.

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.

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.