Skip to content
Snippets Groups Projects
Commit bde5b203 authored by Jay5.Patel@live.uwe.ac.uk's avatar Jay5.Patel@live.uwe.ac.uk
Browse files

Added highscore file system, moved all win/loss checks to main.c

The game is now fully fleshed out with all functions working properly. Only the storytelling section is left now
parent 78dfc28f
No related branches found
No related tags found
No related merge requests found
......@@ -18,28 +18,28 @@ Difficulty ChooseDifficulty(){
//Settings for when it is easy
if (UserInput == 'E'){
d.NoOfMovesLimit = 50;
d.FuelRequired = 5;
d.PetrolRequired = 5;
d.OxygenRequired = 5;
d.ScrapRequired = 5;
d.FuelDepletionRate = 10;
d.PetrolDepletionRate = 10;
d.OxygenDepletionRate = 20;
}
//Settings for when it is medium
else if (UserInput == 'M'){
d.NoOfMovesLimit = 40;
d.FuelRequired = 10;
d.PetrolRequired = 10;
d.OxygenRequired = 10;
d.ScrapRequired = 10;
d.FuelDepletionRate = 8;
d.PetrolDepletionRate = 8;
d.OxygenDepletionRate = 16;
}
//Settings for when it is hard
else if (UserInput == 'H'){
d.NoOfMovesLimit = 30;
d.FuelRequired = 15;
d.PetrolRequired = 15;
d.OxygenRequired = 15;
d.ScrapRequired = 15;
d.FuelDepletionRate = 6;
d.PetrolDepletionRate = 6;
d.OxygenDepletionRate = 12;
}
return d;
......
......@@ -7,10 +7,10 @@
//Struct declaration
typedef struct {
int NoOfMovesLimit;
int FuelDepletionRate;
int PetrolDepletionRate;
int OxygenDepletionRate;
int OxygenRequired;
int FuelRequired;
int PetrolRequired;
int ScrapRequired;
}Difficulty;
......
......@@ -4,6 +4,8 @@
//Version: 2.0 - 1/5/25 - Added movement library and tested player and asteroid movement in the map
//Version 3.0 - 3/5/25 - Added counter for number of moves and comments
//Version 4.0 - 4/5/25 - Now has difficulty settings module integrated
//Version 5.0 - 4/5/25 - Text file storing highscores made, doing this realised some of my code was in the wrong file.
// Moved over the win condition checks.
#include <stdio.h>
#include <stdlib.h>
......@@ -11,15 +13,29 @@
#include "mapgen.h"
#include "movement.h"
#include "difficulty.h"
int main(){
//Initialise array and seed the random generator
srand(time(NULL));
char MapSize[Size][Size];
//Create file to store highscore
int Highscore = 0;
FILE *file = fopen("highscore.txt", "r");
if (!file){ //Create file if not already created
file = fopen("highscore.txt", "w");
fprintf(file, "%d", 0);
fclose(file);
}else {
fscanf(file, "%d", &Highscore);
fclose(file);
}
//Choose Difficulty
Difficulty difficulty = ChooseDifficulty();
//Place the objects randomly in map
InitialiseMap(MapSize);
ObjectPlacement(MapSize, 'O', (difficulty.OxygenRequired + 20));
ObjectPlacement(MapSize, 'P', (difficulty.FuelRequired + 20));
ObjectPlacement(MapSize, 'P', (difficulty.PetrolRequired + 20));
ObjectPlacement(MapSize, 'S', (difficulty.ScrapRequired + 20));
//Place the player in the middle of the map for starting position
Player player = {Size/2,Size/2,difficulty.NoOfMovesLimit,1,1,0,0};
......@@ -32,19 +48,55 @@ int main(){
while (1) {
MapOutput(MapSize);
// NOTE TO SELF: Add more functionality here in regard to leaderboards
// and saving high score to text file
//Show resources
printf("\n|Resources|\n");
printf("Oxygen: %d || Petrol: %d || Scrap: %d\n",player.OxygenCollected,player.PetrolCollected,player.ScrapCollected);
printf("Moves left: %d\n", player.NoOfMovesLeft);
//Check if player has moves left
if (player.NoOfMovesLeft <=0 || player.PetrolCollected <= 0 || player.OxygenCollected <=0){
printf("\nYou have run out of moves\n");
if (player.NoOfMovesLeft <=0) {
printf("\nYou have run out of movements\n");
printf(" || HighScore: %d Moves Left || \n", Highscore);
break;
}
//Check if player has depleting resources available
if (player.PetrolCollected <= 0 || player.OxygenCollected <=0){
printf("\nYou have run out of life supporting resources\n");
printf(" || HighScore: %d Moves Left || \n", Highscore);
break;
}
//Check if player hit an asteroid
if(player.x == asteroid.x && player.y == asteroid.y){
printf("You hit asteroid game over\n");
printf(" || HighScore: %d Moves Left || \n", Highscore);
break;
}
//Boundary win confirm/check and highscore output
if ( player.x == 0 || player.x == Size -1 || player.y == 0 || player.y == Size - 1){
char ExitConfirm;
printf("Do you wish to exit?(Y/N)\n");
scanf(" %c",&ExitConfirm);
if (ExitConfirm == 'Y'){
if (player.ScrapCollected >= difficulty.ScrapRequired && player.OxygenCollected >= difficulty.OxygenRequired && player.PetrolCollected >= difficulty.PetrolRequired){
printf("You have enough to win congrats\n");
if (player.NoOfMovesLeft > Highscore){
file = fopen("highscore.txt", "w");
fprintf(file, "%d", player.NoOfMovesLeft);
fclose(file);
printf("\n || NEW HIGHSCORE!! - %d Moves Left || \n",player.NoOfMovesLeft);
}else {
printf(" || HighScore: %d Moves Left || \n", Highscore);
}
exit(0);
} else {
printf("You did not have enough to win\n");
printf(" || HighScore: %d Moves Left || \n", Highscore);
exit(0);
}
}
}
//Move the player and asteroid based on input
PlayerMove(MapSize,&player, &difficulty);
AsteroidMove(MapSize,&asteroid);
......
......@@ -5,7 +5,7 @@
// the asteroid position was cleared before it knew if it was moving or not. Added store to hold old position.
//Version 3.0 - 3/5/25 - Added resource counter and a countdown for number of moves
//Version 4.0 - 4/5/25 - Added check for non - wasd inputs, fixed the depletion function, Added win condition.
//Version 5.0 - 4/5/25 - Only just clocked that the win condition check was in the library instead of the main.c file.
#include <stdio.h>
#include <stdlib.h>
#include "movement.h"
......@@ -16,10 +16,6 @@ void PlayerMove(char MapSize[Size][Size], Player *p, Difficulty *d) {
int Tempx = (*p).x;
int Tempy = (*p).y;
char Input;
//Show resources
printf("\n|Resources|\n");
printf("Oxygen: %d || Petrol: %d || Scrap: %d\n",(*p).OxygenCollected,(*p).PetrolCollected,(*p).ScrapCollected);
printf("Moves left: %d\n", (*p).NoOfMovesLeft);
//Check to ensure only wasd is input
do {
......@@ -56,21 +52,7 @@ void PlayerMove(char MapSize[Size][Size], Player *p, Difficulty *d) {
(*p).ScrapCollected++;
}
//Boundary win confirm/check
if (Tempx == 0 || Tempx == Size -1 || Tempy == 0 || Tempy == Size - 1){
char ExitConfirm;
printf("Do you wish to exit?(Y/N)\n");
scanf(" %c",&ExitConfirm);
if (ExitConfirm == 'Y'){
if ((*p).ScrapCollected >= (*d).ScrapRequired && (*p).OxygenCollected >= (*d).OxygenRequired && (*p).PetrolCollected >= (*d).FuelRequired){
printf("You have enough to win congrats\n");
exit(0);
} else {
printf("You did not have enough to win\n");
exit(0);
}
}
}
//Update the players position based on their input
MapSize[(*p).x][(*p).y] = '+';
MapSize[Tempx][Tempy] = 'U';
......@@ -83,7 +65,7 @@ void PlayerMove(char MapSize[Size][Size], Player *p, Difficulty *d) {
(*p).MovesCount++;
}
//Depletion of resources
if ((*p).MovesCount % (*d).FuelDepletionRate == 0){
if ((*p).MovesCount % (*d).PetrolDepletionRate == 0){
(*p).PetrolCollected--;
}
if ((*p).MovesCount % (*d).OxygenDepletionRate == 0){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment