Merge pull request #25 from nanochess/main

Ported BAGELS and BANNER to Javascript
This commit is contained in:
Jeff Atwood
2021-02-17 16:05:15 -08:00
committed by GitHub
5 changed files with 349 additions and 3 deletions

View File

@@ -3,7 +3,7 @@
15 REM *** BAGLES NUMBER GUESSING GAME
20 REM *** ORIGINAL SOURCE UNKNOWN BUT SUSPECTED TO BE
25 REM *** LAWRENCE HALL OF SCIENCE, U.C. BERKELY
30 DIM A1(6),A(3),B(3)
30 DIM A1(3),A(3),B(3)
40 Y=0:T=255
50 PRINT:PRINT:PRINT
70 INPUT "WOULD YOU LIKE THE RULES (YES OR NO)";A$
@@ -66,7 +66,7 @@
600 PRINT
605 NEXT I
610 PRINT "OH WELL."
615 PRINT "THAT'S TWNETY GUESSES. MY NUMBER WAS";100*A(1)+10*A(2)+A(3)
615 PRINT "THAT'S TWENTY GUESSES. MY NUMBER WAS";100*A(1)+10*A(2)+A(3)
620 GOTO 700
630 PRINT "TRY GUESSING A THREE-DIGIT NUMBER.":GOTO 230
650 PRINT "OH, I FORGOT TO TELL YOU THAT THE NUMBER I HAVE IN MIND"
@@ -74,7 +74,7 @@
680 PRINT "YOU GOT IT!!!":PRINT
690 Y=Y+1
700 INPUT "PLAY AGAIN (YES OR NO)";A$
720 IF LEFT$(A$,1)="YES" THEN 150
720 IF LEFT$(A$,1)="Y" THEN 150
730 IF Y=0 THEN 750
740 PRINT:PRINT "A";Y;"POINT BAGELS BUFF!!"
750 PRINT "HOPE YOU HAD FUN. BYE."

View File

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

View File

@@ -0,0 +1,160 @@
// BAGELS
//
// 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;
}
print(tab(33) + "BAGELS\n");
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
// *** Bagles number guessing game
// *** Original source unknown but suspected to be
// *** Lawrence Hall of Science, U.C. Berkeley
a1 = [0,0,0,0];
a = [0,0,0,0];
b = [0,0,0,0];
y = 0;
t = 255;
print("\n");
print("\n");
print("\n");
// Main program
async function main()
{
while (1) {
print("WOULD YOU LIKE THE RULES (YES OR NO)");
str = await input();
if (str.substr(0, 1) != "N") {
print("\n");
print("I AM THINKING OF A THREE-DIGIT NUMBER. TRY TO GUESS\n");
print("MY NUMBER AND I WILL GIVE YOU CLUES AS FOLLOWS:\n");
print(" PICO - ONE DIGIT CORRECT BUT IN THE WRONG POSITION\n");
print(" FERMI - ONE DIGIT CORRECT AND IN THE RIGHT POSITION\n");
print(" BAGELS - NO DIGITS CORRECT\n");
}
for (i = 1; i <= 3; i++) {
do {
a[i] = Math.floor(Math.random() * 10);
for (j = i - 1; j >= 1; j--) {
if (a[i] == a[j])
break;
}
} while (j >= 1) ;
}
print("\n");
print("O.K. I HAVE A NUMBER IN MIND.\n");
for (i = 1; i <= 20; i++) {
while (1) {
print("GUESS #" + i);
str = await input();
if (str.length != 3) {
print("TRY GUESSING A THREE-DIGIT NUMBER.\n");
continue;
}
for (z = 1; z <= 3; z++)
a1[z] = str.charCodeAt(z - 1);
for (j = 1; j <= 3; j++) {
if (a1[j] < 48 || a1[j] > 57)
break;
b[j] = a1[j] - 48;
}
if (j <= 3) {
print("WHAT?");
continue;
}
if (b[1] == b[2] || b[2] == b[3] || b[3] == b[1]) {
print("OH, I FORGOT TO TELL YOU THAT THE NUMBER I HAVE IN MIND\n");
print("HAS NO TWO DIGITS THE SAME.\n");
continue;
}
break;
}
c = 0;
d = 0;
for (j = 1; j <= 2; j++) {
if (a[j] == b[j + 1])
c++;
if (a[j + 1] == b[j])
c++;
}
if (a[1] == b[3])
c++;
if (a[3] == b[1])
c++;
for (j = 1; j <= 3; j++) {
if (a[j] == b[j])
d++;
}
if (d == 3)
break;
for (j = 0; j < c; j++)
print("PICO ");
for (j = 0; j < d; j++)
print("FERMI ");
if (c + d == 0)
print("BAGELS");
print("\n");
}
if (i <= 20) {
print("YOU GOT IT!!!\n");
print("\n");
} else {
print("OH WELL.\n");
print("THAT'S A TWENTY GUESS. MY NUMBER WAS " + a[1] + a[2] + a[3]);
}
y++;
print("PLAY AGAIN (YES OR NO)");
str = await input();
if (str.substr(0, 1) != "Y")
break;
}
if (y == 0)
print("HOPE YOU HAD FUN. BYE.\n");
else
print("\nA " + y + " POINT BAGELS BUFF!!\n");
}
main();

View File

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

View File

@@ -0,0 +1,168 @@
// BANNER
//
// 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;
}
var letters = [" ",0,0,0,0,0,0,0,
"A",505,37,35,34,35,37,505,
"G",125,131,258,258,290,163,101,
"E",512,274,274,274,274,258,258,
"T",2,2,2,512,2,2,2,
"W",256,257,129,65,129,257,256,
"L",512,257,257,257,257,257,257,
"S",69,139,274,274,274,163,69,
"O",125,131,258,258,258,131,125,
"N",512,7,9,17,33,193,512,
"F",512,18,18,18,18,2,2,
"K",512,17,17,41,69,131,258,
"B",512,274,274,274,274,274,239,
"D",512,258,258,258,258,131,125,
"H",512,17,17,17,17,17,512,
"M",512,7,13,25,13,7,512,
"?",5,3,2,354,18,11,5,
"U",128,129,257,257,257,129,128,
"R",512,18,18,50,82,146,271,
"P",512,18,18,18,18,18,15,
"Q",125,131,258,258,322,131,381,
"Y",8,9,17,481,17,9,8,
"V",64,65,129,257,129,65,64,
"X",388,69,41,17,41,69,388,
"Z",386,322,290,274,266,262,260,
"I",258,258,258,512,258,258,258,
"C",125,131,258,258,258,131,69,
"J",65,129,257,257,257,129,128,
"1",0,0,261,259,512,257,257,
"2",261,387,322,290,274,267,261,
"*",69,41,17,512,17,41,69,
"3",66,130,258,274,266,150,100,
"4",33,49,41,37,35,512,33,
"5",160,274,274,274,274,274,226,
"6",194,291,293,297,305,289,193,
"7",258,130,66,34,18,10,8,
"8",69,171,274,274,274,171,69,
"9",263,138,74,42,26,10,7,
"=",41,41,41,41,41,41,41,
"!",1,1,1,384,1,1,1,
"0",57,69,131,258,131,69,57,
".",1,1,129,449,129,1,1];
f = [];
j = [];
s = [];
// Main program
async function main()
{
print("HORIZONTAL");
x = parseInt(await input());
print("VERTICAL");
y = parseInt(await input());
print("CENTERED");
ls = await input();
g1 = 0;
if (ls > "P")
g1 = 1;
print("CHARACTER (TYPE 'ALL' IF YOU WANT CHARACTER BEING PRINTED)");
ms = await input();
print("STATEMENT");
as = await input();
print("SET PAGE"); // This means to prepare printer, just press Enter
os = await input();
for (t = 0; t < as.length; t++) {
ps = as.substr(t, 1);
for (o = 0; o < 50 * 8; o += 8) {
if (letters[o] == ps) {
for (u = 1; u <= 7; u++)
s[u] = letters[o + u];
break;
}
}
if (o == 50 * 8) {
ps = " ";
o = 0;
}
// print("Doing " + o + "\n");
if (o == 0) {
for (h = 1; h <= 7 * x; h++)
print("\n");
} else {
xs = ms;
if (ms == "ALL")
xs = ps;
for (u = 1; u <= 7; u++) {
// An inefficient way of extracting bits
// but good enough in BASIC because there
// aren't bit shifting operators.
for (k = 8; k >= 0; k--) {
if (Math.pow(2, k) >= s[u]) {
j[9 - k] = 0;
} else {
j[9 - k] = 1;
s[u] -= Math.pow(2, k);
if (s[u] == 1) {
f[u] = 9 - k;
break;
}
}
}
for (t1 = 1; t1 <= x; t1++) {
str = tab((63 - 4.5 * y) * g1 / xs.length + 1);
for (b = 1; b <= f[u]; b++) {
if (j[b] == 0) {
for (i = 1; i <= y; i++)
str += tab(xs.length);
} else {
for (i = 1; i <= y; i++)
str += xs;
}
}
print(str + "\n");
}
}
for (h = 1; h <= 2 * x; h++)
print("\n");
}
}
}
main();