Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ struct board_display {
WINDOW *lines_window;
};

enum rotation { none, right, invert, left };

static struct board_display *boards = NULL;
static int nboards;
// flag used to indicate that the window layout needs to be refreshed
Expand Down
3 changes: 1 addition & 2 deletions src/solo_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ void print_board() {
printf("\n\e[0;0m");
}
printf(" ");
printf("Points: %d Lines: %d", game_contents->points,
game_contents->lines_cleared);
printf("Points: %d Lines: %d", gvd->points, gvd->lines_cleared);
printf("\n");
}

Expand Down
1 change: 1 addition & 0 deletions src/tetris_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "os_compat.h"
#include "tetris_game.h"
#include "tetris_game_priv.h"

/*
* Destroys a game_contents and frees memory
Expand Down
86 changes: 4 additions & 82 deletions src/tetris_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,16 @@

#include <stddef.h>

#define MAX_BLOCK_UNITS 4
#define BLOCK_START_POSITION \
{ 4, 21 }

#define MAX_AUTO_LOWER 3
#define MAX_SWAP_H 1

#define BOARD_HEIGHT 24
#define BOARD_PLAY_HEIGHT 20
#define BOARD_WIDTH 10

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
struct game_contents;

enum rotation {
none,
right,
invert,
left,
struct position {
int x;
int y;
};
#define ROT_COUNT 4

enum block_type {
no_type,
Expand All @@ -43,74 +33,6 @@ enum block_type {
smashboy,
};

enum block_rotation_type { no_rotate, center_based, corner_based };

struct position {
int x;
int y;
};

struct tetris_block {
enum block_type type;
enum block_rotation_type rotation_type;
unsigned int cell_count;
const struct position *position_offsets;
};

static const struct position orange_block_offsets[] = {
{0, 0}, {0, -1}, {1, -1}, {0, 1}};

static const struct position blue_block_offsets[] = {
{0, 0}, {0, -1}, {-1, -1}, {0, 1}};

static const struct position cleve_block_offsets[] = {
{0, 0}, {-1, 0}, {-1, 1}, {0, -1}};

static const struct position rhode_block_offsets[] = {
{0, 0}, {-1, 0}, {-1, -1}, {0, 1}};

static const struct position teewee_block_offsets[] = {
{0, 0}, {1, 0}, {0, 1}, {-1, 0}};

static const struct position hero_block_offsets[] = {
{0, -1}, {0, 0}, {0, 1}, {0, 2}};

static const struct position smashboy_block_offsets[] = {
{0, 0}, {0, 1}, {1, 0}, {1, 1}};

static const struct tetris_block available_blocks[] = {
{orange, center_based, 4, orange_block_offsets},
{blue, center_based, 4, blue_block_offsets},
{cleve, center_based, 4, cleve_block_offsets},
{rhode, center_based, 4, rhode_block_offsets},
{teewee, center_based, 4, teewee_block_offsets},
{hero, corner_based, 4, hero_block_offsets},
{smashboy, corner_based, 4, smashboy_block_offsets}};

static const struct tetris_block tetris_block_null = {no_type, no_rotate, 0,
NULL};

struct active_block {
struct tetris_block tetris_block;
enum rotation rotation;
/* position of block center */
struct position position;
struct position board_units[MAX_BLOCK_UNITS];
};

struct game_contents {
int points;
int lines_cleared;
int auto_lower_count;
int swap_h_block_count;
int board[BOARD_HEIGHT][BOARD_WIDTH];
unsigned int seed;
struct tetris_block next_block;
struct tetris_block hold_block;
struct active_block *active_block;
struct active_block *shadow_block;
};

struct game_view_data {
int points;
int lines_cleared;
Expand Down
93 changes: 93 additions & 0 deletions src/tetris_game_priv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* tetris-game-priv.h
* Copyright (C) 2019-2021 nitepone <admin@night.horse>
*
* Distributed under terms of the MIT license.
*/

#ifndef TETRIS_GAME_PRIV_H
#define TETRIS_GAME_PRIV_H

#include <stddef.h>

#define MAX_BLOCK_UNITS 4
#define BLOCK_START_POSITION \
{ 4, 21 }

#define MAX_AUTO_LOWER 3
#define MAX_SWAP_H 1

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

enum rotation {
none,
right,
invert,
left,
};
#define ROT_COUNT 4

enum block_rotation_type { no_rotate, center_based, corner_based };

struct tetris_block {
enum block_type type;
enum block_rotation_type rotation_type;
unsigned int cell_count;
const struct position *position_offsets;
};

static const struct position orange_block_offsets[] = {
{0, 0}, {0, -1}, {1, -1}, {0, 1}};

static const struct position blue_block_offsets[] = {
{0, 0}, {0, -1}, {-1, -1}, {0, 1}};

static const struct position cleve_block_offsets[] = {
{0, 0}, {-1, 0}, {-1, 1}, {0, -1}};

static const struct position rhode_block_offsets[] = {
{0, 0}, {-1, 0}, {-1, -1}, {0, 1}};

static const struct position teewee_block_offsets[] = {
{0, 0}, {1, 0}, {0, 1}, {-1, 0}};

static const struct position hero_block_offsets[] = {
{0, -1}, {0, 0}, {0, 1}, {0, 2}};

static const struct position smashboy_block_offsets[] = {
{0, 0}, {0, 1}, {1, 0}, {1, 1}};

static const struct tetris_block available_blocks[] = {
{orange, center_based, 4, orange_block_offsets},
{blue, center_based, 4, blue_block_offsets},
{cleve, center_based, 4, cleve_block_offsets},
{rhode, center_based, 4, rhode_block_offsets},
{teewee, center_based, 4, teewee_block_offsets},
{hero, corner_based, 4, hero_block_offsets},
{smashboy, corner_based, 4, smashboy_block_offsets}};

static const struct tetris_block tetris_block_null = {no_type, no_rotate, 0,
NULL};

struct active_block {
struct tetris_block tetris_block;
enum rotation rotation;
/* position of block center */
struct position position;
struct position board_units[MAX_BLOCK_UNITS];
};

struct game_contents {
int points;
int lines_cleared;
int auto_lower_count;
int swap_h_block_count;
int board[BOARD_HEIGHT][BOARD_WIDTH];
unsigned int seed;
struct tetris_block next_block;
struct tetris_block hold_block;
struct active_block *active_block;
struct active_block *shadow_block;
};

#endif /* !TETRIS_GAME_PRIV_H */
1 change: 1 addition & 0 deletions test/test_tetris_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "tetris_game.h"
#include "tetris_game_priv.h"
#include "unity.h"

/* Is run before every test, put unit init calls here. */
Expand Down