Skip to content

Commit

Permalink
Decompose into individual files
Browse files Browse the repository at this point in the history
  • Loading branch information
zedr committed Jul 10, 2023
1 parent 5441a31 commit 0a2902c
Show file tree
Hide file tree
Showing 29 changed files with 1,776 additions and 982 deletions.
688 changes: 688 additions & 0 deletions .clang-format

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: make test
run: make test
- name: make tests
run: make tests
- name: test_queue
run: ./build/test_queue
- name: test_tinycols
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ build/
old
_*.c
_*.h
.clang-format
.idea
.cores
tmp/
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 16 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
.PHONY: default all test clean

CC ?= gcc
STD := c99

SHELL = /bin/bash
CFLAGS = -g -DLINUX_TARGET -Wall -Werror -pedantic -std=c99
CSOURCES = src/tinycols.c src/queue.c
SHELL := /bin/bash
SOURCES := src/tinycols/*.c src/queue.c
INCDIR := include/
INCLUDES = $(shell find $(INCDIR) -type f -name *.h)
INC := -I ${INCLUDES}
COMPILE := -g -DLINUX_TARGET -Wall -Werror -pedantic -std=${STD}
CFLAGS := ${COMPILE}

default: build/tinycols

Expand All @@ -17,20 +22,24 @@ build/test_tinycols: build
@${CC} ${CFLAGS} \
-D_POSIX_C_SOURCE=199309L \
-o build/test_tinycols \
lib/*.c tests/test_tinycols.c ${CSOURCES}
lib/*.c tests/test_tinycols.c ${SOURCES}

build/test_queue: build
@${CC} ${CFLAGS} \
-D_POSIX_C_SOURCE=199309L \
-o build/test_queue \
lib/*.c tests/test_queue.c ${CSOURCES}
lib/*.c src/queue.c src/tinycols/*.c tests/test_queue.c

build/tinycols: build
@${CC} ${CFLAGS} -std=gnu99 -lncurses \
-o build/tinycols \
src/main.c src/gfx.c ${CSOURCES}
src/main.c src/gfx.c ${SOURCES}

test: build/test_tinycols build/test_queue
tests: build/test_tinycols build/test_queue

test:
@build/test_tinycols
@build/test_queue

clean:
@rm -rf build
4 changes: 3 additions & 1 deletion src/gfx.h → include/gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include <ncurses.h>
#include <stdlib.h>

#include "tinycols.h"
#include "../include/tinycols/game.h"
#include "../include/tinycols/piece.h"
#include "../include/tinycols/grid.h"

WINDOW *setup_gfx(void);

Expand Down
6 changes: 1 addition & 5 deletions src/queue.h → include/queue.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
//
// Created by zedr on 07/04/2023.
//

#ifndef _QUEUE_H_
#define _QUEUE_H_

#include "tinycols.h"
#include "tinycols/game.h"

#define QUEUE_SLOTS_MAX UINT8_MAX
#define QUEUE_SLOT_EVENTS_MAX 128
Expand Down
13 changes: 13 additions & 0 deletions include/tinycols.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef TINYCOLS_H
#define TINYCOLS_H

#include "tinycols/color.h"
#include "tinycols/direction.h"
#include "tinycols/game.h"
#include "tinycols/game_utils.h"
#include "tinycols/grid.h"
#include "tinycols/piece.h"
#include "tinycols/result.h"
#include "tinycols/score.h"

#endif //TINYCOLS_H
45 changes: 45 additions & 0 deletions include/tinycols/color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef TINYCOLS_COLOR_H
#define TINYCOLS_COLOR_H

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#include "grid.h"

#define COLOR_MAX YELLOW

/**
* color - All the colors of a jewel piece.
*/
enum color {
TRANSPARENT,
PURPLE,
ORANGE,
BLUE,
RED,
GREEN,
YELLOW
};

/**
* get_cell_color() - Get the grid cell color at the given row and column.
*
* @param gr: The grid to inspect.
* @param row: The row of the cell.
* @param col: The column of the cell.
* @return The color of the given cell.
*/
enum color get_cell_color(const struct grid *gr, int row, int col);

/**
* set_cell_color() - Set the grid cell color at the given row and column.
*
* @param gr: The grid to inspect.
* @param row: The row of the cell.
* @param col: The column of the cell.
* @param clr: The color to set.
*/
void set_cell_color(struct grid *gr, int row, int col, enum color clr);

#endif //TINYCOLS_COLOR_H
9 changes: 9 additions & 0 deletions include/tinycols/direction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef TINYCOLS_DIRECTION_H
#define TINYCOLS_DIRECTION_H

/**
* direction - The direction a piece can take.
*/
enum direction { UP, DOWN, LEFT, RIGHT };

#endif //TINYCOLS_DIRECTION_H
96 changes: 96 additions & 0 deletions include/tinycols/game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#ifndef TINYCOLS_GAME_H
#define TINYCOLS_GAME_H

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#include "color.h"
#include "piece.h"
#include "score.h"

#define GAME_DEFAULT_LEVEL 0
#define GAME_DEFAULT_COLOR_MIN RED
#define GAME_DEFAULT_COLOR_MAX YELLOW
#define GAME_DEFAULT_JEWELS_FOR_LEVEL 30
#define GAME_SCORE_PER_PIECE 30

/**
* game_status - The possible states a game can take.
*/
enum game_state { GAME_READY, GAME_PAUSED, GAME_OVER, GAME_EXIT };

/**
* game_class - The game skill class, mapped to the appropriate colors
*/
enum game_class {
CLASS_NOVICE = RED,
CLASS_AMATEUR = GREEN,
CLASS_PRO = YELLOW
};

/**
* game - A game of Tiny Cols
*
* @param level: The current difficulty level of the game (starts from zero).
* @param score: The current total score.
* @param last_score: The last sub-score that was recorded in-game.
* @param jewels_removed: The total number of jewels that were removed.
* @param status: The current game status.
* @param color_max: The maximum color that can be used in-game.
* This is linked to the difficulty level.
* @param current_piece: The current in-game piece, controlled by the player.
* @param next_piece: The next piece to be played.
* @param grid: The game grid.
*/
struct game {
unsigned short level;
uint8_t tick;
game_score_t score;
game_score_t last_score;
uint_least16_t jewels_removed;
enum game_state status;
enum color color_max;
struct piece current_piece;
struct piece next_piece;
struct grid *grid;
};

/**
* game_alloc() - Allocate memory for a new game.
*
* @return The new grid, or NULL if memory allocation fails.
*/
struct game *game_alloc(void);

/**
* game_init() - Initialize a game.
*
* @param gm: The game to initialize.
* @param level: The level at which the game must be started.
* @param class: The skill class of the game
*/
void game_init(struct game *gm, unsigned int level, enum game_class class);

/**
* game_adjust() - Adjust the game according to the number of removed jewels.
*
* @param gm: The game to inspect and adjust
* @return Were the game settings changed?
*/
bool game_adjust(struct game *gm);

/**
* game_cycle_piece() - Cycle the current and next pieces in a given game.
*
* @param gm: The game to modify.
*/
void game_cycle_piece(struct game *gm);

/**
* game_free() - Free the memory allocated for a game.
*
* @param gm: The game to free.
*/
void game_free(struct game *gm);
#endif //TINYCOLS_GAME_H
24 changes: 24 additions & 0 deletions include/tinycols/game_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef TINYCOLS_GAME_UTILS_H
#define TINYCOLS_GAME_UTILS_H

#include <stdint.h>

#include "game.h"

/**
* get_level_by_jewels() - Get the level based on the given number of jewels.
*
* @param num: The current number of jewels removed
* @return The level number
*/
unsigned short get_level_by_jewels(uint_least16_t num);

/**
* calc_score() - Compute the score based on the number of jewels removed.
*
* @param jewel_count: The number of jewels that were removed.
* @return The computed score.
*/
game_score_t calc_score(unsigned int jewel_count);

#endif //TINYCOLS_GAME_UTILS_H
Loading

0 comments on commit 0a2902c

Please sign in to comment.