mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Merge pull request #74 from nanochess/main
Ported TRAIN, TRAP and 23 MATCHES to Javascript
This commit is contained in:
9
74 Rock Scissors Paper/javascript/rockscissors.html
Normal file
9
74 Rock Scissors Paper/javascript/rockscissors.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>ROCK, SCISSORS, PAPER</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="rockscissors.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
107
74 Rock Scissors Paper/javascript/rockscissors.js
Normal file
107
74 Rock Scissors Paper/javascript/rockscissors.js
Normal file
@@ -0,0 +1,107 @@
|
||||
// ROCK, SCISSORS, PAPER
|
||||
//
|
||||
// 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(21) + "GAME OF ROCK, SCISSORS, PAPER\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
while (1) {
|
||||
print("HOW MANY GAMES");
|
||||
q = parseInt(await input());
|
||||
if (q >= 11)
|
||||
print("SORRY, BUT WE AREN'T ALLOWED TO PLAY THAT MANY.\n");
|
||||
else
|
||||
break;
|
||||
}
|
||||
h = 0; // Human
|
||||
c = 0; // Computer
|
||||
for (g = 1; g <= q; g++ ) {
|
||||
print("\n");
|
||||
print("GAME NUMBER " + g + "\n");
|
||||
x = Math.floor(Math.random() * 3 + 1);
|
||||
while (1) {
|
||||
print("3=ROCK...2=SCISSORS...1=PAPER\n");
|
||||
print("1...2...3...WHAT'S YOUR CHOICE");
|
||||
k = parseInt(await input());
|
||||
if (k != 1 && k != 2 && k != 3)
|
||||
print("INVALID.\n");
|
||||
else
|
||||
break;
|
||||
}
|
||||
print("THIS IS MY CHOICE...");
|
||||
switch (x) {
|
||||
case 1:
|
||||
print("...PAPER\n");
|
||||
break;
|
||||
case 2:
|
||||
print("...SCISSORS\n");
|
||||
break;
|
||||
case 3:
|
||||
print("...ROCK\n");
|
||||
break;
|
||||
}
|
||||
if (x == k) {
|
||||
print("TIE GAME. NO WINNER.\n");
|
||||
} else if ((x > k && (k != 1 || x != 3)) || (x == 1 && k == 3)) {
|
||||
print("WOW! I WIN!!!\n");
|
||||
c++;
|
||||
} else {
|
||||
print("YOU WIN!!!\n");
|
||||
h++;
|
||||
}
|
||||
}
|
||||
print("\n");
|
||||
print("HERE IS THE FINAL GAME SCORE:\n");
|
||||
print("I HAVE WON " + c + " GAME(S).\n");
|
||||
print("YOU HAVE WON " + h + " GAME(S).\n");
|
||||
print("AND " + (q - (c + h)) + " GAME(S) ENDED IN A TIE.\n");
|
||||
print("\n");
|
||||
print("THANKS FOR PLAYING!!\n");
|
||||
}
|
||||
|
||||
main();
|
||||
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();
|
||||
9
92 Trap/javascript/trap.html
Normal file
9
92 Trap/javascript/trap.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>TRAP</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="trap.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
102
92 Trap/javascript/trap.js
Normal file
102
92 Trap/javascript/trap.js
Normal file
@@ -0,0 +1,102 @@
|
||||
// TRAP
|
||||
//
|
||||
// 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(34) + "TRAP\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
g = 6;
|
||||
n = 100;
|
||||
// Trap
|
||||
// Steve Ullman, Aug/01/1972
|
||||
print("INSTRUCTIONS");
|
||||
str = await input();
|
||||
if (str.substr(0, 1) == "Y") {
|
||||
print("I AM THINKING OF A NUMBER BETWEEN 1 AND " + n + "\n");
|
||||
print("TRY TO GUESS MY NUMBER. ON EACH GUESS,\n");
|
||||
print("YOU ARE TO ENTER 2 NUMBERS, TRYING TO TRAP\n");
|
||||
print("MY NUMBER BETWEEN THE TWO NUMBERS. I WILL\n");
|
||||
print("TELL YOU IF YOU HAVE TRAPPED MY NUMBER, IF MY\n");
|
||||
print("NUMBER IS LARGER THAN YOUR TWO NUMBERS, OR IF\n");
|
||||
print("MY NUMBER IS SMALLER THAN YOUR TWO NUMBERS.\n");
|
||||
print("IF YOU WANT TO GUESS ONE SINGLE NUMBER, TYPE\n");
|
||||
print("YOUR GUESS FOR BOTH YOUR TRAP NUMBERS.\n");
|
||||
print("YOU GET " + g + " GUESSES TO GET MY NUMBER.\n");
|
||||
}
|
||||
while (1) {
|
||||
x = Math.floor(n * Math.random()) + 1;
|
||||
for (q = 1; q <= g; q++) {
|
||||
print("\n");
|
||||
print("GUESS #" + q + " ");
|
||||
str = await input();
|
||||
a = parseInt(str);
|
||||
b = parseInt(str.substr(str.indexOf(",") + 1));
|
||||
if (a == b && x == a) {
|
||||
print("YOU GOT IT!!!\n");
|
||||
break;
|
||||
}
|
||||
if (a > b) {
|
||||
r = a;
|
||||
a = b;
|
||||
b = r;
|
||||
}
|
||||
if (a <= x && x <= b) {
|
||||
print("YOU HAVE TRAPPED MY NUMBER.\n");
|
||||
} else if (x >= a) {
|
||||
print("MY NUMBER IS LARGER THAN YOUR TRAP NUMBERS.\n");
|
||||
} else {
|
||||
print("MY NUMBER IS SMALLER THAN YOUR TRAP NUMBERS.\n");
|
||||
}
|
||||
}
|
||||
print("\n");
|
||||
print("TRY AGAIN.\n");
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
9
93 23 Matches/javascript/23matches.html
Normal file
9
93 23 Matches/javascript/23matches.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>23 MATCHES</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="23matches.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
122
93 23 Matches/javascript/23matches.js
Normal file
122
93 23 Matches/javascript/23matches.js
Normal file
@@ -0,0 +1,122 @@
|
||||
// 23 MATCHES
|
||||
//
|
||||
// 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(31) + "23 MATCHES\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print(" THIS IS A GAME CALLED '23 MATCHES'.\n");
|
||||
print("\n");
|
||||
print("WHEN IT IS YOUR TURN, YOU MAY TAKE ONE, TWO, OR THREE\n");
|
||||
print("MATCHES. THE OBJECT OF THE GAME IS NOT TO HAVE TO TAKE\n");
|
||||
print("THE LAST MATCH.\n");
|
||||
print("\n");
|
||||
print("LET'S FLIP A COIN TO SEE WHO GOES FIRST.\n");
|
||||
print("IF IT COMES UP HEADS, I WILL WIN THE TOSS.\n");
|
||||
print("\n");
|
||||
n = 23;
|
||||
q = Math.floor(2 * Math.random());
|
||||
if (q != 1) {
|
||||
print("TAILS! YOU GO FIRST. \n");
|
||||
print("\n");
|
||||
} else {
|
||||
print("HEADS! I WIN! HA! HA!\n");
|
||||
print("PREPARE TO LOSE, MEATBALL-NOSE!!\n");
|
||||
print("\n");
|
||||
print("I TAKE 2 MATCHES\n");
|
||||
n -= 2;
|
||||
}
|
||||
while (1) {
|
||||
if (q == 1) {
|
||||
print("THE NUMBER OF MATCHES IS NOW " + n + "\n");
|
||||
print("\n");
|
||||
print("YOUR TURN -- YOU MAY TAKE 1, 2 OR 3 MATCHES.\n");
|
||||
}
|
||||
print("HOW MANY DO YOU WISH TO REMOVE ");
|
||||
while (1) {
|
||||
k = parseInt(await input());
|
||||
if (k <= 0 || k > 3) {
|
||||
print("VERY FUNNY! DUMMY!\n");
|
||||
print("DO YOU WANT TO PLAY OR GOOF AROUND?\n");
|
||||
print("NOW, HOW MANY MATCHES DO YOU WANT ");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
n -= k;
|
||||
print("THERE ARE NOW " + n + " MATCHES REMAINING.\n");
|
||||
if (n == 4) {
|
||||
z = 3;
|
||||
} else if (n == 3) {
|
||||
z = 2;
|
||||
} else if (n == 2) {
|
||||
z = 1;
|
||||
} else if (n > 1) {
|
||||
z = 4 - k;
|
||||
} else {
|
||||
print("YOU WON, FLOPPY EARS !\n");
|
||||
print("THINK YOU'RE PRETTY SMART !\n");
|
||||
print("LETS PLAY AGAIN AND I'LL BLOW YOUR SHOES OFF !!\n");
|
||||
break;
|
||||
}
|
||||
print("MY TURN ! I REMOVE " + z + " MATCHES\n");
|
||||
n -= z;
|
||||
if (n <= 1) {
|
||||
print("\n");
|
||||
print("YOU POOR BOOB! YOU TOOK THE LAST MATCH! I GOTCHA!!\n");
|
||||
print("HA ! HA ! I BEAT YOU !!!\n");
|
||||
print("\n");
|
||||
print("GOOD BYE LOSER!\n");
|
||||
break;
|
||||
}
|
||||
q = 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user