a little combat simulator between Romans and Spartans

Game development specific discussions.
Post Reply
ron77
Posts: 212
Joined: Feb 21, 2019 19:24

a little combat simulator between Romans and Spartans

Post by ron77 »

hello...

here is a code that is a combat simulator between 2 armies... all you have to do is choose the size of each army and they fight each other two soldiers at a time until one army is killed and then you get the combat result:

i do not know if this code has bugs in the randomizations of the battle or logic bugs - seems fine i think... please tell me if you see any errors
here is the code:

Code: Select all

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
ron77
Post Reply