Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added stuff, check pr #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

22 changes: 22 additions & 0 deletions src/Missions/cyber_counter.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>

int main() {

}
29 changes: 29 additions & 0 deletions src/Missions/encode_keypad.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>

int main() {

}
20 changes: 20 additions & 0 deletions src/Missions/glowing_greetings.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>

int main() {


}
19 changes: 19 additions & 0 deletions src/Missions/neon_sum.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>

int main() {

}
8 changes: 8 additions & 0 deletions src/Solutions/cyber_counter_sol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>

int main() {
for (int i = 1; i <= 1000; i++) {
printf("%d\n", i);
}
return 0;
}
12 changes: 12 additions & 0 deletions src/Solutions/encode_keypad_sol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>

int main() {
int pin;
printf("Enter PIN code:\n");
scanf("%d", &pin);
if (pin == 1234) {
printf("Access Granted\n");
} else {
printf("Access Denied\n");
}
}
6 changes: 6 additions & 0 deletions src/Solutions/glowing_greetings_sol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

int main() {
printf("Welcome to NeoTokyo 2099!\n");
return 0;
}
12 changes: 12 additions & 0 deletions src/Solutions/neon_sum_sol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>

int main() {
int sum = 0;
for (int i = 1; i <= 1000; i++) {
if (i % 2 == 0) {
sum += i;
}
}
printf("%d\n", sum);
return 0;
}
94 changes: 90 additions & 4 deletions src/game.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "game.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>

Expand All @@ -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;
}

Expand All @@ -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;
// }

26 changes: 25 additions & 1 deletion src/game.h
Original file line number Diff line number Diff line change
@@ -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

28 changes: 28 additions & 0 deletions src/game_globals.c
Original file line number Diff line number Diff line change
@@ -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}
};
23 changes: 23 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "game.h"
// #include "testing_files.c"
#include <stdio.h>
#include <string.h>

Expand All @@ -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);
}