mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-03 16:48:04 -08:00
Removed spaces from top-level directory names.
Spaces tend to cause annoyances in a Unix-style shell environment. This change fixes that.
This commit is contained in:
7
34_Digits/README.md
Normal file
7
34_Digits/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
### Digits
|
||||
|
||||
As published in Basic Computer Games (1978)
|
||||
https://www.atariarchives.org/basicgames/showpage.php?page=58
|
||||
|
||||
Downloaded from Vintage Basic at
|
||||
http://www.vintage-basic.net/games.html
|
||||
3
34_Digits/csharp/README.md
Normal file
3
34_Digits/csharp/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Microsoft C#](https://docs.microsoft.com/en-us/dotnet/csharp/)
|
||||
78
34_Digits/digits.bas
Normal file
78
34_Digits/digits.bas
Normal file
@@ -0,0 +1,78 @@
|
||||
10 PRINT TAB(33);"DIGITS"
|
||||
20 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
|
||||
30 PRINT:PRINT:PRINT
|
||||
210 PRINT "THIS IS A GAME OF GUESSING."
|
||||
220 PRINT "FOR INSTRUCTIONS, TYPE '1', ELSE TYPE '0'";
|
||||
230 INPUT E
|
||||
240 IF E=0 THEN 360
|
||||
250 PRINT
|
||||
260 PRINT "PLEASE TAKE A PIECE OF PAPER AND WRITE DOWN"
|
||||
270 PRINT "THE DIGITS '0', '1', OR '2' THIRTY TIMES AT RANDOM."
|
||||
280 PRINT "ARRANGE THEM IN THREE LINES OF TEN DIGITS EACH."
|
||||
290 PRINT "I WILL ASK FOR THEN TEN AT A TIME."
|
||||
300 PRINT "I WILL ALWAYS GUESS THEM FIRST AND THEN LOOK AT YOUR"
|
||||
310 PRINT "NEXT NUMBER TO SEE IF I WAS RIGHT. BY PURE LUCK,"
|
||||
320 PRINT "I OUGHT TO BE RIGHT TEN TIMES. BUT I HOPE TO DO BETTER"
|
||||
330 PRINT "THAN THAT *****"
|
||||
340 PRINT:PRINT
|
||||
360 READ A,B,C
|
||||
370 DATA 0,1,3
|
||||
380 DIM M(26,2),K(2,2),L(8,2)
|
||||
400 FOR I=0 TO 26: FOR J=0 TO 2: M(I,J)=1: NEXT J: NEXT I
|
||||
410 FOR I=0 TO 2: FOR J=0 TO 2: K(I,J)=9: NEXT J: NEXT I
|
||||
420 FOR I=0 TO 8: FOR J=0 TO 2: L(I,J)=3: NEXT J: NEXT I
|
||||
450 L(0,0)=2: L(4,1)=2: L(8,2)=2
|
||||
480 Z=26: Z1=8: Z2=2
|
||||
510 X=0
|
||||
520 FOR T=1 TO 3
|
||||
530 PRINT
|
||||
540 PRINT "TEN NUMBERS, PLEASE";
|
||||
550 INPUT N(1),N(2),N(3),N(4),N(5),N(6),N(7),N(8),N(9),N(10)
|
||||
560 FOR I=1 TO 10
|
||||
570 W=N(I)-1
|
||||
580 IF W=SGN(W) THEN 620
|
||||
590 PRINT "ONLY USE THE DIGITS '0', '1', OR '2'."
|
||||
600 PRINT "LET'S TRY AGAIN.":GOTO 530
|
||||
620 NEXT I
|
||||
630 PRINT: PRINT "MY GUESS","YOUR NO.","RESULT","NO. RIGHT":PRINT
|
||||
660 FOR U=1 TO 10
|
||||
670 N=N(U): S=0
|
||||
690 FOR J=0 TO 2
|
||||
700 S1=A*K(Z2,J)+B*L(Z1,J)+C*M(Z,J)
|
||||
710 IF S>S1 THEN 760
|
||||
720 IF S<S1 THEN 740
|
||||
730 IF RND(1)<.5 THEN 760
|
||||
740 S=S1: G=J
|
||||
760 NEXT J
|
||||
770 PRINT " ";G," ";N(U),
|
||||
780 IF G=N(U) THEN 810
|
||||
790 PRINT " WRONG",X
|
||||
800 GOTO 880
|
||||
810 X=X+1
|
||||
820 PRINT " RIGHT",X
|
||||
830 M(Z,N)=M(Z,N)+1
|
||||
840 L(Z1,N)=L(Z1,N)+1
|
||||
850 K(Z2,N)=K(Z2,N)+1
|
||||
860 Z=Z-INT(Z/9)*9
|
||||
870 Z=3*Z+N(U)
|
||||
880 Z1=Z-INT(Z/9)*9
|
||||
890 Z2=N(U)
|
||||
900 NEXT U
|
||||
910 NEXT T
|
||||
920 PRINT
|
||||
930 IF X>10 THEN 980
|
||||
940 IF X<10 THEN 1010
|
||||
950 PRINT "I GUESSED EXACTLY 1/3 OF YOUR NUMBERS."
|
||||
960 PRINT "IT'S A TIE GAME."
|
||||
970 GOTO 1030
|
||||
980 PRINT "I GUESSED MORE THAN 1/3 OF YOUR NUMBERS."
|
||||
990 PRINT "I WIN.": FOR Q=1 TO 10: PRINT CHR$(7);: NEXT Q
|
||||
1000 GOTO 1030
|
||||
1010 PRINT "I GUESSED LESS THAN 1/3 OF YOUR NUMBERS."
|
||||
1020 PRINT "YOU BEAT ME. CONGRATULATIONS *****"
|
||||
1030 PRINT
|
||||
1040 PRINT "DO YOU WANT TO TRY AGAIN (1 FOR YES, 0 FOR NO)";
|
||||
1060 INPUT X
|
||||
1070 IF X=1 THEN 400
|
||||
1080 PRINT:PRINT "THANKS FOR THE GAME."
|
||||
1090 END
|
||||
186
34_Digits/java/Digits.java
Normal file
186
34_Digits/java/Digits.java
Normal file
@@ -0,0 +1,186 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* DIGITS
|
||||
* <p>
|
||||
* Converted from BASIC to Java by Aldrin Misquitta (@aldrinm)
|
||||
*/
|
||||
public class Digits {
|
||||
|
||||
public static void main(String[] args) {
|
||||
printIntro();
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
boolean showInstructions = readInstructionChoice(scan);
|
||||
if (showInstructions) {
|
||||
printInstructions();
|
||||
}
|
||||
|
||||
int a = 0, b = 1, c = 3;
|
||||
int[][] m = new int[27][3];
|
||||
int[][] k = new int[3][3];
|
||||
int[][] l = new int[9][3];
|
||||
|
||||
boolean continueGame = true;
|
||||
while (continueGame) {
|
||||
for (int[] ints : m) {
|
||||
Arrays.fill(ints, 1);
|
||||
}
|
||||
for (int[] ints : k) {
|
||||
Arrays.fill(ints, 9);
|
||||
}
|
||||
for (int[] ints : l) {
|
||||
Arrays.fill(ints, 3);
|
||||
}
|
||||
|
||||
l[0][0] = 2;
|
||||
l[4][1] = 2;
|
||||
l[8][2] = 2;
|
||||
|
||||
int z = 26, z1 = 8, z2 = 2, runningCorrect = 0;
|
||||
|
||||
for (int t = 1; t <= 3; t++) {
|
||||
boolean validNumbers = false;
|
||||
int[] numbers = new int[0];
|
||||
while (!validNumbers) {
|
||||
System.out.println();
|
||||
numbers = read10Numbers(scan);
|
||||
validNumbers = true;
|
||||
for (int number : numbers) {
|
||||
if (number < 0 || number > 2) {
|
||||
System.out.println("ONLY USE THE DIGITS '0', '1', OR '2'.");
|
||||
System.out.println("LET'S TRY AGAIN.");
|
||||
validNumbers = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.printf("\n%-14s%-14s%-14s%-14s", "MY GUESS", "YOUR NO.", "RESULT", "NO. RIGHT");
|
||||
for (int number : numbers) {
|
||||
int s = 0;
|
||||
int myGuess = 0;
|
||||
for (int j = 0; j <= 2; j++) {
|
||||
//What did the original author have in mind ? The first expression always results in 0 because a is always 0
|
||||
int s1 = a * k[z2][j] + b * l[z1][j] + c * m[z][j];
|
||||
if (s < s1) {
|
||||
s = s1;
|
||||
myGuess = j;
|
||||
} else if (s1 == s) {
|
||||
if (Math.random() >= 0.5) {
|
||||
myGuess = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String result;
|
||||
if (myGuess != number) {
|
||||
result = "WRONG";
|
||||
} else {
|
||||
runningCorrect++;
|
||||
result = "RIGHT";
|
||||
m[z][number] = m[z][number] + 1;
|
||||
l[z1][number] = l[z1][number] + 1;
|
||||
k[z2][number] = k[z2][number] + 1;
|
||||
z = z - (z / 9) * 9;
|
||||
z = 3 * z + number;
|
||||
}
|
||||
System.out.printf("\n%-14d%-14d%-14s%-14d", myGuess, number, result, runningCorrect);
|
||||
|
||||
z1 = z - (z / 9) * 9;
|
||||
z2 = number;
|
||||
}
|
||||
}
|
||||
|
||||
//print summary report
|
||||
System.out.println();
|
||||
if (runningCorrect > 10) {
|
||||
System.out.println();
|
||||
System.out.println("I GUESSED MORE THAN 1/3 OF YOUR NUMBERS.");
|
||||
System.out.println("I WIN.\u0007");
|
||||
} else if (runningCorrect < 10) {
|
||||
System.out.println("I GUESSED LESS THAN 1/3 OF YOUR NUMBERS.");
|
||||
System.out.println("YOU BEAT ME. CONGRATULATIONS *****");
|
||||
} else {
|
||||
System.out.println("I GUESSED EXACTLY 1/3 OF YOUR NUMBERS.");
|
||||
System.out.println("IT'S A TIE GAME.");
|
||||
}
|
||||
|
||||
continueGame = readContinueChoice(scan);
|
||||
}
|
||||
|
||||
System.out.println("\nTHANKS FOR THE GAME.");
|
||||
}
|
||||
|
||||
private static boolean readContinueChoice(Scanner scan) {
|
||||
System.out.print("\nDO YOU WANT TO TRY AGAIN (1 FOR YES, 0 FOR NO) ? ");
|
||||
int choice;
|
||||
try {
|
||||
choice = scan.nextInt();
|
||||
return choice == 1;
|
||||
} catch (InputMismatchException ex) {
|
||||
return false;
|
||||
} finally {
|
||||
scan.nextLine();
|
||||
}
|
||||
}
|
||||
|
||||
private static int[] read10Numbers(Scanner scan) {
|
||||
System.out.print("TEN NUMBERS, PLEASE ? ");
|
||||
int[] numbers = new int[10];
|
||||
|
||||
for (int i = 0; i < numbers.length; i++) {
|
||||
boolean validInput = false;
|
||||
while (!validInput) {
|
||||
try {
|
||||
int n = scan.nextInt();
|
||||
validInput = true;
|
||||
numbers[i] = n;
|
||||
} catch (InputMismatchException ex) {
|
||||
System.out.println("!NUMBER EXPECTED - RETRY INPUT LINE");
|
||||
} finally {
|
||||
scan.nextLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return numbers;
|
||||
}
|
||||
|
||||
private static void printInstructions() {
|
||||
System.out.println("\n");
|
||||
System.out.println("PLEASE TAKE A PIECE OF PAPER AND WRITE DOWN");
|
||||
System.out.println("THE DIGITS '0', '1', OR '2' THIRTY TIMES AT RANDOM.");
|
||||
System.out.println("ARRANGE THEM IN THREE LINES OF TEN DIGITS EACH.");
|
||||
System.out.println("I WILL ASK FOR THEN TEN AT A TIME.");
|
||||
System.out.println("I WILL ALWAYS GUESS THEM FIRST AND THEN LOOK AT YOUR");
|
||||
System.out.println("NEXT NUMBER TO SEE IF I WAS RIGHT. BY PURE LUCK,");
|
||||
System.out.println("I OUGHT TO BE RIGHT TEN TIMES. BUT I HOPE TO DO BETTER");
|
||||
System.out.println("THAN THAT *****");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
private static boolean readInstructionChoice(Scanner scan) {
|
||||
System.out.print("FOR INSTRUCTIONS, TYPE '1', ELSE TYPE '0' ? ");
|
||||
int choice;
|
||||
try {
|
||||
choice = scan.nextInt();
|
||||
return choice == 1;
|
||||
} catch (InputMismatchException ex) {
|
||||
return false;
|
||||
} finally {
|
||||
scan.nextLine();
|
||||
}
|
||||
}
|
||||
|
||||
private static void printIntro() {
|
||||
System.out.println(" DIGITS");
|
||||
System.out.println(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println("\n\n");
|
||||
System.out.println("THIS IS A GAME OF GUESSING.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
3
34_Digits/java/README.md
Normal file
3
34_Digits/java/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Oracle Java](https://openjdk.java.net/)
|
||||
3
34_Digits/javascript/README.md
Normal file
3
34_Digits/javascript/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Shells)
|
||||
9
34_Digits/javascript/digits.html
Normal file
9
34_Digits/javascript/digits.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>DIGITS</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="digits.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
175
34_Digits/javascript/digits.js
Normal file
175
34_Digits/javascript/digits.js
Normal file
@@ -0,0 +1,175 @@
|
||||
// DIGITS
|
||||
//
|
||||
// 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) + "DIGITS\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("THIS IS A GAME OF GUESSING.\n");
|
||||
print("FOR INSTRUCTIONS, TYPE '1', ELSE TYPE '0'");
|
||||
e = parseInt(await input());
|
||||
if (e != 0) {
|
||||
print("\n");
|
||||
print("PLEASE TAKE A PIECE OF PAPER AND WRITE DOWN\n");
|
||||
print("THE DIGITS '0', '1', OR '2' THIRTY TIMES AT RANDOM.\n");
|
||||
print("ARRANGE THEM IN THREE LINES OF TEN DIGITS EACH.\n");
|
||||
print("I WILL ASK FOR THEN TEN AT A TIME.\n");
|
||||
print("I WILL ALWAYS GUESS THEM FIRST AND THEN LOOK AT YOUR\n");
|
||||
print("NEXT NUMBER TO SEE IF I WAS RIGHT. BY PURE LUCK,\n");
|
||||
print("I OUGHT TO BE RIGHT TEN TIMES. BUT I HOPE TO DO BETTER\n");
|
||||
print("THAN THAT *****\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
}
|
||||
a = 0;
|
||||
b = 1;
|
||||
c = 3;
|
||||
m = [];
|
||||
k = [];
|
||||
l = [];
|
||||
n = [];
|
||||
while (1) {
|
||||
for (i = 0; i <= 26; i++) {
|
||||
m[i] = [];
|
||||
for (j = 0; j <= 2; j++) {
|
||||
m[i][j] = 1;
|
||||
}
|
||||
}
|
||||
for (i = 0; i <= 2; i++) {
|
||||
k[i] = [];
|
||||
for (j = 0; j <= 2; j++) {
|
||||
k[i][j] = 9;
|
||||
}
|
||||
}
|
||||
for (i = 0; i <= 8; i++) {
|
||||
l[i] = [];
|
||||
for (j = 0; j <= 2; j++) {
|
||||
l[i][j] = 3;
|
||||
}
|
||||
}
|
||||
l[0][0] = 2;
|
||||
l[4][1] = 2;
|
||||
l[8][2] = 2;
|
||||
z = 26;
|
||||
z1 = 8;
|
||||
z2 = 2;
|
||||
x = 0;
|
||||
for (t = 1; t <= 3; t++) {
|
||||
while (1) {
|
||||
print("\n");
|
||||
print("TEN NUMBERS, PLEASE");
|
||||
str = await input();
|
||||
for (i = 1; i <= 10; i++) {
|
||||
n[i] = parseInt(str);
|
||||
j = str.indexOf(",");
|
||||
if (j >= 0) {
|
||||
str = str.substr(j + 1);
|
||||
}
|
||||
if (n[i] < 0 || n[i] > 2)
|
||||
break;
|
||||
}
|
||||
if (i <= 10) {
|
||||
print("ONLY USE THE DIGITS '0', '1', OR '2'.\n");
|
||||
print("LET'S TRY AGAIN.\n");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
print("\n");
|
||||
print("MY GUESS\tYOUR NO.\tRESULT\tNO. RIGHT\n");
|
||||
print("\n");
|
||||
for (u = 1; u <= 10; u++) {
|
||||
n2 = n[u];
|
||||
s = 0;
|
||||
for (j = 0; j <= 2; j++) {
|
||||
s1 = a * k[z2][j] + b * l[z1][j] + c * m[z][j];
|
||||
if (s > s1)
|
||||
continue;
|
||||
if (s < s1 || Math.random() >= 0.5) {
|
||||
s = s1;
|
||||
g = j;
|
||||
}
|
||||
}
|
||||
print(" " + g + "\t\t " + n[u] + "\t\t");
|
||||
if (g == n[u]) {
|
||||
x++;
|
||||
print(" RIGHT\t " + x + "\n");
|
||||
m[z][n2]++;
|
||||
l[z1][n2]++;
|
||||
k[z2][n2]++;
|
||||
z = z % 9;
|
||||
z = 3 * z + n[u];
|
||||
} else {
|
||||
print(" WRONG\t " + x + "\n");
|
||||
}
|
||||
z1 = z % 9;
|
||||
z2 = n[u];
|
||||
}
|
||||
}
|
||||
print("\n");
|
||||
if (x > 10) {
|
||||
print("I GUESSED MORE THAN 1/3 OF YOUR NUMBERS.\n");
|
||||
print("I WIN.\n");
|
||||
} else if (x == 10) {
|
||||
print("I GUESSED EXACTLY 1/3 OF YOUR NUMBERS.\n");
|
||||
print("IT'S A TIE GAME.\n");
|
||||
} else {
|
||||
print("I GUESSED LESS THAN 1/3 OF YOUR NUMBERS.\n");
|
||||
print("YOU BEAT ME. CONGRATULATIONS *****\n");
|
||||
}
|
||||
print("\n");
|
||||
print("DO YOU WANT TO TRY AGAIN (1 FOR YES, 0 FOR NO)");
|
||||
x = parseInt(await input());
|
||||
if (x != 1)
|
||||
break;
|
||||
}
|
||||
print("\n");
|
||||
print("THANKS FOR THE GAME.\n");
|
||||
}
|
||||
|
||||
main();
|
||||
3
34_Digits/pascal/README.md
Normal file
3
34_Digits/pascal/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Pascal](https://en.wikipedia.org/wiki/Pascal_(programming_language))
|
||||
3
34_Digits/perl/README.md
Normal file
3
34_Digits/perl/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Perl](https://www.perl.org/)
|
||||
3
34_Digits/python/README.md
Normal file
3
34_Digits/python/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Python](https://www.python.org/about/)
|
||||
3
34_Digits/ruby/README.md
Normal file
3
34_Digits/ruby/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Ruby](https://www.ruby-lang.org/en/)
|
||||
3
34_Digits/vbnet/README.md
Normal file
3
34_Digits/vbnet/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original BASIC source [downloaded from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Visual Basic .NET](https://en.wikipedia.org/wiki/Visual_Basic_.NET)
|
||||
Reference in New Issue
Block a user