Merge pull request #10 from nanochess/main

Ported ANIMAL to Javascript
This commit is contained in:
Jeff Atwood
2021-02-16 19:28:24 -08:00
committed by GitHub
7 changed files with 645 additions and 1 deletions

View File

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

View File

@@ -0,0 +1,135 @@
// ACEY DUCEY
//
// 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(26) + "ACEY DUCEY CARD GAME\n");
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
print("\n");
print("\n");
print("ACEY-DUCEY IS PLAYED IN THE FOLLOWING MANNER\n");
print("THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP\n");
print("YOU HAVE AN OPTION TO BET OR NOT BET DEPENDING\n");
print("ON WHETHER OR NOT YOU FEEL THE CARD WILL HAVE\n");
print("A VALUE BETWEEN THE FIRST TWO.\n");
print("IF YOU DO NOT WANT TO BET, INPUT A 0\n");
function show_card(card)
{
if (card < 11)
print(card + "\n");
else if (card == 11)
print("JACK\n");
else if (card == 12)
print("QUEEN\n");
else if (card == 13)
print("KING\n");
else
print("ACE\n");
}
// Main program
async function main()
{
q = 100;
while (1) {
print("YOU NOW HAVE " + q + " DOLLARS.\n");
print("\n");
do {
print("HERE ARE YOUR NEXT TWO CARDS: \n");
do {
a = Math.floor(Math.random() * 13 + 2);
b = Math.floor(Math.random() * 13 + 2);
} while (a >= b) ;
show_card(a);
show_card(b);
print("\n");
while (1) {
print("\n");
print("WHAT IS YOUR BET");
m = parseInt(await input());
if (m > 0) {
if (m > q) {
print("SORRY, MY FRIEND, BUT YOU BET TOO MUCH.\n");
print("YOU HAVE ONLY " + q + "DOLLARS TO BET.\n");
continue;
}
break;
}
m = 0;
print("CHICKEN!!\n");
print("\n");
break;
}
} while (m == 0) ;
c = Math.floor(Math.random() * 13 + 2);
show_card(c);
if (c > a && c < b) {
print("YOU WIN!!!\n");
q = q + m;
} else {
print("SORRY, YOU LOSE\n");
if (m >= q) {
print("\n");
print("\n");
print("SORRY, FRIEND, BUT YOU BLEW YOUR WAD.\n");
print("\n");
print("\n");
print("TRY AGAIN (YES OR NO)");
a = await input();
print("\n");
print("\n");
if (a == "YES") {
q = 100;
} else {
print("O.K., HOPE YOU HAD FUN!");
break;
}
} else {
q = q - m;
}
}
}
}
main();

View File

@@ -80,7 +80,7 @@
680 IF W(R+1,S)<>0 THEN 740
685 IF S<>V THEN 700
690 IF Z=1 THEN 730
695 Q=1:GOTO 830
695 Q=1:GOTO 710
700 IF W(R,S+1)<>0 THEN 730
710 X=INT(RND(1)*2+1)
720 ON X GOTO 860,910

View File

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

View File

@@ -0,0 +1,319 @@
// AMAZING
//
// 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(28) + "AMAZING PROGRAM\n");
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
print("\n");
print("\n");
print("\n");
print("FOR EXAMPLE TYPE 10,10 AND PRESS ENTER\n");
print("\n");
// Main program
async function main()
{
while (1) {
print("WHAT ARE YOUR WIDTH AND LENGTH");
a = await input();
h = parseInt(a);
v2 = parseInt(a.substr(a.indexOf(",") + 1));
if (h > 1 && v2 > 1)
break;
print("MEANINGLESS DIMENSIONS. TRY AGAIN.\n");
}
w = [];
v = [];
for (i = 1; i <= h; i++) {
w[i] = [];
v[i] = [];
for (j = 1; j <= v2; j++) {
w[i][j] = 0;
v[i][j] = 0;
}
}
print("\n");
print("\n");
print("\n");
print("\n");
q = 0;
z = 0;
x = Math.floor(Math.random() * h + 1);
for (i = 1; i <= h; i++) {
if (i == x)
print(". ");
else
print(".--");
}
print(".\n");
c = 1;
w[x][1] = c;
c++;
r = x;
s = 1;
entry = 0;
while (1) {
if (entry == 2) { // Search for a non-explored cell
do {
if (r < h) {
r++;
} else if (s < v2) {
r = 1;
s++;
} else {
r = 1;
s = 1;
}
} while (w[r][s] == 0) ;
}
if (entry == 0 && r - 1 > 0 && w[r - 1][s] == 0) { // Can go left?
if (s - 1 > 0 && w[r][s - 1] == 0) { // Can go up?
if (r < h && w[r + 1][s] == 0) { // Can go right?
// Choose left/up/right
x = Math.floor(Math.random() * 3 + 1);
} else if (s < v2) {
if (w[r][s + 1] == 0) { // Can go down?
// Choose left/up/down
x = Math.floor(Math.random() * 3 + 1);
if (x == 3)
x = 4;
} else {
x = Math.floor(Math.random() * 2 + 1);
}
} else if (z == 1) {
x = Math.floor(Math.random() * 2 + 1);
} else {
q = 1;
x = Math.floor(Math.random() * 3 + 1);
if (x == 3)
x = 4;
}
} else if (r < h && w[r + 1][s] == 0) { // Can go right?
if (s < v2) {
if (w[r][s + 1] == 0) { // Can go down?
// Choose left/right/down
x = Math.floor(Math.random() * 3 + 1);
} else {
x = Math.floor(Math.random() * 2 + 1);
}
if (x >= 2)
x++;
} else if (z == 1) {
x = Math.floor(Math.random() * 2 + 1);
if (x >= 2)
x++;
} else {
q = 1;
x = Math.floor(Math.random() * 3 + 1);
if (x >= 2)
x++;
}
} else if (s < v2) {
if (w[r][s + 1] == 0) { // Can go down?
// Choose left/down
x = Math.floor(Math.random() * 2 + 1);
if (x == 2)
x = 4;
} else {
x = 1;
}
} else if (z == 1) {
x = 1;
} else {
q = 1;
x = Math.floor(Math.random() * 2 + 1);
if (x == 2)
x = 4;
}
} else if (s - 1 > 0 && w[r][s - 1] == 0) { // Can go up?
if (r < h && w[r + 1][s] == 0) {
if (s < v2) {
if (w[r][s + 1] == 0)
x = Math.floor(Math.random() * 3 + 2);
else
x = Math.floor(Math.random() * 2 + 2);
} else if (z == 1) {
x = Math.floor(Math.random() * 2 + 2);
} else {
q = 1;
x = Math.floor(Math.random() * 3 + 2);
}
} else if (s < v2) {
if (w[r][s + 1] == 0) {
x = Math.floor(Math.random() * 2 + 2);
if (x == 3)
x = 4;
} else {
x = 2;
}
} else if (z == 1) {
x = 2;
} else {
q = 1;
x = Math.floor(Math.random() * 2 + 2);
if (x == 3)
x = 4;
}
} else if (r < h && w[r + 1][s] == 0) { // Can go right?
if (s < v2) {
if (w[r][s + 1] == 0)
x = Math.floor(Math.random() * 2 + 3);
else
x = 3;
} else if (z == 1) {
x = 3;
} else {
q = 1;
x = Math.floor(Math.random() * 2 + 3);
}
} else if (s < v2) {
if (w[r][s + 1] == 0) // Can go down?
x = 4;
else {
entry = 2; // Blocked!
continue;
}
} else if (z == 1) {
entry = 2; // Blocked!
continue;
} else {
q = 1;
x = 4;
}
if (x == 1) { // Left
w[r - 1][s] = c;
c++;
v[r - 1][s] = 2;
r--;
if (c == h * v2 + 1)
break;
q = 0;
entry = 0;
} else if (x == 2) { // Up
w[r][s - 1] = c;
c++;
v[r][s - 1] = 1;
s--;
if (c == h * v2 + 1)
break;
q = 0;
entry = 0;
} else if (x == 3) { // Right
w[r + 1][s] = c;
c++;
if (v[r][s] == 0)
v[r][s] = 2;
else
v[r][s] = 3;
r++;
if (c == h * v2 + 1)
break;
entry = 1;
} else if (x == 4) { // Down
if (q != 1) { // Only if not blocked
w[r][s + 1] = c;
c++;
if (v[r][s] == 0)
v[r][s] = 1;
else
v[r][s] = 3;
s++;
if (c == h * v2 + 1)
break;
entry = 0;
} else {
z = 1;
if (v[r][s] == 0) {
v[r][s] = 1;
q = 0;
r = 1;
s = 1;
while (w[r][s] == 0) {
if (r < h) {
r++;
} else if (s < v2) {
r = 1;
s++;
} else {
r = 1;
s = 1;
}
}
entry = 0;
} else {
v[r][s] = 3;
q = 0;
entry = 2;
}
}
}
}
for (j = 1; j <= v2; j++) {
str = "I";
for (i = 1; i <= h; i++) {
if (v[i][j] < 2)
str += " I";
else
str += " ";
}
print(str + "\n");
str = "";
for (i = 1; i <= h; i++) {
if (v[i][j] == 0 || v[i][j] == 2)
str += ":--";
else
str += ": ";
}
print(str + ".\n");
}
// If you want to see the order of visited cells
// for (j = 1; j <= v2; j++) {
// str = "I";
// for (i = 1; i <= h; i++) {
// str += w[i][j] + " ";
// }
// print(str + "\n");
// }
}
main();

View File

@@ -0,0 +1,10 @@
<html>
<head>
<title>ANIMAL</title>
</head>
<body>
<pre id="output" style="font-size: 12pt;"></pre>
<!--<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>-->
<script src="animal.js"></script>
</body>
</html>

View File

@@ -0,0 +1,162 @@
// ANIMAL
//
// 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(32) + "ANIMAL\n");
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
print("\n");
print("\n");
print("\n");
print("PLAY 'GUESS THE ANIMAL'\n");
print("\n");
print("THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT.\n");
print("\n");
var k;
var n;
var str;
var q;
var z;
var c;
var t;
var animals = [
"\\QDOES IT SWIM\\Y1\\N2\\",
"\\AFISH",
"\\ABIRD",
];
n = animals.length;
function show_animals() {
var x;
print("\n");
print("ANIMALS I ALREADY KNOW ARE:\n");
str = "";
x = 0;
for (var i = 0; i < n; i++) {
if (animals[i].substr(0, 2) == "\\A") {
while (str.length < 15 * x)
str += " ";
for (var z = 2; z < animals[i].length; z++) {
if (animals[i][z] == "\\")
break;
str += animals[i][z];
}
x++;
if (x == 4) {
x = 0;
print(str + "\n");
str = "";
}
}
}
if (str != "")
print(str + "\n");
}
// Main control section
async function main()
{
while (1) {
while (1) {
print("ARE YOU THINKING OF AN ANIMAL");
str = await input();
if (str == "LIST")
show_animals();
if (str[0] == "Y")
break;
}
k = 0;
do {
// Subroutine to print questions
q = animals[k];
while (1) {
str = "";
for (z = 2; z < q.length; z++) {
if (q[z] == "\\")
break;
str += q[z];
}
print(str);
c = await input();
if (c[0] == "Y" || c[0] == "N")
break;
}
t = "\\" + c[0];
x = q.indexOf(t);
k = parseInt(q.substr(x + 2));
} while (animals[k].substr(0,2) == "\\Q") ;
print("IS IT A " + animals[k].substr(2));
a = await input();
if (a[0] == "Y") {
print("WHY NOT TRY ANOTHER ANIMAL?\n");
continue;
}
print("THE ANIMAL YOU WERE THINKING OF WAS A ");
v = await input();
print("PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A\n");
print(v + " FROM A " + animals[k].substr(2) + "\n");
x = await input();
while (1) {
print("FOR A " + v + " THE ANSWER WOULD BE ");
a = await input();
a = a.substr(0, 1);
if (a == "Y" || a == "N")
break;
}
if (a == "Y")
b = "N";
if (a == "N")
b = "Y";
z1 = animals.length;
animals[z1] = animals[k];
animals[z1 + 1] = "\\A" + v;
animals[k] = "\\Q" + x + "\\" + a + (z1 + 1) + "\\" + b + z1 + "\\";
}
}
main();