From 99cbfe3ccdb9ba51a5b19e16ccb0fecdf225fe1c Mon Sep 17 00:00:00 2001 From: Ashwin Date: Wed, 3 Jul 2024 19:52:43 +1000 Subject: [PATCH] added stuff, check pr --- run.sh | 6 +- src/Missions/cyber_counter.c | 22 +++++++ src/Missions/encode_keypad.c | 29 +++++++++ src/Missions/glowing_greetings.c | 20 ++++++ src/Missions/neon_sum.c | 19 ++++++ src/Solutions/cyber_counter_sol.c | 8 +++ src/Solutions/encode_keypad_sol.c | 12 ++++ src/Solutions/glowing_greetings_sol.c | 6 ++ src/Solutions/neon_sum_sol.c | 12 ++++ src/game.c | 94 +++++++++++++++++++++++++-- src/game.h | 26 +++++++- src/game_globals.c | 28 ++++++++ src/main.c | 23 +++++++ 13 files changed, 297 insertions(+), 8 deletions(-) create mode 100644 src/Missions/cyber_counter.c create mode 100644 src/Missions/encode_keypad.c create mode 100644 src/Missions/glowing_greetings.c create mode 100644 src/Missions/neon_sum.c create mode 100644 src/Solutions/cyber_counter_sol.c create mode 100644 src/Solutions/encode_keypad_sol.c create mode 100644 src/Solutions/glowing_greetings_sol.c create mode 100644 src/Solutions/neon_sum_sol.c create mode 100644 src/game_globals.c diff --git a/run.sh b/run.sh index d2bb916..6478f81 100755 --- a/run.sh +++ b/run.sh @@ -2,13 +2,13 @@ # Get tmp file name out_file=$(mktemp -q "./out.XXXXXX") +#remove tmp file (even if killed by ctrl+c) +trap 'rm -f "$out_file"' EXIT # Compile main.c -gcc src/main.c src/game.c -o "$out_file" +gcc src/main.c src/game.c src/game_globals.c -o "$out_file" # Run "./$out_file" -# Remove -rm "$out_file" diff --git a/src/Missions/cyber_counter.c b/src/Missions/cyber_counter.c new file mode 100644 index 0000000..8dd030c --- /dev/null +++ b/src/Missions/cyber_counter.c @@ -0,0 +1,22 @@ +/* +MISSION 1: Cyber Counter + +Good Afternoon, Agent. Here are your mission details. + +Problem: In the neon-lit city of NeoTokyo, a hacker needs to print out a list of the +first 1000 ID numbers of drones patrolling the city. + +Task: Write a program that prints the numbers from 1 to 1000. + +Example Output: +1 +2 +3 +... +1000 +*/ +#include + +int main() { + +} diff --git a/src/Missions/encode_keypad.c b/src/Missions/encode_keypad.c new file mode 100644 index 0000000..bb01567 --- /dev/null +++ b/src/Missions/encode_keypad.c @@ -0,0 +1,29 @@ +/* +MISSION 4: Encode Keypad + +Good Afternoon, Agent. Here are your mission details. + +Problem: Attackers are arriving soon... Encode the keypads on cyber doors so it only accepts a 4-digit PIN code (4201). + + Write a program that asks the user for a 4-digit PIN code and then prints "Access Granted" if the PIN is correct (4201), otherwise print "Access Denied". + +Task: Write a program that asks the user for a 4-digit PIN code and then prints "Access Granted" + if the PIN is correct (1234), otherwise print "Access Denied". + +Note: Code is (1234) for example + +Example Output: +Enter PIN code: +4201 +Access Granted + +Example Output: +Enter PIN code: +1024 +Access Denied +*/ +#include + +int main() { + +} \ No newline at end of file diff --git a/src/Missions/glowing_greetings.c b/src/Missions/glowing_greetings.c new file mode 100644 index 0000000..ee1596e --- /dev/null +++ b/src/Missions/glowing_greetings.c @@ -0,0 +1,20 @@ +/* +MISSION 2: Glowing Greetings + +Good Afternoon, Agent. Here are your mission details. + +Problem: A cybernetic billboard needs to display a welcome message to citizens of the future. + +Task: Write a program that prints "Welcome to NeoTokyo 2099!". + + +Example Output: +Welcome to NeoTokyo 2099! +*/ + +#include + +int main() { + + +} \ No newline at end of file diff --git a/src/Missions/neon_sum.c b/src/Missions/neon_sum.c new file mode 100644 index 0000000..1acb552 --- /dev/null +++ b/src/Missions/neon_sum.c @@ -0,0 +1,19 @@ +/* +MISSION 3: Neon Sum + +Good Afternoon, Agent. Here are your mission details. + +Problem: A hacker has encyrpted all the data of our secret base down at Neon Alley. We need to find the password to decrypt the information. +We got intel that the password is the sum of all even numbers from 1 to 1000 to decrypt a code. + +Task: Write a program that calculates and prints the sum of all even numbers from 1 to 1000 + +Note: this is not the answer +Example Output: +123456 +*/ +#include + +int main() { + +} \ No newline at end of file diff --git a/src/Solutions/cyber_counter_sol.c b/src/Solutions/cyber_counter_sol.c new file mode 100644 index 0000000..ba0a1f3 --- /dev/null +++ b/src/Solutions/cyber_counter_sol.c @@ -0,0 +1,8 @@ +#include + +int main() { + for (int i = 1; i <= 1000; i++) { + printf("%d\n", i); + } + return 0; +} \ No newline at end of file diff --git a/src/Solutions/encode_keypad_sol.c b/src/Solutions/encode_keypad_sol.c new file mode 100644 index 0000000..d29bd3f --- /dev/null +++ b/src/Solutions/encode_keypad_sol.c @@ -0,0 +1,12 @@ +#include + +int main() { + int pin; + printf("Enter PIN code:\n"); + scanf("%d", &pin); + if (pin == 1234) { + printf("Access Granted\n"); + } else { + printf("Access Denied\n"); + } +} \ No newline at end of file diff --git a/src/Solutions/glowing_greetings_sol.c b/src/Solutions/glowing_greetings_sol.c new file mode 100644 index 0000000..1dc5c14 --- /dev/null +++ b/src/Solutions/glowing_greetings_sol.c @@ -0,0 +1,6 @@ +#include + +int main() { + printf("Welcome to NeoTokyo 2099!\n"); + return 0; +} \ No newline at end of file diff --git a/src/Solutions/neon_sum_sol.c b/src/Solutions/neon_sum_sol.c new file mode 100644 index 0000000..70c8a28 --- /dev/null +++ b/src/Solutions/neon_sum_sol.c @@ -0,0 +1,12 @@ +#include + +int main() { + int sum = 0; + for (int i = 1; i <= 1000; i++) { + if (i % 2 == 0) { + sum += i; + } + } + printf("%d\n", sum); + return 0; +} \ No newline at end of file diff --git a/src/game.c b/src/game.c index 5c7c3c0..95fb6fb 100644 --- a/src/game.c +++ b/src/game.c @@ -1,5 +1,6 @@ #include "game.h" #include +#include #include #include @@ -17,7 +18,25 @@ Player build_player() { strcpy(player.name, name); // TODO: the rest of building player + slow_print("> How old are you?\n"); + int age; + //?? may need error checking here + scanf("%d", &age); + player.age = age; + slow_print("> How tall are you?\n"); + int height; + //?? may need error checking here + scanf("%d", &height); + player.height = height; + + slow_print("> What is your favourite food?\n"); + char food[1024]; + scanf("%s", food); + strcpy(player.favourite_food, food); + + + return player; } @@ -26,17 +45,84 @@ void travelling(Player player) { } char* choose_a_vehicle() { + slow_print("> Agent your first task is to fix the vehicle selection system." + " Please navigate to game.c and complete the task \n"); + // TODO: implement this - return NULL; + // SOLUTION START// + slow_print("> Choose a vehicle:"); + slow_print("> %d. %s\n", 1, vehicles[0]); + slow_print("> %d. %s\n", 2, vehicles[1]); + slow_print("> %d. %s\n", 3, vehicles[2]); + + int choice; + while (1) { + slow_print("> Enter your choice:"); + scanf("%d", &choice); + if (choice == 1 || choice == 2 || choice == 3) { + break; + } + slow_print("> Invalid choice, please try again."); + } + if (choice == 1) { + return vehicles[0]; + } else if (choice == 2) { + return vehicles[1]; + } else { + return vehicles[2]; + } + // SOLUTION END// +} + +void choose_a_mission() { + slow_print("Please select the mission you want to complete:"); + + for (int i = 0; i < 10; i++) { + slow_print("%d.\tCodename: %-20sDifficulty: %d\n\tDescription: %s\n", i + 1, missions[i].name, missions[i].difficulty, missions[i].description); + } } // UTILITY FUNCTION -void slow_print(char *str) { - for (int i = 0; i < strlen(str); i++) { - putchar(str[i]); +void slow_print(const char *format, ...) { + char buffer[1024]; // Ensure this buffer is large enough for your needs + va_list args; + va_start(args, format); + vsnprintf(buffer, sizeof(buffer), format, args); + va_end(args); + + for (int i = 0; i < strlen(buffer); i++) { + putchar(buffer[i]); fflush(stdout); usleep(10000); } putchar('\n'); } + +// Function to save player progress +// void save_progress(Player player) { +// FILE *file = fopen("savefile.txt", "w"); +// if (file == NULL) { +// printf("Error opening save file for writing.\n"); +// return; +// } +// fprintf(file, "%s\n%d\n%d\n%s\n%s\n%s\n", player.name, player.age, player.height, player.favourite_food, player.vehicle, player.current_location.name); +// fclose(file); +// } + +// // Function to load player progress +// Player load_progress() { +// Player player; +// FILE *file = fopen("savefile.txt", "r"); +// if (file == NULL) { +// printf("No save file found, starting a new game.\n"); +// // Initialize player with default or empty values +// strcpy(player.name, ""); +// strcpy(player.vehicle, ""); +// return player; +// } +// fscanf(file, "%1023s\n%d\n%d\n%1023s\n%1023s\n%1023s\n", player.name, player.age, player.height, player.favourite_food, player.vehicle, player.current_location.name); +// fclose(file); +// return player; +// } + diff --git a/src/game.h b/src/game.h index e3c5593..3e17bbc 100644 --- a/src/game.h +++ b/src/game.h @@ -1,21 +1,45 @@ #ifndef GAME #define GAME +// STRUCTS ============================ +typedef struct location { + char name[1024]; + // char description[1024]; +} Location; + +typedef struct mission { + char name[1024]; + char description[1024]; + int difficulty; +} Mission; + typedef struct player { int age; int height; char name[1024]; + char favourite_food[1024]; // TODO: add the rest of the player struct + char vehicle[1024]; + Mission current_mission; } Player; // GAME FUNCTIONS ===================== Player build_player(); void travelling(Player player); char* choose_a_vehicle(); +void choose_a_mission(); // TODO: add the rest of the game functions // UTILITY FUNCTIONS ================== // prints str character by character -void slow_print(char str[]); +void slow_print(const char *format, ...); + +//GAME ARRAYS +extern Location locations[]; + +extern char vehicles[][1024]; + +extern Mission missions[]; #endif // !GAME + diff --git a/src/game_globals.c b/src/game_globals.c new file mode 100644 index 0000000..5041102 --- /dev/null +++ b/src/game_globals.c @@ -0,0 +1,28 @@ +#include "game.h" + +Location locations[] = { + {"CyberNEx Corp Training Facility"}, + {"CyberNEx Corp Headquarters"}, + {"CyberNEx Corp Research Lab"}, + {"CyberNEx Corp Server Room"}, + {"CyberNEx Corp Data Centre"} +}; + +char vehicles[][1024] = { + "Quantum High Speed Car", + "CyberNEx Corp Hoverbike", + "Nano Diffusion Jetpack" +}; + +Mission missions[] = { + {"Cyber Counter", "Mission 1 description", 1}, + {"Glowing Greetings", "Mission 2 description", 1}, + {"Neon Sum", "Mission 3 description", 2}, + {"Encode Keypad", "Mission 4 description", 2} + // {"Mission Ocean", "Mission 5 description", 3}, + // {"Mission Swamp", "Mission 6 description", 3}, + // {"Mission Taiga", "Mission 7 description", 4}, + // {"Mission Cave", "Mission 8 description", 4}, + // {"Mission Nether", "Mission 9 description", 5}, + // {"Mission End", "Mission 10 description", 5} + }; \ No newline at end of file diff --git a/src/main.c b/src/main.c index 7d0eae9..e4d6bda 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include "game.h" +// #include "testing_files.c" #include #include @@ -8,6 +9,28 @@ int main(void) { "> Welcome to CyberNEx Corp candidate, today is your first day on the " "job and your final test, please check the file linked to neurochip. " "It includes all the information and details of mission REDACTED.\n"); + + slow_print("> Before we get you to your final training mission we need to get you prepped up.\n"); + Player player = build_player(); + // TODO: fill in based on the spec + // create and pick a vehicle + slow_print("> A vehicle will be provided on behalf of the company," + " please choose the one that most suits your driving style.\n"); + strcpy(player.vehicle, choose_a_vehicle()); + + //?? check if vehicle has been chosen properly + //test_vehicle_selection(player); + + slow_print("> You have chosen the %s, good choice.\n", player.vehicle); + + slow_print("> Now that you have chosen your vehicle, you can start your missions.\n"); + + slow_print("> Please select the mission you want to complete:\n"); + choose_a_mission(); + // player.current_mission = choose_a_mission(); + //?? check if mission has been chosen properly + + slow_print("> You have chosen the mission: %s, please navigate to the mission file.\n Good luck Agent!\n", player.current_mission.name); }