Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

050 #5

Merged
merged 4 commits into from
Dec 18, 2023
Merged
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
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bin/
out/
50 changes: 25 additions & 25 deletions Makefile
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
############################################################
# 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
HEADERS += prep.h
HEADERS += worm.h
HEADERS += worm_model.h
HEADERS += board_model.h
#Please add all header files in./ here
FILES += prep
FILES += worm
FILES += worm_model
FILES += messages
FILES += board_model

# 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 All @@ -51,23 +46,28 @@ RM_DIR = rm -rf
MKDIR = mkdir
SHELL = /bin/bash
BIN_DIR = bin
OBJ_DIR = out

OBJECTS=$(patsubst %, $(OBJ_DIR)/%.o, $(FILES))

#### Default target
all: $(BIN_DIR) $(TARGET)

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

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

#### Binaries
$(TARGET) : $(OBJECTS)
$(TARGET) : $(OBJ_DIR) $(OBJECTS)
$(CC) $(CFLAGS) -o $@ $(OBJECTS) $(LDLIBS)

$(BIN_DIR):
$(MKDIR) $(BIN_DIR)

$(OBJ_DIR):
$(MKDIR) $(OBJ_DIR)

.PHONY: clean
clean :
$(RM_DIR) $(BIN_DIR) $(OBJECTS)
$(RM_DIR) $(BIN_DIR) $(OBJ_DIR)
24 changes: 9 additions & 15 deletions board_model.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
#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) {

void placeItem(const struct Pos pos, const chtype symbol,
const 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(pos.y, pos.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 - MA_ROWS_RESERVED; }

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

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

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

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

#endif // #define _BOARD_MODEL_H
#endif // #define _BOARD_MODEL_H
75 changes: 75 additions & 0 deletions messages.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "messages.h"

#include <curses.h>

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

// Clear an entire line on the display
void clearLineInMessageArea(const int row) {
move(row, 0);
for (int i = 1; i <= COLS; i++) {
addch(' ');
}
}

// 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;

const struct Pos headpos = getWormHeadPos(worm);
mvprintw(pos_line2, 1, "Wurm ist an Position: y=%3d x=%3d", headpos.y,
headpos.x);
}

// Display a dialog in the message area and wait for confirmation
// String prompt1 is displayed in the second line of the message area
// String prompt2 is displayed in the third line of the message area
int showDialog(char *prompt1, char *prompt2) {
const int pos_line1 = LINES - MA_ROWS_RESERVED + 1;
const int pos_line2 = LINES - MA_ROWS_RESERVED + 2;
const int pos_line3 = LINES - MA_ROWS_RESERVED + 3;

if (prompt1 == NULL) {
return RES_FAILED;
}

// Delete lines in the message area
clearLineInMessageArea(pos_line1);
clearLineInMessageArea(pos_line2);
clearLineInMessageArea(pos_line3);

// Display message
mvprintw(pos_line2, 1, "%s", prompt1);
if (prompt2 != NULL) {
mvprintw(pos_line3, 1, "%s", prompt2);
}
refresh();

nodelay(stdscr, FALSE);
const int ch = getch(); // Wait for user to press an arbitrary key
nodelay(stdscr, TRUE);

// Delete lines in the message area
clearLineInMessageArea(pos_line1);
clearLineInMessageArea(pos_line2);
clearLineInMessageArea(pos_line3);

// Display changes
refresh();

// Return code of key pressed
return ch;
}
11 changes: 11 additions & 0 deletions messages.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef _MESSAGES_H
#define _MESSAGES_H

#include "worm_model.h"

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

#endif // #define _MESSAGES_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 @@
void initializeCursesApplication();
void cleanupCursesApp();

#endif // #define _PREP_H
#endif // #define _PREP_H
Loading