mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-28 13:46:06 -08:00
Merge pull request #238 from nanochess/main
Ported TICTACTOE1 to Javascript
This commit is contained in:
9
88 3-D Tic-Tac-Toe/javascript/qubit.html
Normal file
9
88 3-D Tic-Tac-Toe/javascript/qubit.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>QUBIT</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="qubit.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
448
88 3-D Tic-Tac-Toe/javascript/qubit.js
Normal file
448
88 3-D Tic-Tac-Toe/javascript/qubit.js
Normal file
@@ -0,0 +1,448 @@
|
||||
// QUBIT
|
||||
//
|
||||
// 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 xa = [];
|
||||
var la = [];
|
||||
var ma = [[],
|
||||
[,1,2,3,4], // 1
|
||||
[,5,6,7,8], // 2
|
||||
[,9,10,11,12], // 3
|
||||
[,13,14,15,16], // 4
|
||||
[,17,18,19,20], // 5
|
||||
[,21,22,23,24], // 6
|
||||
[,25,26,27,28], // 7
|
||||
[,29,30,31,32], // 8
|
||||
[,33,34,35,36], // 9
|
||||
[,37,38,39,40], // 10
|
||||
[,41,42,43,44], // 11
|
||||
[,45,46,47,48], // 12
|
||||
[,49,50,51,52], // 13
|
||||
[,53,54,55,56], // 14
|
||||
[,57,58,59,60], // 15
|
||||
[,61,62,63,64], // 16
|
||||
[,1,17,33,49], // 17
|
||||
[,5,21,37,53], // 18
|
||||
[,9,25,41,57], // 19
|
||||
[,13,29,45,61], // 20
|
||||
[,2,18,34,50], // 21
|
||||
[,6,22,38,54], // 22
|
||||
[,10,26,42,58], // 23
|
||||
[,14,30,46,62], // 24
|
||||
[,3,19,35,51], // 25
|
||||
[,7,23,39,55], // 26
|
||||
[,11,27,43,59], // 27
|
||||
[,15,31,47,63], // 28
|
||||
[,4,20,36,52], // 29
|
||||
[,8,24,40,56], // 30
|
||||
[,12,28,44,60], // 31
|
||||
[,16,32,48,64], // 32
|
||||
[,1,5,9,13], // 33
|
||||
[,17,21,25,29], // 34
|
||||
[,33,37,41,45], // 35
|
||||
[,49,53,57,61], // 36
|
||||
[,2,6,10,14], // 37
|
||||
[,18,22,26,30], // 38
|
||||
[,34,38,42,46], // 39
|
||||
[,50,54,58,62], // 40
|
||||
[,3,7,11,15], // 41
|
||||
[,19,23,27,31], // 42
|
||||
[,35,39,43,47], // 43
|
||||
[,51,55,59,63], // 44
|
||||
[,4,8,12,16], // 45
|
||||
[,20,24,28,32], // 46
|
||||
[,36,40,44,48], // 47
|
||||
[,52,56,60,64], // 48
|
||||
[,1,6,11,16], // 49
|
||||
[,17,22,27,32], // 50
|
||||
[,33,38,43,48], // 51
|
||||
[,49,54,59,64], // 52
|
||||
[,13,10,7,4], // 53
|
||||
[,29,26,23,20], // 54
|
||||
[,45,42,39,36], // 55
|
||||
[,61,58,55,52], // 56
|
||||
[,1,21,41,61], // 57
|
||||
[,2,22,42,62], // 58
|
||||
[,3,23,43,63], // 59
|
||||
[,4,24,44,64], // 60
|
||||
[,49,37,25,13], // 61
|
||||
[,50,38,26,14], // 62
|
||||
[,51,39,27,15], // 63
|
||||
[,52,40,28,16], // 64
|
||||
[,1,18,35,52], // 65
|
||||
[,5,22,39,56], // 66
|
||||
[,9,26,43,60], // 67
|
||||
[,13,30,47,64], // 68
|
||||
[,49,34,19,4], // 69
|
||||
[,53,38,23,8], // 70
|
||||
[,57,42,27,12], // 71
|
||||
[,61,46,31,16], // 72
|
||||
[,1,22,43,64], // 73
|
||||
[,16,27,38,49], // 74
|
||||
[,4,23,42,61], // 75
|
||||
[,13,26,39,52] // 76
|
||||
];
|
||||
var ya = [,1,49,52,4,13,61,64,16,22,39,23,38,26,42,27,43];
|
||||
|
||||
function show_board()
|
||||
{
|
||||
for (xx = 1; xx <= 9; xx++)
|
||||
print("\n");
|
||||
for (i = 1; i <= 4; i++) {
|
||||
for (j = 1; j <= 4; j++) {
|
||||
str = "";
|
||||
for (i1 = 1; i1 <= j; i1++)
|
||||
str += " ";
|
||||
for (k = 1; k <= 4; k++) {
|
||||
q = 16 * i + 4 * j + k - 20;
|
||||
if (xa[q] == 0)
|
||||
str += "( ) ";
|
||||
if (xa[q] == 5)
|
||||
str += "(M) ";
|
||||
if (xa[q] == 1)
|
||||
str += "(Y) ";
|
||||
if (xa[q] == 1 / 8)
|
||||
str += "( ) ";
|
||||
}
|
||||
print(str + "\n");
|
||||
print("\n");
|
||||
}
|
||||
print("\n");
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
function process_board()
|
||||
{
|
||||
for (i = 1; i <= 64; i++) {
|
||||
if (xa[i] == 1 / 8)
|
||||
xa[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
function check_for_lines()
|
||||
{
|
||||
for (s = 1; s <= 76; s++) {
|
||||
j1 = ma[s][1];
|
||||
j2 = ma[s][2];
|
||||
j3 = ma[s][3];
|
||||
j4 = ma[s][4];
|
||||
la[s] = xa[j1] + xa[j2] + xa[j3] + xa[j4];
|
||||
}
|
||||
}
|
||||
|
||||
function show_square(m)
|
||||
{
|
||||
k1 = Math.floor((m - 1) / 16) + 1;
|
||||
j2 = m - 16 * (k1 - 1);
|
||||
k2 = Math.floor((j2 - 1) / 4) + 1;
|
||||
k3 = m - (k1 - 1) * 16 - (k2 - 1) * 4;
|
||||
m = k1 * 100 + k2 * 10 + k3;
|
||||
print(" " + m + " ");
|
||||
}
|
||||
|
||||
function select_move() {
|
||||
if (i % 4 <= 1) {
|
||||
a = 1;
|
||||
} else {
|
||||
a = 2;
|
||||
}
|
||||
for (j = a; j <= 5 - a; j += 5 - 2 * a) {
|
||||
if (xa[ma[i][j]] == s)
|
||||
break;
|
||||
}
|
||||
if (j > 5 - a)
|
||||
return false;
|
||||
xa[ma[i][j]] = s;
|
||||
m = ma[i][j];
|
||||
print("MACHINE TAKES");
|
||||
show_square(m);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Main control section
|
||||
async function main()
|
||||
{
|
||||
print(tab(33) + "QUBIC\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
while (1) {
|
||||
print("DO YOU WANT INSTRUCTIONS");
|
||||
str = await input();
|
||||
str = str.substr(0, 1);
|
||||
if (str == "Y" || str == "N")
|
||||
break;
|
||||
print("INCORRECT ANSWER. PLEASE TYPE 'YES' OR 'NO'");
|
||||
}
|
||||
if (str == "Y") {
|
||||
print("\n");
|
||||
print("THE GAME IS TIC-TAC-TOE IN A 4 X 4 X 4 CUBE.\n");
|
||||
print("EACH MOVE IS INDICATED BY A 3 DIGIT NUMBER, WITH EACH\n");
|
||||
print("DIGIT BETWEEN 1 AND 4 INCLUSIVE. THE DIGITS INDICATE THE\n");
|
||||
print("LEVEL, ROW, AND COLUMN, RESPECTIVELY, OF THE OCCUPIED\n");
|
||||
print("PLACE. \n");
|
||||
print("\n");
|
||||
print("TO PRINT THE PLAYING BOARD, TYPE 0 (ZERO) AS YOUR MOVE.\n");
|
||||
print("THE PROGRAM WILL PRINT THE BOARD WITH YOUR MOVES INDI-\n");
|
||||
print("CATED WITH A (Y), THE MACHINE'S MOVES WITH AN (M), AND\n");
|
||||
print("UNUSED SQUARES WITH A ( ). OUTPUT IS ON PAPER.\n");
|
||||
print("\n");
|
||||
print("TO STOP THE PROGRAM RUN, TYPE 1 AS YOUR MOVE.\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
}
|
||||
while (1) {
|
||||
for (i = 1; i <= 64; i++)
|
||||
xa[i] = 0;
|
||||
z = 1;
|
||||
print("DO YOU WANT TO MOVE FIRST");
|
||||
while (1) {
|
||||
str = await input();
|
||||
str = str.substr(0, 1);
|
||||
if (str == "Y" || str == "N")
|
||||
break;
|
||||
print("INCORRECT ANSWER. PLEASE TYPE 'YES' OR 'NO'");
|
||||
}
|
||||
while (1) {
|
||||
while (1) {
|
||||
print(" \n");
|
||||
print("YOUR MOVE");
|
||||
j1 = parseInt(await input());
|
||||
if (j1 == 0) {
|
||||
show_board();
|
||||
continue;
|
||||
}
|
||||
if (j1 == 1)
|
||||
return;
|
||||
k1 = Math.floor(j1 / 100);
|
||||
j2 = j1 - k1 * 100;
|
||||
k2 = Math.floor(j2 / 10);
|
||||
k3 = j2 - k2 * 10;
|
||||
m = 16 * k1 + 4 * k2 + k3 - 20;
|
||||
if (k1 < 1 || k2 < 1 || k3 < 1 || k1 > 4 || k2 > 4 || k3 >> 4) {
|
||||
print("INCORRECT MOVE, RETYPE IT--");
|
||||
} else {
|
||||
process_board();
|
||||
if (xa[m] != 0) {
|
||||
print("THAT SQUARE IS USED, TRY AGAIN.\n");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
xa[m] = 1;
|
||||
check_for_lines();
|
||||
status = 0;
|
||||
for (j = 1; j <= 3; j++) {
|
||||
for (i = 1; i <= 76; i++) {
|
||||
if (j == 1) {
|
||||
if (la[i] != 4)
|
||||
continue;
|
||||
print("YOU WIN AS FOLLOWS");
|
||||
for (j = 1; j <= 4; j++) {
|
||||
m = ma[i][j];
|
||||
show_square(m);
|
||||
}
|
||||
status = 1;
|
||||
break;
|
||||
}
|
||||
if (j == 2) {
|
||||
if (la[i] != 15)
|
||||
continue;
|
||||
for (j = 1; j <= 4; j++) {
|
||||
m = ma[i][j];
|
||||
if (xa[m] != 0)
|
||||
continue;
|
||||
xa[m] = 5;
|
||||
print("MACHINE MOVES TO ");
|
||||
show_square(m);
|
||||
}
|
||||
print(", AND WINS AS FOLLOWS");
|
||||
for (j = 1; j <= 4; j++) {
|
||||
m = ma[i][j];
|
||||
show_square(m);
|
||||
}
|
||||
status = 1;
|
||||
break;
|
||||
}
|
||||
if (j == 3) {
|
||||
if (la[i] != 3)
|
||||
continue;
|
||||
print("NICE TRY, MACHINE MOVES TO");
|
||||
for (j = 1; j <= 4; j++) {
|
||||
m = ma[i][j];
|
||||
if (xa[m] != 0)
|
||||
continue;
|
||||
xa[m] = 5;
|
||||
show_square(m);
|
||||
status = 2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i <= 76)
|
||||
break;
|
||||
}
|
||||
if (status == 2)
|
||||
continue;
|
||||
if (status == 1)
|
||||
break;
|
||||
// x = x; non-useful in original
|
||||
i = 1;
|
||||
do {
|
||||
la[i] = xa[ma[i][1]] + xa[ma[i][2]] + xa[ma[i][3]] + xa[ma[i][4]];
|
||||
l = la[i];
|
||||
if (l == 10) {
|
||||
for (j = 1; j <= 4; j++) {
|
||||
if (xa[ma[i][j]] == 0)
|
||||
xa[ma[i][j]] = 1 / 8;
|
||||
}
|
||||
}
|
||||
} while (++i <= 76) ;
|
||||
check_for_lines();
|
||||
i = 1;
|
||||
do {
|
||||
if (la[i] == 0.5) {
|
||||
s = 1 / 8;
|
||||
select_move();
|
||||
break;
|
||||
}
|
||||
if (la[i] == 5 + 3 / 8) {
|
||||
s = 1 / 8;
|
||||
select_move();
|
||||
break;
|
||||
}
|
||||
} while (++i <= 76) ;
|
||||
if (i <= 76)
|
||||
continue;
|
||||
|
||||
process_board();
|
||||
|
||||
i = 1;
|
||||
do {
|
||||
la[i] = xa[ma[i][1]] + xa[ma[i][2]] + xa[ma[i][3]] + xa[ma[i][4]];
|
||||
l = la[i];
|
||||
if (l == 2) {
|
||||
for (j = 1; j <= 4; j++) {
|
||||
if (xa[ma[i][j]] == 0)
|
||||
xa[ma[i][j]] = 1 / 8;
|
||||
}
|
||||
}
|
||||
} while (++i <= 76) ;
|
||||
check_for_lines();
|
||||
i = 1;
|
||||
do {
|
||||
if (la[i] == 0.5) {
|
||||
s = 1 / 8;
|
||||
select_move();
|
||||
break;
|
||||
}
|
||||
if (la[i] == 1 + 3 / 8) {
|
||||
s = 1 / 8;
|
||||
select_move();
|
||||
break;
|
||||
}
|
||||
} while (++i <= 76) ;
|
||||
if (i <= 76)
|
||||
continue;
|
||||
|
||||
for (k = 1; k <= 18; k++) {
|
||||
p = 0;
|
||||
for (i = 4 * k - 3; i <= 4 * k; i++) {
|
||||
for (j = 1; j <= 4; j++)
|
||||
p += xa[ma[i][j]];
|
||||
}
|
||||
if (p == 4 || p == 9) {
|
||||
s = 1 / 8;
|
||||
for (i = 4 * k - 3; i <= 4 * k; i++) {
|
||||
if (select_move())
|
||||
break;
|
||||
}
|
||||
s = 0;
|
||||
}
|
||||
}
|
||||
if (k <= 18)
|
||||
continue
|
||||
process_board();
|
||||
z = 1;
|
||||
do {
|
||||
if (xa[ya[z]] == 0)
|
||||
break;
|
||||
} while (++z < 17) ;
|
||||
if (z >= 17) {
|
||||
for (i = 1; i <= 64; i++) {
|
||||
if (xa[i] == 0) {
|
||||
xa[i] = 5;
|
||||
m = i;
|
||||
print("MACHINE LIKES");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i > 64) {
|
||||
print("THE GAME IS A DRAW.\n");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
m = ya[z];
|
||||
xa[m] = 5;
|
||||
print("MACHINE MOVES TO");
|
||||
}
|
||||
show_square(m);
|
||||
}
|
||||
print(" \n");
|
||||
print("DO YOU WANT TO TRY ANOTHER GAME");
|
||||
while (1) {
|
||||
str = await input();
|
||||
str = str.substr(0, 1);
|
||||
if (str == "Y" || str == "N")
|
||||
break;
|
||||
print("INCORRECT ANSWER. PLEASE TYPE 'YES' OR 'NO'");
|
||||
}
|
||||
if (str == "N")
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
9
89 Tic-Tac-Toe/javascript/tictactoe1.html
Normal file
9
89 Tic-Tac-Toe/javascript/tictactoe1.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>TIC TAC TOE 1</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="tictactoe1.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
147
89 Tic-Tac-Toe/javascript/tictactoe1.js
Normal file
147
89 Tic-Tac-Toe/javascript/tictactoe1.js
Normal file
@@ -0,0 +1,147 @@
|
||||
// TIC TAC TOE 1
|
||||
//
|
||||
// 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 mf(x)
|
||||
{
|
||||
return x - 8 * Math.floor((x - 1) / 8);
|
||||
}
|
||||
|
||||
function computer_moves()
|
||||
{
|
||||
print("COMPUTER MOVES " + m + "\n");
|
||||
}
|
||||
|
||||
var m;
|
||||
|
||||
// Main control section
|
||||
async function main()
|
||||
{
|
||||
print(tab(30) + "TIC TAC TOE\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
//
|
||||
// This program plays Tic Tac Toe
|
||||
// The machine goes first
|
||||
print("THE GAME BOARD IS NUMBERED:\n");
|
||||
print("\n");
|
||||
print("1 2 3\n");
|
||||
print("8 9 4\n");
|
||||
print("7 6 5\n");
|
||||
print("\n");
|
||||
//
|
||||
// Main program
|
||||
while (1) {
|
||||
print("\n");
|
||||
print("\n");
|
||||
a = 9;
|
||||
m = a;
|
||||
|
||||
computer_moves();
|
||||
print("YOUR MOVE");
|
||||
m = parseInt(await input());
|
||||
|
||||
p = m;
|
||||
b = mf(p + 1);
|
||||
m = b;
|
||||
|
||||
computer_moves();
|
||||
print("YOUR MOVE");
|
||||
m = parseInt(await input());
|
||||
|
||||
q = m;
|
||||
if (q != mf(b + 4)) {
|
||||
c = mf(b + 4);
|
||||
m = c;
|
||||
computer_moves();
|
||||
print("AND WINS ********\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
c = mf(b + 2);
|
||||
m = c;
|
||||
|
||||
computer_moves();
|
||||
print("YOUR MOVE");
|
||||
m = parseInt(await input());
|
||||
|
||||
r = m;
|
||||
if (r != mf(c + 4)) {
|
||||
d = mf(c + 4);
|
||||
m = d;
|
||||
computer_moves();
|
||||
print("AND WINS ********\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (p % 2 == 0) {
|
||||
d = mf(c + 7);
|
||||
m = d;
|
||||
computer_moves();
|
||||
print("AND WINS ********\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
d = mf(c + 3);
|
||||
m = d;
|
||||
|
||||
computer_moves();
|
||||
print("YOUR MOVE");
|
||||
m = parseInt(await input());
|
||||
|
||||
s = m;
|
||||
if (s != mf(d + 4)) {
|
||||
e = mf(d + 4);
|
||||
m = e;
|
||||
computer_moves();
|
||||
}
|
||||
e = mf(d + 6);
|
||||
m = e;
|
||||
computer_moves();
|
||||
print("THE GAME IS A DRAW.\n");
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
9
89 Tic-Tac-Toe/javascript/tictactoe2.html
Normal file
9
89 Tic-Tac-Toe/javascript/tictactoe2.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>TIC TAC TOE 2</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="tictactoe2.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
288
89 Tic-Tac-Toe/javascript/tictactoe2.js
Normal file
288
89 Tic-Tac-Toe/javascript/tictactoe2.js
Normal file
@@ -0,0 +1,288 @@
|
||||
// TIC TAC TOE 2
|
||||
//
|
||||
// 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 = [];
|
||||
|
||||
function who_win(piece)
|
||||
{
|
||||
if (piece == -1) {
|
||||
print("I WIN, TURKEY!!!\n");
|
||||
} else if (piece == 1) {
|
||||
print("YOU BEAT ME!! GOOD GAME.\n");
|
||||
}
|
||||
}
|
||||
|
||||
function show_board()
|
||||
{
|
||||
print("\n");
|
||||
for (i = 1; i <= 9; i++) {
|
||||
print(" ");
|
||||
if (s[i] == -1) {
|
||||
print(qs + " ");
|
||||
} else if (s[i] == 0) {
|
||||
print(" ");
|
||||
} else {
|
||||
print(ps + " ");
|
||||
}
|
||||
if (i == 3 || i == 6) {
|
||||
print("\n");
|
||||
print("---+---+---\n");
|
||||
} else if (i != 9) {
|
||||
print("!");
|
||||
}
|
||||
}
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
for (i = 1; i <= 7; i += 3) {
|
||||
if (s[i] && s[i] == s[i + 1] && s[i] == s[i + 2]) {
|
||||
who_win(s[i]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (i = 1; i <= 3; i++) {
|
||||
if (s[i] && s[i] == s[i + 3] && s[i] == s[i + 6]) {
|
||||
who_win(s[i]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (s[1] && s[1] == s[5] && s[1] == s[9]) {
|
||||
who_win(s[1]);
|
||||
return true;
|
||||
}
|
||||
if (s[3] && s[3] == s[5] && s[3] == s[7]) {
|
||||
who_win(s[3]);
|
||||
return true;
|
||||
}
|
||||
for (i = 1; i <= 9; i++) {
|
||||
if (s[i] == 0)
|
||||
break;
|
||||
}
|
||||
if (i > 9) {
|
||||
print("IT'S A DRAW. THANK YOU.\n");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Main control section
|
||||
async function main()
|
||||
{
|
||||
print(tab(30) + "TIC-TAC-TOE\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
for (i = 1; i <= 9; i++)
|
||||
s[i] = 0;
|
||||
print("THE BOARD IS NUMBERED:\n");
|
||||
print(" 1 2 3\n");
|
||||
print(" 4 5 6\n");
|
||||
print(" 7 8 9\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("DO YOU WANT 'X' OR 'O'");
|
||||
str = await input();
|
||||
if (str == "X") {
|
||||
ps = "X";
|
||||
qs = "O";
|
||||
first_time = true;
|
||||
} else {
|
||||
ps = "O";
|
||||
qs = "X";
|
||||
first_time = false;
|
||||
}
|
||||
while (1) {
|
||||
if (!first_time) {
|
||||
g = -1;
|
||||
h = 1;
|
||||
if (s[5] == 0) {
|
||||
s[5] = -1;
|
||||
} else if (s[5] == 1 && s[1] == 0) {
|
||||
s[1] = -1;
|
||||
} else if (s[5] != 1 && s[2] == 1 && s[1] == 0 || s[5] != 1 && s[4] == 1 && s[1] == 0) {
|
||||
s[1] = -1;
|
||||
} else if (s[5] != 1 && s[6] == 1 && s[9] == 0 || s[5] != 1 && s[8] == 1 && s[9] == 0) {
|
||||
s[9] = -1;
|
||||
} else {
|
||||
while (1) {
|
||||
played = false;
|
||||
if (g == 1) {
|
||||
j = 3 * Math.floor((m - 1) / 3) + 1;
|
||||
if (3 * Math.floor((m - 1) / 3) + 1 == m)
|
||||
k = 1;
|
||||
if (3 * Math.floor((m - 1) / 3) + 2 == m)
|
||||
k = 2;
|
||||
if (3 * Math.floor((m - 1) / 3) + 3 == m)
|
||||
k = 3;
|
||||
} else {
|
||||
j = 1;
|
||||
k = 1;
|
||||
}
|
||||
while (1) {
|
||||
if (s[j] == g) {
|
||||
if (s[j + 2] == g) {
|
||||
if (s[j + 1] == 0) {
|
||||
s[j + 1] = -1;
|
||||
played = true;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (s[j + 2] == 0 && s[j + 1] == g) {
|
||||
s[j + 2] = -1;
|
||||
played = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (s[j] != h && s[j + 2] == g && s[j + 1] == g) {
|
||||
s[j] = -1;
|
||||
played = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (s[k] == g) {
|
||||
if (s[k + 6] == g) {
|
||||
if (s[k + 3] == 0) {
|
||||
s[k + 3] = -1;
|
||||
played = true;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (s[k + 6] == 0 && s[k + 3] == g) {
|
||||
s[k + 6] = -1;
|
||||
played = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (s[k] != h && s[k + 6] == g && s[k + 3] == g) {
|
||||
s[k] = -1;
|
||||
played = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (g == 1)
|
||||
break;
|
||||
if (j == 7 && k == 3)
|
||||
break;
|
||||
k++;
|
||||
if (k > 3) {
|
||||
k = 1;
|
||||
j += 3;
|
||||
if (j > 7)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!played) {
|
||||
if (s[5] == g) {
|
||||
if (s[3] == g && s[7] == 0) {
|
||||
s[7] = -1;
|
||||
played = true;
|
||||
} else if (s[9] == g && s[1] == 0) {
|
||||
s[1] = -1;
|
||||
played = true;
|
||||
} else if (s[7] == g && s[3] == 0) {
|
||||
s[3] = -1;
|
||||
played = true;
|
||||
} else if (s[9] == 0 && s[1] == g) {
|
||||
s[9] = -1;
|
||||
played = true;
|
||||
}
|
||||
}
|
||||
if (!played) {
|
||||
if (g == -1) {
|
||||
g = 1;
|
||||
h = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (played)
|
||||
break;
|
||||
}
|
||||
if (!played) {
|
||||
if (s[9] == 1 && s[3] == 0 && s[1] != 1) {
|
||||
s[3] = -1;
|
||||
} else {
|
||||
for (i = 2; i <= 9; i++) {
|
||||
if (s[i] == 0) {
|
||||
s[i] = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i > 9) {
|
||||
s[1] = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
print("\n");
|
||||
print("THE COMPUTER MOVES TO...");
|
||||
if (show_board())
|
||||
break;
|
||||
}
|
||||
first_time = false;
|
||||
while (1) {
|
||||
print("\n");
|
||||
print("WHERE DO YOU MOVE");
|
||||
m = parseInt(await input());
|
||||
if (m == 0) {
|
||||
print("THANKS FOR THE GAME.\n");
|
||||
break;
|
||||
}
|
||||
if (m >= 1 && m <= 9 && s[m] == 0)
|
||||
break;
|
||||
print("THAT SQUARE IS OCCUPIED.\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
}
|
||||
g = 1;
|
||||
s[m] = 1;
|
||||
if (show_board())
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
9
90 Tower/javascript/tower.html
Normal file
9
90 Tower/javascript/tower.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>TOWER</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="tower.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
254
90 Tower/javascript/tower.js
Normal file
254
90 Tower/javascript/tower.js
Normal file
@@ -0,0 +1,254 @@
|
||||
// TOWER
|
||||
//
|
||||
// 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 ta = [];
|
||||
|
||||
// Print subroutine
|
||||
function show_towers()
|
||||
{
|
||||
var z;
|
||||
|
||||
for (var k = 1; k <= 7; k++) {
|
||||
z = 10;
|
||||
str = "";
|
||||
for (var j = 1; j <= 3; j++) {
|
||||
if (ta[k][j] != 0) {
|
||||
while (str.length < z - Math.floor(ta[k][j] / 2))
|
||||
str += " ";
|
||||
for (v = 1; v <= ta[k][j]; v++)
|
||||
str += "*";
|
||||
} else {
|
||||
while (str.length < z)
|
||||
str += " ";
|
||||
str += "*";
|
||||
}
|
||||
z += 21;
|
||||
}
|
||||
print(str + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Main control section
|
||||
async function main()
|
||||
{
|
||||
print(tab(33) + "TOWERS\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
while (1) {
|
||||
print("\n");
|
||||
// Initialize
|
||||
e = 0;
|
||||
for (d = 1; d <= 7; d++) {
|
||||
ta[d] = [];
|
||||
for (n = 1; n <= 3; n++)
|
||||
ta[d][n] = 0;
|
||||
}
|
||||
print("TOWERS OF HANOI PUZZLE.\n");
|
||||
print("\n");
|
||||
print("YOU MUST TRANSFER THE DISKS FROM THE LEFT TO THE RIGHT\n");
|
||||
print("TOWER, ONE AT A TIME, NEVER PUTTING A LARGER DISK ON A\n");
|
||||
print("SMALLER DISK.\n");
|
||||
print("\n");
|
||||
while (1) {
|
||||
print("HOW MANY DISKS DO YOU WANT TO MOVE (7 IS MAX)");
|
||||
s = parseInt(await input());
|
||||
print("\n");
|
||||
m = 0;
|
||||
if (s >= 1 && s <= 7)
|
||||
break;
|
||||
e++;
|
||||
if (e < 2) {
|
||||
print("SORRY, BUT I CAN'T DO THAT JOB FOR YOU.\n");
|
||||
continue;
|
||||
}
|
||||
print("ALL RIGHT, WISE GUY, IF YOU CAN'T PLAY THE GAME RIGHT, I'LL\n");
|
||||
print("JUST TAKE MY PUZZLE AND GO HOME. SO LONG.\n");
|
||||
return;
|
||||
}
|
||||
// Store disks from smallest to largest
|
||||
print("IN THIS PROGRAM, WE SHALL REFER TO DISKS BY NUMERICAL CODE.\n");
|
||||
print("3 WILL REPRESENT THE SMALLEST DISK, 5 THE NEXT SIZE,\n");
|
||||
print("7 THE NEXT, AND SO ON, UP TO 15. IF YOU DO THE PUZZLE WITH\n");
|
||||
print("2 DISKS, THEIR CODE NAMES WOULD BE 13 AND 15. WITH 3 DISKS\n");
|
||||
print("THE CODE NAMES WOULD BE 11, 13 AND 15, ETC. THE NEEDLES\n");
|
||||
print("ARE NUMBERED FROM LEFT TO RIGHT, 1 TO 3. WE WILL\n");
|
||||
print("START WITH THE DISKS ON NEEDLE 1, AND ATTEMPT TO MOVE THEM\n");
|
||||
print("TO NEEDLE 3.\n");
|
||||
print("\n");
|
||||
print("GOOD LUCK!\n");
|
||||
print("\n");
|
||||
y = 7;
|
||||
d = 15;
|
||||
for (x = s; x >= 1; x--) {
|
||||
ta[y][1] = d;
|
||||
d -= 2;
|
||||
y--;
|
||||
}
|
||||
show_towers();
|
||||
while (1) {
|
||||
print("WHICH DISK WOULD YOU LIKE TO MOVE");
|
||||
e = 0;
|
||||
while (1) {
|
||||
d = parseInt(await input());
|
||||
if (d % 2 == 0 || d < 3 || d > 15) {
|
||||
print("ILLEGAL ENTRY... YOU MAY ONLY TYPE 3,5,7,9,11,13, OR 15.\n");
|
||||
e++;
|
||||
if (e <= 1)
|
||||
continue;
|
||||
print("STOP WASTING MY TIME. GO BOTHER SOMEONE ELSE.\n");
|
||||
return;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Check if requested disk is below another
|
||||
for (r = 1; r <= 7; r++) {
|
||||
for (c = 1; c <= 3; c++) {
|
||||
if (ta[r][c] == d)
|
||||
break;
|
||||
}
|
||||
if (c <= 3)
|
||||
break;
|
||||
}
|
||||
for (q = r; q >= 1; q--) {
|
||||
if (ta[q][c] != 0 && ta[q][c] < d)
|
||||
break;
|
||||
}
|
||||
if (q >= 1) {
|
||||
print("THAT DISK IS BELOW ANOTHER ONE. MAKE ANOTHER CHOICE.\n");
|
||||
continue;
|
||||
}
|
||||
e = 0;
|
||||
while (1) {
|
||||
print("PLACE DISK ON WHICH NEEDLE");
|
||||
n = parseInt(await input());
|
||||
if (n >= 1 && n <= 3)
|
||||
break;
|
||||
e++;
|
||||
if (e <= 1) {
|
||||
print("I'LL ASSUME YOU HIT THE WRONG KEY THI TIME. BUT WATCH IT,\n");
|
||||
print("I ONLY ALLOW ONE MISTAKE.\n");
|
||||
continue;
|
||||
} else {
|
||||
print("I TRIED TO WARN YOU, BUT YOU WOULDN'T LISTEN.\n");
|
||||
print("BYE BYE, BIG SHOT.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Check if requested disk is below another
|
||||
for (r = 1; r <= 7; r++) {
|
||||
if (ta[r][n] != 0)
|
||||
break;
|
||||
}
|
||||
if (r <= 7) {
|
||||
// Check if disk to be placed on a larger one
|
||||
if (d >= ta[r][n]) {
|
||||
print("YOU CAN'T PLACE A LARGER DISK ON TOP OF A SMALLER ONE,\n");
|
||||
print("IT MIGHT CRUSH IT!\n");
|
||||
print("NOW THEN, ");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Move relocated disk
|
||||
for (v = 1; v <= 7; v++) {
|
||||
for (w = 1; w <= 3; w++) {
|
||||
if (ta[v][w] == d)
|
||||
break;
|
||||
}
|
||||
if (w <= 3)
|
||||
break;
|
||||
}
|
||||
// Locate empty space on needle n
|
||||
for (u = 1; u <= 7; u++) {
|
||||
if (ta[u][n] != 0)
|
||||
break;
|
||||
}
|
||||
ta[--u][n] = ta[v][w];
|
||||
ta[v][w] = 0;
|
||||
// Print out current status
|
||||
show_towers();
|
||||
// Check if done
|
||||
m++;
|
||||
for (r = 1; r <= 7; r++) {
|
||||
for (c = 1; c <= 2; c++) {
|
||||
if (ta[r][c] != 0)
|
||||
break;
|
||||
}
|
||||
if (c <= 2)
|
||||
break;
|
||||
}
|
||||
if (r > 7)
|
||||
break;
|
||||
if (m > 128) {
|
||||
print("SORRY, BUT I HAVE ORDERS TO STOP IF YOU MAKE MORE THAN\n");
|
||||
print("128 MOVES.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (m == Math.pow(2, s) - 1) {
|
||||
print("\n");
|
||||
print("CONGRATULATIONS!!\n");
|
||||
print("\n");
|
||||
}
|
||||
print("YOU HAVE PERFORMED THE TASK IN " + m + " MOVES.\n");
|
||||
print("\n");
|
||||
print("TRY AGAIN (YES OR NO)");
|
||||
while (1) {
|
||||
str = await input();
|
||||
if (str == "YES" || str == "NO")
|
||||
break;
|
||||
print("\n");
|
||||
print("'YES' OR 'NO' PLEASE");
|
||||
}
|
||||
if (str == "NO")
|
||||
break;
|
||||
}
|
||||
print("\n");
|
||||
print("THANKS FOR THE GAME!\n");
|
||||
print("\n");
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user