mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-02 00:00:14 -08:00
Merge pull request #199 from nanochess/main
Ported FUR TRADER and GOMOKO to Javascript
This commit is contained in:
9
38 Fur Trader/javascript/furtrader.html
Normal file
9
38 Fur Trader/javascript/furtrader.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>FUR TRADER</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="furtrader.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
227
38 Fur Trader/javascript/furtrader.js
Normal file
227
38 Fur Trader/javascript/furtrader.js
Normal file
@@ -0,0 +1,227 @@
|
||||
// FUR TRADER
|
||||
//
|
||||
// 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 f = [];
|
||||
var bs = [, "MINK", "BEAVER", "ERMINE", "FOX"];
|
||||
|
||||
function reset_stats()
|
||||
{
|
||||
for (var j = 1; j <= 4; j++)
|
||||
f[j] = 0;
|
||||
}
|
||||
|
||||
// Main program
|
||||
async function main()
|
||||
{
|
||||
print(tab(31) + "FUR TRADER\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
first_time = true;
|
||||
while (1) {
|
||||
if (first_time) {
|
||||
print("YOU ARE THE LEADER OF A FRENCH FUR TRADING EXPEDITION IN \n");
|
||||
print("1776 LEAVING THE LAKE ONTARIO AREA TO SELL FURS AND GET\n");
|
||||
print("SUPPLIES FOR THE NEXT YEAR. YOU HAVE A CHOICE OF THREE\n");
|
||||
print("FORTS AT WHICH YOU MAY TRADE. THE COST OF SUPPLIES\n");
|
||||
print("AND THE AMOUNT YOU RECEIVE FOR YOUR FURS WILL DEPEND\n");
|
||||
print("ON THE FORT THAT YOU CHOOSE.\n");
|
||||
i = 600;
|
||||
print("DO YOU WISH TO TRADE FURS?\n");
|
||||
first_time = false;
|
||||
}
|
||||
print("ANSWER YES OR NO\t");
|
||||
str = await input();
|
||||
if (str == "NO")
|
||||
break;
|
||||
print("\n");
|
||||
print("YOU HAVE $" + i + " SAVINGS.\n");
|
||||
print("AND 190 FURS TO BEGIN THE EXPEDITION.\n");
|
||||
e1 = Math.floor((0.15 * Math.random() + 0.95) * Math.pow(10, 2) + 0.5) / Math.pow(10, 2);
|
||||
b1 = Math.floor((0.25 * Math.random() + 1.00) * Math.pow(10, 2) + 0.5) / Math.pow(10, 2);
|
||||
print("\n");
|
||||
print("YOUR 190 FURS ARE DISTRIBUTED AMONG THE FOLLOWING\n");
|
||||
print("KINDS OF PELTS: MINK, BEAVER, ERMINE AND FOX.\n");
|
||||
reset_stats();
|
||||
for (j = 1; j <= 4; j++) {
|
||||
print("\n");
|
||||
print("HOW MANY " + bs[j] + " PELTS DO YOU HAVE\n");
|
||||
f[j] = parseInt(await input());
|
||||
f[0] = f[1] + f[2] + f[3] + f[4];
|
||||
if (f[0] == 190)
|
||||
break;
|
||||
if (f[0] > 190) {
|
||||
print("\n");
|
||||
print("YOU MAY NOT HAVE THAT MANY FURS.\n");
|
||||
print("DO NOT TRY TO CHEAT. I CAN ADD.\n");
|
||||
print("YOU MUST START AGAIN.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (f[0] > 190) {
|
||||
first_time = true;
|
||||
continue;
|
||||
}
|
||||
print("YOU MAY TRADE YOUR FURS AT FORT 1, FORT 2,\n");
|
||||
print("OR FORT 3. FORT 1 IS FORT HOCHELAGA (MONTREAL)\n");
|
||||
print("AND IS UNDER THE PROTECTION OF THE FRENCH ARMY.\n");
|
||||
print("FORT 2 IS FORT STADACONA (QUEBEC) AND IS UNDER THE\n");
|
||||
print("PROTECTION OF THE FRENCH ARMY. HOWEVER, YOU MUST\n");
|
||||
print("MAKE A PORTAGE AND CROSS THE LACHINE RAPIDS.\n");
|
||||
print("FORT 3 IS FORT NEW YORK AND IS UNDER DUTCH CONTROL.\n");
|
||||
print("YOU MUST CROSS THROUGH IROQUOIS LAND.\n");
|
||||
do {
|
||||
print("ANSWER 1, 2, OR 3.\n");
|
||||
b = parseInt(await input());
|
||||
if (b == 1) {
|
||||
print("YOU HAVE CHOSEN THE EASIEST ROUTE. HOWEVER, THE FORT\n");
|
||||
print("IS FAR FROM ANY SEAPORT. THE VALUE\n");
|
||||
print("YOU RECEIVE FOR YOUR FURS WILL BE LOW AND THE COST\n");
|
||||
print("OF SUPPLIES HIGHER THAN AT FORTS STADACONA OR NEW YORK.\n");
|
||||
} else if (b == 2) {
|
||||
print("YOU HAVE CHOSEN A HARD ROUTE. IT IS, IN COMPARSION,\n");
|
||||
print("HARDER THAN THE ROUTE TO HOCHELAGA BUT EASIER THAN\n");
|
||||
print("THE ROUTE TO NEW YORK. YOU WILL RECEIVE AN AVERAGE VALUE\n");
|
||||
print("FOR YOUR FURS AND THE COST OF YOUR SUPPLIES WILL BE AVERAGE.\n");
|
||||
} else {
|
||||
print("YOU HAVE CHOSEN THE MOST DIFFICULT ROUTE. AT\n");
|
||||
print("FORT NEW YORK YOU WILL RECEIVE THE HIGHEST VALUE\n");
|
||||
print("FOR YOUR FURS. THE COST OF YOUR SUPPLIES\n");
|
||||
print("WILL BE LOWER THAN AT ALL THE OTHER FORTS.\n");
|
||||
}
|
||||
if (b >= 1 && b <= 3) {
|
||||
print("DO YOU WANT TO TRADE AT ANOTHER FORT?\n");
|
||||
print("ANSWER YES OR NO\t");
|
||||
str = await input();
|
||||
if (str == "YES") {
|
||||
b = 0;
|
||||
}
|
||||
}
|
||||
} while (b < 1 || b > 3) ;
|
||||
show_beaver = true;
|
||||
show_all = true;
|
||||
if (b == 1) {
|
||||
i -= 160;
|
||||
print("\n");
|
||||
m1 = Math.floor((0.2 * Math.random() + 0.7) * Math.pow(10, 2) + 0.5) / Math.pow(10, 2);
|
||||
e1 = Math.floor((0.2 * Math.random() + 0.65) * Math.pow(10, 2) + 0.5) / Math.pow(10, 2);
|
||||
b1 = Math.floor((0.2 * Math.random() + 0.75) * Math.pow(10, 2) + 0.5) / Math.pow(10, 2);
|
||||
d1 = Math.floor((0.2 * Math.random() + 0.8) * Math.pow(10, 2) + 0.5) / Math.pow(10, 2);
|
||||
print("SUPPLIES AT FORT HOCHELAGA COST $150.00.\n");
|
||||
print("YOUR TRAVEL EXPENSES TO HOCHELAGA WERE $10.00.\n");
|
||||
} else if (b == 2) {
|
||||
i -= 140;
|
||||
print("\n");
|
||||
m1 = Math.floor((0.3 * Math.random() + 0.85) * Math.pow(10, 2) + 0.5) / Math.pow(10, 2);
|
||||
e1 = Math.floor((0.15 * Math.random() + 0.8) * Math.pow(10, 2) + 0.5) / Math.pow(10, 2);
|
||||
b1 = Math.floor((0.2 * Math.random() + 0.9) * Math.pow(10, 2) + 0.5) / Math.pow(10, 2);
|
||||
p = Math.floor(10 * Math.random()) + 1;
|
||||
if (p <= 2) {
|
||||
f[2] = 0;
|
||||
print("YOUR BEAVER WERE TOO HEAVY TO CARRY ACROSS\n");
|
||||
print("THE PORTAGE. YOU HAD TO LEAVE THE PELTS, BUT FOUND\n");
|
||||
print("THEM STOLEN WHEN YOU RETURNED.\n");
|
||||
show_beaver = false;
|
||||
} else if (p <= 6) {
|
||||
print("YOU ARRIVED SAFELY AT FORT STADACONA.\n");
|
||||
} else if (p <= 8) {
|
||||
reset_stats();
|
||||
print("YOUR CANOE UPSET IN THE LACHINE RAPIDS. YOU\n");
|
||||
print("LOST ALL YOUR FURS.\n");
|
||||
show_all = false;
|
||||
} else if (p <= 10) {
|
||||
f[4] = 0;
|
||||
print("YOUR FOX PELTS WERE NOT CURED PROPERLY.\n");
|
||||
print("NO ONE WILL BUY THEM.\n");
|
||||
}
|
||||
print("SUPPLIES AT FORT STADACONA COST $125.00.\n");
|
||||
print("YOUR TRAVEL EXPENSES TO STADACONA WERE $15.00.\n");
|
||||
|
||||
d1 = Math.floor((0.2 * Math.random() + 0.8) * Math.pow(10, 2) + 0.5) / Math.pow(10, 2);
|
||||
} else if (b == 3) {
|
||||
i -= 105;
|
||||
print("\n");
|
||||
m1 = Math.floor((0.15 * Math.random() + 1.05) * Math.pow(10, 2) + 0.5) / Math.pow(10, 2);
|
||||
d1 = Math.floor((0.25 * Math.random() + 1.1) * Math.pow(10, 2) + 0.5) / Math.pow(10, 2);
|
||||
p = Math.floor(10 * Math.random()) + 1;
|
||||
if (p <= 2) {
|
||||
print("YOU WERE ATTACKED BY A PARTY OF IROQUOIS.\n");
|
||||
print("ALL PEOPLE IN YOUR TRADING GROUP WERE\n");
|
||||
print("KILLED. THIS ENDS THE GAME.\n");
|
||||
break;
|
||||
} else if (p <= 6) {
|
||||
print("YOU WERE LUCKY. YOU ARRIVED SAFELY\n");
|
||||
print("AT FORT NEW YORK.\n");
|
||||
} else if (p <= 8) {
|
||||
reset_stats();
|
||||
print("YOU NARROWLY ESCAPED AN IROQUOIS RAIDING PARTY.\n");
|
||||
print("HOWEVER, YOU HAD TO LEAVE ALL YOUR FURS BEHIND.\n");
|
||||
show_all = false;
|
||||
} else if (p <= 10) {
|
||||
b1 /= 2;
|
||||
m1 /= 2;
|
||||
print("YOUR MINK AND BEAVER WERE DAMAGED ON YOUR TRIP.\n");
|
||||
print("YOU RECEIVE ONLY HALF THE CURRENT PRICE FOR THESE FURS.\n");
|
||||
}
|
||||
print("SUPPLIES AT NEW YORK COST $80.00.\n");
|
||||
print("YOUR TRAVEL EXPENSES TO NEW YORK WERE $25.00.\n");
|
||||
}
|
||||
print("\n");
|
||||
if (show_all) {
|
||||
if (show_beaver)
|
||||
print("YOUR BEAVER SOLD FOR $" + b1 * f[2] + " ");
|
||||
print("YOUR FOX SOLD FOR $" + d1 * f[4] + "\n");
|
||||
print("YOUR ERMINE SOLD FOR $" + e1 * f[3] + " ");
|
||||
print("YOUR MINK SOLD FOR $" + m1 * f[1] + "\n");
|
||||
}
|
||||
i += m1 * f[1] + b1 * f[2] + e1 * f[3] + d1 * f[4];
|
||||
print("\n");
|
||||
print("YOU NOW HAVE $" + i + " INCLUDING YOUR PREVIOUS SAVINGS\n");
|
||||
print("\n");
|
||||
print("DO YOU WANT TO TRADE FURS NEXT YEAR?\n");
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
9
40 Gomoko/javascript/gomoko.html
Normal file
9
40 Gomoko/javascript/gomoko.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>GOMOKO</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="gomoko.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
174
40 Gomoko/javascript/gomoko.js
Normal file
174
40 Gomoko/javascript/gomoko.js
Normal file
@@ -0,0 +1,174 @@
|
||||
// GOMOKO
|
||||
//
|
||||
// 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;
|
||||
}
|
||||
|
||||
function reset_stats()
|
||||
{
|
||||
for (var j = 1; j <= 4; j++)
|
||||
f[j] = 0;
|
||||
}
|
||||
|
||||
var a = [];
|
||||
var x;
|
||||
var y;
|
||||
var n;
|
||||
|
||||
// *** PRINT THE BOARD ***
|
||||
function print_board()
|
||||
{
|
||||
for (i = 1; i <= n; i++) {
|
||||
for (j = 1; j <= n; j++) {
|
||||
print(" " + a[i][j] + " ");
|
||||
}
|
||||
print("\n");
|
||||
}
|
||||
print("\n");
|
||||
}
|
||||
|
||||
// Is valid the movement
|
||||
function is_valid()
|
||||
{
|
||||
if (x < 1 || x > n || y < 1 || y > n)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Main program
|
||||
async function main()
|
||||
{
|
||||
print(tab(33) + "GOMOKO\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
for (i = 0; i <= 19; i++) {
|
||||
a[i] = [];
|
||||
for (j = 0; j <= 19; j++)
|
||||
a[i][j] = 0;
|
||||
}
|
||||
print("WELCOME TO THE ORIENTAL GAME OF GOMOKO.\n");
|
||||
print("\n");
|
||||
print("THE GAME IS PLAYED ON AN N BY N GRID OF A SIZE\n");
|
||||
print("THAT YOU SPECIFY. DURING YOUR PLAY, YOU MAY COVER ONE GRID\n");
|
||||
print("INTERSECTION WITH A MARKER. THE OBJECT OF THE GAME IS TO GET\n");
|
||||
print("5 ADJACENT MARKERS IN A ROW -- HORIZONTALLY, VERTICALLY, OR\n");
|
||||
print("DIAGONALLY. ON THE BOARD DIAGRAM, YOUR MOVES ARE MARKED\n");
|
||||
print("WITH A '1' AND THE COMPUTER MOVES WITH A '2'.\n");
|
||||
print("\n");
|
||||
print("THE COMPUTER DOES NOT KEEP TRACK OF WHO HAS WON.\n");
|
||||
print("TO END THE GAME, TYPE -1,-1 FOR YOUR MOVE.\n");
|
||||
print("\n");
|
||||
while (1) {
|
||||
print("WHAT IS YOUR BOARD SIZE (MIN 7/ MAX 19)");
|
||||
while (1) {
|
||||
n = parseInt(await input());
|
||||
if (n >= 7 && n<= 19)
|
||||
break;
|
||||
print("I SAID, THE MINIMUM IS 7, THE MAXIMUM IS 19.\n");
|
||||
}
|
||||
for (i = 1; i <= n; i++) {
|
||||
for (j = 1; j <= n; j++) {
|
||||
a[i][j] = 0;
|
||||
}
|
||||
}
|
||||
print("\n");
|
||||
print("WE ALTERNATE MOVES. YOU GO FIRST...\n");
|
||||
print("\n");
|
||||
while (1) {
|
||||
print("YOUR PLAY (I,J)");
|
||||
str = await input();
|
||||
i = parseInt(str);
|
||||
j = parseInt(str.substr(str.indexOf(",") + 1));
|
||||
print("\n");
|
||||
if (i == -1)
|
||||
break;
|
||||
x = i;
|
||||
y = j;
|
||||
if (!is_valid()) {
|
||||
print("ILLEGAL MOVE. TRY AGAIN...\n");
|
||||
continue;
|
||||
}
|
||||
if (a[i][j] != 0) {
|
||||
print("SQUARE OCCUPIED. TRY AGAIN...\n");
|
||||
continue;
|
||||
}
|
||||
a[i][j] = 1;
|
||||
// *** Computer tries an intelligent move ***
|
||||
found = false;
|
||||
for (e = -1; e <= 1; e++) {
|
||||
for (f = -1; f <= 1; f++) {
|
||||
if (e + f - e * f == 0)
|
||||
continue;
|
||||
x = i + f;
|
||||
y = j + f;
|
||||
if (!is_valid())
|
||||
continue;
|
||||
if (a[x][y] == 1) {
|
||||
x = i - e;
|
||||
y = j - f;
|
||||
if (is_valid() || a[x][y] == 0)
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
// *** Computer tries a random move ***
|
||||
do {
|
||||
x = Math.floor(n * Math.random() + 1);
|
||||
y = Math.floor(n * Math.random() + 1);
|
||||
} while (!is_valid() || a[x][y] != 0) ;
|
||||
}
|
||||
a[x][y] = 2;
|
||||
print_board();
|
||||
}
|
||||
print("\n");
|
||||
print("THANKS FOR THE GAME!!\n");
|
||||
print("PLAY AGAIN (1 FOR YES, 0 FOR NO)");
|
||||
q = parseInt(await input());
|
||||
if (q != 1)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
9
46 Hexapawn/javascript/hexapawn.html
Normal file
9
46 Hexapawn/javascript/hexapawn.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>HEXAPAWN</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="hexapawn.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
337
46 Hexapawn/javascript/hexapawn.js
Normal file
337
46 Hexapawn/javascript/hexapawn.js
Normal file
@@ -0,0 +1,337 @@
|
||||
// HEXAPAWN
|
||||
//
|
||||
// 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 ba = [,
|
||||
[,-1,-1,-1,1,0,0,0,1,1],
|
||||
[,-1,-1,-1,0,1,0,1,0,1],
|
||||
[,-1,0,-1,-1,1,0,0,0,1],
|
||||
[,0,-1,-1,1,-1,0,0,0,1],
|
||||
[,-1,0,-1,1,1,0,0,1,0],
|
||||
[,-1,-1,0,1,0,1,0,0,1],
|
||||
[,0,-1,-1,0,-1,1,1,0,0],
|
||||
[,0,-1,-1,-1,1,1,1,0,0],
|
||||
[,-1,0,-1,-1,0,1,0,1,0],
|
||||
[,0,-1,-1,0,1,0,0,0,1],
|
||||
[,0,-1,-1,0,1,0,1,0,0],
|
||||
[,-1,0,-1,1,0,0,0,0,1],
|
||||
[,0,0,-1,-1,-1,1,0,0,0],
|
||||
[,-1,0,0,1,1,1,0,0,0],
|
||||
[,0,-1,0,-1,1,1,0,0,0],
|
||||
[,-1,0,0,-1,-1,1,0,0,0],
|
||||
[,0,0,-1,-1,1,0,0,0,0],
|
||||
[,0,-1,0,1,-1,0,0,0,0],
|
||||
[,-1,0,0,-1,1,0,0,0,0]];
|
||||
var ma = [,
|
||||
[,24,25,36,0],
|
||||
[,14,15,36,0],
|
||||
[,15,35,36,47],
|
||||
[,36,58,59,0],
|
||||
[,15,35,36,0],
|
||||
[,24,25,26,0],
|
||||
[,26,57,58,0],
|
||||
[,26,35,0,0],
|
||||
[,47,48,0,0],
|
||||
[,35,36,0,0],
|
||||
[,35,36,0,0],
|
||||
[,36,0,0,0],
|
||||
[,47,58,0,0],
|
||||
[,15,0,0,0],
|
||||
[,26,47,0,0],
|
||||
[,47,58,0,0],
|
||||
[,35,36,47,0],
|
||||
[,28,58,0,0],
|
||||
[,15,47,0,0]];
|
||||
var s = [];
|
||||
var t = [];
|
||||
var ps = "X.O";
|
||||
|
||||
function show_board()
|
||||
{
|
||||
print("\n");
|
||||
for (var i = 1; i <= 3; i++) {
|
||||
print(tab(10));
|
||||
for (var j = 1; j <= 3; j++) {
|
||||
print(ps[s[(i - 1) * 3 + j] + 1]);
|
||||
}
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
function mirror(x)
|
||||
{
|
||||
if (x == 1)
|
||||
return 3;
|
||||
if (x == 3)
|
||||
return 1;
|
||||
if (x == 6)
|
||||
return 4;
|
||||
if (x == 4)
|
||||
return 6;
|
||||
if (x == 9)
|
||||
return 7;
|
||||
if (x == 7)
|
||||
return 9;
|
||||
return x;
|
||||
}
|
||||
|
||||
// Main program
|
||||
async function main()
|
||||
{
|
||||
print(tab(32) + "HEXAPAWN\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
// HEXAPAWN: INTERPRETATION OF HEXAPAWN GAME AS PRESENTED IN
|
||||
// MARTIN GARDNER'S "THE UNEXPECTED HANGING AND OTHER MATHEMATIC-
|
||||
// AL DIVERSIONS", CHAPTER EIGHT: A MATCHBOX GAME-LEARNING MACHINE
|
||||
// ORIGINAL VERSION FOR H-P TIMESHARE SYSTEM BY R.A. KAAPKE 5/5/76
|
||||
// INSTRUCTIONS BY JEFF DALTON
|
||||
// CONVERSION TO MITS BASIC BY STEVE NORTH
|
||||
for (i = 0; i <= 9; i++) {
|
||||
s[i] = 0;
|
||||
}
|
||||
w = 0;
|
||||
l = 0;
|
||||
do {
|
||||
print("INSTRUCTIONS (Y-N)");
|
||||
str = await input();
|
||||
str = str.substr(0, 1);
|
||||
} while (str != "Y" && str != "N") ;
|
||||
if (str == "Y") {
|
||||
print("\n");
|
||||
print("THIS PROGRAM PLAYS THE GAME OF HEXAPAWN.\n");
|
||||
print("HEXAPAWN IS PLAYED WITH CHESS PAWNS ON A 3 BY 3 BOARD.\n");
|
||||
print("THE PAWNS ARE MOVED AS IN CHESS - ONE SPACE FORWARD TO\n");
|
||||
print("AN EMPTY SPACE OR ONE SPACE FORWARD AND DIAGONALLY TO\n");
|
||||
print("CAPTURE AN OPPOSING MAN. ON THE BOARD, YOUR PAWNS\n");
|
||||
print("ARE 'O', THE COMPUTER'S PAWNS ARE 'X', AND EMPTY \n");
|
||||
print("SQUARES ARE '.'. TO ENTER A MOVE, TYPE THE NUMBER OF\n");
|
||||
print("THE SQUARE YOU ARE MOVING FROM, FOLLOWED BY THE NUMBER\n");
|
||||
print("OF THE SQUARE YOU WILL MOVE TO. THE NUMBERS MUST BE\n");
|
||||
print("SEPERATED BY A COMMA.\n");
|
||||
print("\n");
|
||||
print("THE COMPUTER STARTS A SERIES OF GAMES KNOWING ONLY WHEN\n");
|
||||
print("THE GAME IS WON (A DRAW IS IMPOSSIBLE) AND HOW TO MOVE.\n");
|
||||
print("IT HAS NO STRATEGY AT FIRST AND JUST MOVES RANDOMLY.\n");
|
||||
print("HOWEVER, IT LEARNS FROM EACH GAME. THUS, WINNING BECOMES\n");
|
||||
print("MORE AND MORE DIFFICULT. ALSO, TO HELP OFFSET YOUR\n");
|
||||
print("INITIAL ADVANTAGE, YOU WILL NOT BE TOLD HOW TO WIN THE\n");
|
||||
print("GAME BUT MUST LEARN THIS BY PLAYING.\n");
|
||||
print("\n");
|
||||
print("THE NUMBERING OF THE BOARD IS AS FOLLOWS:\n");
|
||||
print(tab(10) + "123\n");
|
||||
print(tab(10) + "456\n");
|
||||
print(tab(10) + "789\n");
|
||||
print("\n");
|
||||
print("FOR EXAMPLE, TO MOVE YOUR RIGHTMOST PAWN FORWARD,\n");
|
||||
print("YOU WOULD TYPE 9,6 IN RESPONSE TO THE QUESTION\n");
|
||||
print("'YOUR MOVE ?'. SINCE I'M A GOOD SPORT, YOU'LL ALWAYS\n");
|
||||
print("GO FIRST.\n");
|
||||
print("\n");
|
||||
}
|
||||
while (1) {
|
||||
x = 0;
|
||||
y = 0;
|
||||
s[4] = 0;
|
||||
s[5] = 0;
|
||||
s[6] = 0;
|
||||
s[1] = -1;
|
||||
s[2] = -1;
|
||||
s[3] = -1;
|
||||
s[7] = 1;
|
||||
s[8] = 1;
|
||||
s[9] = 1;
|
||||
show_board();
|
||||
while (1) {
|
||||
while (1) {
|
||||
print("YOUR MOVE");
|
||||
str = await input();
|
||||
m1 = parseInt(str);
|
||||
m2 = parseInt(str.substr(str.indexOf(",") + 1));
|
||||
if (m1 > 0 && m1 < 10 && m2 > 0 && m2 < 10) {
|
||||
if (s[m1] != 1 || s[m2] == 1 || (m2 - m1 != -3 && s[m2] != -1) || (m2 > m1) || (m2 - m1 == -3 && s[m2] != 0) || (m2 - m1 < -4) || (m1 == 7 && m2 == 3))
|
||||
print("ILLEGAL MOVE.\n");
|
||||
else
|
||||
break;
|
||||
} else {
|
||||
print("ILLEGAL CO-ORDINATES.\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Move player's pawn
|
||||
s[m1] = 0;
|
||||
s[m2] = 1;
|
||||
show_board();
|
||||
|
||||
// Find computer pawns
|
||||
for (i = 1; i <= 9; i++) {
|
||||
if (s[i] == -1)
|
||||
break;
|
||||
}
|
||||
// If none or player reached top then finish
|
||||
if (i > 9 || s[1] == 1 || s[2] == 1 || s[3] == 1) {
|
||||
computer = false;
|
||||
break;
|
||||
}
|
||||
// Find computer pawns with valid move
|
||||
for (i = 1; i <= 9; i++) {
|
||||
if (s[i] != -1)
|
||||
continue;
|
||||
if (s[i + 3] == 0
|
||||
|| (mirror(i) == i && (s[i + 2] == 1 || s[i + 4] == 1))
|
||||
|| (i <= 3 && s[5] == 1)
|
||||
|| s[8] == 1)
|
||||
break;
|
||||
}
|
||||
if (i > 9) { // Finish if none possible
|
||||
computer = false;
|
||||
break;
|
||||
}
|
||||
for (i = 1; i <= 19; i++) {
|
||||
for (j = 1; j <= 3; j++) {
|
||||
for (k = 3; k >= 1; k--) {
|
||||
t[(j - 1) * 3 + k] = ba[i][(j - 1) * 3 + 4 - k];
|
||||
}
|
||||
}
|
||||
for (j = 1; j <= 9; j++) {
|
||||
if (s[j] != ba[i][j])
|
||||
break;
|
||||
}
|
||||
if (j > 9) {
|
||||
r = 0;
|
||||
break;
|
||||
}
|
||||
for (j = 1; j <= 9; j++) {
|
||||
if (s[j] != t[j])
|
||||
break;
|
||||
}
|
||||
if (j > 9) {
|
||||
r = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i > 19) {
|
||||
print("ILLEGAL BOARD PATTERN\n");
|
||||
break;
|
||||
}
|
||||
x = i;
|
||||
for (i = 1; i <= 4; i++) {
|
||||
if (ma[x][i] != 0)
|
||||
break;
|
||||
}
|
||||
if (i > 4) {
|
||||
print("I RESIGN.\n");
|
||||
computer = false;
|
||||
break;
|
||||
}
|
||||
// Select random move from possibilities
|
||||
do {
|
||||
y = Math.floor(Math.random() * 4 + 1);
|
||||
} while (ma[x][y] == 0) ;
|
||||
// Announce move
|
||||
if (r == 0) {
|
||||
print("I MOVE FROM " + Math.floor(ma[x][y] / 10) + " TO " + ma[x][y] % 10 + "\n");
|
||||
s[Math.floor(ma[x][y] / 10)] = 0;
|
||||
s[ma[x][y] % 10] = -1;
|
||||
} else {
|
||||
print("I MOVE FROM " + mirror(Math.floor(ma[x][y] / 10)) + " TO " + mirror(ma[x][y]) % 10 + "\n");
|
||||
s[mirror(Math.floor(ma[x][y] / 10))] = 0;
|
||||
s[mirror(ma[x][y] % 10)] = -1;
|
||||
}
|
||||
show_board();
|
||||
// Finish if computer reaches bottom
|
||||
if (s[7] == -1 || s[8] == -1 || s[9] == -1) {
|
||||
computer = true;
|
||||
break;
|
||||
}
|
||||
// Finish if no player pawns
|
||||
for (i = 1; i <= 9; i++) {
|
||||
if (s[i] == 1)
|
||||
break;
|
||||
}
|
||||
if (i > 9) {
|
||||
computer = true;
|
||||
break;
|
||||
}
|
||||
// Finish if player cannot move
|
||||
for (i = 1; i <= 9; i++) {
|
||||
if (s[i] != 1)
|
||||
continue;
|
||||
if (s[i - 3] == 0)
|
||||
break;
|
||||
if (mirror(i) != i) {
|
||||
if (i >= 7) {
|
||||
if (s[5] == -1)
|
||||
break;
|
||||
} else {
|
||||
if (s[2] == -1)
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (s[i - 2] == -1 || s[i - 4] == -1)
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
if (i > 9) {
|
||||
print("YOU CAN'T MOVE, SO ");
|
||||
computer = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (computer) {
|
||||
print("I WIN.\n");
|
||||
w++;
|
||||
} else {
|
||||
print("YOU WIN\n");
|
||||
ma[x][y] = 0;
|
||||
l++;
|
||||
}
|
||||
print("I HAVE WON " + w + " AND YOU " + l + " OUT OF " + (l + w) + " GAMES.\n");
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
9
47 Hi-Lo/javascript/hi-lo.html
Normal file
9
47 Hi-Lo/javascript/hi-lo.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>HI-LO</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="hi-lo.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
96
47 Hi-Lo/javascript/hi-lo.js
Normal file
96
47 Hi-Lo/javascript/hi-lo.js
Normal file
@@ -0,0 +1,96 @@
|
||||
// HI-LO
|
||||
//
|
||||
// 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) + "HI LO\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("THIS IS THE GAME OF HI LO.\n");
|
||||
print("\n");
|
||||
print("YOU WILL HAVE 6 TRIES TO GUESS THE AMOUNT OF MONEY IN THE\n");
|
||||
print("HI LO JACKPOT, WHICH IS BETWEEN 1 AND 100 DOLLARS. IF YOU\n");
|
||||
print("GUESS THE AMOUNT, YOU WIN ALL THE MONEY IN THE JACKPOT!\n");
|
||||
print("THEN YOU GET ANOTHER CHANCE TO WIN MORE MONEY. HOWEVER,\n");
|
||||
print("IF YOU DO NOT GUESS THE AMOUNT, THE GAME ENDS.\n");
|
||||
print("\n");
|
||||
r = 0;
|
||||
while (1) {
|
||||
b = 0;
|
||||
print("\n");
|
||||
y = Math.floor(100 * Math.random());
|
||||
for (b = 1; b <= 6; b++) {
|
||||
print("YOUR GUESS");
|
||||
a = parseInt(await input());
|
||||
if (a < y) {
|
||||
print("YOUR GUESS IS TOO LOW.\n");
|
||||
} else if (a > y) {
|
||||
print("YOUR GUESS IS TOO HIGH.\n");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
print("\n");
|
||||
}
|
||||
if (b > 6) {
|
||||
print("YOU BLEW IT...TOO BAD...THE NUMBER WAS " + y + "\n");
|
||||
r = 0;
|
||||
} else {
|
||||
print("GOT IT!!!!!!!!!! YOU WIN " + y + " DOLLARS.\n");
|
||||
r += y;
|
||||
print("YOUR TOTAL WINNINGS ARE NOW " + r + " DOLLARS.\n");
|
||||
}
|
||||
print("\n");
|
||||
print("PLAY AGAIN (YES OR NO)");
|
||||
str = await input();
|
||||
if (str != "YES")
|
||||
break;
|
||||
}
|
||||
print("\n");
|
||||
print("SO LONG. HOPE YOU ENJOYED YOURSELF!!!\n");
|
||||
}
|
||||
|
||||
main();
|
||||
9
48 High IQ/javascript/highiq.html
Normal file
9
48 High IQ/javascript/highiq.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>H-I-Q</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="highiq.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
244
48 High IQ/javascript/highiq.js
Normal file
244
48 High IQ/javascript/highiq.js
Normal file
@@ -0,0 +1,244 @@
|
||||
// H-I-Q
|
||||
//
|
||||
// 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 b = [];
|
||||
var t = [];
|
||||
var m = [,13,14,15,
|
||||
22,23,24,
|
||||
29,30,31,32,33,34,35,
|
||||
38,39,40,41,42,43,44,
|
||||
47,48,49,50,51,52,53,
|
||||
58,59,60,
|
||||
67,68,69];
|
||||
var z;
|
||||
var p;
|
||||
|
||||
//
|
||||
// Print board
|
||||
//
|
||||
function print_board()
|
||||
{
|
||||
for (x = 1; x <= 9; x++) {
|
||||
str = "";
|
||||
for (y = 1; y <= 9; y++) {
|
||||
if (x == 1 || x == 9 || y == 1 || y == 9)
|
||||
continue;
|
||||
if (x == 4 || x == 5 || x == 6 || y == 4 || y == 5 || y == 6) {
|
||||
while (str.length < y * 2)
|
||||
str += " ";
|
||||
if (t[x][y] == 5)
|
||||
str += "!";
|
||||
else
|
||||
str += "O";
|
||||
}
|
||||
}
|
||||
print(str + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Update board
|
||||
//
|
||||
function update_board()
|
||||
{
|
||||
c = 1;
|
||||
for (var x = 1; x <= 9; x++) {
|
||||
for (var y = 1; y <= 9; y++, c++) {
|
||||
if (c != z)
|
||||
continue;
|
||||
if (c + 2 == p) {
|
||||
if (t[x][y + 1] == 0)
|
||||
return false;
|
||||
t[x][y + 2] = 5;
|
||||
t[x][y + 1] = 0;
|
||||
b[c + 1] = -3;
|
||||
} else if (c + 18 == p) {
|
||||
if (t[x + 1][y] == 0)
|
||||
return false;
|
||||
t[x + 2][y] = 5;
|
||||
t[x + 1][y] = 0;
|
||||
b[c + 9] = -3;
|
||||
} else if (c - 2 == p) {
|
||||
if (t[x][y - 1] == 0)
|
||||
return false;
|
||||
t[x][y - 2] = 5;
|
||||
t[x][y - 1] = 0;
|
||||
b[c - 1] = -3;
|
||||
} else if (c - 18 == p) {
|
||||
if (t[x - 1][y] == 0)
|
||||
return false;
|
||||
t[x - 2][y] = 5;
|
||||
t[x - 1][y] = 0;
|
||||
b[c - 9] = -3;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
b[z] = -3;
|
||||
b[p] = -7;
|
||||
t[x][y] = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Check for game over
|
||||
//
|
||||
// Rewritten because original subroutine was buggy
|
||||
//
|
||||
function check_game_over()
|
||||
{
|
||||
f = 0;
|
||||
for (r = 2; r <= 8; r++) {
|
||||
for (c = 2; c <= 8; c++) {
|
||||
if (t[r][c] != 5)
|
||||
continue;
|
||||
f++;
|
||||
if (r > 3 && t[r - 1][c] == 5 && t[r - 2][c] == 0)
|
||||
return false;
|
||||
if (c > 3 && t[r][c - 1] == 5 && t[r][c - 2] == 0)
|
||||
return false;
|
||||
if (r < 7 && t[r + 1][c] == 5 && t[r + 2][c] == 0)
|
||||
return false;
|
||||
if (c < 7 && t[r][c + 1] == 5 && t[r][c + 2] == 0)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Main program
|
||||
async function main()
|
||||
{
|
||||
print(tab(33) + "H-I-Q\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
for (r = 0; r <= 70; r++)
|
||||
b[r] = 0;
|
||||
print("HERE IS THE BOARD:\n");
|
||||
print("\n");
|
||||
print(" ! ! !\n");
|
||||
print(" 13 14 15\n");
|
||||
print("\n");
|
||||
print(" ! ! !\n");
|
||||
print(" 22 23 24\n");
|
||||
print("\n");
|
||||
print("! ! ! ! ! ! !\n");
|
||||
print("29 30 31 32 33 34 35\n");
|
||||
print("\n");
|
||||
print("! ! ! ! ! ! !\n");
|
||||
print("38 39 40 41 42 43 44\n");
|
||||
print("\n");
|
||||
print("! ! ! ! ! ! !\n");
|
||||
print("47 48 49 50 51 52 53\n");
|
||||
print("\n");
|
||||
print(" ! ! !\n");
|
||||
print(" 58 59 60\n");
|
||||
print("\n");
|
||||
print(" ! ! !\n");
|
||||
print(" 67 68 69\n");
|
||||
print("\n");
|
||||
print("TO SAVE TYPING TIME, A COMPRESSED VERSION OF THE GAME BOARD\n");
|
||||
print("WILL BE USED DURING PLAY. REFER TO THE ABOVE ONE FOR PEG\n");
|
||||
print("NUMBERS. OK, LET'S BEGIN.\n");
|
||||
while (1) {
|
||||
// Set up board
|
||||
for (r = 1; r <= 9; r++) {
|
||||
t[r] = [];
|
||||
for (c = 1; c <= 9; c++) {
|
||||
if (r == 4 || r == 5 || r == 6 || c == 4 || c == 5 || c == 6 && (r != 1 && c != 1 && r != 9 && c != 9)) {
|
||||
t[r][c] = 5;
|
||||
} else {
|
||||
t[r][c] = -5;
|
||||
}
|
||||
}
|
||||
}
|
||||
t[5][5] = 0;
|
||||
print_board();
|
||||
// Init secondary board
|
||||
for (w = 1; w <= 33; w++) {
|
||||
b[m[w]] = -7;
|
||||
}
|
||||
b[41] = -3;
|
||||
// Input move and check on legality
|
||||
do {
|
||||
while (1) {
|
||||
print("MOVE WHICH PIECE");
|
||||
z = parseInt(await input());
|
||||
if (b[z] == -7) {
|
||||
print("TO WHERE");
|
||||
p = parseInt(await input());
|
||||
if (p != z
|
||||
&& b[p] != 0
|
||||
&& b[p] != -7
|
||||
&& (z + p) % 2 == 0
|
||||
&& (Math.abs(z - p) - 2) * (Math.abs(z - p) - 18) == 0
|
||||
&& update_board())
|
||||
break;
|
||||
}
|
||||
print("ILLEGAL MOVE, TRY AGAIN...\n");
|
||||
}
|
||||
print_board();
|
||||
} while (!check_game_over()) ;
|
||||
// Game is over
|
||||
print("THE GAME IS OVER.\n");
|
||||
print("YOU HAD " + f + " PIECES REMAINING.\n");
|
||||
if (f == 1) {
|
||||
print("BRAVO! YOU MADE A PERFECT SCORE!\n");
|
||||
print("SAVE THIS PAPER AS A RECORD OF YOUR ACCOMPLISHMENT!\n");
|
||||
}
|
||||
print("\n");
|
||||
print("PLAY AGAIN (YES OR NO)");
|
||||
str = await input();
|
||||
if (str == "NO")
|
||||
break;
|
||||
}
|
||||
print("\n");
|
||||
print("SO LONG FOR NOW.\n");
|
||||
print("\n");
|
||||
}
|
||||
|
||||
main();
|
||||
9
55 Life/javascript/life.html
Normal file
9
55 Life/javascript/life.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>LIFE</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="life.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
191
55 Life/javascript/life.js
Normal file
191
55 Life/javascript/life.js
Normal file
@@ -0,0 +1,191 @@
|
||||
// LIFE
|
||||
//
|
||||
// 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 bs = [];
|
||||
var a = [];
|
||||
|
||||
// Main program
|
||||
async function main()
|
||||
{
|
||||
print(tab(34) + "LIFE\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("ENTER YOUR PATTERN:\n");
|
||||
x1 = 1;
|
||||
y1 = 1;
|
||||
x2 = 24;
|
||||
y2 = 70;
|
||||
for (c = 1; c <= 24; c++) {
|
||||
bs[c] = "";
|
||||
a[c] = [];
|
||||
for (d = 1; d <= 70; d++)
|
||||
a[c][d] = 0;
|
||||
}
|
||||
c = 1;
|
||||
while (1) {
|
||||
bs[c] = await input();
|
||||
if (bs[c] == "DONE") {
|
||||
bs[c] = "";
|
||||
break;
|
||||
}
|
||||
if (bs[c].substr(0, 1) == ".")
|
||||
bs[c] = " " + bs[c].substr(1);
|
||||
c++;
|
||||
}
|
||||
c--;
|
||||
l = 0;
|
||||
for (x = 1; x <= c - 1; x++) {
|
||||
if (bs[x].length > l)
|
||||
l = bs[x].length;
|
||||
}
|
||||
x1 = 11 - (c >> 1);
|
||||
y1 = 33 - (l >> 1);
|
||||
p = 0;
|
||||
for (x = 1; x <= c; x++) {
|
||||
for (y = 1; y <= bs[x].length; y++) {
|
||||
if (bs[x][y - 1] != " ") {
|
||||
a[x1 + x][y1 + y] = 1;
|
||||
p++;
|
||||
}
|
||||
}
|
||||
}
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
i9 = false;
|
||||
g = 0;
|
||||
while (g < 100) {
|
||||
print("GENERATION: " + g + " POPULATION: " + p + " ");
|
||||
if (i9)
|
||||
print("INVALID!");
|
||||
x3 = 24;
|
||||
y3 = 70;
|
||||
x4 = 1;
|
||||
y4 = 1;
|
||||
p = 0;
|
||||
g++;
|
||||
for (x = 1; x <= x1 - 1; x++)
|
||||
print("\n");
|
||||
for (x = x1; x <= x2; x++) {
|
||||
print("\n");
|
||||
str = "";
|
||||
for (y = y1; y <= y2; y++) {
|
||||
if (a[x][y] == 2) {
|
||||
a[x][y] = 0;
|
||||
continue;
|
||||
} else if (a[x][y] == 3) {
|
||||
a[x][y] = 1;
|
||||
} else if (a[x][y] != 1) {
|
||||
continue;
|
||||
}
|
||||
while (str.length < y)
|
||||
str += " ";
|
||||
str += "*";
|
||||
if (x < x3)
|
||||
x3 = x;
|
||||
if (x > x4)
|
||||
x4 = x;
|
||||
if (y < y3)
|
||||
y3 = y;
|
||||
if (y > y4)
|
||||
y4 = y;
|
||||
}
|
||||
print(str);
|
||||
}
|
||||
for (x = x2 + 1; x <= 24; x++)
|
||||
print("\n");
|
||||
x1 = x3;
|
||||
x2 = x4;
|
||||
y1 = y3;
|
||||
y2 = y4;
|
||||
if (x1 < 3) {
|
||||
x1 = 3;
|
||||
i9 = true;
|
||||
}
|
||||
if (x2 > 22) {
|
||||
x2 = 22;
|
||||
i9 = true;
|
||||
}
|
||||
if (y1 < 3) {
|
||||
y1 = 3;
|
||||
i9 = true;
|
||||
}
|
||||
if (y2 > 68) {
|
||||
y2 = 68;
|
||||
i9 = true;
|
||||
}
|
||||
p = 0;
|
||||
for (x = x1 - 1; x <= x2 + 1; x++) {
|
||||
for (y = y1 - 1; y <= y2 + 1; y++) {
|
||||
c = 0;
|
||||
for (i = x - 1; i <= x + 1; i++) {
|
||||
for (j = y - 1; j <= y + 1; j++) {
|
||||
if (a[i][j] == 1 || a[i][j] == 2)
|
||||
c++;
|
||||
}
|
||||
}
|
||||
if (a[x][y] == 0) {
|
||||
if (c == 3) {
|
||||
a[x][y] = 3;
|
||||
p++;
|
||||
}
|
||||
} else {
|
||||
if (c < 3 || c > 4) {
|
||||
a[x][y] = 2;
|
||||
} else {
|
||||
p++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
x1--;
|
||||
y1--;
|
||||
x2++;
|
||||
y2++;
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user