-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
142 lines (118 loc) · 3.32 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <wchar.h>
#include <curses.h>
#include <unistd.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <strings.h>
#include <locale.h>
#include "protos.h"
#define rowPrompt LINES / 2 - 1
#define colPrompt COLS / 2 - 15
// Centered title screen
void showTitleScreen();
// Fonction for the gameplay
void playGame(Grid *grid);
int main()
{
srand(time(NULL));
initscr(); // Initialize curses library
curs_set(0);
start_color(); // Activate colors
init_pair(1, COLOR_YELLOW, COLOR_BLACK);
setlocale(LC_ALL, "");
keypad(stdscr, TRUE);
int choice;
char filename[50];
showTitleScreen();
char *menuChoices[] = {
"New game",
"New custom game",
"Load game",
"Continue game",
"Save Game",
"Leave"};
int numMenuChoices = sizeof(menuChoices) / sizeof(menuChoices[0]);
// Initialize the grid with malloc
Grid *grid = (Grid *)malloc(sizeof(Grid));
if (grid == NULL)
{
perror("Error while allocating memory");
endwin();
return 1;
}
do
{
clear();
// Only show "continue" if there's a save file
if (hasSaveFile("autosave.txt"))
{
menuChoices[3] = "Continue game";
menuChoices[4] = "Save Game";
}
else
{
menuChoices[3] = ""; // Empty option if no file
menuChoices[4] = "";
}
choice = showMenu("Main menu", menuChoices, numMenuChoices);
switch (choice)
{
case 0: // Start New Game
chooseSize(grid);
break;
case 1: // Create a custom sized game
chooseCustomSize(grid);
break;
case 2: // Load Game
clear();
curs_set(2);
mvprintw(LINES / 2 - 4 , COLS / 2 - 37, "If the selected file is incorrect, you will be redirected to the current game. ");
mvprintw(rowPrompt, colPrompt, "Enter the file name: ");
refresh();
getstr(filename);
curs_set(0);
loadGame(grid, filename);
playGame(grid);
break;
case 3: // Continue Game
loadGame(grid, "autosave.txt"); // Load the autosave
if (strlen(menuChoices[2]) > 0)
{
playGame(grid);
break;
}
case 4: // Save Game
loadGame(grid, "autosave.txt"); // Load the autosave
if (hasSaveFile("autosave.txt"))
{
clear();
curs_set(2);
mvprintw(rowPrompt, colPrompt,("Enter the file name to save: "));
refresh();
getstr(filename);
curs_set(0);
saveGame(grid, filename);
break;
}
case 5: // Quit
break;
default:
printw("Invalid choice, please retry.\n");
refresh();
}
} while (choice != numMenuChoices - 1);
// Free the used memory
for (int i = 0; i < grid->rows; ++i)
{
free(grid->lights[i]);
}
free(grid->lights);
free(grid);
endwin(); // Stop curses library utilization
return 0;
}