Merge pull request #87 from nanochess/main

Ported BOMBARDMENT, BOMBS AWAY and BOUNCE to Javascript
This commit is contained in:
Jeff Atwood
2021-02-24 12:27:29 -08:00
committed by GitHub
6 changed files with 511 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
<html>
<head>
<title>BOMBARDMENT</title>
</head>
<body>
<pre id="output" style="font-size: 12pt;"></pre>
<script src="bombardment.js"></script>
</body>
</html>

View File

@@ -0,0 +1,167 @@
// BOMBARDMENT
//
// 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 program
async function main()
{
print(tab(33) + "BOMBARDMENT\n");
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
print("\n");
print("\n");
print("\n");
print("YOU ARE ON A BATTLEFIELD WITH 4 PLATOONS AND YOU\n");
print("HAVE 25 OUTPOSTS AVAILABLE WHERE THEY MAY BE PLACED.\n");
print("YOU CAN ONLY PLACE ONE PLATOON AT ANY ONE OUTPOST.\n");
print("THE COMPUTER DOES THE SAME WITH ITS FOUR PLATOONS.\n");
print("\n");
print("THE OBJECT OF THE GAME IS TO FIRE MISSILES AT THE\n");
print("OUTPOSTS OF THE COMPUTER. IT WILL DO THE SAME TO YOU.\n");
print("THE ONE WHO DESTROYS ALL FOUR OF THE ENEMY'S PLATOONS\n");
print("FIRST IS THE WINNER.\n");
print("\n");
print("GOOD LUCK... AND TELL US WHERE YOU WANT THE BODIES SENT!\n");
print("\n");
// "TEAR OFF" because it supposed this to be printed on a teletype
print("TEAR OFF MATRIX AND USE IT TO CHECK OFF THE NUMBERS.\n");
for (r = 1; r <= 5; r++)
print("\n");
ma = [];
for (r = 1; r <= 100; r++)
ma[r] = 0;
p = 0;
q = 0;
z = 0;
for (r = 1; r <= 5; r++) {
i = (r - 1) * 5 + 1;
print(i + "\t" + (i + 1) + "\t" + (i + 2) + "\t" + (i + 3) + "\t" + (i + 4) + "\n");
}
for (r = 1; r <= 10; r++)
print("\n");
c = Math.floor(Math.random() * 25) + 1;
do {
d = Math.floor(Math.random() * 25) + 1;
e = Math.floor(Math.random() * 25) + 1;
f = Math.floor(Math.random() * 25) + 1;
} while (c == d || c == e || c == f || d == e || d == f || e == f) ;
print("WHAT ARE YOUR FOUR POSITIONS");
str = await input();
g = parseInt(str);
str = str.substr(str.indexOf(",") + 1);
h = parseInt(str);
str = str.substr(str.indexOf(",") + 1);
k = parseInt(str);
str = str.substr(str.indexOf(",") + 1);
l = parseInt(str);
print("\n");
// Another "bug" your outpost can be in the same position as a computer outpost
// Let us suppose both live in a different matrix.
while (1) {
// The original game didn't limited the input to 1-25
do {
print("WHERE DO YOU WISH TO FIRE YOUR MISSLE");
y = parseInt(await input());
} while (y < 0 || y > 25) ;
if (y == c || y == d || y == e || y == f) {
// The original game has a bug. You can shoot the same outpost
// several times. This solves it.
if (y == c)
c = 0;
if (y == d)
d = 0;
if (y == e)
e = 0;
if (y == f)
f = 0;
q++;
if (q == 1) {
print("ONE DOWN. THREE TO GO.\n");
} else if (q == 2) {
print("TWO DOWN. TWO TO GO.\n");
} else if (q == 3) {
print("THREE DOWN. ONE TO GO.\n");
} else {
print("YOU GOT ME, I'M GOING FAST. BUT I'LL GET YOU WHEN\n");
print("MY TRANSISTO&S RECUP%RA*E!\n");
break;
}
} else {
print("HA, HA YOU MISSED. MY TURN NOW:\n");
}
print("\n");
print("\n");
do {
m = Math.floor(Math.random() * 25 + 1);
p++;
n = p - 1;
for (t = 1; t <= n; t++) {
if (m == ma[t])
break;
}
} while (t <= n) ;
x = m;
ma[p] = m;
if (x == g || x == h || x == l || x == k) {
z++;
if (z < 4)
print("I GOT YOU. IT WON'T BE LONG NOW. POST " + x + " WAS HIT.\n");
if (z == 1) {
print("YOU HAVE ONLY THREE OUTPOSTS LEFT.\n");
} else if (z == 2) {
print("YOU HAVE ONLY TWO OUTPOSTS LEFT.\n");
} else if (z == 3) {
print("YOU HAVE ONLY ONE OUTPOST LEFT.\n");
} else {
print("YOU'RE DEAD. YOUR LAST OUTPOST WAS AT " + x + ". HA, HA, HA.\n");
print("BETTER LUCK NEXT TIME.\n");
}
} else {
print("I MISSED YOU, YOU DIRTY RAT. I PICKED " + m + ". YOUR TURN:\n");
}
print("\n");
print("\n");
}
}
main();

View File

@@ -0,0 +1,9 @@
<html>
<head>
<title>BOMBARDMENT</title>
</head>
<body>
<pre id="output" style="font-size: 12pt;"></pre>
<script src="bombsaway.js"></script>
</body>
</html>

View File

@@ -0,0 +1,205 @@
// BOMBS AWAY
//
// 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 program
async function main()
{
while (1) {
print("YOU ARE A PILOT IN A WORLD WAR II BOMBER.\n");
while (1) {
print("WHAT SIDE -- ITALY(1), ALLIES(2), JAPAN(3), GERMANY(4)");
a = parseInt(await input());
if (a < 1 || a > 4)
print("TRY AGAIN...\n");
else
break;
}
if (a == 1) {
while (1) {
print("YOUR TARGET -- ALBANIA(1), GREECE(2), NORTH AFRICA(3)");
b = parseInt(await input());
if (b < 1 || b > 3)
print("TRY AGAIN...\n");
else
break;
}
print("\n");
if (b == 1) {
print("SHOULD BE EASY -- YOU'RE FLYING A NAZI-MADE PLANE.\n");
} else if (b == 2) {
print("BE CAREFUL!!!\n");
} else {
print("YOU'RE GOING FOR THE OIL, EH?\n");
}
} else if (a == 2) {
while (1) {
print("AIRCRAFT -- LIBERATOR(1), B-29(2), B-17(3), LANCASTER(4)");
g = parseInt(await input());
if (g < 1 || g > 4)
print("TRY AGAIN...\n");
else
break;
}
print("\n");
if (g == 1) {
print("YOU'VE GOT 2 TONS OF BOMBS FLYING FOR PLOESTI.\n");
} else if (g == 2) {
print("YOU'RE DUMPING THE A-BOMB ON HIROSHIMA.\n");
} else if (g == 3) {
print("YOU'RE CHASING THE BISMARK IN THE NORTH SEA.\n");
} else {
print("YOU'RE BUSTING A GERMAN HEAVY WATER PLANT IN THE RUHR.\n");
}
} else if (a == 3) {
print("YOU'RE FLYING A KAMIKAZE MISSION OVER THE USS LEXINGTON.\n");
print("YOUR FIRST KAMIKAZE MISSION(Y OR N)");
str = await input();
if (str == "N") {
s = 0;
} else {
s = 1;
print("\n");
}
} else {
while (1) {
print("A NAZI, EH? OH WELL. ARE YOU GOING FOR RUSSIA(1),\n");
print("ENGLAND(2), OR FRANCE(3)");
m = parseInt(await input());
if (m < 1 || m > 3)
print("TRY AGAIN...\n");
else
break;
}
print("\n");
if (m == 1) {
print("YOU'RE NEARING STALINGRAD.\n");
} else if (m == 2) {
print("NEARING LONDON. BE CAREFUL, THEY'VE GOT RADAR.\n");
} else if (m == 3) {
print("NEARING VERSAILLES. DUCK SOUP. THEY'RE NEARLY DEFENSELESS.\n");
}
}
if (a != 3) {
print("\n");
while (1) {
print("HOW MANY MISSIONS HAVE YOU FLOWN");
d = parseInt(await input());
if (d < 160)
break;
print("MISSIONS, NOT MILES...\n");
print("150 MISSIONS IS HIGH EVEN FOR OLD-TIMERS.\n");
print("NOW THEN, ");
}
print("\n");
if (d >= 100) {
print("THAT'S PUSHING THE ODDS!\n");
} else if (d < 25) {
print("FRESH OUT OF TRAINING, EH?\n");
}
print("\n");
if (d >= 160 * Math.random())
hit = true;
else
hit = false;
} else {
if (s == 0) {
hit = false;
} else if (Math.random() > 0.65) {
hit = true;
} else {
hit = false;
s = 100;
}
}
if (hit) {
print("DIRECT HIT!!!! " + Math.floor(100 * Math.random()) + " KILLED.\n");
print("MISSION SUCCESSFUL.\n");
} else {
t = 0;
if (a != 3) {
print("MISSED TARGET BY " + Math.floor(2 + 30 * Math.random()) + " MILES!\n");
print("NOW YOU'RE REALLY IN FOR IT !!\n");
print("\n");
while (1) {
print("DOES THE ENEMY HAVE GUNS(1), MISSILE(2), OR BOTH(3)");
r = parseInt(await input());
if (r < 1 || r > 3)
print("TRY AGAIN...\n");
else
break;
}
print("\n");
if (r != 2) {
print("WHAT'S THE PERCENT HIT RATE OF ENEMY GUNNERS (10 TO 50)");
s = parseInt(await input());
if (s < 10)
print("YOU LIE, BUT YOU'LL PAY...\n");
print("\n");
}
print("\n");
if (r > 1)
t = 35;
}
if (s + t <= 100 * Math.random()) {
print("YOU MADE IT THROUGH TREMENDOUS FLAK!!\n");
} else {
print("* * * * BOOM * * * *\n");
print("YOU HAVE BEEN SHOT DOWN.....\n");
print("DEARLY BELOVED, WE ARE GATHERED HERE TODAY TO PAY OUR\n");
print("LAST TRIBUTE...\n");
}
}
print("\n");
print("\n");
print("\n");
print("ANOTHER MISSION (Y OR N)");
str = await input();
if (str != "Y")
break;
}
print("CHICKEN !!!\n");
print("\n");
}
main();

View File

@@ -0,0 +1,9 @@
<html>
<head>
<title>BOUNCE</title>
</head>
<body>
<pre id="output" style="font-size: 12pt;"></pre>
<script src="bounce.js"></script>
</body>
</html>

View File

@@ -0,0 +1,112 @@
// BOUNCE
//
// 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 program
async function main()
{
print(tab(33) + "BOUNCE\n");
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
print("\n");
print("\n");
print("\n");
ta = [];
print("THIS SIMULATION LETS YOU SPECIFY THE INITIAL VELOCITY\n");
print("OF A BALL THROWN STRAIGHT UP, AND THE COEFFICIENT OF\n");
print("ELASTICITY OF THE BALL. PLEASE USE A DECIMAL FRACTION\n");
print("COEFFICIENCY (LESS THAN 1).\n");
print("\n");
print("YOU ALSO SPECIFY THE TIME INCREMENT TO BE USED IN\n");
print("'STROBING' THE BALL'S FLIGHT (TRY .1 INITIALLY).\n");
print("\n");
while (1) {
print("TIME INCREMENT (SEC)");
s2 = parseFloat(await input());
print("\n");
print("VELOCITY (FPS)");
v = parseFloat(await input());
print("\n");
print("COEFFICIENT");
c = parseFloat(await input());
print("\n");
print("FEET\n");
print("\n");
s1 = Math.floor(70 / (v / (16 * s2)));
for (i = 1; i <= s1; i++)
ta[i] = v * Math.pow(c, i - 1) / 16;
for (h = Math.floor(-16 * Math.pow(v / 32, 2) + Math.pow(v, 2) / 32 + 0.5); h >= 0; h -= 0.5) {
str = "";
if (Math.floor(h) == h)
str += " " + h + " ";
l = 0;
for (i = 1; i <= s1; i++) {
for (t = 0; t <= ta[i]; t += s2) {
l += s2;
if (Math.abs(h - (0.5 * (-32) * Math.pow(t, 2) + v * Math.pow(c, i - 1) * t)) <= 0.25) {
while (str.length < l / s2)
str += " ";
str += "0";
}
}
t = ta[i + 1] / 2;
if (-16 * Math.pow(t, 2) + v * Math.pow(c, i - 1) * t < h)
break;
}
print(str + "\n");
}
str = " ";
for (i = 1; i < Math.floor(l + 1) / s2 + 1; i++)
str += ".";
print(str + "\n");
str = " 0";
for (i = 1; i < Math.floor(l + 0.9995); i++) {
while (str.length < Math.floor(i / s2))
str += " ";
str += i;
}
print(str + "\n");
print(tab(Math.floor(l + 1) / (2 * s2) - 2) + "SECONDS\n");
}
}
main();