mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Ported BASKETBALL, BATNUM and CHOMP to Javascript
This commit is contained in:
9
07 Basketball/javascript/basketball.html
Normal file
9
07 Basketball/javascript/basketball.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>BASKETBALL</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="basketball.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
384
07 Basketball/javascript/basketball.js
Normal file
384
07 Basketball/javascript/basketball.js
Normal file
@@ -0,0 +1,384 @@
|
||||
// BASKETBALL
|
||||
//
|
||||
// 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 s = [0, 0];
|
||||
var z;
|
||||
var d;
|
||||
var p;
|
||||
var your_turn;
|
||||
var game_restart;
|
||||
|
||||
function two_minutes()
|
||||
{
|
||||
print("\n");
|
||||
print(" *** TWO MINUTES LEFT IN THE GAME ***\n");
|
||||
print("\n");
|
||||
}
|
||||
|
||||
function show_scores()
|
||||
{
|
||||
print("SCORE: " + s[1] + " TO " + s[0] + "\n");
|
||||
}
|
||||
|
||||
function score_computer()
|
||||
{
|
||||
s[0] = s[0] + 2;
|
||||
show_scores();
|
||||
}
|
||||
|
||||
function score_player()
|
||||
{
|
||||
s[1] = s[1] + 2;
|
||||
show_scores();
|
||||
}
|
||||
|
||||
function half_time()
|
||||
{
|
||||
print("\n");
|
||||
print(" ***** END OF FIRST HALF *****\n");
|
||||
print("SCORE: DARMOUTH: " + s[1] + " " + os + ": " + s[0] + "\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
}
|
||||
|
||||
function foul()
|
||||
{
|
||||
if (Math.random() <= 0.49) {
|
||||
print("SHOOTER MAKES BOTH SHOTS.\n");
|
||||
s[1 - p] = s[1 - p] + 2;
|
||||
show_scores();
|
||||
} else if (Math.random() <= 0.75) {
|
||||
print("SHOOTER MAKES ONE SHOT AND MISSES ONE.\n");
|
||||
s[1 - p] = s[1 - p] + 1;
|
||||
show_scores();
|
||||
} else {
|
||||
print("BOTH SHOTS MISSED.\n");
|
||||
show_scores();
|
||||
}
|
||||
}
|
||||
|
||||
function player_play()
|
||||
{
|
||||
if (z == 1 || z == 2) {
|
||||
t++;
|
||||
if (t == 50) {
|
||||
half_time();
|
||||
game_restart = 1;
|
||||
return;
|
||||
}
|
||||
if (t == 92)
|
||||
two_minutes();
|
||||
print("JUMP SHOT\n");
|
||||
if (Math.random() <= 0.341 * d / 8) {
|
||||
print("SHOT IS GOOD.\n");
|
||||
score_player();
|
||||
return;
|
||||
}
|
||||
if (Math.random() <= 0.682 * d / 8) {
|
||||
print("SHOT IS OFF TARGET.\n");
|
||||
if (d / 6 * Math.random() >= 0.45) {
|
||||
print("REBOUND TO " + os + "\n");
|
||||
return;
|
||||
}
|
||||
print("DARTMOUTH CONTROLS THE REBOUND.\n");
|
||||
if (Math.random() > 0.4) {
|
||||
if (d == 6) {
|
||||
if (Math.random() > 0.6) {
|
||||
print("PASS STOLEN BY " + os + " EASY LAYUP.\n");
|
||||
score_computer();
|
||||
return;
|
||||
}
|
||||
}
|
||||
print("BALL PASSED BACK TO YOU. ");
|
||||
your_turn = 1;
|
||||
return;
|
||||
}
|
||||
} else if (Math.random() <= 0.782 * d / 8) {
|
||||
print("SHOT IS BLOCKED. BALL CONTROLLED BY ");
|
||||
if (Math.random() <= 0.5) {
|
||||
print("DARTMOUTH.\n");
|
||||
your_turn = 1;
|
||||
return;
|
||||
}
|
||||
print(os + ".\n");
|
||||
return;
|
||||
} else if (Math.random() <= 0.843 * d / 8) {
|
||||
print("SHOOTER IS FOULED. TWO SHOTS.\n");
|
||||
foul();
|
||||
return;
|
||||
// In original code but lines 1180-1195 aren't used (maybe replicate from computer's play)
|
||||
// } else if (Math.random() <= 0.9 * d / 8) {
|
||||
// print("PLAYER FOULED, TWO SHOTS.\n");
|
||||
// foul();
|
||||
// return;
|
||||
} else {
|
||||
print("CHARGING FOUL. DARTMOUTH LOSES BALL.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
while (1) {
|
||||
if (++t == 50) {
|
||||
half_time();
|
||||
game_restart = 1;
|
||||
return;
|
||||
}
|
||||
if (t == 92)
|
||||
two_minutes();
|
||||
if (z == 0) {
|
||||
your_turn = 2;
|
||||
return;
|
||||
}
|
||||
if (z <= 3)
|
||||
print("LAY UP.\n");
|
||||
else
|
||||
print("SET SHOT.\n");
|
||||
if (7 / d * Math.random() <= 0.4) {
|
||||
print("SHOT IS GOOD. TWO POINTS.\n");
|
||||
score_player();
|
||||
return;
|
||||
}
|
||||
if (7 / d * Math.random() <= 0.7) {
|
||||
print("SHOT IS OFF THE RIM.\n");
|
||||
if (Math.random() <= 2.0 / 3.0) {
|
||||
print(os + " CONTROLS THE REBOUND.\n");
|
||||
return;
|
||||
}
|
||||
print("DARMOUTH CONTROLS THE REBOUND.\n");
|
||||
if (Math.random() <= 0.4)
|
||||
continue;
|
||||
print("BALL PASSED BACK TO YOU.\n");
|
||||
your_turn = 1;
|
||||
return;
|
||||
}
|
||||
if (7 /d * Math.random() <= 0.875) {
|
||||
print("SHOOTER FOULED. TWO SHOTS.\n");
|
||||
foul();
|
||||
return;
|
||||
}
|
||||
if (7 /d * Math.random() <= 0.925) {
|
||||
print("SHOT BLOCKED. " + os + "'S BALL.\n");
|
||||
return;
|
||||
}
|
||||
print("CHARGING FOUL. DARTHMOUTH LOSES THE BALL.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function computer_play()
|
||||
{
|
||||
rebound = 0;
|
||||
while (1) {
|
||||
p = 1;
|
||||
if (++t == 50) {
|
||||
half_time();
|
||||
game_restart = 1;
|
||||
return;
|
||||
}
|
||||
print("\n");
|
||||
z1 = 10 / 4 * Math.random() + 1;
|
||||
if (z1 <= 2) {
|
||||
print("JUMP SHOT.\n");
|
||||
if (8 / d * Math.random() <= 0.35) {
|
||||
print("SHOT IS GOOD.\n");
|
||||
score_computer();
|
||||
return;
|
||||
}
|
||||
if (8 / d * Math.random() <= 0.75) {
|
||||
print("SHOT IS OFF RIM.\n");
|
||||
if (d / 6 * Math.random() <= 0.5) {
|
||||
print("DARMOUTH CONTROLS THE REBOUND.\n");
|
||||
return;
|
||||
}
|
||||
print(os + " CONTROLS THE REBOUND.\n");
|
||||
if (d == 6) {
|
||||
if (Math.random() <= 0.75) {
|
||||
print("BALL STOLEN. EASY LAP UP FOR DARTMOUTH.\n");
|
||||
score_player();
|
||||
continue;
|
||||
}
|
||||
if (Math.random() > 0.6) {
|
||||
print("PASS STOLEN BY " + os + " EASY LAYUP.\n");
|
||||
score_computer();
|
||||
return;
|
||||
}
|
||||
print("BALL PASSED BACK TO YOU. ");
|
||||
return;
|
||||
}
|
||||
if (Math.random() <= 0.5) {
|
||||
print("PASS BACK TO " + os + " GUARD.\n");
|
||||
continue;
|
||||
}
|
||||
} else if (8 / d * Math.random() <= 0.90) {
|
||||
print("PLAYER FOULED. TWO SHOTS.\n");
|
||||
foul();
|
||||
return;
|
||||
} else {
|
||||
print("OFFENSIVE FOUL. DARTMOUTH'S BALL.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
while (1) {
|
||||
if (z1 > 3) {
|
||||
print("SET SHOT.\n");
|
||||
} else {
|
||||
print("LAY UP.\n");
|
||||
}
|
||||
if (7 / d * Math.random() <= 0.413) {
|
||||
print("SHOT IS GOOD.\n");
|
||||
score_computer();
|
||||
return;
|
||||
}
|
||||
print("SHOT IS MISSED.\n");
|
||||
// Spaguetti jump, better to replicate code
|
||||
if (d / 6 * Math.random() <= 0.5) {
|
||||
print("DARMOUTH CONTROLS THE REBOUND.\n");
|
||||
return;
|
||||
}
|
||||
print(os + " CONTROLS THE REBOUND.\n");
|
||||
if (d == 6) {
|
||||
if (Math.random() <= 0.75) {
|
||||
print("BALL STOLEN. EASY LAP UP FOR DARTMOUTH.\n");
|
||||
score_player();
|
||||
break;
|
||||
}
|
||||
if (Math.random() > 0.6) {
|
||||
print("PASS STOLEN BY " + os + " EASY LAYUP.\n");
|
||||
score_computer();
|
||||
return;
|
||||
}
|
||||
print("BALL PASSED BACK TO YOU. ");
|
||||
return;
|
||||
}
|
||||
if (Math.random() <= 0.5) {
|
||||
print("PASS BACK TO " + os + " GUARD.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Main program
|
||||
async function main()
|
||||
{
|
||||
print(tab(31) + "BASKETBALL\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("THIS IS DARTMOUTH COLLEGE BASKETBALL. YOU WILL BE DARTMOUTH\n");
|
||||
print(" CAPTAIN AND PLAYMAKER. CALL SHOTS AS FOLLOWS: 1. LONG\n");
|
||||
print(" (30 FT.) JUMP SHOT; 2. SHORT (15 FT.) JUMP SHOT; 3. LAY\n");
|
||||
print(" UP; 4. SET SHOT.\n");
|
||||
print("BOTH TEAMS WILL USE THE SAME DEFENSE. CALL DEFENSE AS\n");
|
||||
print("FOLLOWS: 6. PRESS; 6.5 MAN-TO MAN; 7. ZONE; 7.5 NONE.\n");
|
||||
print("TO CHANGE DEFENSE, JUST TYPE 0 AS YOUR NEXT SHOT.\n");
|
||||
print("YOUR STARTING DEFENSE WILL BE");
|
||||
t = 0;
|
||||
p = 0;
|
||||
d = parseFloat(await input());
|
||||
if (d < 6) {
|
||||
your_turn = 2;
|
||||
} else {
|
||||
print("\n");
|
||||
print("CHOOSE YOUR OPPONENT");
|
||||
os = await input();
|
||||
game_restart = 1;
|
||||
}
|
||||
while (1) {
|
||||
if (game_restart) {
|
||||
game_restart = 0;
|
||||
print("CENTER JUMP\n");
|
||||
if (Math.random() > 3.0 / 5.0) {
|
||||
print("DARMOUTH CONTROLS THE TAP.\n");
|
||||
} else {
|
||||
print(os + " CONTROLS THE TAP.\n");
|
||||
computer_play();
|
||||
}
|
||||
}
|
||||
if (your_turn == 2) {
|
||||
print("YOUR NEW DEFENSIVE ALLIGNMENT IS");
|
||||
d = parseFloat(await input());
|
||||
}
|
||||
print("\n");
|
||||
while (1) {
|
||||
print("YOUR SHOT");
|
||||
z = parseInt(await input());
|
||||
p = 0;
|
||||
if (z != Math.floor(z) || z < 0 || z > 4)
|
||||
print("INCORRECT ANSWER. RETYPE IT. ");
|
||||
else
|
||||
break;
|
||||
}
|
||||
if (Math.random() < 0.5 || t < 100) {
|
||||
game_restart = 0;
|
||||
your_turn = 0;
|
||||
player_play();
|
||||
if (game_restart == 0 && your_turn == 0)
|
||||
computer_play();
|
||||
} else {
|
||||
print("\n");
|
||||
if (s[1] == s[0]) {
|
||||
print("\n");
|
||||
print(" ***** END OF SECOND HALF *****\n");
|
||||
print("\n");
|
||||
print("SCORE AT END OF REGULATION TIME:\n");
|
||||
print(" DARTMOUTH: " + s[1] + " " + os + ": " + s[0] + "\n");
|
||||
print("\n");
|
||||
print("BEGIN TWO MINUTE OVERTIME PERIOD\n");
|
||||
t = 93;
|
||||
print("CENTER JUMP\n");
|
||||
if (Math.random() > 3.0 / 5.0)
|
||||
print("DARMOUTH CONTROLS THE TAP.\n");
|
||||
else
|
||||
print(os + " CONTROLS THE TAP.\n");
|
||||
} else {
|
||||
print(" ***** END OF GAME *****\n");
|
||||
print("FINAL SCORE: DARMOUTH: " + s[1] + " " + os + ": " + s[0] + "\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
9
08 Batnum/javascript/batnum.html
Normal file
9
08 Batnum/javascript/batnum.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>BATNUM</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="batnum.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
161
08 Batnum/javascript/batnum.js
Normal file
161
08 Batnum/javascript/batnum.js
Normal file
@@ -0,0 +1,161 @@
|
||||
// BATNUM
|
||||
//
|
||||
// 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(33) + "BATNUM\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("THIS PROGRAM IS A 'BATTLE OF NUMBERS' GAME, WHERE THE\n");
|
||||
print("COMPUTER IS YOUR OPPONENT.\n");
|
||||
print("\n");
|
||||
print("THE GAME STARTS WITH AN ASSUMED PILE OF OBJECTS. YOU\n");
|
||||
print("AND YOUR OPPONENT ALTERNATELY REMOVE OBJECTS FROM THE PILE.\n");
|
||||
print("WINNING IS DEFINED IN ADVANCE AS TAKING THE LAST OBJECT OR\n");
|
||||
print("NOT. YOU CAN ALSO SPECIFY SOME OTHER BEGINNING CONDITIONS.\n");
|
||||
print("DON'T USE ZERO, HOWEVER, IN PLAYING THE GAME.\n");
|
||||
print("ENTER A NEGATIVE NUMBER FOR NEW PILE SIZE TO STOP PLAYING.\n");
|
||||
print("\n");
|
||||
first_time = 1;
|
||||
while (1) {
|
||||
while (1) {
|
||||
if (first_time == 1) {
|
||||
first_time = 0;
|
||||
} else {
|
||||
for (i = 1; i <= 10; i++)
|
||||
print("\n");
|
||||
}
|
||||
print("ENTER PILE SIZE");
|
||||
n = parseInt(await input());
|
||||
if (n >= 1)
|
||||
break;
|
||||
}
|
||||
while (1) {
|
||||
print("ENTER WIN OPTION - 1 TO TAKE LAST, 2 TO AVOID LAST: ");
|
||||
m = parseInt(await input());
|
||||
if (m == 1 || m == 2)
|
||||
break;
|
||||
}
|
||||
while (1) {
|
||||
print("ENTER MIN AND MAX ");
|
||||
str = await input();
|
||||
a = parseInt(str);
|
||||
b = parseInt(str.substr(str.indexOf(",") + 1));
|
||||
if (a <= b && a >= 1)
|
||||
break;
|
||||
}
|
||||
while (1) {
|
||||
print("ENTER START OPTION - 1 COMPUTER FIRST, 2 YOU FIRST ");
|
||||
s = parseInt(await input());
|
||||
print("\n");
|
||||
print("\n");
|
||||
if (s == 1 || s == 2)
|
||||
break;
|
||||
}
|
||||
w = 0;
|
||||
c = a + b;
|
||||
while (1) {
|
||||
if (s == 1) {
|
||||
// Computer's turn
|
||||
q = n;
|
||||
if (m != 1)
|
||||
q--;
|
||||
if (m != 1 && n <= a) {
|
||||
w = 1;
|
||||
print("COMPUTER TAKES " + n + " AND LOSES.\n");
|
||||
} else if (m == 1 && n <= b) {
|
||||
w = 1;
|
||||
print("COMPUTER TAKES " + n + " AND WINS.\n");
|
||||
} else {
|
||||
p = q - c * Math.floor(q / c);
|
||||
if (p < a)
|
||||
p = a;
|
||||
if (p > b)
|
||||
p = b;
|
||||
n -= p;
|
||||
print("COMPUTER TAKES " + p + " AND LEAVES " + n + "\n");
|
||||
w = 0;
|
||||
}
|
||||
s = 2;
|
||||
}
|
||||
if (w)
|
||||
break;
|
||||
if (s == 2) {
|
||||
while (1) {
|
||||
print("\n");
|
||||
print("YOUR MOVE ");
|
||||
p = parseInt(await input());
|
||||
if (p == 0) {
|
||||
print("I TOLD YOU NOT TO USE ZERO! COMPUTER WINS BY FORFEIT.\n");
|
||||
w = 1;
|
||||
break;
|
||||
} else if (p >= a && p <= b && n - p >= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (p != 0) {
|
||||
n -= p;
|
||||
if (n == 0) {
|
||||
if (m != 1) {
|
||||
print("TOUGH LUCK, YOU LOSE.\n");
|
||||
} else {
|
||||
print("CONGRATULATIONS, YOU WIN.\n");
|
||||
}
|
||||
w = 1;
|
||||
} else {
|
||||
w = 0;
|
||||
}
|
||||
}
|
||||
s = 1;
|
||||
}
|
||||
if (w)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -99,6 +99,6 @@
|
||||
1010 PRINT "YOU LOSE, PLAYER";P1
|
||||
1020 PRINT
|
||||
1030 PRINT "AGAIN (1=YES, 0=NO!)";
|
||||
1040 INPUT R$
|
||||
1040 INPUT R
|
||||
1050 IF R=1 THEN 340
|
||||
1060 END
|
||||
|
||||
9
26 Chomp/javascript/chomp.html
Normal file
9
26 Chomp/javascript/chomp.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>CHOMP</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="chomp.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
175
26 Chomp/javascript/chomp.js
Normal file
175
26 Chomp/javascript/chomp.js
Normal file
@@ -0,0 +1,175 @@
|
||||
// CHOMP
|
||||
//
|
||||
// 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 a = [];
|
||||
var r;
|
||||
var c;
|
||||
|
||||
function init_board()
|
||||
{
|
||||
for (i = 1; i <= r; i++)
|
||||
for (j = 1; j <= c; j++)
|
||||
a[i][j] = 1;
|
||||
a[1][1] = -1;
|
||||
}
|
||||
|
||||
function show_board()
|
||||
{
|
||||
print("\n");
|
||||
print(tab(7) + "1 2 3 4 5 6 7 8 9\n");
|
||||
for (i = 1; i <= r; i++) {
|
||||
str = i + tab(6);
|
||||
for (j = 1; j <= c; j++) {
|
||||
if (a[i][j] == -1)
|
||||
str += "P ";
|
||||
else if (a[i][j] == 0)
|
||||
break;
|
||||
else
|
||||
str += "* ";
|
||||
}
|
||||
print(str + "\n");
|
||||
}
|
||||
print("\n");
|
||||
}
|
||||
|
||||
// Main program
|
||||
async function main()
|
||||
{
|
||||
print(tab(33) + "CHOMP\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
for (i = 1; i <= 10; i++)
|
||||
a[i] = [];
|
||||
// *** THE GAME OF CHOMP *** COPYRIGHT PCC 1973 ***
|
||||
print("\n");
|
||||
print("THIS IS THE GAME OF CHOMP (SCIENTIFIC AMERICAN, JAN 1973)\n");
|
||||
print("DO YOU WANT THE RULES (1=YES, 0=NO!)");
|
||||
r = parseInt(await input());
|
||||
if (r != 0) {
|
||||
f = 1;
|
||||
r = 5;
|
||||
c = 7;
|
||||
print("CHOMP IS FOR 1 OR MORE PLAYERS (HUMANS ONLY).\n");
|
||||
print("\n");
|
||||
print("HERE'S HOW A BOARD LOOKS (THIS ONE IS 5 BY 7):\n");
|
||||
init_board();
|
||||
show_board();
|
||||
print("\n");
|
||||
print("THE BOARD IS A BIG COOKIE - R ROWS HIGH AND C COLUMNS\n");
|
||||
print("WIDE. YOU INPUT R AND C AT THE START. IN THE UPPER LEFT\n");
|
||||
print("CORNER OF THE COOKIE IS A POISON SQUARE (P). THE ONE WHO\n");
|
||||
print("CHOMPS THE POISON SQUARE LOSES. TO TAKE A CHOMP, TYPE THE\n");
|
||||
print("ROW AND COLUMN OF ONE OF THE SQUARES ON THE COOKIE.\n");
|
||||
print("ALL OF THE SQUARES BELOW AND TO THE RIGHT OF THAT SQUARE\n");
|
||||
print("INCLUDING THAT SQUARE, TOO) DISAPPEAR -- CHOMP!!\n");
|
||||
print("NO FAIR CHOMPING SQUARES THAT HAVE ALREADY BEEN CHOMPED,\n");
|
||||
print("OR THAT ARE OUTSIDE THE ORIGINAL DIMENSIONS OF THE COOKIE.\n");
|
||||
print("\n");
|
||||
}
|
||||
while (1) {
|
||||
print("HERE WE GO...\n");
|
||||
f = 0;
|
||||
for (i = 1; i <= 10; i++) {
|
||||
a[i] = [];
|
||||
for (j = 1; j <= 10; j++) {
|
||||
a[i][j] = 0;
|
||||
}
|
||||
}
|
||||
print("\n");
|
||||
print("HOW MANY PLAYERS");
|
||||
p = parseInt(await input());
|
||||
i1 = 0;
|
||||
while (1) {
|
||||
print("HOW MANY ROWS");
|
||||
r = parseInt(await input());
|
||||
if (r <= 9)
|
||||
break;
|
||||
print("TOO MANY ROWS (9 IS MAXIMUM). NOW ");
|
||||
}
|
||||
while (1) {
|
||||
print("HOW MANY COLUMNS");
|
||||
c = parseInt(await input());
|
||||
if (c <= 9)
|
||||
break;
|
||||
print("TOO MANY COLUMNS (9 IS MAXIMUM). NOW ");
|
||||
}
|
||||
print("\n");
|
||||
init_board();
|
||||
while (1) {
|
||||
// Print the board
|
||||
show_board();
|
||||
// Get chomps for each player in turn
|
||||
i1++;
|
||||
p1 = i1 - Math.floor(i1 / p) * p;
|
||||
if (p1 == 0)
|
||||
p1 = p;
|
||||
while (1) {
|
||||
print("PLAYER " + p1 + "\n");
|
||||
print("COORDINATES OF CHOMP (ROW,COLUMN)");
|
||||
str = await input();
|
||||
r1 = parseInt(str);
|
||||
c1 = parseInt(str.substr(str.indexOf(",") + 1));
|
||||
if (r1 >= 1 && r1 <= r && c1 >= 1 && c1 <= c && a[r1][c1] != 0)
|
||||
break;
|
||||
print("NO FAIR. YOU'RE TRYING TO CHOMP ON EMPTY SPACE!\n");
|
||||
}
|
||||
if (a[r1][c1] == -1)
|
||||
break;
|
||||
for (i = r1; i <= r; i++)
|
||||
for (j = c1; j <= c; j++)
|
||||
a[i][j] = 0;
|
||||
}
|
||||
// End of game detected
|
||||
print("YOU LOSE, PLAYER " + p1 + "\n");
|
||||
print("\n");
|
||||
print("AGAIN (1=YES, 0=NO!)");
|
||||
r = parseInt(await input());
|
||||
if (r != 1)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user