-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
180 lines (146 loc) · 3.94 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <limits.h>
#include <unistd.h>
#include <libgen.h>
#include "./libs/strcap.h"
#include "./libs/colors.h"
#include "./libs/CGLgeneral.h"
int checkInput(char *input);
void instructions();
void help(void);
void list(void);
#define totalGames 2
char *games[2][totalGames] = {{"tictactoe", "battleships"}, {"1", "2"}};
int main(int argc, char *argv[])
{
clear();
instructions();
while(1)
{
char input[21];
do
{
printf("ConsoleGamesLib> ");
int tmp = scanf(" %20s", input);
if (tmp == EOF || tmp == -1)
{
return 1;
}
int c;
while ((c = getchar()) != '\n' && c != EOF); // Clear input buffer
if (!(tmp > 0))
{
continue;
}
}
while ((checkInput(strlow(input)) - 1) < 0);
int gameNum = -1;
char rungame[strlen(input) + 1];
memset(rungame, 0, sizeof(rungame));
for (int i = 0; i < strlen(input); i++)
{
rungame[i] = 0;
}
gameNum = checkInput(input) - 1;
strcpy(rungame, games[0][gameNum]);
char *path = malloc(1024);
if (strlen(argv[0]) > 3)
{
strcat(path, argv[0]);
// Find the last occurrence of '/' in the program path
char *last_slash = strrchr(path, '/');
if (last_slash == NULL)
{
perror("Error finding path");
return 1;
}
sprintf(last_slash + 1, "%s", "\0");
}
else
{
sprintf(path, "/usr/local/bin/");
}
char *command = malloc(strlen(path) + strlen(rungame) + 1);
sprintf(command, "%s%s", path, rungame);
/* printf("Game is:\n%s\n", rungame);
printf("Launch cmd is:\n%s\n", command);
while (getchar() != '\n'); */
int tmp = system(command);
if (tmp == -1)
{
return 1;
}
clear();
printf(red"Welcome back\n"off);
free(command);
free(path);
}
}
int checkInput(char *input)
{
bool valid = false;
int gameNum = 0;
if (strcmp(input, "quit") == 0)
{
quit();
}
else if (strcmp(input, "help") == 0)
{
help();
}
else if (strcmp(input, "list") == 0)
{
list();
}
for (int i = 0; i < totalGames; i++)
{
if (strcmp(games[0][i], input) == 0)
{
valid = true;
gameNum = i + 1;
break;
}
if (strcmp(games[1][i], input) == 0)
{
valid = true;
gameNum = i + 1;
break;
}
}
if (valid == true)
{
return gameNum;
}
else
{
return 0;
}
}
void instructions(void)
{
printf("%sWelcome to %sConsoleGamesLib%s!\n", red, bgWhite, off);
printf("%s---------------------------------------%s\n\n", green, off);
printf("%sConsoleGamesLib%s is a collection of small games that you can play in the command line.\n", yellow, off);
printf("To see a list of all available games, type %slist%s.\n", cyan, off);
printf("To start a game, type its number or full name and hit enter.\n");
printf("For example, to start the game of Tic Tac Toe, type %s1%s or %stictactoe%s.\n\n", cyan, off, cyan, off);
printf("To see a list of all available commands, type %shelp%s.\n", purple, off);
printf("Enjoy your gaming experience with %sConsoleGamesLib%s!\n\n", blue, off);
}
void help(void)
{
printf("\n%s- \"quit\" to exit CGL%s\n", warning, off);
printf("%s- \"list\" to list all available games%s\n\n", caution, off);
}
void list(void)
{
printf("\n");
for (int i = 0; i < totalGames; i++)
{
printf("%d. %s\n", i + 1, games[0][i]);
}
printf("\n");
}