-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
81 lines (69 loc) · 1.44 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
#include <time.h>
#include "utf8.h"
#include "player.h"
#include "game.h"
#include "stringbuilder.h"
#include "gamedefs.h"
#include "display.h"
#include "io.h"
void display_stats(game g)
{
printf("Round %lu:\nPlayer: %s\nScore: %lu\nMoves: %lu\nElapsed Time: %ld sec\n", g->round, g->p->name, g->p->score, g->turn, time(0) - g->start_time);
}
int main() {
setlocale(LC_ALL, "UTF-8");
clear_screen();
char *pname;
pname = readline("Please enter a name for player 1: ");
player p = create_player(pname);
free(pname);
game g = create_game(p);
// Input loop
unsigned char ending = 0;
while(!ending)
{
clear_screen();
char* b = board_string(g->board, 0, 0, g->board_width, g->board_height);
printf("%s", b);
free(b);
display_stats(g);
char c = getkey();
switch(c)
{
case 'a':
take_turn(g, LEFT);
break;
case 's':
take_turn(g, DOWN);
break;
case 'd':
take_turn(g, RIGHT);
break;
case 'w':
take_turn(g, UP);
break;
default:
printf("Unknown keystroke, use AWSD to move\n");
pause_term();
break;
}
if(game_is_win(g))
{
g->round++;
score_round(g);
clear_screen();
display_stats(g);
pause_term();
game_reset(g); // Regenerates the maze and increases the round
}
}
// Cleanup
destroy_game(g);
destroy_player(p);
}