commit 7ca6c6fbca0f472aac50d020ec622c7ec5f66ca7
parent 170cef5140d70307b7042353108f6e45975a4f6f
Author: Christos Margiolis <christos@margiolis.net>
Date: Thu, 13 May 2021 20:02:13 +0300
might be needed
Diffstat:
18 files changed, 533 insertions(+), 3 deletions(-)
diff --git a/c-os2/ex2/ex3_server.c b/c-os2/ex2/ex3_server.c
@@ -12,7 +12,7 @@
* Εργαστήριο ΛΣ2 (Δ6) / Εργασία 2: Άσκηση 3 (server) / 2020-2021
* Ονοματεπώνυμο: Χρήστος Μαργιώλης
* ΑΜ: 19390133
- * Τρόπος μεταγλώττισης: `cc ex3_server.c -lpthread -o ex3_server`
+ * Τρόπος μεταγλώττισης: `cc ex3_server.c -o ex3_server`
*/
/* Results to be sent back to the client. */
@@ -180,7 +180,7 @@ main(int argc, char *argv[])
if (bind(sfd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
die("bind");
- if (listen(sfd, BACKLOG) == -1)
+ if (listen(sfd, backlog) == -1)
die("listen");
f = emalloc(sizeof(struct foo));
diff --git a/cpp-oop/5-spreadsheets/res/errlog.csv b/cpp-oop/5-spreadsheets/res/errlog.csv
@@ -1773,4 +1773,3 @@ Student;SR001962;Missing
Student;SR001963;Missing
Student;SR001964;Missing
Student;SR001965;Missing
-Student;SR001966;Missing
diff --git a/cpp-oop/game/Engine.cc b/cpp-oop/game/Engine.cc
@@ -0,0 +1,196 @@
+#include "Engine.hpp"
+
+enum Color {
+ WALL = 1,
+ PATH,
+ POTTER,
+ GNOME,
+ TRAAL,
+ LAST
+};
+
+
+Engine::Engine()
+{
+}
+
+Engine::~Engine()
+{
+ for (auto&& e : entities)
+ if (e != NULL)
+ delete e;
+ (void)endwin();
+}
+
+void
+Engine::init(const char *mapfile, const char *scorefile)
+{
+ if (!load_map(mapfile))
+ throw "load_map failed: " + std::string(mapfile);
+ if (!init_curses())
+ throw "init_curses failed";
+ if (!init_entities())
+ throw "init_entities failed";
+ if (!init_score(scorefile))
+ throw "init_score failed: " + std::string(scorefile);
+ f_running = 1;
+
+ /* Initialize player */
+}
+
+bool
+Engine::load_map(const char *mapfile)
+{
+ std::ifstream f;
+ std::vector<char> row;
+ char c;
+
+ f.exceptions(std::ifstream::badbit);
+ f.open(mapfile);
+ if (!f.is_open())
+ return false;
+ while (f.get(c)) {
+ if (f.eof())
+ break;
+ row.push_back(c);
+ if (c == '\n') {
+ map.push_back(row);
+ row.clear();
+ }
+ }
+ f.close();
+
+ return true;
+}
+
+bool
+Engine::init_curses()
+{
+ /* Initialize curses(3) environment */
+ if (!initscr())
+ return false;
+ noecho();
+ cbreak();
+ curs_set(false);
+ keypad(stdscr, true);
+ set_escdelay(0);
+ /* TODO: make it async */
+
+ xmax = getmaxx(stdscr);
+ ymax = getmaxy(stdscr);
+
+ /* Enable and initialize color pairs */
+ colors.push_back(COLOR_BLUE); /* Wall */
+ colors.push_back(COLOR_GREEN); /* Path */
+ colors.push_back(COLOR_MAGENTA); /* Potter */
+ colors.push_back(COLOR_CYAN); /* Gnome */
+ colors.push_back(COLOR_YELLOW); /* Traal */
+
+ start_color();
+ use_default_colors();
+ for (int i = 1; i < Color::LAST; i++)
+ (void)init_pair(i, colors[i-1], -1);
+
+ return true;
+}
+
+bool
+Engine::init_entities()
+{
+ srand(time(nullptr));
+
+ entities.push_back(new Potter(15, 10, Movable::Direction::DOWN, 'P'));
+ entities.push_back(new Gnome(30, 20, Movable::Direction::DOWN, 'G'));
+ entities.push_back(new Traal(50, 26, Movable::Direction::DOWN, 'T'));
+
+ player = (Potter *)entities[0];
+
+ return true;
+}
+
+bool
+Engine::init_score(const char *scorefile)
+{
+ return true;
+}
+
+void
+Engine::kbd_input()
+{
+ int key, dir;
+
+ switch (key = getch()) {
+ case KEY_LEFT:
+ dir = Movable::Direction::LEFT;
+ break;
+ case KEY_RIGHT:
+ dir = Movable::Direction::RIGHT;
+ break;
+ case KEY_UP:
+ dir = Movable::Direction::UP;
+ break;
+ case KEY_DOWN:
+ dir = Movable::Direction::DOWN;
+ break;
+ case 'q':
+ f_running = 0;
+ return;
+ }
+
+ player->set_newpos(dir, xmax, ymax);
+}
+
+void
+Engine::collisions()
+{
+}
+
+void
+Engine::upd_score()
+{
+}
+
+void
+Engine::redraw()
+{
+ int color;
+
+ erase();
+ /* TODO: add scores and stuff */
+ printw("(%d, %d)", player->get_x(), player->get_y());
+ mvhline(1, 0, ACS_HLINE, xmax);
+ move (2, 0);
+ attron(A_REVERSE);
+ for (const auto& row : map) {
+ for (const auto& c : row) {
+ if (c == '*')
+ color = COLOR_PAIR(Color::WALL);
+ else if (c == ' ')
+ color = COLOR_PAIR(Color::PATH);
+ attron(color);
+ addch(c);
+ attroff(color);
+ }
+ }
+ for (const auto& entity : entities) {
+ if (dynamic_cast<Potter *>(entity) != nullptr)
+ color = COLOR_PAIR(Color::POTTER);
+ else if (dynamic_cast<Gnome *>(entity) != nullptr)
+ color = COLOR_PAIR(Color::GNOME);
+ else if (dynamic_cast<Traal *>(entity) != nullptr)
+ color = COLOR_PAIR(Color::TRAAL);
+
+ attron(color);
+ mvaddch(entity->get_y(), entity->get_x(),
+ entity->get_sym());
+ attroff(color);
+ }
+ attroff(A_REVERSE);
+ refresh();
+}
+
+bool
+Engine::is_running()
+{
+ return f_running;
+}
diff --git a/cpp-oop/game/Engine.hpp b/cpp-oop/game/Engine.hpp
@@ -0,0 +1,48 @@
+#ifndef _ENGINE_HPP_
+#define _ENGINE_HPP_
+
+#include <cstdlib>
+#include <ctime>
+#include <iostream>
+#include <fstream>
+#include <string>
+#include <vector>
+
+#include <ncurses.h>
+
+#include "Gnome.hpp"
+#include "Potter.hpp"
+#include "Traal.hpp"
+#include "Score.hpp"
+
+class Engine {
+private:
+
+ std::vector<Movable *> entities;
+ std::vector<std::vector<char>> map;
+ std::vector<int> colors;
+ Potter *player;
+ Score score;
+ int xmax;
+ int ymax;
+ int f_running;
+
+public:
+ Engine();
+ ~Engine();
+
+ void init(const char *mapfile, const char *scorefile);
+ void kbd_input();
+ void collisions();
+ void upd_score();
+ void redraw();
+ bool is_running();
+
+private:
+ bool load_map(const char *mapfile);
+ bool init_curses();
+ bool init_entities();
+ bool init_score(const char *scorefile);
+};
+
+#endif /* _ENGINE_HPP_ */
diff --git a/cpp-oop/game/Gnome.cc b/cpp-oop/game/Gnome.cc
@@ -0,0 +1,6 @@
+#include "Gnome.hpp"
+
+Gnome::Gnome(int x, int y, int dir, char sym)
+ :Movable(x, y, dir, sym)
+{
+}
diff --git a/cpp-oop/game/Gnome.hpp b/cpp-oop/game/Gnome.hpp
@@ -0,0 +1,11 @@
+#ifndef _GNOME_HPP_
+#define _GNOME_HPP_
+
+#include "Movable.hpp"
+
+class Gnome: public Movable {
+public:
+ Gnome(int x, int y, int dir, char sym);
+};
+
+#endif /* _GNOME_HPP_ */
diff --git a/cpp-oop/game/Makefile b/cpp-oop/game/Makefile
@@ -0,0 +1,47 @@
+# See LICENSE file for copyright and license details.
+# game
+
+# compiler
+CC = c++
+
+# includes and libs
+INCS = -Iinclude
+LIBS = -Llib -lncursesw
+
+# flags
+CFLAGS = -std=c++14 -pedantic -Wall -Os ${INCS}
+LDFLAGS = ${LIBS}
+
+BIN = game
+
+SRC = main.cc \
+ Engine.cc \
+ Gnome.cc \
+ Movable.cc \
+ Potter.cc \
+ Score.cc \
+ Traal.cc \
+
+OBJ = ${SRC:.cc=.o}
+
+all: options ${BIN}
+
+options:
+ @echo ${BIN} build options:
+ @echo "CFLAGS = ${CFLAGS}"
+ @echo "LDFLAGS = ${LDFLAGS}"
+ @echo "CC = ${CC}"
+
+${BIN}: ${OBJ}
+ ${CC} ${LDFLAGS} ${OBJ} -o $@
+
+.cc.o:
+ ${CC} -c ${CFLAGS} $<
+
+run:
+ ./${BIN}
+
+clean:
+ rm -f ${BIN} ${OBJ} *.core
+
+.PHONY: all options clean run
diff --git a/cpp-oop/game/Movable.cc b/cpp-oop/game/Movable.cc
@@ -0,0 +1,55 @@
+#include "Movable.hpp"
+
+Movable::Movable()
+{
+}
+
+Movable::Movable(int x, int y, int dir, char sym)
+ :x(x), y(y), dir(dir), sym(sym)
+{
+}
+
+Movable::~Movable()
+{
+}
+
+void
+Movable::set_newpos(int dir, int xmax, int ymax)
+{
+ switch (dir) {
+ case Direction::LEFT:
+ if (--x < 0)
+ x = 0;
+ break;
+ case Direction::RIGHT:
+ if (++x > xmax - 1)
+ x = xmax - 1;
+ break;
+ case Direction::UP:
+ if (--y < 2)
+ y = 2;
+ break;
+ case Direction::DOWN:
+ if (++y > ymax - 1)
+ y = ymax - 1;
+ break;
+ }
+}
+
+int
+Movable::get_x()
+{
+ return x;
+}
+
+int
+Movable::get_y()
+{
+ return y;
+}
+
+char
+Movable::get_sym()
+{
+ return sym;
+}
diff --git a/cpp-oop/game/Movable.hpp b/cpp-oop/game/Movable.hpp
@@ -0,0 +1,25 @@
+#ifndef _MOVABLE_HPP_
+#define _MOVABLE_HPP_
+
+class Movable {
+protected:
+ int x;
+ int y;
+ int dir;
+ char sym;
+
+public:
+ enum Direction { LEFT, RIGHT, UP, DOWN };
+
+public:
+ Movable();
+ Movable(int x, int y, int dir, char sym);
+ virtual ~Movable();
+
+ void set_newpos(int dir, int xmax, int ymax);
+ int get_x();
+ int get_y();
+ char get_sym();
+};
+
+#endif /* _MOVABLE_HPP_ */
diff --git a/cpp-oop/game/Potter.cc b/cpp-oop/game/Potter.cc
@@ -0,0 +1,6 @@
+#include "Potter.hpp"
+
+Potter::Potter(int x, int y, int dir, char sym)
+ :Movable(x, y, dir, sym)
+{
+}+
\ No newline at end of file
diff --git a/cpp-oop/game/Potter.hpp b/cpp-oop/game/Potter.hpp
@@ -0,0 +1,11 @@
+#ifndef _POTTER_HPP_
+#define _POTTER_HPP_
+
+#include "Movable.hpp"
+
+class Potter: public Movable {
+public:
+ Potter(int x, int y, int dir, char sym);
+};
+
+#endif /* _POTTER_HPP_ */
diff --git a/cpp-oop/game/Score.cc b/cpp-oop/game/Score.cc
@@ -0,0 +1 @@
+#include "Score.hpp"
diff --git a/cpp-oop/game/Score.hpp b/cpp-oop/game/Score.hpp
@@ -0,0 +1,7 @@
+#ifndef _SCORE_HPP_
+#define _SCORE_HPP_
+
+class Score {
+};
+
+#endif /* _SCORE_HPP_ */
diff --git a/cpp-oop/game/Traal.cc b/cpp-oop/game/Traal.cc
@@ -0,0 +1,6 @@
+#include "Traal.hpp"
+
+Traal::Traal(int x, int y, int dir, char sym)
+ :Movable(x, y, dir, sym)
+{
+}+
\ No newline at end of file
diff --git a/cpp-oop/game/Traal.hpp b/cpp-oop/game/Traal.hpp
@@ -0,0 +1,11 @@
+#ifndef _TRAAL_HPP_
+#define _TRAAL_HPP_
+
+#include "Movable.hpp"
+
+class Traal: public Movable {
+public:
+ Traal(int x, int y, int dir, char sym);
+};
+
+#endif /* _TRAAL_HPP_ */
diff --git a/cpp-oop/game/main.cc b/cpp-oop/game/main.cc
@@ -0,0 +1,54 @@
+#include "Engine.hpp"
+
+/* Function declarations */
+static void die(const std::string&);
+
+/* Program name */
+static char *argv0;
+
+static void
+die(const std::string& str)
+{
+ std::cerr << argv0 << ": " << str << std::endl;
+ exit(1);
+}
+
+static void
+usage()
+{
+ std::cerr << "usage: " << argv0 << " map_file score_file" << std::endl;
+ exit(1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ Engine eng;
+ char *mapfile, *scorefile;
+
+ argv0 = *argv++;
+
+ if (argc < 3)
+ usage();
+ mapfile = *argv++;
+ scorefile = *argv;
+ if (!strcmp(mapfile, scorefile))
+ die("input files must not be the same");
+ if (!setlocale(LC_ALL, ""))
+ die("setlocale");
+
+ try {
+ eng.init(mapfile, scorefile);
+ } catch (std::string e) {
+ die(e);
+ }
+
+ while (eng.is_running()) {
+ eng.kbd_input();
+ eng.collisions();
+ eng.upd_score();
+ eng.redraw();
+ }
+
+ return 0;
+}
diff --git a/cpp-oop/game/map b/cpp-oop/game/map
@@ -0,0 +1,45 @@
+**********************************************************************************************************************************************
+********************** ******************************************************************************************************
+*************************************** ************************ **************************************************************************
+*************************************** ****************** *****************************************************************************
+*************************************** ************ ***********************************************************************************
+* **************** ****** ****************************************************************************************
+********************* **************** ** ************************ **********************
+********************* **************** ***************************** ********************************** *********** **********************
+********** **************** ******************************** ********************************** *********** **********************
+********************** **************** ******************************** ********************************** *********** **********************
+********************** **************** ******************************* ********************************** *********** **********************
+********************** ***************** ********************************** *********** **********************
+********************** ************************************************* ********************************** *********** **********************
+********************** ************************************************* ********************************** *********** **********************
+********************** ************************************************* ********************************** *********** **********************
+********************** ************************************************* ********************************** *********** **********************
+********************** ************************************************* ********* *********** **********************
+********************** ********* ************************ ***********
+*********************** ********** *********************************************** ************************ **********************************
+*********************** ********** *********************************************** ************************ **********************************
+*********************** ********** *********************************************** ************************ **********************************
+************ ********** ******************** **********************************
+************ ********************* ******************** **************************************************************************************
+************ ********************* ******************* **************************************************************************************
+************ ********************* **************************************************************************************
+************ ****************************************** **************************************************************************************
+************ ****************************************** **************************************************************************************
+************ ****************************************** **************************************************************************************
+************ *************************** *** **************************************************************************************
+************ *************************** * ******* **************************************************************************************
+************ *************************** ************** *********************************
+************ *************************** ******************************************************************* *********************************
+************ *************************** ******************************************************************* *********************************
+************ *************************** ******************************************************************* *********************************
+************ *************************** ******************************************************************* *********************************
+************ ******************************************************************* *********************************
+************************************************************************************************************ *********************************
+************************************************************************************************************ *********************************
+************************************************************************************************************ *********************************
+************************************************************************************************************ *********************************
+************************************************************************************************************ *********************************
+************************************************************************************************************ *********************************
+************************************************************************************************************ *********************************
+************************************************************************************************************ *********************************
+************************************************************************************************************ *********************************
diff --git a/cpp-oop/game/score b/cpp-oop/game/score