mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-20 00:31:18 -08:00
train in Lua and D
This commit is contained in:
3
00_Alternate_Languages/91_Train/D/README.md
Normal file
3
00_Alternate_Languages/91_Train/D/README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Original source downloaded from [Vintage Basic](http://www.vintage-basic.net/games.html).
|
||||||
|
|
||||||
|
Conversion to [MiniScript](https://dlang.org).
|
||||||
50
00_Alternate_Languages/91_Train/D/train.d
Normal file
50
00_Alternate_Languages/91_Train/D/train.d
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import std.stdio;
|
||||||
|
import std.random : uniform;
|
||||||
|
|
||||||
|
float abs(float num) {
|
||||||
|
if(num<0){
|
||||||
|
return num*-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
|
||||||
|
writeln("\nTIME - SPEED DISTANCE EXERCISE");
|
||||||
|
|
||||||
|
bool keep_playing = true;
|
||||||
|
float error_margin = 5.0;
|
||||||
|
|
||||||
|
while(keep_playing){
|
||||||
|
int car_speed = uniform!"[]"(40,65); //Random number between 40 and 65
|
||||||
|
int delta_time = uniform!"(]"(4,20); //Between 5 and 20
|
||||||
|
int train_speed = uniform!"[)"(20,40); //Between 20 and 39; This is the default if not specified: uniform(x,y)
|
||||||
|
|
||||||
|
writeln("\nA CAR TRAVELING AT ", car_speed, " MPH CAN MAKE A CERTAIN TRIP IN ", delta_time,
|
||||||
|
" HOURS LESS THAN A TRAIN TRAVELING AT ", train_speed, "MPH." );
|
||||||
|
|
||||||
|
float input;
|
||||||
|
write("HOW LONG DOES THE TRIP TAKE BY CAR? ");
|
||||||
|
readf!"%f\n"(input);
|
||||||
|
|
||||||
|
float car_time = cast(float)delta_time * train_speed / (car_speed - train_speed);
|
||||||
|
int percent = cast(int)( abs(car_time-input) * 100 / car_time + .5);
|
||||||
|
|
||||||
|
if(percent > error_margin){
|
||||||
|
writeln("SORRY. YOU WERE OFF BY ", percent, " PERCENT.");
|
||||||
|
}else{
|
||||||
|
writeln("GOOD! ANSWER WITHIN ", percent, " PERCENT.");
|
||||||
|
}
|
||||||
|
writeln("CORRECT ANSWER IS ", car_time, " HOURS.");
|
||||||
|
|
||||||
|
string answer;
|
||||||
|
write("\nANOTHER PROBLEM (YES OR NO)? ");
|
||||||
|
readf!"%s\n"(answer);
|
||||||
|
|
||||||
|
if( !(answer == "YES" || answer == "Y" || answer == "yes" || answer == "y") ){
|
||||||
|
keep_playing = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
59
91_Train/lua/train.lua
Normal file
59
91_Train/lua/train.lua
Normal 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()
|
||||||
Reference in New Issue
Block a user