mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-20 00:31:18 -08:00
Removed spaces from top-level directory names.
Spaces tend to cause annoyances in a Unix-style shell environment. This change fixes that.
This commit is contained in:
7
91_Train/README.md
Normal file
7
91_Train/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
### Train
|
||||
|
||||
As published in Basic Computer Games (1978)
|
||||
https://www.atariarchives.org/basicgames/showpage.php?page=175
|
||||
|
||||
Downloaded from Vintage Basic at
|
||||
http://www.vintage-basic.net/games.html
|
||||
3
91_Train/csharp/README.md
Normal file
3
91_Train/csharp/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Microsoft C#](https://docs.microsoft.com/en-us/dotnet/csharp/)
|
||||
3
91_Train/java/README.md
Normal file
3
91_Train/java/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Oracle Java](https://openjdk.java.net/)
|
||||
109
91_Train/java/src/Train.java
Normal file
109
91_Train/java/src/Train.java
Normal file
@@ -0,0 +1,109 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Train
|
||||
* <p>
|
||||
* Based on the Basic program Train here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/91%20Train/train.bas
|
||||
* <p>
|
||||
* Note: The idea was to create a version of the 1970's Basic program in Java, without introducing
|
||||
* new features - no additional text, error checking, etc has been added.
|
||||
*/
|
||||
public class Train {
|
||||
|
||||
private final Scanner kbScanner;
|
||||
|
||||
public Train() {
|
||||
kbScanner = new Scanner(System.in);
|
||||
}
|
||||
|
||||
public void process() {
|
||||
|
||||
intro();
|
||||
|
||||
boolean gameOver = false;
|
||||
|
||||
do {
|
||||
double carMph = (int) (25 * Math.random() + 40);
|
||||
double hours = (int) (15 * Math.random() + 5);
|
||||
double train = (int) (19 * Math.random() + 20);
|
||||
|
||||
System.out.println(" A CAR TRAVELING " + (int) carMph + " MPH CAN MAKE A CERTAIN TRIP IN");
|
||||
System.out.println((int) hours + " HOURS LESS THAN A TRAIN TRAVELING AT " + (int) train + " MPH.");
|
||||
|
||||
double howLong = Double.parseDouble(displayTextAndGetInput("HOW LONG DOES THE TRIP TAKE BY CAR? "));
|
||||
|
||||
double hoursAnswer = hours * train / (carMph - train);
|
||||
int percentage = (int) (Math.abs((hoursAnswer - howLong) * 100 / howLong) + .5);
|
||||
if (percentage > 5) {
|
||||
System.out.println("SORRY. YOU WERE OFF BY " + percentage + " PERCENT.");
|
||||
} else {
|
||||
System.out.println("GOOD! ANSWER WITHIN " + percentage + " PERCENT.");
|
||||
}
|
||||
System.out.println("CORRECT ANSWER IS " + hoursAnswer + " HOURS.");
|
||||
|
||||
System.out.println();
|
||||
if (!yesEntered(displayTextAndGetInput("ANOTHER PROBLEM (YES OR NO)? "))) {
|
||||
gameOver = true;
|
||||
}
|
||||
|
||||
} while (!gameOver);
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void intro() {
|
||||
System.out.println("TRAIN");
|
||||
System.out.println("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println();
|
||||
System.out.println("TIME - SPEED DISTANCE EXERCISE");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
/*
|
||||
* Print a message on the screen, then accept input from Keyboard.
|
||||
*
|
||||
* @param text message to be displayed on screen.
|
||||
* @return what was typed by the player.
|
||||
*/
|
||||
private String displayTextAndGetInput(String text) {
|
||||
System.out.print(text);
|
||||
return kbScanner.next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether player entered Y or YES to a question.
|
||||
*
|
||||
* @param text player string from kb
|
||||
* @return true of Y or YES was entered, otherwise false
|
||||
*/
|
||||
private boolean yesEntered(String text) {
|
||||
return stringIsAnyValue(text, "Y", "YES");
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a string equals one of a variable number of values
|
||||
* Useful to check for Y or YES for example
|
||||
* Comparison is case insensitive.
|
||||
*
|
||||
* @param text source string
|
||||
* @param values a range of values to compare against the source string
|
||||
* @return true if a comparison was found in one of the variable number of strings passed
|
||||
*/
|
||||
private boolean stringIsAnyValue(String text, String... values) {
|
||||
|
||||
return Arrays.stream(values).anyMatch(str -> str.equalsIgnoreCase(text));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Program startup.
|
||||
*
|
||||
* @param args not used (from command line).
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Train train = new Train();
|
||||
train.process();
|
||||
}
|
||||
}
|
||||
3
91_Train/javascript/README.md
Normal file
3
91_Train/javascript/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Shells)
|
||||
9
91_Train/javascript/train.html
Normal file
9
91_Train/javascript/train.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>TRAIN</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="train.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
80
91_Train/javascript/train.js
Normal file
80
91_Train/javascript/train.js
Normal file
@@ -0,0 +1,80 @@
|
||||
// TRAIN
|
||||
//
|
||||
// Converted from BASIC to Javascript by Oscar Toledo G. (nanochess)
|
||||
//
|
||||
|
||||
function print(str)
|
||||
{
|
||||
document.getElementById("output").appendChild(document.createTextNode(str));
|
||||
}
|
||||
|
||||
function input()
|
||||
{
|
||||
var input_element;
|
||||
var input_str;
|
||||
|
||||
return new Promise(function (resolve) {
|
||||
input_element = document.createElement("INPUT");
|
||||
|
||||
print("? ");
|
||||
input_element.setAttribute("type", "text");
|
||||
input_element.setAttribute("length", "50");
|
||||
document.getElementById("output").appendChild(input_element);
|
||||
input_element.focus();
|
||||
input_str = undefined;
|
||||
input_element.addEventListener("keydown", function (event) {
|
||||
if (event.keyCode == 13) {
|
||||
input_str = input_element.value;
|
||||
document.getElementById("output").removeChild(input_element);
|
||||
print(input_str);
|
||||
print("\n");
|
||||
resolve(input_str);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function tab(space)
|
||||
{
|
||||
var str = "";
|
||||
while (space-- > 0)
|
||||
str += " ";
|
||||
return str;
|
||||
}
|
||||
|
||||
// Main control section
|
||||
async function main()
|
||||
{
|
||||
print(tab(33) + "TRAIN\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("TIME - SPEED DISTANCE EXERCISE\n");
|
||||
print("\n ");
|
||||
while (1) {
|
||||
c = Math.floor(25 * Math.random()) + 40;
|
||||
d = Math.floor(15 * Math.random()) + 5;
|
||||
t = Math.floor(19 * Math.random()) + 20;
|
||||
print(" A CAR TRAVELING " + c + " MPH CAN MAKE A CERTAIN TRIP IN\n");
|
||||
print(d + " HOURS LESS THAN A TRAIN TRAVELING AT " + t + " MPH.\n");
|
||||
print("HOW LONG DOES THE TRIP TAKE BY CAR");
|
||||
a = parseFloat(await input());
|
||||
v = d * t / (c - t);
|
||||
e = Math.floor(Math.abs((v - a) * 100 / a) + 0.5);
|
||||
if (e > 5) {
|
||||
print("SORRY. YOU WERE OFF BY " + e + " PERCENT.\n");
|
||||
} else {
|
||||
print("GOOD! ANSWER WITHIN " + e + " PERCENT.\n");
|
||||
}
|
||||
print("CORRECT ANSWER IS " + v + " HOURS.\n");
|
||||
print("\n");
|
||||
print("ANOTHER PROBLEM (YES OR NO)\n");
|
||||
str = await input();
|
||||
print("\n");
|
||||
if (str.substr(0, 1) != "Y")
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
3
91_Train/pascal/README.md
Normal file
3
91_Train/pascal/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Pascal](https://en.wikipedia.org/wiki/Pascal_(programming_language))
|
||||
3
91_Train/perl/README.md
Normal file
3
91_Train/perl/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Perl](https://www.perl.org/)
|
||||
34
91_Train/perl/train.pl
Executable file
34
91_Train/perl/train.pl
Executable file
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
print ' 'x33 ."TRAIN\n";
|
||||
print ' 'x15 ."CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n";
|
||||
print "\n\n\n";
|
||||
print "TIME - SPEED DISTANCE EXERCISE\n"; print "\n";
|
||||
|
||||
|
||||
my $A= ""; #We must declare this before...
|
||||
do {
|
||||
my $C= int(25*rand(1))+40;
|
||||
my $D= int(15*rand(1))+5;
|
||||
my $T= int(19*rand(1))+20;
|
||||
|
||||
print " A CAR TRAVELING $C MPH CAN MAKE A CERTAIN TRIP IN\n";
|
||||
print "$D HOURS LESS THAN A TRAIN TRAVELING AT $T MPH.\n";
|
||||
print "HOW LONG DOES THE TRIP TAKE BY CAR\n";
|
||||
chomp ($A = <STDIN>);
|
||||
|
||||
my $V= $D*$T/($C-$T);
|
||||
my $E= int(abs(($V-$A)*100/$A)+.5);
|
||||
if ($E>5) {
|
||||
print "SORRY. YOU WERE OFF BY $E PERCENT.\n";
|
||||
} else {
|
||||
print "GOOD! ANSWER WITHIN $E PERCENT.\n";
|
||||
}
|
||||
|
||||
print "CORRECT ANSWER IS $V HOURS.\n";
|
||||
print "\n";
|
||||
print "ANOTHER PROBLEM (YES OR NO)\n";
|
||||
chomp ($A = uc(<STDIN>)); #Uppercased
|
||||
} until ($A ne "YES");
|
||||
3
91_Train/python/README.md
Normal file
3
91_Train/python/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Python](https://www.python.org/about/)
|
||||
44
91_Train/python/train.py
Normal file
44
91_Train/python/train.py
Normal file
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
# TRAIN
|
||||
#
|
||||
# Converted from BASIC to Python by Trevor Hobson
|
||||
|
||||
import random
|
||||
|
||||
|
||||
def play_game():
|
||||
"""Play one round of the game"""
|
||||
car_speed = random.randint(40, 65)
|
||||
time_difference = random.randint(5, 20)
|
||||
train_speed = random.randint(20, 39)
|
||||
print("\nA car travelling", car_speed, "MPH can make a certain trip in")
|
||||
print(time_difference, "hours less than a train travelling at", train_speed, "MPH")
|
||||
time_answer = 0
|
||||
while time_answer == 0:
|
||||
try:
|
||||
time_answer = float(input("How long does the trip take by car "))
|
||||
except ValueError:
|
||||
print("Please enter a number.")
|
||||
car_time = time_difference*train_speed/(car_speed-train_speed)
|
||||
error_percent = int(abs((car_time-time_answer)*100/time_answer)+.5)
|
||||
if error_percent > 5:
|
||||
print("Sorry. You were off by", error_percent, "percent.")
|
||||
print("Correct answer is", round(car_time, 6), "hours")
|
||||
else:
|
||||
print("Good! Answer within", error_percent, "percent.")
|
||||
|
||||
|
||||
def main():
|
||||
print(" " * 33 + "TRAIN")
|
||||
print(" " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n")
|
||||
print("Time - speed distance exercise")
|
||||
|
||||
keep_playing = True
|
||||
while keep_playing:
|
||||
play_game()
|
||||
keep_playing = input(
|
||||
"\nAnother problem (yes or no) ").lower().startswith("y")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
3
91_Train/ruby/README.md
Normal file
3
91_Train/ruby/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Ruby](https://www.ruby-lang.org/en/)
|
||||
58
91_Train/ruby/train.rb
Normal file
58
91_Train/ruby/train.rb
Normal file
@@ -0,0 +1,58 @@
|
||||
def intro
|
||||
puts " TRAIN
|
||||
CREATIVE COMPUTING MORRISTOWN, NEW JERSEY
|
||||
|
||||
|
||||
|
||||
TIME - SPEED DISTANCE EXERCISE
|
||||
|
||||
"
|
||||
end
|
||||
|
||||
def get_user_guess
|
||||
while true
|
||||
begin
|
||||
number = Float(gets.chomp)
|
||||
return number
|
||||
rescue ArgumentError
|
||||
# Ignored
|
||||
end
|
||||
|
||||
puts "!NUMBER EXPECTED - RETRY INPUT LINE"
|
||||
print "? "
|
||||
end
|
||||
end
|
||||
|
||||
def main
|
||||
intro
|
||||
|
||||
loop do
|
||||
car_speed = rand(25) + 40
|
||||
car_time = rand(15) + 5
|
||||
train_speed = rand(19) + 20
|
||||
|
||||
print " A CAR TRAVELING #{car_speed} MPH CAN MAKE A CERTAIN TRIP IN
|
||||
#{car_time} HOURS LESS THAN A TRAIN TRAVELING AT #{train_speed} MPH.
|
||||
HOW LONG DOES THE TRIP TAKE BY CAR? "
|
||||
guess = get_user_guess
|
||||
|
||||
answer = ((car_time * train_speed) / (car_speed - train_speed).to_f).round(5)
|
||||
delta = (((answer - guess) * 100 / guess) + 0.5).abs.to_i
|
||||
|
||||
if delta > 5
|
||||
puts "SORRY. YOU WERE OFF BY #{delta} PERCENT."
|
||||
else
|
||||
puts "GOOD! ANSWER WITHIN #{delta} PERCENT."
|
||||
end
|
||||
|
||||
print "CORRECT ANSWER IS #{answer == answer.to_i ? answer.to_i : answer} HOURS.
|
||||
|
||||
ANOTHER PROBLEM (YES OR NO)? "
|
||||
option = (gets || '').chomp.upcase
|
||||
break unless option == 'YES'
|
||||
end
|
||||
end
|
||||
|
||||
trap "SIGINT" do puts; exit 130 end
|
||||
|
||||
main
|
||||
24
91_Train/train.bas
Normal file
24
91_Train/train.bas
Normal file
@@ -0,0 +1,24 @@
|
||||
1 PRINT TAB(33);"TRAIN"
|
||||
2 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
|
||||
3 PRINT: PRINT: PRINT
|
||||
4 PRINT "TIME - SPEED DISTANCE EXERCISE": PRINT
|
||||
10 C=INT(25*RND(1))+40
|
||||
15 D=INT(15*RND(1))+5
|
||||
20 T=INT(19*RND(1))+20
|
||||
25 PRINT " A CAR TRAVELING";C;"MPH CAN MAKE A CERTAIN TRIP IN"
|
||||
30 PRINT D;"HOURS LESS THAN A TRAIN TRAVELING AT";T;"MPH."
|
||||
35 PRINT "HOW LONG DOES THE TRIP TAKE BY CAR";
|
||||
40 INPUT A
|
||||
45 V=D*T/(C-T)
|
||||
50 E=INT(ABS((V-A)*100/A)+.5)
|
||||
55 IF E>5 THEN 70
|
||||
60 PRINT "GOOD! ANSWER WITHIN";E;"PERCENT."
|
||||
65 GOTO 80
|
||||
70 PRINT "SORRY. YOU WERE OFF BY";E;"PERCENT."
|
||||
80 PRINT "CORRECT ANSWER IS";V;"HOURS."
|
||||
90 PRINT
|
||||
95 PRINT "ANOTHER PROBLEM (YES OR NO)";
|
||||
100 INPUT A$
|
||||
105 PRINT
|
||||
110 IF A$="YES" THEN 10
|
||||
999 END
|
||||
3
91_Train/vbnet/README.md
Normal file
3
91_Train/vbnet/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original BASIC source [downloaded from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Visual Basic .NET](https://en.wikipedia.org/wiki/Visual_Basic_.NET)
|
||||
Reference in New Issue
Block a user