Skip to content

Commit 0ec5ee2

Browse files
[feature] Play module initial implementation (#7)
2 parents 8d47b78 + 9e52b57 commit 0ec5ee2

File tree

17 files changed

+424
-5
lines changed

17 files changed

+424
-5
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
3+
name: "REPORT A BUG, HERE YOU HAVE A PATTERN"
4+
about: "Lets talk about bug or changes that can help us to improve the project."
5+
title: " 👉 - Short description of the bug"
6+
labels: bug
7+
assignees: ""
8+
9+
---
10+
11+
## REPORT (Necessary)👀
12+
13+
### EXPECTED BEHAVIOR
14+
15+
Describe lo que esperabas que ocurriera al realizar los pasos anteriores.
16+
Explain to us what you were expecting from the program result after you introduced the data
17+
18+
### SCREENSHOTS OR FILE THAT SUPPLY EVIDENCE
19+
20+
If you could, please attach the log error or a screenshot
21+
22+
### ADDITIONAL INFORMATIONl
23+
24+
Any important information that you want to mention.

libs/libs.cbp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
</Linker>
3535
</Target>
3636
</Build>
37+
<Unit filename="list/main.c">
38+
<Option compilerVar="CC" />
39+
</Unit>
40+
<Unit filename="list/main.h" />
3741
<Unit filename="macros.h" />
3842
<Unit filename="main.h" />
3943
<Unit filename="structs.h" />

libs/list/main.c

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#include "./main.h"
2+
3+
#include "../macros.h"
4+
5+
Node* __getElementAt(List* _list, const size_t index);
6+
7+
// Constructor
8+
void newList(List* list) { *list = NULL; }
9+
10+
// Destroyer
11+
void destroyList(List* _list) {
12+
Node* nextNode;
13+
14+
while ((*_list) != NULL) {
15+
nextNode = *_list;
16+
*(_list) = nextNode->__next;
17+
18+
free(nextNode->__data);
19+
free(nextNode);
20+
}
21+
}
22+
23+
// Getters
24+
Node* __getElementAt(List* _list, const size_t index) {
25+
size_t length = 0;
26+
27+
while ((*_list) != NULL && length != index) {
28+
_list = &(*_list)->__next;
29+
length++;
30+
}
31+
32+
return *_list;
33+
}
34+
35+
unsigned char getHead(const List* _list, void* store, const size_t sizeOfStore) {
36+
if ((*_list) == NULL) return 0;
37+
38+
memcpy(store, (*_list)->__data, MIN(sizeOfStore, (*_list)->__sizeOfData));
39+
40+
return 1;
41+
}
42+
43+
unsigned char isListEmpty(List* _list) { return (*_list) == NULL; }
44+
45+
unsigned char isListFull(List* _list, const size_t sizeOfStore) {
46+
Node* newNode;
47+
void* newNodeData;
48+
49+
newNode = malloc(sizeof(Node));
50+
newNodeData = malloc(sizeOfStore);
51+
52+
free(newNode);
53+
free(newNodeData);
54+
55+
return ((newNode == NULL) || (newNodeData == NULL));
56+
}
57+
58+
size_t getLength(List* _list) {
59+
size_t length = 0;
60+
61+
while ((*_list) != NULL) {
62+
length++;
63+
_list = &(*_list)->__next;
64+
}
65+
66+
return length;
67+
}
68+
// Methods
69+
unsigned char pushElement(List* _list, void* data, const size_t sizeOfData) {
70+
Node* newNode;
71+
72+
while ((*_list) != NULL) {
73+
_list = &(*_list)->__next;
74+
};
75+
76+
if ((newNode = malloc(sizeof(Node))) == NULL ||
77+
(newNode->__data = malloc(sizeOfData)) == NULL) {
78+
free(newNode);
79+
return 0;
80+
}
81+
82+
memcpy(newNode->__data, data, sizeOfData);
83+
newNode->__sizeOfData = sizeOfData;
84+
newNode->__next = NULL;
85+
*_list = newNode;
86+
87+
return 1;
88+
}
89+
90+
unsigned char popElement(List* _list, void* store, const size_t sizeOfStore) {
91+
if ((*_list) == NULL) {
92+
return 0;
93+
}
94+
95+
while ((*_list)->__next != NULL) {
96+
_list = &(*_list)->__next;
97+
}
98+
99+
memcpy(store, (*_list)->__data, MIN(sizeOfStore, (*_list)->__sizeOfData));
100+
free((*_list)->__data);
101+
free(*_list);
102+
*_list = NULL;
103+
104+
return 1;
105+
}
106+
107+
void randomSort(List* _list) {
108+
size_t length = 0;
109+
size_t index = 0;
110+
size_t rndIndex;
111+
112+
void* auxPointer;
113+
size_t elementSize;
114+
115+
List* head = _list;
116+
Node* helper;
117+
118+
while ((*_list) != NULL) {
119+
length++;
120+
_list = &(*_list)->__next;
121+
}
122+
123+
_list = head;
124+
125+
while ((*_list) != NULL) {
126+
while ((rndIndex = (rand() % (length))) == index);
127+
helper = __getElementAt(head, rndIndex);
128+
auxPointer = (*_list)->__data;
129+
elementSize = (*_list)->__sizeOfData;
130+
131+
(*_list)->__data = helper->__data;
132+
(*_list)->__sizeOfData = helper->__sizeOfData;
133+
134+
helper->__data = auxPointer;
135+
helper->__sizeOfData = elementSize;
136+
137+
_list = &(*_list)->__next;
138+
index++;
139+
}
140+
}

libs/list/main.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef LIBS__LIST_H_INCLUDED
2+
#define LIBS__LIST_H_INCLUDED
3+
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
#include "../structs.h"
8+
9+
/* ---------- List ---------- */
10+
11+
typedef Node* List;
12+
13+
// Constructor
14+
void newList(List* list);
15+
16+
// Destroyer
17+
void destroyList(List* _list);
18+
19+
// Getters
20+
unsigned char getHead(const List* _list, void* store, const size_t sizeOfStore);
21+
22+
unsigned char isListEmpty(List* _list);
23+
24+
unsigned char isListFull(List* _list, const size_t sizeOfStore);
25+
26+
size_t getLength(List* _list);
27+
28+
// Methods
29+
unsigned char pushElement(List* _list, void* data, const size_t sizeOfData);
30+
31+
unsigned char popElement(List* _list, void* store, const size_t sizeOfStore);
32+
33+
void randomSort(List* _list);
34+
35+
#endif // LIBS__LIST_H_INCLUDED

libs/macros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
#ifndef LIBS_MACROS_H_INCLUDED
33
#define LIBS_MACROS_H_INCLUDED
44

5-
// TODO
5+
#define MIN(A, B) ((A) < (B) ? (A) : (B))
66

77
#endif // LIBS_MACROS_H_INCLUDED

libs/main.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
#include "./structs.h"
88
#include "./utilities.h"
99

10+
// List
11+
#include "./list/main.h"
12+
1013
#endif // LIBS_MAIN_H_INCLUDED

libs/structs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
#include <stdlib.h>
66

7+
typedef struct Node {
8+
void* __data;
9+
size_t __sizeOfData;
10+
struct Node* __next;
11+
} Node;
12+
713
typedef struct {
814
size_t x;
915
size_t y;

src/api/main.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
#include "../../libs/main.h"
3+
4+
#include "./main.h"
5+
6+
unsigned char postAPI(const Configuration* config, List* list) {
7+
return 0;
8+
// TODO
9+
}
10+
11+
unsigned char createLocalFile(List* list, const char* localFilePath) {
12+
// TODO
13+
return 0;
14+
}

src/api/main.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef SRC__API_H_INCLUDED
2+
#define SRC__API_H_INCLUDED
3+
4+
#include "../../libs/main.h"
5+
#include "../configuration/main.h"
6+
7+
unsigned char postAPI(const Configuration* config, List* list);
8+
9+
unsigned char createLocalFile(List* list, const char* localFilePath);
10+
11+
#endif // SRC__API_H_INCLUDED

src/macros.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
#define CONFIGURATION_PATH "./statics/configuration.txt"
1111

12+
#define LOCAL_FILE_PATH "./statics/local-storage/informe-juego_"
13+
14+
#define LOCAL_FILE_PATH_LENGTH (sizeof(LOCAL_FILE_PATH) + sizeof("YYYY-MM-DD-HH-mm") + 1)
15+
1216
/* --------------------------------- Player --------------------------------- */
1317

1418
#define PLAYER_NAME_LENGTH 64

0 commit comments

Comments
 (0)