Skip to content

Commit

Permalink
Merge pull request #6 from h3rmt-thi/070
Browse files Browse the repository at this point in the history
Fertig 070
  • Loading branch information
H3rmt authored Dec 18, 2023
2 parents 857c766 + 71824c6 commit 7dd8eb5
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 153 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ MACHINE := $(shell uname -m)
$(info $$MACHINE is $(MACHINE))
ifeq ($(MACHINE), i686)
CFLAGS = -g -Wall
LDLIBS = -lncurses
LDLIBS = -lncursesw
else ifeq ($(MACHINE), armv7l)
CFLAGS = -g -Wall
LDLIBS = -lncurses
LDLIBS = -lncursesw
else ifeq ($(MACHINE), arm64)
CFLAGS = -g -Wall
LDLIBS = -lncurses
LDLIBS = -lncursesw
else ifeq ($(MACHINE), x86_64)
CFLAGS = -g -Wall
LDLIBS = -lncurses
LDLIBS = -lncursesw
endif

#### Fixed variable definitions
Expand Down
100 changes: 91 additions & 9 deletions board_model.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,102 @@
#include "board_model.h"

#include "messages.h"
#include "worm.h"
#include <curses.h>

// Place an item onto the curses display.
void placeItem(const struct Pos pos, const chtype symbol,
int getLastRowOnBoard(const struct Board *board) { return board->last_row; }

int getLastColOnBoard(const struct Board *board) { return board->last_col; }

int getNumberOfFoodItems(const struct Board *board) {
return board->food_items;
}

enum BoardCodes getContentAt(const struct Board *board,
const struct Pos position) {
return board->cells[position.y][position.x];
}

void setNumberOfFoodItems(struct Board *board, const int n) {
board->food_items = n;
}

void decrementNumberOfFoodItems(struct Board *board) { board->food_items -= 1; }

enum ResCodes initializeBoard(struct Board *board) {
// Check dimensions of the board
if (COLS < MIN_NUMBER_OF_COLS ||
LINES < MIN_NUMBER_OF_ROWS + MA_ROWS_RESERVED) {
char buf[100];
sprintf(buf, "Das Fenster ist zu klein: wir brauchen %dx%d",
MIN_NUMBER_OF_COLS, MIN_NUMBER_OF_ROWS + MA_ROWS_RESERVED);
showDialog(buf, "Bitte eine Taste druecken");
return RES_FAILED;
}
// Maximal index of a row
board->last_row = MIN_NUMBER_OF_ROWS - 1;
// Maximal index of a column
board->last_col = MIN_NUMBER_OF_COLS - 1;
return RES_OK;
}

void placeItem(struct Board *board, const int y, const int x,
const enum BoardCodes code, const char *symbol,
const enum ColorPairs color_pair) {
// Store item on the display (symbol code)
move(pos.y, pos.x); // Move cursor to (y,x)
move(y, x); // Move cursor to (y,x)
attron(COLOR_PAIR(color_pair)); // Start writing in selected color
addch(symbol); // Store symbol on the virtual display
addstr(symbol); // Store symbol on the virtual display
attroff(COLOR_PAIR(color_pair)); // Stop writing in selected color
board->cells[y][x] = code;
}

// Get the last usable row on the display
int getLastRow() { return LINES - 1 - MA_ROWS_RESERVED; }
enum ResCodes initializeLevel(struct Board *board) {
// define local variables for loops etc
// Fill board and screen buffer with empty cells.
for (int y = 0; y <= board->last_row; y++) {
for (int x = 0; x <= board->last_col; x++) {
placeItem(board, y, x, BC_FREE_CELL, SYMBOL_FREE_CELL,
COLP_FREE_CELL);
}
}
// Draw a line in order to separate the message area
// Note: we cannot use function placeItem() since the message area
// is outside the board!
int y = board->last_row + 1;
for (int x = 0; x < board->last_col; x++) {
move(y, x);
attron(COLOR_PAIR(COLP_BARRIER));
addstr(SYMBOL_BARRIER);
attroff(COLOR_PAIR(COLP_BARRIER));
}
// Draw a line to signal the rightmost column of the board.
for (y = 0; y <= board->last_row; y++) {
placeItem(board, y, board->last_col, BC_BARRIER, SYMBOL_BARRIER,
COLP_BARRIER);
}
// Barriers: use a loop
for (y = 0; y <= 13; y++) {
placeItem(board, y + 9, board->last_col / 3, BC_BARRIER, SYMBOL_BARRIER,
COLP_BARRIER);
}
for (y = 0; y <= 13; y++) {
placeItem(board, y + 3, board->last_col / 3 * 2, BC_BARRIER,
SYMBOL_BARRIER, COLP_BARRIER);
}
// Food
placeItem(board, 3, 3, BC_FOOD_1, SYMBOL_FOOD_1, COLP_FOOD_1);
placeItem(board, 6, 6, BC_FOOD_1, SYMBOL_FOOD_1, COLP_FOOD_1);
placeItem(board, 16, 5, BC_FOOD_1, SYMBOL_FOOD_2, COLP_FOOD_2);
placeItem(board, 22, 44, BC_FOOD_1, SYMBOL_FOOD_2, COLP_FOOD_2);
placeItem(board, 4, 13, BC_FOOD_1, SYMBOL_FOOD_2, COLP_FOOD_2);
placeItem(board, 19, 9, BC_FOOD_1, SYMBOL_FOOD_2, COLP_FOOD_2);
placeItem(board, 17, 16, BC_FOOD_1, SYMBOL_FOOD_3, COLP_FOOD_3);
placeItem(board, 22, 33, BC_FOOD_1, SYMBOL_FOOD_3, COLP_FOOD_3);
placeItem(board, 15, 37, BC_FOOD_1, SYMBOL_FOOD_3, COLP_FOOD_3);
placeItem(board, 7, 56, BC_FOOD_1, SYMBOL_FOOD_3, COLP_FOOD_3);

// Get the last usable column on the display
int getLastCol() { return COLS - 1; }
// Initialize number of food items
// Attention: must match number of items placed on the board above
board->food_items = 10;
return RES_OK;
}
54 changes: 49 additions & 5 deletions board_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,56 @@

// Positions on the board
struct Pos {
int y;
int x;
int y;
int x;
};

void placeItem(struct Pos, chtype symbol, enum ColorPairs);
int getLastRow();
int getLastCol();
// Codes on the board
enum BoardCodes {
BC_FREE_CELL, // Cell is free
BC_USED_BY_WORM, // Cell occupied by worm
BC_FOOD_1, // Food type 1; if hit by worm -> bonus of type 1
BC_FOOD_2, // Food type 2; if hit by worm -> bonus of type 2
BC_FOOD_3, // Food type 3; if hit by worm -> bonus of type 3
BC_BARRIER // A barrier; if hit by worm -> game over
};

// Board
// A board structure
struct Board {
int last_row; // Last usable row on the board
int last_col; // Last usable column on the board

enum BoardCodes cells[MIN_NUMBER_OF_ROWS][MIN_NUMBER_OF_COLS];
// A 2-dimensional array for storing the contents of the board.
//
// Since the worm is not permitted to cross over itsself
// nor other elements (apart from food) we do not need a reference
// counter for occupied cells.

int food_items; // Number of food items left in the current level
};

// ### Codes for the array of positions ###
// Unused element in the worm arrays of positions
#define UNUSED_POS_ELEM (-1)
#define WORM_LENGTH (MIN_NUMBER_OF_ROWS * MIN_NUMBER_OF_COLS)
#define WORM_INITIAL_LENGTH 6

enum ResCodes initializeBoard(struct Board *);
void placeItem(struct Board *, int y, int x, enum BoardCodes,
const char *constsymbol,
enum ColorPairs);
enum ResCodes initializeLevel(struct Board *);

// Getters
int getNumberOfFoodItems(const struct Board *);
enum BoardCodes getContentAt(const struct Board *, struct Pos);
int getLastRowOnBoard(const struct Board *);
int getLastColOnBoard(const struct Board *);

// Setters
void decrementNumberOfFoodItems(struct Board *);
void setNumberOfFoodItems(struct Board *, int n);

#endif // #define _BOARD_MODEL_H
25 changes: 9 additions & 16 deletions messages.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "messages.h"

#include <curses.h>

#include "board_model.h"
#include "messages.h"
#include "worm.h"
#include "worm_model.h"

Expand All @@ -14,24 +13,18 @@ void clearLineInMessageArea(const int row) {
}
}

// Display the board line in order to separate the message area
void showBorderLine() {
const int pos_line0 = LINES - MA_ROWS_RESERVED;
for (int i = 0; i < COLS; i++) {
move(pos_line0, i);
attron(COLOR_PAIR(COLP_BARRIER));
addch(SYMBOL_BARRIER);
attroff(COLOR_PAIR(COLP_BARRIER));
}
}

// Display status about the game in the message area
void showStatus(const struct Worm *worm) {
const int pos_line2 = LINES - MA_ROWS_RESERVED + 2;
void showStatus(struct Board *board, struct Worm *worm) {
int pos_line1 = LINES - MA_ROWS_RESERVED + 1;
int pos_line2 = LINES - MA_ROWS_RESERVED + 2;
int pos_line3 = LINES - MA_ROWS_RESERVED + 3;

const struct Pos headpos = getWormHeadPos(worm);
struct Pos headpos = getWormHeadPos(worm);
mvprintw(pos_line1, 1, "Anzahl verbleibender Futterbrocken: %2d ",
getNumberOfFoodItems(board));
mvprintw(pos_line2, 1, "Wurm ist an Position: y=%3d x=%3d", headpos.y,
headpos.x);
mvprintw(pos_line3, 1, "Laenge des Wurms: %3d", getWormLength(worm));
}

// Display a dialog in the message area and wait for confirmation
Expand Down
9 changes: 4 additions & 5 deletions messages.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#ifndef _MESSAGES_H
#define _MESSAGES_H
#ifndef MESSAGES_H
#define MESSAGES_H

#include "worm_model.h"

void clearLineInMessageArea(int row);
void showBorderLine();
void showStatus(const struct Worm *);
void showStatus(struct Board *board, struct Worm *worm);
int showDialog(char *prompt1, char *prompt2);

#endif // #define _MESSAGES_H
#endif // #define MESSAGES_H
2 changes: 2 additions & 0 deletions prep.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "prep.h"

#include <curses.h>
#include <locale.h>

// Initialize application with respect to curses settings
void initializeCursesApplication() {
setlocale(LC_ALL, "");
initscr(); // Initialize the curses screen

// Note:
Expand Down
Loading

0 comments on commit 7dd8eb5

Please sign in to comment.