Merge pull request #251 from nanochess/main

Ported GOLF to Javascript
This commit is contained in:
Jeff Atwood
2021-03-25 11:27:08 -07:00
committed by GitHub
6 changed files with 966 additions and 0 deletions

View File

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

369
39 Golf/javascript/golf.js Normal file
View File

@@ -0,0 +1,369 @@
// GOLF
//
// 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 la = [];
var f;
var s1;
var g2;
var g3;
var x;
var hole_data = [
361,4,4,2,389,4,3,3,206,3,4,2,500,5,7,2,
408,4,2,4,359,4,6,4,424,4,4,2,388,4,4,4,
196,3,7,2,400,4,7,2,560,5,7,2,132,3,2,2,
357,4,4,4,294,4,2,4,475,5,2,3,375,4,4,2,
180,3,6,2,550,5,6,6,
];
function show_obstacle()
{
switch (la[x]) {
case 1:
print("FAIRWAY.\n");
break;
case 2:
print("ROUGH.\n");
break;
case 3:
print("TREES.\n");
break;
case 4:
print("ADJACENT FAIRWAY.\n");
break;
case 5:
print("TRAP.\n");
break;
case 6:
print("WATER.\n");
break;
}
}
function show_score()
{
g2 += s1;
print("TOTAL PAR FOR " + (f - 1) + " HOLES IS " + g3 + " YOUR TOTAL IS " + g2 + "\n");
}
// Main program
async function main()
{
print(tab(34) + "GOLF\n");
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
print("\n");
print("\n");
print("\n");
print("WELCOME TO THE CREATIVE COMPUTING COUNTRY CLUB,\n");
print("AN EIGHTEEN HOLE CHAMPIONSHIP LAYOUT LOCATED A SHORT\n");
print("DISTANCE FROM SCENIC DOWNTOWN MORRISTOWN. THE\n");
print("COMMENTATOR WILL EXPLAIN THE GAME AS YOU PLAY.\n");
print("ENJOY YOUR GAME; SEE YOU AT THE 19TH HOLE...\n");
print("\n");
print("\n");
next_hole = 0;
g1 = 18;
g2 = 0;
g3 = 0;
a = 0;
n = 0.8;
s2 = 0;
f = 1;
while (1) {
print("WHAT IS YOUR HANDICAP");
h = parseInt(await input());
print("\n");
if (h < 0 || h > 30) {
print("PGA HANDICAPS RANGE FROM 0 TO 30.\n");
} else {
break;
}
}
do {
print("DIFFICULTIES AT GOLF INCLUDE:\n");
print("0=HOOK, 1=SLICE, 2=POOR DISTANCE, 4=TRAP SHOTS, 5=PUTTING\n");
print("WHICH ONE (ONLY ONE) IS YOUR WORST");
t = parseInt(await input());
print("\n");
} while (t > 5) ;
s1 = 0;
first_routine = true;
while (1) {
if (first_routine) {
la[0] = 0;
j = 0;
q = 0;
s2++;
k = 0;
if (f != 1) {
print("YOUR SCORE ON HOLE " + (f - 1) + " WAS " + s1 + "\n");
show_score();
if (g1 == f - 1) // Completed all holes?
return; // Exit game
if (s1 > p + 2) {
print("KEEP YOUR HEAD DOWN.\n");
} else if (s1 == p) {
print("A PAR. NICE GOING.\n");
} else if (s1 == p - 1) {
print("A BIRDIE.\n");
} else if (s1 == p - 2) {
if (p != 3)
print("A GREAT BIG EAGLE.\n");
else
print("A HOLE IN ONE.\n");
}
}
if (f == 19) {
print("\n");
show_score();
if (g1 == f - 1)
return;
}
s1 = 0;
print("\n");
if (s1 != 0 && la[0] < 1)
la[0] = 1;
}
if (s1 == 0) {
d = hole_data[next_hole++];
p = hole_data[next_hole++];
la[1] = hole_data[next_hole++];
la[2] = hole_data[next_hole++];
print("\n");
print("YOU ARE AT THE TEE OFF HOLE " + f + " DISTANCE " + d + " YARDS, PAR " + p + "\n");
g3 += p;
print("ON YOUR RIGHT IS ");
x = 1;
show_obstacle();
print("ON YOUR LEFT IS ");
x = 2
show_obstacle();
} else {
x = 0;
if (la[0] > 5) {
if (la[0] > 6) {
print("YOUR SHOT WENT OUT OF BOUNDS.\n");
} else {
print("YOUR SHOT WENT INTO THE WATER.\n");
}
s1++;
print("PENALTY STROKE ASSESSED. HIT FROM PREVIOUS LOCATION.\n");
j++;
la[0] = 1;
d = b;
} else {
print("SHOT WENT " + d1 + " YARDS. IT'S " + d2 + " YARDS FROM THE CUP.\n");
print("BALL IS " + Math.floor(o) + " YARDS OFF LINE... IN ");
show_obstacle();
}
}
while (1) {
if (a != 1) {
print("SELECTION OF CLUBS\n");
print("YARDAGE DESIRED SUGGESTED CLUBS\n");
print("200 TO 280 YARDS 1 TO 4\n");
print("100 TO 200 YARDS 19 TO 13\n");
print(" 0 TO 100 YARDS 29 TO 23\n");
a = 1;
}
print("WHAT CLUB DO YOU CHOOSE");
c = parseInt(await input());
print("\n");
if (c >= 1 && c <= 29 && (c < 5 || c >= 12)) {
if (c > 4)
c -= 6;
if (la[0] <= 5 || c == 14 || c == 23) {
s1++;
w = 1;
if (c <= 13) {
if (f % 3 == 0 && s2 + q + (10 * (f - 1) / 18) < (f - 1) * (72 + ((h + 1) / 0.85)) / 18) {
q++;
if (s1 % 2 != 0 && d >= 95) {
print("BALL HIT TREE - BOUNCED INTO ROUGH " + (d - 75) + " YARDS FROM HOLE.\n");
d -= 75;
continue;
}
print("YOU DUBBED IT.\n");
d1 = 35;
second_routine = 1;
break;
} else if (c < 4 && la[0] == 2) {
print("YOU DUBBED IT.\n");
d1 = 35;
second_routine = 1;
break;
} else {
second_routine = 0;
break;
}
} else {
print("NOW GAUGE YOUR DISTANCE BY A PERCENTAGE (1 TO 100)\n");
print("OF A FULL SWING");
w = parseInt(await input());
w /= 100;
print("\n");
if (w <= 1) {
if (la[0] == 5) {
if (t == 3) {
if (Math.random() <= n) {
n *= 0.2;
print("SHOT DUBBED, STILL IN TRAP.\n");
continue;
}
n = 0.8;
}
d2 = 1 + (3 * Math.floor((80 / (40 - h)) * Math.random()));
second_routine = 2;
break;
}
if (c != 14)
c -= 10;
second_routine = 0;
break;
}
s1--;
// Fall through to THAT CLUB IS NOT IN THE BAG.
}
}
}
print("THAT CLUB IS NOT IN THE BAG.\n");
print("\n");
}
if (second_routine == 0) {
if (s1 > 7 && d < 200) {
d2 = 1 + (3 * Math.floor((80 / (40 - h)) * Math.random()));
second_routine = 2;
} else {
d1 = Math.floor(((30 - h) * 2.5 + 187 - ((30 - h) * 0.25 + 15) * c / 2) + 25 * Math.random());
d1 = Math.floor(d1 * w);
if (t == 2)
d1 = Math.floor(d1 * 0.85);
}
}
if (second_routine <= 1) {
o = (Math.random() / 0.8) * (2 * h + 16) * Math.abs(Math.tan(d1 * 0.0035));
d2 = Math.floor(Math.sqrt(Math.pow(o, 2) + Math.pow(Math.abs(d - d1), 2)));
if (d - d1 < 0) {
if (d2 >= 20)
print("TOO MUCH CLUB, YOU'RE PAST THE HOLE.\n");
}
b = d;
d = d2;
if (d2 > 27) {
if (o < 30 || j > 0) {
la[0] = 1;
} else {
if (t <= 0) {
s9 = (s2 + 1) / 15;
if (Math.floor(s9) == s9) {
print("YOU SLICED- ");
la[0] = la[1];
} else {
print("YOU HOOKED- ");
la[0] = la[2];
}
} else {
s9 = (s2 + 1) / 15;
if (Math.floor(s9) == s9) {
print("YOU HOOKED- ");
la[0] = la[2];
} else {
print("YOU SLICED- ");
la[0] = la[1];
}
}
if (o > 45)
print("BADLY.\n");
}
first_routine = false;
} else if (d2 > 20) {
la[0] = 5;
first_routine = false;
} else if (d2 > 0.5) {
la[0] = 8;
d2 = Math.floor(d2 * 3);
second_routine = 2;
} else {
la[0] = 9;
print("YOU HOLED IT.\n");
print("\n");
f++;
first_routine = true;
}
}
if (second_routine == 2) {
while (1) {
print("ON GREEN, " + d2 + " FEET FROM THE PIN.\n");
print("CHOOSE YOUR PUTT POTENCY (1 TO 13):");
i = parseInt(await input());
s1++;
if (s1 + 1 - p <= (h * 0.072) + 2 && k <= 2) {
k++;
if (t == 4)
d2 -= i * (4 + 1 * Math.random()) + 1;
else
d2 -= i * (4 + 2 * Math.random()) + 1.5;
if (d2 < -2) {
print("PASSED BY CUP.\n");
d2 = Math.floor(-d2);
continue;
}
if (d2 > 2) {
print("PUTT SHORT.\n");
d2 = Math.floor(d2);
continue;
}
}
print("YOU HOLED IT.\n");
print("\n");
f++;
break;
}
first_routine = true;
}
}
}
main();

View File

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

View File

@@ -0,0 +1,209 @@
// LIFE FOR TWO
//
// 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 na = [];
var ka = [, 3,102,103,120,130,121,112,111,12,
21,30,1020,1030,1011,1021,1003,1002,1012];
var aa = [,-1,0,1,0,0,-1,0,1,-1,-1,1,-1,-1,1,1,1];
var xa = [];
var ya = [];
var j;
var k;
var m2;
var m3;
function show_data()
{
k = 0;
m2 = 0;
m3 = 0;
for (j = 0; j <= 6; j++) {
print("\n");
for (k = 0; k <= 6; k++) {
if (j == 0 || j == 6) {
if (k == 6)
print(" 0 ");
else
print(" " + k + " ");
} else if (k == 0 || k == 6) {
if (j == 6)
print(" 0\n");
else
print(" " + j + " ");
} else {
if (na[j][k] >= 3) {
for (o1 = 1; o1 <= 18; o1++) {
if (na[j][k] == ka[o1])
break;
}
if (o1 <= 18) {
if (o1 <= 9) {
na[j][k] = 100;
m2++;
print(" * ");
} else {
na[j][k] = 1000;
m3++;
print(" # ");
}
} else {
na[j][k] = 0;
print(" ");
}
} else {
na[j][k] = 0;
print(" ");
}
}
}
}
}
function process_board()
{
for (j = 1; j <= 5; j++) {
for (k = 1; k <= 5; k++) {
if (na[j][k] > 99) {
b = 1;
if (na[j][k] > 999)
b = 10;
for (o1 = 1; o1 <= 15; o1 += 2) {
na[j + aa[o1]][k + aa[o1 + 1]] = na[j + aa[o1]][k + aa[o1 + 1]] + b;
}
}
}
}
show_data();
}
// Main program
async function main()
{
print(tab(33) + "LIFE2\n");
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
print("\n");
print("\n");
print("\n");
print(tab(10) + "U.B. LIFE GAME\n");
m2 = 0;
m3 = 0;
for (j = 0; j <= 6; j++) {
na[j] = [];
for (k = 0; k <= 6; k++)
na[j][k] = 0;
}
for (b = 1; b <= 2; b++) {
p1 = (b == 2) ? 30 : 3;
print("\n");
print("PLAYER " + b + " - 3 LIVE PIECES.\n");
for (k1 = 1; k1 <= 3; k1++) {
while (1) {
print("X,Y\n");
str = await input();
ya[b] = parseInt(str);
xa[b] = parseInt(str.substr(str.indexOf(",") + 1));
if (xa[b] > 0 && xa[b] < 6 && ya[b] > 0 && ya[b] < 5 && na[xa[b]][ya[b]] == 0)
break;
print("ILLEGAL COORDS. RETYPE\n");
}
if (b != 1) {
if (xa[1] == xa[2] && ya[1] == ya[2]) {
print("SAME COORD. SET TO 0\n");
na[xa[b] + 1][ya[b] + 1] = 0;
b = 99;
}
}
na[xa[b]][ya[b]] = p1;
}
}
show_data();
while (1) {
print("\n");
process_board();
if (m2 == 0 && m3 == 0) {
print("\n");
print("A DRAW\n");
break;
}
if (m3 == 0) {
print("\n");
print("PLAYER 1 IS THE WINNER\n");
break;
}
if (m2 == 0) {
print("\n");
print("PLAYER 2 IS THE WINNER\n");
break;
}
for (b = 1; b <= 2; b++) {
print("\n");
print("\n");
print("PLAYER " + b + " ");
while (1) {
print("X,Y\n");
str = await input();
ya[b] = parseInt(str);
xa[b] = parseInt(str.substr(str.indexOf(",") + 1));
if (xa[b] > 0 && xa[b] < 6 && ya[b] > 0 && ya[b] < 5 && na[xa[b]][ya[b]] == 0)
break;
print("ILLEGAL COORDS. RETYPE\n");
}
if (b != 1) {
if (xa[1] == xa[2] && ya[1] == ya[2]) {
print("SAME COORD. SET TO 0\n");
na[xa[b] + 1][ya[b] + 1] = 0;
b = 99;
}
}
if (b == 99)
break;
}
if (b <= 2) {
na[x[1]][y[1]] = 100;
na[x[2]][y[2]] = 1000;
}
}
}
main();

View File

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

View File

@@ -0,0 +1,361 @@
// MASTERMIND
//
// 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 p9;
var c9;
var b;
var w;
var f;
var m;
var qa;
var sa;
var ss;
var as;
var gs;
var hs;
function initialize_qa()
{
for (s = 1; s <= p9; s++)
qa[s] = 0;
}
function increment_qa()
{
if (qa[1] <= 0) {
// If zero, this is our firt increment: make all ones
for (s = 1; s <= p9; s++)
qa[s] = 1;
} else {
q = 1;
while (1) {
qa[q] = qa[q] + 1;
if (qa[q] <= c9)
return;
qa[q] = 1;
q++;
}
}
}
function convert_qa()
{
for (s = 1; s <= p9; s++) {
as[s] = ls.substr(qa[s] - 1, 1);
}
}
function get_number()
{
b = 0;
w = 0;
f = 0;
for (s = 1; s <= p9; s++) {
if (gs[s] == as[s]) {
b++;
gs[s] = String.fromCharCode(f);
as[s] = String.fromCharCode(f + 1);
f += 2;
} else {
for (t = 1; t <= p9; t++) {
if (gs[s] == as[t] && gs[t] != as[t]) {
w++;
as[t] = String.fromCharCode(f);
gs[s] = String.fromCharCode(f + 1);
f += 2;
break;
}
}
}
}
}
function convert_qa_hs()
{
for (s = 1; s <= p9; s++) {
hs[s] = ls.substr(qa[s] - 1, 1);
}
}
function copy_hs()
{
for (s = 1; s <= p9; s++) {
gs[s] = hs[s];
}
}
function board_printout()
{
print("\n");
print("BOARD\n");
print("MOVE GUESS BLACK WHITE\n");
for (z = 1; z <= m - 1; z++) {
str = " " + z + " ";
while (str.length < 9)
str += " ";
str += ss[z];
while (str.length < 25)
str += " ";
str += sa[z][1];
while (str.length < 35)
str += " ";
str += sa[z][2];
print(str + "\n");
}
print("\n");
}
function quit()
{
print("QUITTER! MY COMBINATION WAS: ");
convert_qa();
for (x = 1; x <= p9; x++) {
print(as[x]);
}
print("\n");
print("GOOD BYE\n");
}
function show_score()
{
print("SCORE:\n");
show_points();
}
function show_points()
{
print(" COMPUTER " + c + "\n");
print(" HUMAN " + h + "\n");
print("\n");
}
var color = ["BLACK", "WHITE", "RED", "GREEN",
"ORANGE", "YELLOW", "PURPLE", "TAN"];
// Main program
async function main()
{
print(tab(30) + "MASTERMIND\n");
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
print("\n");
print("\n");
print("\n");
//
// MASTERMIND II
// STEVE NORTH
// CREATIVE COMPUTING
// PO BOX 789-M MORRISTOWN NEW JERSEY 07960
//
//
while (1) {
print("NUMBER OF COLORS");
c9 = parseInt(await input());
if (c9 <= 8)
break;
print("NO MORE THAN 8, PLEASE!\n");
}
print("NUMBER OF POSITIONS");
p9 = parseInt(await input());
print("NUMBER OF ROUNDS");
r9 = parseInt(await input());
p = Math.pow(c9, p9);
print("TOTAL POSSIBILITIES = " + p + "\n");
h = 0;
c = 0;
qa = [];
sa = [];
ss = [];
as = [];
gs = [];
ia = [];
hs = [];
ls = "BWRGOYPT";
print("\n");
print("\n");
print("COLOR LETTER\n");
print("===== ======\n");
for (x = 1; x <= c9; x++) {
str = color[x - 1];
while (str.length < 13)
str += " ";
str += ls.substr(x - 1, 1);
print(str + "\n");
}
print("\n");
for (r = 1; r <= r9; r++) {
print("\n");
print("ROUND NUMBER " + r + " ----\n");
print("\n");
print("GUESS MY COMBINATION.\n");
print("\n");
// Get a combination
a = Math.floor(p * Math.random() + 1);
initialize_qa();
for (x = 1; x <= a; x++) {
increment_qa();
}
for (m = 1; m <= 10; m++) {
while (1) {
print("MOVE # " + m + " GUESS ");
str = await input();
if (str == "BOARD") {
board_printout();
} else if (str == "QUIT") {
quit();
return;
} else if (str.length != p9) {
print("BAD NUMBER OF POSITIONS.\n");
} else {
// Unpack str into gs(1-p9)
for (x = 1; x <= p9; x++) {
y = ls.indexOf(str.substr(x - 1, 1));
if (y < 0) {
print("'" + str.substr(x - 1, 1) + "' IS UNRECOGNIZED.\n");
break;
}
gs[x] = str.substr(x - 1, 1);
}
if (x > p9)
break;
}
}
// Now we convert qa(1-p9) into as(1-p9) [ACTUAL GUESS]
convert_qa();
// And get number of blacks and white
get_number();
if (b == p9) {
print("YOU GUESSED IT IN " + m + " MOVES!\n");
break;
}
// Save all this stuff for board printout later
ss[m] = str;
sa[m] = [];
sa[m][1] = b;
sa[m][2] = w;
}
if (m > 10) {
print("YOU RAN OUT OF MOVES! THAT'S ALL YOU GET!\n");
}
h += m;
show_score();
//
// Now computer guesses
//
for (x = 1; x <= p; x++)
ia[x] = 1;
print("NOW I GUESS. THINK OF A COMBINATION.\n");
print("HIT RETURN WHEN READY:");
str = await input();
for (m = 1; m <= 10; m++) {
initialize_qa();
// Find a guess
g = Math.floor(p * Math.random() + 1);
if (ia[g] != 1) {
for (x = g; x <= p; x++) {
if (ia[x] == 1)
break;
}
if (x > p) {
for (x = 1; x <= g; x++) {
if (ia[x] == 1)
break;
}
if (x > g) {
print("YOU HAVE GIVEN ME INCONSISTENT INFORMATION.\n");
print("TRY AGAIN, AND THIS TIME PLEASE BE MORE CAREFUL.\n");
for (x = 1; x <= p; x++)
ia[x] = 1;
print("NOW I GUESS. THINK OF A COMBINATION.\n");
print("HIT RETURN WHEN READY:");
str = await input();
m = 0;
continue;
}
}
g = x;
}
// Now we convert guess #g into gs
for (x = 1; x <= g; x++) {
increment_qa();
}
convert_qa_hs();
print("MY GUESS IS: ");
for (x = 1; x <= p9; x++) {
print(hs[x]);
}
print(" BLACKS, WHITES ");
str = await input();
b1 = parseInt(str);
w1 = parseInt(str.substr(str.indexOf(",") + 1));
if (b1 == p9) {
print("I GOT IT IN " + m + " MOVES!\n");
break;
}
initialize_qa();
for (x = 1; x <= p; x++) {
increment_qa();
if (ia[x] != 0) {
copy_hs();
convert_qa();
get_number();
if (b1 != b || w1 != w)
ia[x] = 0;
}
}
}
if (m > 10) {
print("I USED UP ALL MY MOVES!\n");
print("I GUESS MY CPU I JUST HAVING AN OFF DAY.\n");
}
c += m;
show_score();
}
print("GAME OVER\n");
print("FINAL SCORE:\n");
show_points();
}
main();