train in Lua and D

This commit is contained in:
RFigCon
2023-09-25 02:49:58 +01:00
parent 25faf4aeeb
commit e40ed4e55d
3 changed files with 112 additions and 0 deletions

59
91_Train/lua/train.lua Normal file
View File

@@ -0,0 +1,59 @@
print [[
TRAIN
CREATIVE COMPUTING MORRISTOWN, NEW JERSY
TIME - SPEED DISTANCE EXERCISE]]
math.randomseed(os.time())
local error_margin = 5.0
function play()
local car_speed = math.random(40, 65)
local delta_time = math.random(5, 20)
local train_speed = math.random(20, 39)
print( string.format("\nA CAR TRAVELING AT %u MPH CAN MAKE A CERTAIN TRIP IN %u HOURS LESS THAN A TRAIN TRAVELING AT %u MPH.", car_speed, delta_time, train_speed) )
local try = true
local input
while try do
io.write("HOW LONG DOES THE TRIP TAKE BY CAR? ")
input = io.read("n")
if input == nil then
print("<!>PLEASE INSERT A NUMBER<!>")
else
try = false
end
io.read()
end
local car_time = delta_time * train_speed / (car_speed - train_speed)
local percent = ( math.abs(car_time-input) * 100 / car_time + .5)
if percent > error_margin then
print( string.format("SORRY. YOU WERE OFF BY %f PERCENT.", percent) )
else
print( string.format("GOOD! ANSWER WITHIN %f PERCENT.", percent) )
end
print( string.format("CORRECT ANSWER IS %f HOURS.", car_time) )
end
function game_loop()
local keep_playing = true
while keep_playing do
play()
io.write("\nANOTHER PROBLEM (YES OR NO)? ")
answer = io.read("l")
if not (answer == "YES" or answer == "Y" or answer == "yes" or answer == "y") then
keep_playing = false
end
end
end
game_loop()