forked from snapshot112/minigame-menu
wip menu
This commit is contained in:
60
rooster.c
60
rooster.c
@@ -5,6 +5,8 @@
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
// TODO: End the eternal NULL checks.
|
||||
|
||||
/*
|
||||
* The rooster type this program is build around.
|
||||
*/
|
||||
@@ -16,7 +18,40 @@ typedef struct rooster_data {
|
||||
} rooster;
|
||||
|
||||
|
||||
rooster *rooster_maak(char* input);
|
||||
rooster *rooster_maak(const char* input) {
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
const size_t len = strlen(input) / sizeof(char);
|
||||
if (input == NULL || len == 0) {
|
||||
perror("rooster_maak");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (int i = 0; input[i] != '\n'; i++) {
|
||||
width++;
|
||||
}
|
||||
|
||||
height = (int)len / (width + 1);
|
||||
|
||||
for (int i = 0; i < height; i = i + width + 1) {
|
||||
if ((int)strcspn(&input[i], "\n") != width) {
|
||||
perror("rooster_maak");
|
||||
exit(1);
|
||||
};
|
||||
}
|
||||
|
||||
const int grid_size = (width + 1) * height + 1;
|
||||
|
||||
rooster *rp = malloc(sizeof(rooster));
|
||||
rp->rost = malloc(grid_size * sizeof(char));
|
||||
rp->height = height;
|
||||
rp->width = width;
|
||||
rp->state = STATE_BEGIN;
|
||||
|
||||
strcpy(rp->rost, input);
|
||||
|
||||
return rp;
|
||||
}
|
||||
|
||||
/*
|
||||
* Sets a grids width and height
|
||||
@@ -71,7 +106,7 @@ rooster *rooster_lees(FILE *fh) {
|
||||
NULL,
|
||||
0,
|
||||
0,
|
||||
BEGIN
|
||||
STATE_BEGIN
|
||||
};
|
||||
|
||||
// Sets the width and height of the rooster.
|
||||
@@ -129,23 +164,26 @@ toestand rooster_vraag_toestand(const rooster *rp) {
|
||||
if (rp != NULL) {
|
||||
return rp->state;
|
||||
}
|
||||
return VERLOREN;
|
||||
return STATE_VERLOREN;
|
||||
}
|
||||
|
||||
void rooster_zet_toestand(rooster *rp, toestand t) {
|
||||
if (rp != NULL) {
|
||||
switch (t) {
|
||||
case BEGIN:
|
||||
rp->state = BEGIN;
|
||||
case STATE_BEGIN:
|
||||
rp->state = STATE_BEGIN;
|
||||
break;
|
||||
case AAN_HET_SPELEN:
|
||||
rp->state = AAN_HET_SPELEN;
|
||||
case STATE_AAN_HET_SPELEN:
|
||||
rp->state = STATE_AAN_HET_SPELEN;
|
||||
break;
|
||||
case GEWONNEN:
|
||||
rp->state = GEWONNEN;
|
||||
case STATE_GEWONNEN:
|
||||
rp->state = STATE_GEWONNEN;
|
||||
break;
|
||||
case VERLOREN:
|
||||
rp->state = VERLOREN;
|
||||
case STATE_VERLOREN:
|
||||
rp->state = STATE_VERLOREN;
|
||||
break;
|
||||
case STATE_QUIT:
|
||||
rp->state = STATE_QUIT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user