mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 15:37:51 -08:00
Ported DEPTH CHARGE, DIAMOND, and DICE to Javascript
This commit is contained in:
9
31 Depth Charge/javascript/depthcharge.html
Normal file
9
31 Depth Charge/javascript/depthcharge.html
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>DEPTH CHARGE</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<pre id="output" style="font-size: 12pt;"></pre>
|
||||||
|
<script src="depthcharge.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
112
31 Depth Charge/javascript/depthcharge.js
Normal file
112
31 Depth Charge/javascript/depthcharge.js
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
// DEPTH CHARGE
|
||||||
|
//
|
||||||
|
// 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(30) + "DEPTH CHARGE\n");
|
||||||
|
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||||
|
print("\n");
|
||||||
|
print("\n");
|
||||||
|
print("\n");
|
||||||
|
print("DIMENSION OF THE SEARCH AREA");
|
||||||
|
g = Math.floor(await input());
|
||||||
|
n = Math.floor(Math.log(g) / Math.log(2)) + 1;
|
||||||
|
print("YOU ARE THE CAPTAIN OF THE DESTROYER USS COMPUTER\n");
|
||||||
|
print("AN ENEMY SUB HAS BEEN CAUSING YOU TROUBLE. YOUR\n");
|
||||||
|
print("MISSION IS TO DESTROY IT. YOU HAVE " + n + " SHOTS.\n");
|
||||||
|
print("SPECIFY DEPTH CHARGE EXPLOSION POINT WITH A\n");
|
||||||
|
print("TRIO OF NUMBERS -- THE FIRST TWO ARE THE\n");
|
||||||
|
print("SURFACE COORDINATES; THE THIRD IS THE DEPTH.\n");
|
||||||
|
do {
|
||||||
|
print("\n");
|
||||||
|
print("GOOD LUCK !\n");
|
||||||
|
print("\n");
|
||||||
|
a = Math.floor(Math.random() * g);
|
||||||
|
b = Math.floor(Math.random() * g);
|
||||||
|
c = Math.floor(Math.random() * g);
|
||||||
|
for (d = 1; d <= n; d++) {
|
||||||
|
print("\n");
|
||||||
|
print("TRIAL #" + d + " ");
|
||||||
|
str = await input();
|
||||||
|
x = parseInt(str);
|
||||||
|
y = parseInt(str.substr(str.indexOf(",") + 1));
|
||||||
|
z = parseInt(str.substr(str.lastIndexOf(",") + 1));
|
||||||
|
if (Math.abs(x - a) + Math.abs(y - b) + Math.abs(z - c) == 0)
|
||||||
|
break;
|
||||||
|
if (y > b)
|
||||||
|
print("NORTH");
|
||||||
|
if (y < b)
|
||||||
|
print("SOUTH");
|
||||||
|
if (x > a)
|
||||||
|
print("EAST");
|
||||||
|
if (x < a)
|
||||||
|
print("WEST");
|
||||||
|
if (y != b || x != a)
|
||||||
|
print(" AND");
|
||||||
|
if (z > c)
|
||||||
|
print(" TOO LOW.\n");
|
||||||
|
if (z < c)
|
||||||
|
print(" TOO HIGH.\n");
|
||||||
|
if (z == c)
|
||||||
|
print(" DEPTH OK.\n");
|
||||||
|
print("\n");
|
||||||
|
}
|
||||||
|
if (d <= n) {
|
||||||
|
print("\n");
|
||||||
|
print("B O O M ! ! YOU FOUND IT IN " + d + " TRIES!\n");
|
||||||
|
} else {
|
||||||
|
print("\n");
|
||||||
|
print("YOU HAVE BEEN TORPEDOED! ABANDON SHIP!\n");
|
||||||
|
print("THE SUBMARINE WAS AT " + a + "," + b + "," + c + "\n");
|
||||||
|
}
|
||||||
|
print("\n");
|
||||||
|
print("\n");
|
||||||
|
print("ANOTHER GAME (Y OR N)");
|
||||||
|
str = await input();
|
||||||
|
} while (str.substr(0, 1) == "Y") ;
|
||||||
|
print("OK. HOPE YOU ENJOYED YOURSELF.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
9
32 Diamond/javascript/diamond.html
Normal file
9
32 Diamond/javascript/diamond.html
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>DIAMOND</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<pre id="output" style="font-size: 12pt;"></pre>
|
||||||
|
<script src="diamond.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
94
32 Diamond/javascript/diamond.js
Normal file
94
32 Diamond/javascript/diamond.js
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
// DIAMOND
|
||||||
|
//
|
||||||
|
// 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) + "DIAMOND\n");
|
||||||
|
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||||
|
print("\n");
|
||||||
|
print("\n");
|
||||||
|
print("\n");
|
||||||
|
print("FOR A PRETTY DIAMOND PATTERN,\n");
|
||||||
|
print("TYPE IN AN ODD NUMBER BETWEEN 5 AND 21");
|
||||||
|
r = parseInt(await input());
|
||||||
|
q = Math.floor(60 / r);
|
||||||
|
as = "CC"
|
||||||
|
x = 1;
|
||||||
|
y = r;
|
||||||
|
z = 2;
|
||||||
|
for (l = 1; l <= q; l++) {
|
||||||
|
for (n = x; z < 0 ? n >= y : n <= y; n += z) {
|
||||||
|
str = "";
|
||||||
|
while (str.length < (r - n) / 2)
|
||||||
|
str += " ";
|
||||||
|
for (m = 1; m <= q; m++) {
|
||||||
|
c = 1;
|
||||||
|
for (a = 1; a <= n; a++) {
|
||||||
|
if (c > as.length)
|
||||||
|
str += "!";
|
||||||
|
else
|
||||||
|
str += as[c++ - 1];
|
||||||
|
}
|
||||||
|
if (m == q)
|
||||||
|
break;
|
||||||
|
while (str.length < r * m + (r - n) / 2)
|
||||||
|
str += " ";
|
||||||
|
}
|
||||||
|
print(str + "\n");
|
||||||
|
}
|
||||||
|
if (x != 1) {
|
||||||
|
x = 1;
|
||||||
|
y = r;
|
||||||
|
z = 2;
|
||||||
|
} else {
|
||||||
|
x = r - 2;
|
||||||
|
y = 1;
|
||||||
|
z = -2;
|
||||||
|
l--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
9
33 Dice/javascript/dice.html
Normal file
9
33 Dice/javascript/dice.html
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>DICE</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<pre id="output" style="font-size: 12pt;"></pre>
|
||||||
|
<script src="dice.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
84
33 Dice/javascript/dice.js
Normal file
84
33 Dice/javascript/dice.js
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
// DICE
|
||||||
|
//
|
||||||
|
// 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(34) + "DICE\n");
|
||||||
|
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||||
|
print("\n");
|
||||||
|
print("\n");
|
||||||
|
print("\n");
|
||||||
|
f = [];
|
||||||
|
// Danny Freidus
|
||||||
|
print("THIS PROGRAM SIMULATES THE ROLLING OF A\n");
|
||||||
|
print("PAIR OF DICE.\n");
|
||||||
|
print("YOU ENTER THE NUMBER OF TIMES YOU WANT THE COMPUTER TO\n");
|
||||||
|
print("'ROLL' THE DICE. WATCH OUT, VERY LARGE NUMBERS TAKE\n");
|
||||||
|
print("A LONG TIME. IN PARTICULAR, NUMBERS OVER 5000.\n");
|
||||||
|
do {
|
||||||
|
for (q = 1; q <= 12; q++)
|
||||||
|
f[q] = 0;
|
||||||
|
print("\n");
|
||||||
|
print("HOW MANY ROLLS");
|
||||||
|
x = parseInt(await input());
|
||||||
|
for (s = 1; s <= x; s++) {
|
||||||
|
a = Math.floor(Math.random() * 6 + 1);
|
||||||
|
b = Math.floor(Math.random() * 6 + 1);
|
||||||
|
r = a + b;
|
||||||
|
f[r]++;
|
||||||
|
}
|
||||||
|
print("\n");
|
||||||
|
print("TOTAL SPOTS\tNUMBER OF TIMES\n");
|
||||||
|
for (v = 2; v <= 12; v++) {
|
||||||
|
print("\t" + v + "\t" + f[v] + "\n");
|
||||||
|
}
|
||||||
|
print("\n");
|
||||||
|
print("\n");
|
||||||
|
print("TRY AGAIN");
|
||||||
|
str = await input();
|
||||||
|
} while (str.substr(0, 1) == "Y") ;
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
Reference in New Issue
Block a user