Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
H3rmt committed Dec 6, 2023
1 parent 4aefc09 commit c064341
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 112 deletions.
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TabWidth: 4
UseTab: "Never"
IndentWidth: 4
24 changes: 12 additions & 12 deletions Makefile
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
############################################################
# General purpose makefile
#General purpose makefile
#
# Works for all C-projects where
# - binaries are compiled into sub-dir bin
# - binaries are created from a multiple c-sources
# and depend on all headers and object files in ./
#Works for all C - projects where
#- binaries are compiled into sub - dir bin
#- binaries are created from a multiple c - sources
#and depend on all headers and object files in./
#
# Note: due to the dependencies encoded multiple targets
# are not sensible
#Note : due to the dependencies encoded multiple targets
#are not sensible
#

# Please add all header files in ./ here
#Please add all header files in./ here
HEADERS += prep.h
HEADERS += worm.h
HEADERS += worm_model.h
HEADERS += board_model.h

# Please add all object files in ./ here
#Please add all object files in./ here
OBJECTS += prep.o
OBJECTS += worm.o
OBJECTS += worm_model.o
OBJECTS += board_model.o

# Please add THE target in ./bin here
#Please add THE target in./ bin here
TARGET += $(BIN_DIR)/worm

#################################################
# There is no need to edit below this line
#There is no need to edit below this line
#################################################

MACHINE := $(shell uname -m)
Expand Down Expand Up @@ -57,7 +57,7 @@ all: $(BIN_DIR) $(TARGET)

#### Fixed build rules for binaries with multiple object files

# Object files
#Object files
%.o : %.c $(HEADERS)
$(CC) -c $(CFLAGS) $<

Expand Down
18 changes: 7 additions & 11 deletions board_model.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
#include "board_model.h"

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

// Place an item onto the curses display.
void placeItem(int y, int x, chtype symbol, enum ColorPairs color_pair) {

// Store item on the display (symbol code)
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
attroff(COLOR_PAIR(color_pair)); // Stop writing in selected color
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
attroff(COLOR_PAIR(color_pair)); // Stop writing in selected color
}

// Getters

// Get the last usable row on the display
int getLastRow() {
return LINES - 1;
}
int getLastRow() { return LINES - 1; }

// Get the last usable column on the display
int getLastCol() {
return COLS - 1;
}
int getLastCol() { return COLS - 1; }
4 changes: 2 additions & 2 deletions board_model.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef _BOARD_MODEL_H
#define _BOARD_MODEL_H

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

extern void placeItem(int y, int x, chtype symbol, enum ColorPairs color_pair);
extern int getLastRow();
extern int getLastCol();

#endif // #define _BOARD_MODEL_H
#endif // #define _BOARD_MODEL_H
20 changes: 10 additions & 10 deletions prep.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ void initializeCursesApplication() {
initscr(); // Initialize the curses screen

// Note:
// The call to initscr() defines various global variables of the curses framework.
// stdscr, LINES, COLS, TRUE, FALSE
// The call to initscr() defines various global variables of the curses
// framework. stdscr, LINES, COLS, TRUE, FALSE

noecho(); // Characters typed ar not echoed
cbreak(); // No buffering of stdin
nonl(); // Do not translate 'return key' on keyboard to newline character
noecho(); // Characters typed ar not echoed
cbreak(); // No buffering of stdin
nonl(); // Do not translate 'return key' on keyboard to newline character
keypad(stdscr, TRUE); // Enable the keypad
curs_set(0); // Make cursor invisible
// Begin in non-single-step mode (getch will not block)
nodelay(stdscr, TRUE); // make getch to be a non-blocking call
nodelay(stdscr, TRUE); // make getch to be a non-blocking call
}

// Reset display to normale state and terminate curses application
void cleanupCursesApp(void) {
standend(); // Turn off all attributes
refresh(); // Write changes to terminal
curs_set(1); // Set cursor state to normal visibility
endwin(); // Terminate curses application
standend(); // Turn off all attributes
refresh(); // Write changes to terminal
curs_set(1); // Set cursor state to normal visibility
endwin(); // Terminate curses application
}
2 changes: 1 addition & 1 deletion prep.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
extern void initializeCursesApplication();
extern void cleanupCursesApp();

#endif // #define _PREP_H
#endif // #define _PREP_H
78 changes: 41 additions & 37 deletions worm.c
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
#include <curses.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

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

// void initializeColors();

// void readUserInput(enum GameStates *agame_state);

// enum ResCodes doLevel();


// Initialize colors of the game
void initializeColors() {
// Define colors of the game
Expand All @@ -34,27 +33,27 @@ void readUserInput(enum GameStates *agame_state) {
// Is there some user input?
// Blocking or non-blocking depends of config of getch
switch (ch) {
case 'q' : // User wants to end the show
*agame_state = WORM_GAME_QUIT;
break;
case KEY_UP :// User wants up
setWormHeading(WORM_UP);
break;
case KEY_DOWN :// User wants down
setWormHeading(WORM_DOWN);
break;
case KEY_LEFT :// User wants left
setWormHeading(WORM_LEFT);
break;
case KEY_RIGHT :// User wants right
setWormHeading(WORM_RIGHT);
break;
case 's' : // User wants single step
nodelay(stdscr, FALSE);
break;
case ' ' : // Terminate single step; make getch non-blocking again
nodelay(stdscr, TRUE);
break;
case 'q': // User wants to end the show
*agame_state = WORM_GAME_QUIT;
break;
case KEY_UP: // User wants up
setWormHeading(WORM_UP);
break;
case KEY_DOWN: // User wants down
setWormHeading(WORM_DOWN);
break;
case KEY_LEFT: // User wants left
setWormHeading(WORM_LEFT);
break;
case KEY_RIGHT: // User wants right
setWormHeading(WORM_RIGHT);
break;
case 's': // User wants single step
nodelay(stdscr, FALSE);
break;
case ' ': // Terminate single step; make getch non-blocking again
nodelay(stdscr, TRUE);
break;
}
}
return;
Expand All @@ -66,7 +65,7 @@ enum ResCodes doLevel() {
enum ResCodes res_code; // Result code from functions
bool end_level_loop; // Indicates whether we should leave the main loop

int bottomLeft_y, bottomLeft_x; // Start positions of the worm
int bottomLeft_y, bottomLeft_x; // Start positions of the worm

// At the beginnung of the level, we still have a chance to win
game_state = WORM_GAME_ONGOING;
Expand All @@ -76,7 +75,8 @@ enum ResCodes doLevel() {
bottomLeft_y = getLastRow();
bottomLeft_x = 0;

res_code = initializeWorm(bottomLeft_y, bottomLeft_x, WORM_RIGHT, COLP_USER_WORM, WORM_LENGTH);
res_code = initializeWorm(bottomLeft_y, bottomLeft_x, WORM_RIGHT,
COLP_USER_WORM, WORM_LENGTH);
if (res_code != RES_OK) {
return res_code;
}
Expand All @@ -94,7 +94,8 @@ enum ResCodes doLevel() {
readUserInput(&game_state);
if (game_state == WORM_GAME_QUIT) {
end_level_loop = true;
continue; // Go to beginning of the loop's block and check loop condition
continue; // Go to beginning of the loop's block and check loop
// condition
}

// Process userworm
Expand All @@ -108,12 +109,15 @@ enum ResCodes doLevel() {

// Bail out of the loop if something bad happened
if (game_state != WORM_GAME_ONGOING) {
// placeItem(theworm_wormpos_y[theworm_headindex], theworm_wormpos_x[theworm_headindex], SYMBOL_WORM_INNER_ELEMENT, COLP_DATA);
showWorm(true);
refresh();
// placeItem(theworm_wormpos_y[theworm_headindex],
// theworm_wormpos_x[theworm_headindex], SYMBOL_WORM_INNER_ELEMENT,
// COLP_DATA);
showWorm(true);
refresh();
end_level_loop = true;
napms(NAP_TIME * 10);
continue; // Go to beginning of the loop's block and check loop condition
continue; // Go to beginning of the loop's block and check loop
// condition
}

// Sleep a bit before we show the updated window
Expand All @@ -137,17 +141,17 @@ enum ResCodes doLevel() {
}

int main(void) {
int res_code; // Result code from functions
int res_code; // Result code from functions

// Here we start
initializeCursesApplication(); // Init various settings of our application
initializeColors(); // Init colors used in the game
initializeCursesApplication(); // Init various settings of our application
initializeColors(); // Init colors used in the game

// Maximal LINES and COLS are set by curses for the current window size.
// Note: we do not cope with resizing in this simple examples!

// Check if the window is large enough to display messages in the message area
// a has space for at least one line for the worm
// Check if the window is large enough to display messages in the message
// area a has space for at least one line for the worm
if (LINES < MIN_NUMBER_OF_ROWS || COLS < MIN_NUMBER_OF_COLS) {
// Since we not even have the space for displaying messages
// we print a conventional error message via printf after
Expand Down
10 changes: 6 additions & 4 deletions worm.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ enum ColorPairs {
};

// Dimensions and bounds
#define NAP_TIME 100 // Time in milliseconds to sleep between updates of display
#define MIN_NUMBER_OF_ROWS 3 // The guaranteed number of rows available for the board
#define MIN_NUMBER_OF_COLS 10 // The guaranteed number of columns available for the board
#define NAP_TIME 100 // Time in milliseconds to sleep between updates of display
#define MIN_NUMBER_OF_ROWS \
3 // The guaranteed number of rows available for the board
#define MIN_NUMBER_OF_COLS \
10 // The guaranteed number of columns available for the board
#define WORM_LENGTH 30 // Maximal length of the worm

// Numbers for color pairs used by curses macro COLOR_PAIR
Expand All @@ -36,4 +38,4 @@ enum GameStates {
WORM_GAME_QUIT,
};

#endif // #define _WORM_H
#endif // #define _WORM_H
Loading

0 comments on commit c064341

Please sign in to comment.