From e27357cadd54bf9fa0fb87945a19f16bd478b7d9 Mon Sep 17 00:00:00 2001 From: thmaillarb Date: Mon, 6 Dec 2021 11:34:53 +0100 Subject: [PATCH] Added IslandOrBridge + converter --- Pontu/include/model/IslandOrBridge.h | 42 ++++++++++++++++++++++++++++ Pontu/src/model/IslandOrBridge.c | 32 +++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 Pontu/include/model/IslandOrBridge.h create mode 100644 Pontu/src/model/IslandOrBridge.c diff --git a/Pontu/include/model/IslandOrBridge.h b/Pontu/include/model/IslandOrBridge.h new file mode 100644 index 0000000..e9ceccb --- /dev/null +++ b/Pontu/include/model/IslandOrBridge.h @@ -0,0 +1,42 @@ +/** + * \file IslandOrBridge.h + * \brief Used to convert board coordinates into usable ones + * \author Théotime Maillarbaux + * \date 06/12/2021 + */ + +#ifndef ISLANDORBRIDGE_H +#define ISLANDORBRIDGE_H + +#include "model/Coord.h" + +/** + * \enum EntityType + * \brief Indicates which entity was clicked. + */ +typedef enum { + WATER, ///< Nothing in particular was clicked + ISLAND, ///< An Island was clicked + VBRIDGE, ///< A vertical bridge was clicked + HBRIDGE ///< A horizontal bridge was clicked +} EntityType; + +/** + * \struct IslandOrBridge + * \brief Represents a set of coordinates coherent with the CoordType. + */ +typedef struct { + int x; ///< The coordinate on the X-axis. + int y; ///< The coordinate on the Y-axis. + EntityType type; ///< The type of the entity clicked. +} IslandOrBridge; + +/** + * \brief Converts a set of board coordinates into a usable set of coordinates. + * \param[in] c The set of board coordinates. + * \return An IslandOrBridge struct with coherent coordinates with the type of entity. + */ +IslandOrBridge coordToEntity(Coord c); + +#endif // ISLANDORBRIDGE_H + diff --git a/Pontu/src/model/IslandOrBridge.c b/Pontu/src/model/IslandOrBridge.c new file mode 100644 index 0000000..dcc2a4d --- /dev/null +++ b/Pontu/src/model/IslandOrBridge.c @@ -0,0 +1,32 @@ +#include "model/IslandOrBridge.h" + +IslandOrBridge(Coord c) { + IslandOrBridge res; + + if (c.x % 2 == 0) { + if (c.y % 2 == 0) { + // Island + res.x = c.x/2; + res.y = c.y/2; + res.type = ISLAND; + } else { // c.y % 2 != 0 + // Vertical bridge + res.x = c.x/2; + res.y = c.y - 1; + res.type = VBRIDGE; + } + } else { // c.x % 2 != 0 + if (c.y % 2 == 0) { + // Horizontal bridge + res.x = c.x - 1; + res.y = c.y / 2; + res.type = HBRIDGE; + } else { // c.y % 2 != 0 + // Nothing in particular + res.type = WATER; + } + } + + return res; +} +