News:

Welcome to RetroCoders Community

Main Menu

Battle Simulation

Started by ron77, Sep 16, 2022, 10:23 AM

Previous topic - Next topic

ron77

Here is a Battle Simulation between two armies you get to decide the size of each army and see the results of the battle...

SCREEN 19

CONST AS SINGLE roman_max_health = 180.0
CONST AS SINGLE roman_attack_chance = 0.6
CONST AS SINGLE roman_damage = 150.0

CONST AS SINGLE spartan_max_health = 240.0
CONST AS SINGLE spartan_attack_chance = 0.8
CONST AS SINGLE spartan_damage = 200.0

DIM AS SINGLE roman_health = roman_max_health
DIM AS Single spartan_health = spartan_max_health
DIM roman_number AS INTEGER
DIM spartan_number AS INTEGER
DIM AS boolean turn = FALSE
DIM AS INTEGER total_roman, total_spartan
DIM battle_result AS SINGLE

RANDOMIZE TIMER

SUB CPRINT(row AS INTEGER, s AS STRING)
   LOCATE row , (LOWORD(WIDTH) - LEN(s)) SHR 1 : PRINT s
END SUB

Function rnd_range (first As Double, last As Double) As Double
    RETURN Rnd * (last - first) + first
End FUNCTION

CPRINT 2,"COMBAT SIMULATOR BETWEEN ROMAN'S AND SPARTAN'S"
PRINT
INPUT "enter number of roman's soldiers: ", roman_number
INPUT "enter number of spartan's solidiers: ", spartan_number

total_roman = roman_number
total_spartan = spartan_number


WHILE ((roman_number > 0) AND (spartan_number > 0))
   
   battle_result = RND_RANGE(0.0, 1.0)
   
   IF (turn = FALSE) THEN
      IF (battle_result > roman_attack_chance) THEN
         spartan_health -= roman_damage
         IF spartan_health < 0 THEN
            spartan_number -= 1
            spartan_health = spartan_max_health
         ENDIF
         
      ENDIF
      turn = TRUE
   ENDIF
   IF turn = TRUE THEN
      IF (battle_result > spartan_attack_chance) THEN
         roman_health -= spartan_damage
         IF roman_health < 0 THEN
            roman_number -= 1
            roman_health = roman_max_health
         ENDIF
      ENDIF
      turn = FALSE   
   ENDIF
WEND

IF roman_number > 0 THEN
   CPRINT 7, "battle results: roman's have won!"
ELSE
   CPRINT 7, "battle results: spartan's have won!"
ENDIF

CPRINT 9, "====================================="
CPRINT 11, (total_roman - roman_number) & "roman's and " & (total_spartan - spartan_number) & " spartan's lost their lives..."

sleep

__blackjack__

The `turn` variable has no effect, because the IF tests are always true.  I guess that's a bug.
,,Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." —Brian W. Kernighan