Post your code!

User avatar
Stix
Member
 
Posts: 1329
Joined: Fri Aug 04, 2017 14:19
Location: USA
IRC: nil
In-game: Stix [+alts]

Re: Post your code!

by Stix » Sun Jan 13, 2019 23:13

I edited my previous program to fix some bugs and colorize the health status information.

Example:
High-health = colorize green.
Medium-health = colorize yellow.
Low-health = colorize red.

And here's the updated source-code:
Code: Select all
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define RED   "\x1B[31m"
#define GRN   "\x1B[32m"
#define YEL   "\x1B[33m"
#define RESET "\x1B[0m"

int hunger;
int food;
int thirst;
int water;
int medkit;
int day;
int choice;
int amount;
int turn;
int status;
int status1;
int condition;
int item_value;
int situation;
int health;
int energy;
int attack;
int recover;
int enemy_health;
int enemy_energy;
int enemy_attack;
int enemy_recover;
char death[100];
char item[100];
char situation1[100];
char choice1[100];
char choice2[100];
char choice3[100];
char choice4[100];

int defaults() //stores defaults values that are called at th beginning of the program
{
  status1 = 0;
  status = 0;
  day = 0;
  health = 10;
  hunger = 0;
  thirst = 0;
  food = 5;
  water = 3;
  medkit = 1;
}

int fight_defaults() //stores default values for the functions fight()
{
  enemy_health = 10;
  enemy_energy = 4;
  enemy_attack = 3;
  enemy_recover = 2;
 
  energy = 4;
  attack = 3;
  recover = 2;
}

int drink()
{
  if(water < 1)
    printf("\nYou have no more water to drink.\n");
  else
    if(thirst > 0)
      {
   thirst -= 5;
   water--;
      }
  if(thirst < 0)
    thirst = 0;
}

int eat()
{
  if(food < 1)
    printf("\nYou have no more food to eat.\n");
  else
    if(hunger > 0)
      {
   hunger -= 3;
   food--;
      }
  if(hunger < 0)
    hunger = 0;
}

int do_nothing() //a function that does nothing (hack)
{
  status = status;
}

int heal()
{
  if(medkit < 1)
    printf("\nYou don't have any medkits.\n");
  else 
    if(health < 10)
      {
   printf("\nYou healed.\n");
   health += 3;
   medkit--;
      }
    else
      printf("\nYou are already at full health.\n");
}

int health_bar()
{
  if(condition == 0)
    {
      if(health <= 3)
   printf(RED "\nHealth: %d out of 10"RESET, health);
      if(health > 3)
   {
     if(health < 7)
       printf(YEL "\nHealth: %d out of 10"RESET, health);
   }
      if(health > 6)
   printf(GRN "\nHealth: %d out of 10"RESET, health);
    }
     
  if(condition == 1)
    {
      if(health <= 3)
   printf(RED "\n Health: %d" RESET, health);
      if(health > 3)
   {
     if(health < 7)
       printf(YEL "\n Health: %d" RESET, health);
   }
      if(health > 6)
   printf(GRN "\n Health: %d" RESET, health);
    }
 
  if(condition == 2)
    {
      if(enemy_health <= 3)
   printf(RED "           Health: %d" RESET, enemy_health);
      if(enemy_health > 3)
   {
     if(enemy_health < 7)
       printf(YEL "           Health: %d" RESET, enemy_health);
   }
      if(enemy_health > 6)
   printf(GRN "           Health: %d" RESET, enemy_health);
    }
}
     
int choices() //a function that gives a set of choices based on the current situation
{
  int r2 = rand() % 3;
  int r3 = rand() % 5;
  int r4 = rand() % 2;
 
  if(situation == 1)
    {
      if(choice == 1)
   drink();
      if(choice == 2)
   eat();
      if(choice == 3)
   do_nothing;
      if(choice == 4)
   heal();
      if(choice == 5)
   status = 1;
    }
 
  if(situation == 2)
    {
      switch(choice)
   {
   case 1:
     printf("\nYou started a fight!\n");
     fight();
     break;
   case 2:       
     if(r2 == 2)
       {
         printf("\nYou got attacked by a stranger!\n");
         fight();
       }
     if(r2 <= 1)
       {
         printf("\nYou ran away from a fight.\n");
         do_nothing();
       }
     break;
   case 3:      
     if(r4 == 0)
       break;
     if(r4 == 1)
       printf("\nYou got attacked by a stranger!\n");
     fight();
     break;
   case 4:
     do_nothing();
     break;
   case 5:
     status = 1;
     break;
   }
    }
 
  if(situation == 3)
    {         
      if(r3 == 0)
   {
     strcpy(item, "food-item");
     item_value = 1;
   }
      if(r3 == 1)
   {
     strcpy(item, "water-bottle");
     item_value = 2;
   }
      if(r3 == 2)
   {
     strcpy(item, "medkit");
     item_value = 3;
   } 
      if(r3 >= 3)
   printf("\nThe box was empty!\n");
               
      amount = r3;
   
      if(r3 <= 2)
   {   
     if(choice == 1)
       {
         printf("\nYou found %d %s(s).\n", amount, item);
        
         if(item_value == 1)
      food += amount;
         if(item_value == 2)
      water += amount;
         if(item_value == 3)
      medkit += amount;
       }
   }
     
      if(choice == 2)
   do_nothing();
      if(choice == 3)
   do_nothing();
      if(choice == 4)
   do_nothing();
      if(choice == 5)
   status = 1;
    }
}


int situations() //a function that randomly picks a situation to be current
{
  int r = rand() % 5; //grabbing a random number (0-4)

  if(r <= 2)
    {
      strcpy(situation1, "You are walking through an abandoned town."); //setting situation
      strcpy(choice1, "Drink."); //setting choices for the situation
      strcpy(choice2, "Eat.");
      strcpy(choice3, "Do nothing.");
      strcpy(choice4, "Heal.");
      situation = 1;
    }
 
  if(r == 3)
    {
      strcpy(situation1, "While walking you meet a stranger."); //setting situation
      strcpy(choice1, "Fight."); //setting choices for the situation
      strcpy(choice2, "Run.");
      strcpy(choice3, "Do nothing.");
      strcpy(choice4, " ");
      situation = 2;
    }

  if(r == 4)
    {
      strcpy(situation1, "While walking you find a box."); //setting situation
      strcpy(choice1, "Open it."); //setting choices for the situation
      strcpy(choice2, "Do nothing.");
      strcpy(choice3, " ");
      strcpy(choice4, " ");
      situation = 3;
    }
}

int fight() //a function that handles fighting between the player and enemy ai
{
  fight_defaults();
 
  while(1) //loop repeats until a 'break' is called
    {
      printf("\n>>>>>>>STATS<<<<<<<");
      printf("  >>>>>>>ENEMY<<<<<<<");
      printf("\n*******************");
      printf("  *******************");
      condition = 1;
      health_bar();
      condition = 2;
      health_bar();
      condition = 0;
      printf("\n Energy: %d        ", energy);
      printf("   Energy: %d         ", enemy_energy); 
      printf("\n Attack: %d        ", attack);   
      printf("   Attack: %d         ", enemy_attack);
      printf("\n Recover: %d       ", recover);
      printf("   Recover: %d        ", enemy_recover);
      printf("\n*******************");
      printf("  *******************\n");
     
      turn = 0; //sets turn to players beginning of every loop
     
      if(turn == 0) //takes input if conditions are met
   {
     printf("\nYour turn!\n");
    
     while(turn == 0)
       {
         printf("\n1) Fight.\n2) Recover.\n3) Skip turn.\n");
         scanf("%d", &choice);
        
         if(choice == 1) //calculates the input and makes decisions based on it     
      {
        if(energy >= 1)
          {
            enemy_health += enemy_recover - attack; //attacking enemy
            energy -= 1;
            break; //breaking the loop
          }
        else
          printf("\nYou dont have enough energy to attack!\n");
        continue; //restarting the loop
      }
        
         if(choice == 2)
      energy += recover; //recover energy
         break;
         if(choice == 3)
      do_nothing(); //the if statement executes nothing (hack)
         break;           
       }                      
   }
 
      if(enemy_health < 1) //checking if the player won the fight
   {
     printf("\nYou won the fight!\n");
     break;
   }
     
      death_event();

      if(status1 == 1)
   break;
     
      turn = 1; //sets turn to enemy() function end of every loop
      enemy();
    }
}

int enemy() //a function that takes a number (0-2) and makes decisions based on the result
{
  if(turn == 1)
    {
      printf("\nEnemy's turn!");
      int r = rand() % 3; //grabbing a number (0-2)
     
      switch(r)
   {
   case 0:
     /*fall through (which makes the enemy more likely to attack)*/
   case 1:   
     if(enemy_energy >= 1)
       {
         health += recover - enemy_attack; //attacking player
         enemy_energy -= 1;
         printf("\n\nThe enemy attacked!\n");
         break;
       }
     /*fall through (so the enemy has to recover if they're unable to attack)*/
   case 2:   
     enemy_energy += enemy_recover; //recovering energy
     printf("\n\nThe enemy recovered energy.\n");
     break;
   }
    }


int death_event() //Computes cause of death and prints the death-message accordingly.
{
  if(hunger > 10) //checks if the player has starved
    {
      status = 3;
      strcpy(death, "hunger");
    }
  if(thirst > 10) //checks if the player is dehydrated
    {
      status = 3;
      strcpy(death, "thirst");
    }
  if(health < 1) //checks if the player was killed by the enemy ai
    {
      status = 3;
      strcpy(death, "a stranger");
    }
  if(status == 3)
    {
      /*notifying the user of their characters death*/
      printf("\nDeath by %s, you survived %d days. ", death, day);
      status = 1;
      printf("Press \'1\' to restart or \'2\' to quit: ");
      scanf("%d", &choice);
     
      if(choice == 1)
   {
     status = 2;
     status1 = 1;
   }
     
      if(choice == 2)
   {
     status = 1;
     status1 = 1;
   }
     
      if(choice > 2)
   {    
     /*informing the user of an invalid number for input*/
     printf("\nError: to high a value.\n\n");
     status = 1;
   }
    }
}


int win() //checks if the user wins and notifies them accordingly
{
  if(day > 29)
    printf("\nCongratulations, you survived all %d days!\n\n", day);
}


main()
{
 
  defaults(); //grabbing default values
 
  for(day = 1; day < 31; day++) //a loop that will run until day == 30
    {
      situations(); //executing a function that randomly sets the situation
     
      printf("\nDay %d: %s\n", day, situation1);
      printf("\n>>>>>>>>>>>>>>>>>>STATS<<<<<<<<<<<<<<<<<<");
      printf("\n*****************************************");
      health_bar();
      printf("\nHunger: %d out of 10", hunger);
      printf("\nThirst: %d out of 10", thirst);
      printf("\n\nYou have %d water-bottle(s).", water);
      printf("\nYou have %d food-item(s).", food);
      printf("\nYou have %d medkit(s).", medkit);
      printf("\n*****************************************\n");
      printf("\nShould you:\n1) %s\n2) %s\n3) %s\n4) %s\n", choice1, choice2, choice3, choice4);
     
      scanf("%d", &choice);
     
      /*executing a function that will make decisions based on the current situation and
        the users input*/
      choices();

      if(situation == 1) //incrementing hunger/thirst every loop if the condition is met
   {
     hunger++;
     thirst+=2;
   }

      if(status == 1)
   break;
      if(status == 2)
   defaults(); //grabbing default values
      continue;

      death_event(); //executing a function that checks if the player died
      win(); //executing a function that will check if the user won
     
      if(status == 1)
   break;
      if(status == 2)
   defaults(); //grabbing default values
      continue;
    }

  return(0);
}
Hey, what can i say? I'm the bad guy.
Next release of your favorite game? "Bugfixes" --Wuzzy
[OLD]The new Minetest-offtopic can be found here: https://minetestoftopic.createaforum.com/index.php
 

User avatar
sorcerykid
Member
 
Posts: 907
Joined: Fri Aug 26, 2016 15:36
Location: Illinois, USA
GitHub: sorcerykid
In-game: Nemo

Re: Post your code!

by sorcerykid » Mon Jan 14, 2019 11:57

I have a habit of going through code and finding astounding amounts of extraneous logic. When it comes to senseless redundancy in programming, I'll admit I'm very anal retentive. Nearly half the mods used on JT2 have been refactored or rewritten from scratch, ultimately culling hundreds of lines. Here's a section of code from Zorman3000's pathfinder API that I recently cleaned up:

I managed to reduce this:
Code: Select all
  local start_x_sign = (start_pos.x - end_pos.x) / math.abs(start_pos.x - end_pos.x)
  local start_z_sign = (start_pos.z - end_pos.z) / math.abs(start_pos.z - end_pos.z)
  local end_x_sign = (end_pos.x - start_pos.x) / math.abs(end_pos.x - start_pos.x)
  local end_z_sign = (end_pos.z - start_pos.z) / math.abs(end_pos.z - start_pos.z)

  -- Correct the signs if they are nan
  if math.abs(start_pos.x - end_pos.x) == 0 then
    start_x_sign = -1
    end_x_sign = 1
  end
  if math.abs(start_pos.z - end_pos.z) == 0 then
    start_z_sign = -1
    end_z_sign = 1
  end


down to only this:
Code: Select all
local base_x_sign = base.pos.x <= goal_pos.x and -1 or 1
local goal_x_sign = -base_x_sign
local base_z_sign = base_pos.z <= goal_pos.z and -1 or 1
local goal_z_sign = -base_z_sign
 

Previous

Return to General Discussion



Who is online

Users browsing this forum: No registered users and 1 guest