Skip to content

Commit e40ed4e

Browse files
author
RFigCon
committed
train in Lua and D
1 parent 25faf4a commit e40ed4e

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Original source downloaded from [Vintage Basic](http://www.vintage-basic.net/games.html).
2+
3+
Conversion to [MiniScript](https://dlang.org).
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import std.stdio;
2+
import std.random : uniform;
3+
4+
float abs(float num) {
5+
if(num<0){
6+
return num*-1;
7+
}
8+
9+
return num;
10+
}
11+
12+
void main() {
13+
14+
writeln("\nTIME - SPEED DISTANCE EXERCISE");
15+
16+
bool keep_playing = true;
17+
float error_margin = 5.0;
18+
19+
while(keep_playing){
20+
int car_speed = uniform!"[]"(40,65); //Random number between 40 and 65
21+
int delta_time = uniform!"(]"(4,20); //Between 5 and 20
22+
int train_speed = uniform!"[)"(20,40); //Between 20 and 39; This is the default if not specified: uniform(x,y)
23+
24+
writeln("\nA CAR TRAVELING AT ", car_speed, " MPH CAN MAKE A CERTAIN TRIP IN ", delta_time,
25+
" HOURS LESS THAN A TRAIN TRAVELING AT ", train_speed, "MPH." );
26+
27+
float input;
28+
write("HOW LONG DOES THE TRIP TAKE BY CAR? ");
29+
readf!"%f\n"(input);
30+
31+
float car_time = cast(float)delta_time * train_speed / (car_speed - train_speed);
32+
int percent = cast(int)( abs(car_time-input) * 100 / car_time + .5);
33+
34+
if(percent > error_margin){
35+
writeln("SORRY. YOU WERE OFF BY ", percent, " PERCENT.");
36+
}else{
37+
writeln("GOOD! ANSWER WITHIN ", percent, " PERCENT.");
38+
}
39+
writeln("CORRECT ANSWER IS ", car_time, " HOURS.");
40+
41+
string answer;
42+
write("\nANOTHER PROBLEM (YES OR NO)? ");
43+
readf!"%s\n"(answer);
44+
45+
if( !(answer == "YES" || answer == "Y" || answer == "yes" || answer == "y") ){
46+
keep_playing = false;
47+
}
48+
}
49+
50+
}

91_Train/lua/train.lua

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
print [[
2+
TRAIN
3+
CREATIVE COMPUTING MORRISTOWN, NEW JERSY
4+
5+
6+
7+
TIME - SPEED DISTANCE EXERCISE]]
8+
9+
math.randomseed(os.time())
10+
11+
local error_margin = 5.0
12+
13+
function play()
14+
local car_speed = math.random(40, 65)
15+
local delta_time = math.random(5, 20)
16+
local train_speed = math.random(20, 39)
17+
18+
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) )
19+
20+
local try = true
21+
local input
22+
while try do
23+
io.write("HOW LONG DOES THE TRIP TAKE BY CAR? ")
24+
input = io.read("n")
25+
if input == nil then
26+
print("<!>PLEASE INSERT A NUMBER<!>")
27+
else
28+
try = false
29+
end
30+
io.read()
31+
end
32+
33+
local car_time = delta_time * train_speed / (car_speed - train_speed)
34+
local percent = ( math.abs(car_time-input) * 100 / car_time + .5)
35+
36+
if percent > error_margin then
37+
print( string.format("SORRY. YOU WERE OFF BY %f PERCENT.", percent) )
38+
else
39+
print( string.format("GOOD! ANSWER WITHIN %f PERCENT.", percent) )
40+
end
41+
42+
print( string.format("CORRECT ANSWER IS %f HOURS.", car_time) )
43+
end
44+
45+
function game_loop()
46+
local keep_playing = true
47+
while keep_playing do
48+
play()
49+
io.write("\nANOTHER PROBLEM (YES OR NO)? ")
50+
answer = io.read("l")
51+
52+
if not (answer == "YES" or answer == "Y" or answer == "yes" or answer == "y") then
53+
keep_playing = false
54+
end
55+
end
56+
57+
end
58+
59+
game_loop()

0 commit comments

Comments
 (0)