Author Topic: A Game in 25 lines.  (Read 2037 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
A Game in 25 lines.
« on: April 07, 2023, 08:34:56 PM »
Was challenged to write a RPG in less than 25 lines.

Code - C#: [Select]
  1. Console.WriteLine(" -------------------");
  2.  
  3.    GameLoop();
  4.  
  5.    Console.WriteLine(" -------------------");
  6.  
  7.    Console.ReadKey();
  8. }
  9.  
  10. private static void GameLoop()
  11. {
  12.    Random random = new Random();
  13.    int monsterHealth = 16, heroHealth = 16, fate = 0;
  14.    while (( heroHealth > 0 ) && ( monsterHealth > 0 ))
  15.    {
  16.       fate = random.Next(1, 11);
  17.       monsterHealth -= fate;
  18.       Console.WriteLine($"Monster was damaged and lost {fate} health " +
  19.          $"and now has {monsterHealth} health.");
  20.       if (monsterHealth <= 0)
  21.       {
  22.          Console.WriteLine("Monster suffers a horrible death !! ");
  23.          continue;
  24.       }
  25.  
  26.       fate = random.Next(1, 11);
  27.       heroHealth -= fate;
  28.       Console.WriteLine($"Hero was wounded and lost {fate} health " +
  29.          $"and now has {heroHealth} health.");
  30.  
  31.       if (heroHealth <= 0) { Console.WriteLine("Hero achieves a heroic martyrdom !! "); }          
  32.    }
  33. }
  34.  
  35.  



Quote
-------------------
Monster was damaged and lost 1 health and now has 15 health.
Hero was wounded and lost 6 health and now has 10 health.
Monster was damaged and lost 2 health and now has 13 health.
Hero was wounded and lost 3 health and now has 7 health.
Monster was damaged and lost 7 health and now has 6 health.
Hero was wounded and lost 10 health and now has -3 health.
Hero achieves a heroic martyrdom !!
 -------------------
 

24 lines in the game loop was a win.
. . . but definitely not Legend of Zelda
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: A Game in 25 lines.
« Reply #1 on: April 07, 2023, 11:11:34 PM »
but line 31  :whistling:

Waiting for the latest Zelda, due out next month , I've played them all!

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: A Game in 25 lines.
« Reply #2 on: April 08, 2023, 02:49:06 AM »
but line 31  :whistling:

Waiting for the latest Zelda, due out next month , I've played them all!

My personal style allows for a single line unnested statement in a conditional when there is a single result action.
. . . . that's my story , and it just happened to reduce the code mass by 2 lines. Lucky, hey?  :angel:

This satisfies my rules too :
Code - C#: [Select]
  1. if (heroHealth <= 0)
  2.     { Console.WriteLine("Hero achieves a heroic martyrdom !! "); }
« Last Edit: April 08, 2023, 02:52:18 AM by _kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.