mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-03 08:37:59 -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
61_Math_Dice/README.md
Normal file
7
61_Math_Dice/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
### Math Dice
|
||||
|
||||
As published in Basic Computer Games (1978)
|
||||
https://www.atariarchives.org/basicgames/showpage.php?page=113
|
||||
|
||||
Downloaded from Vintage Basic at
|
||||
http://www.vintage-basic.net/games.html
|
||||
8
61_Math_Dice/csharp/GameState.cs
Normal file
8
61_Math_Dice/csharp/GameState.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace MathDice
|
||||
{
|
||||
public enum GameState
|
||||
{
|
||||
FirstAttempt = 0,
|
||||
SecondAttempt = 1,
|
||||
}
|
||||
}
|
||||
9
61_Math_Dice/csharp/MathDice.csproj
Normal file
9
61_Math_Dice/csharp/MathDice.csproj
Normal file
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<RootNamespace>MathDice</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
25
61_Math_Dice/csharp/MathDice.sln
Normal file
25
61_Math_Dice/csharp/MathDice.sln
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.31025.194
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MathDice", "MathDice.csproj", "{4F28D7EB-3CD6-434F-B643-0CEA5F09F96D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{4F28D7EB-3CD6-434F-B643-0CEA5F09F96D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4F28D7EB-3CD6-434F-B643-0CEA5F09F96D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4F28D7EB-3CD6-434F-B643-0CEA5F09F96D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4F28D7EB-3CD6-434F-B643-0CEA5F09F96D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {A1337DCA-4EFA-45EE-A1A9-22E37E74F362}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
121
61_Math_Dice/csharp/Program.cs
Normal file
121
61_Math_Dice/csharp/Program.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
|
||||
namespace MathDice
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
readonly static Random random = new Random();
|
||||
|
||||
static int DieOne = 0;
|
||||
static int DieTwo = 0;
|
||||
|
||||
private const string NoPips = "I I";
|
||||
private const string LeftPip = "I * I";
|
||||
private const string CentrePip = "I * I";
|
||||
private const string RightPip = "I * I";
|
||||
private const string TwoPips = "I * * I";
|
||||
private const string Edge = " ----- ";
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
int answer;
|
||||
|
||||
GameState gameState = GameState.FirstAttempt;
|
||||
|
||||
Console.WriteLine("MATH DICE".CentreAlign());
|
||||
Console.WriteLine("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY".CentreAlign());
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("THIS PROGRAM GENERATES SUCCESSIVE PICTURES OF TWO DICE.");
|
||||
Console.WriteLine("WHEN TWO DICE AND AN EQUAL SIGN FOLLOWED BY A QUESTION");
|
||||
Console.WriteLine("MARK HAVE BEEN PRINTED, TYPE YOUR ANSWER AND THE RETURN KEY.");
|
||||
Console.WriteLine("TO CONCLUDE THE LESSON, TYPE CONTROL-C AS YOUR ANSWER.");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine();
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (gameState == GameState.FirstAttempt)
|
||||
{
|
||||
Roll(ref DieOne);
|
||||
Roll(ref DieTwo);
|
||||
|
||||
DrawDie(DieOne);
|
||||
Console.WriteLine(" +");
|
||||
DrawDie(DieTwo);
|
||||
}
|
||||
|
||||
answer = GetAnswer();
|
||||
|
||||
if (answer == DieOne + DieTwo)
|
||||
{
|
||||
Console.WriteLine("RIGHT!");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("THE DICE ROLL AGAIN...");
|
||||
|
||||
gameState = GameState.FirstAttempt;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (gameState == GameState.FirstAttempt)
|
||||
{
|
||||
Console.WriteLine("NO, COUNT THE SPOTS AND GIVE ANOTHER ANSWER.");
|
||||
gameState = GameState.SecondAttempt;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"NO, THE ANSWER IS{DieOne + DieTwo}");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("THE DICE ROLL AGAIN...");
|
||||
gameState = GameState.FirstAttempt;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int GetAnswer()
|
||||
{
|
||||
int answer;
|
||||
|
||||
Console.Write(" =?");
|
||||
var input = Console.ReadLine();
|
||||
|
||||
int.TryParse(input, out answer);
|
||||
|
||||
return answer;
|
||||
}
|
||||
|
||||
private static void DrawDie(int pips)
|
||||
{
|
||||
Console.WriteLine(Edge);
|
||||
Console.WriteLine(OuterRow(pips, true));
|
||||
Console.WriteLine(CentreRow(pips));
|
||||
Console.WriteLine(OuterRow(pips, false));
|
||||
Console.WriteLine(Edge);
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
private static void Roll(ref int die) => die = random.Next(1, 7);
|
||||
|
||||
private static string OuterRow(int pips, bool top)
|
||||
{
|
||||
return pips switch
|
||||
{
|
||||
1 => NoPips,
|
||||
var x when x == 2 || x == 3 => top ? LeftPip : RightPip,
|
||||
_ => TwoPips
|
||||
};
|
||||
}
|
||||
|
||||
private static string CentreRow(int pips)
|
||||
{
|
||||
return pips switch
|
||||
{
|
||||
var x when x == 2 || x == 4 => NoPips,
|
||||
6 => TwoPips,
|
||||
_ => CentrePip
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
10
61_Math_Dice/csharp/README.md
Normal file
10
61_Math_Dice/csharp/README.md
Normal file
@@ -0,0 +1,10 @@
|
||||
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/)
|
||||
|
||||
Conversion Notes
|
||||
|
||||
- There are minor spacing issues which have been preserved in this port.
|
||||
- This implementation uses switch expressions to concisely place the dice pips in the right place.
|
||||
- Random() is only pseudo-random but perfectly adequate for the purposes of simulating dice rolls.
|
||||
- Console width is assumed to be 120 chars for the purposes of centrally aligned the intro text.
|
||||
15
61_Math_Dice/csharp/StringExtensions.cs
Normal file
15
61_Math_Dice/csharp/StringExtensions.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace MathDice
|
||||
{
|
||||
public static class StringExtensions
|
||||
{
|
||||
private const int ConsoleWidth = 120; // default console width
|
||||
|
||||
public static string CentreAlign(this string value)
|
||||
{
|
||||
int spaces = ConsoleWidth - value.Length;
|
||||
int leftPadding = spaces / 2 + value.Length;
|
||||
|
||||
return value.PadLeft(leftPadding).PadRight(ConsoleWidth);
|
||||
}
|
||||
}
|
||||
}
|
||||
73
61_Math_Dice/java/Die.java
Normal file
73
61_Math_Dice/java/Die.java
Normal file
@@ -0,0 +1,73 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class Die {
|
||||
private static final int DEFAULT_SIDES = 6;
|
||||
private int faceValue;
|
||||
private int sides;
|
||||
private Random generator = new Random();
|
||||
|
||||
/**
|
||||
* Construct a new Die with default sides
|
||||
*/
|
||||
public Die() {
|
||||
this.sides = DEFAULT_SIDES;
|
||||
this.faceValue = 1 + generator.nextInt(sides);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a new random number between 1 and sides to be stored in faceValue
|
||||
*/
|
||||
private void throwDie() {
|
||||
this.faceValue = 1 + generator.nextInt(sides);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return the faceValue
|
||||
*/
|
||||
public int getFaceValue() {
|
||||
return faceValue;
|
||||
}
|
||||
|
||||
|
||||
public void printDie() {
|
||||
throwDie();
|
||||
int x = this.getFaceValue();
|
||||
|
||||
System.out.println(" ----- ");
|
||||
|
||||
if(x==4||x==5||x==6) {
|
||||
printTwo();
|
||||
} else if(x==2||x==3) {
|
||||
System.out.println("| * |");
|
||||
} else {
|
||||
printZero();
|
||||
}
|
||||
|
||||
if(x==1||x==3||x==5) {
|
||||
System.out.println("| * |");
|
||||
} else if(x==2||x==4) {
|
||||
printZero();
|
||||
} else {
|
||||
printTwo();
|
||||
}
|
||||
|
||||
if(x==4||x==5||x==6) {
|
||||
printTwo();
|
||||
} else if(x==2||x==3) {
|
||||
System.out.println("| * |");
|
||||
} else {
|
||||
printZero();
|
||||
}
|
||||
|
||||
System.out.println(" ----- ");
|
||||
}
|
||||
|
||||
private void printZero() {
|
||||
System.out.println("| |");
|
||||
}
|
||||
|
||||
private void printTwo() {
|
||||
System.out.println("| * * |");
|
||||
}
|
||||
}
|
||||
53
61_Math_Dice/java/MathDice.java
Normal file
53
61_Math_Dice/java/MathDice.java
Normal file
@@ -0,0 +1,53 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class MathDice {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
Die dieOne = new Die();
|
||||
Die dieTwo = new Die();
|
||||
int guess = 1;
|
||||
int answer;
|
||||
|
||||
System.out.println("Math Dice");
|
||||
System.out.println("https://github.com/coding-horror/basic-computer-games");
|
||||
System.out.println();
|
||||
System.out.print("This program generates images of two dice.\n"
|
||||
+ "When two dice and an equals sign followed by a question\n"
|
||||
+ "mark have been printed, type your answer, and hit the ENTER\n" + "key.\n"
|
||||
+ "To conclude the program, type 0.\n");
|
||||
|
||||
while (true) {
|
||||
dieOne.printDie();
|
||||
System.out.println(" +");
|
||||
dieTwo.printDie();
|
||||
System.out.println(" =");
|
||||
int tries = 0;
|
||||
answer = dieOne.getFaceValue() + dieTwo.getFaceValue();
|
||||
|
||||
while (guess!=answer && tries < 2) {
|
||||
if(tries == 1)
|
||||
System.out.println("No, count the spots and give another answer.");
|
||||
try{
|
||||
guess = in.nextInt();
|
||||
} catch(Exception e) {
|
||||
System.out.println("Thats not a number!");
|
||||
in.nextLine();
|
||||
}
|
||||
|
||||
if(guess == 0)
|
||||
System.exit(0);
|
||||
|
||||
tries++;
|
||||
}
|
||||
|
||||
if(guess != answer){
|
||||
System.out.println("No, the answer is " + answer + "!");
|
||||
} else {
|
||||
System.out.println("Correct");
|
||||
}
|
||||
System.out.println("The dice roll again....");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
3
61_Math_Dice/java/README.md
Normal file
3
61_Math_Dice/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
61_Math_Dice/javascript/README.md
Normal file
3
61_Math_Dice/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
61_Math_Dice/javascript/mathdice.html
Normal file
9
61_Math_Dice/javascript/mathdice.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>MATH DICE</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="mathdice.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
113
61_Math_Dice/javascript/mathdice.js
Normal file
113
61_Math_Dice/javascript/mathdice.js
Normal file
@@ -0,0 +1,113 @@
|
||||
// MATH DICE
|
||||
//
|
||||
// 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(31) + "MATH DICE\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("THIS PROGRAM GENERATES SUCCESSIVE PICTURES OF TWO DICE.\n");
|
||||
print("WHEN TWO DICE AND AN EQUAL SIGN FOLLOWED BY A QUESTION\n");
|
||||
print("MARK HAVE BEEN PRINTED, TYPE YOUR ANSWER AND THE RETURN KEY.\n"),
|
||||
print("TO CONCLUDE THE LESSON, TYPE ZERO AS YOUR ANSWER.\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
n = 0;
|
||||
while (1) {
|
||||
n++;
|
||||
d = Math.floor(6 * Math.random() + 1);
|
||||
print(" ----- \n");
|
||||
if (d == 1)
|
||||
print("I I\n");
|
||||
else if (d == 2 || d == 3)
|
||||
print("I * I\n");
|
||||
else
|
||||
print("I * * I\n");
|
||||
if (d == 2 || d == 4)
|
||||
print("I I\n");
|
||||
else if (d == 6)
|
||||
print("I * * I\n");
|
||||
else
|
||||
print("I * I\n");
|
||||
if (d == 1)
|
||||
print("I I\n");
|
||||
else if (d == 2 || d == 3)
|
||||
print("I * I\n");
|
||||
else
|
||||
print("I * * I\n");
|
||||
print(" ----- \n");
|
||||
print("\n");
|
||||
if (n != 2) {
|
||||
print(" +\n");
|
||||
print("\n");
|
||||
a = d;
|
||||
continue;
|
||||
}
|
||||
t = d + a;
|
||||
print(" =");
|
||||
t1 = parseInt(await input());
|
||||
if (t1 == 0)
|
||||
break;
|
||||
if (t1 != t) {
|
||||
print("NO, COUNT THE SPOTS AND GIVE ANOTHER ANSWER.\n");
|
||||
print(" =");
|
||||
t1 = parseInt(await input());
|
||||
if (t1 != t) {
|
||||
print("NO, THE ANSWER IS " + t + "\n");
|
||||
}
|
||||
}
|
||||
if (t1 == t) {
|
||||
print("RIGHT!\n");
|
||||
}
|
||||
print("\n");
|
||||
print("THE DICE ROLL AGAIN...\n");
|
||||
print("\n");
|
||||
n = 0;
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
60
61_Math_Dice/mathdice.bas
Normal file
60
61_Math_Dice/mathdice.bas
Normal file
@@ -0,0 +1,60 @@
|
||||
10 PRINT TAB(31);"MATH DICE"
|
||||
20 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
|
||||
30 PRINT:PRINT:PRINT
|
||||
40 PRINT "THIS PROGRAM GENERATES SUCCESSIVE PICTURES OF TWO DICE."
|
||||
50 PRINT "WHEN TWO DICE AND AN EQUAL SIGN FOLLOWED BY A QUESTION"
|
||||
60 PRINT "MARK HAVE BEEN PRINTED, TYPE YOUR ANSWER AND THE RETURN KEY."
|
||||
70 PRINT "TO CONCLUDE THE LESSON, TYPE CONTROL-C AS YOUR ANSWER."
|
||||
80 PRINT
|
||||
90 PRINT
|
||||
100 N=N+1
|
||||
110 D=INT(6*RND(1)+1)
|
||||
120 PRINT" ----- "
|
||||
130 IF D=1 THEN 200
|
||||
140 IF D=2 THEN 180
|
||||
150 IF D=3 THEN 180
|
||||
160 PRINT "I * * I"
|
||||
170 GOTO 210
|
||||
180 PRINT "I * I"
|
||||
190 GOTO 210
|
||||
200 PRINT "I I"
|
||||
210 IF D=2 THEN 260
|
||||
220 IF D=4 THEN 260
|
||||
230 IF D=6 THEN 270
|
||||
240 PRINT "I * I"
|
||||
250 GOTO 280
|
||||
260 PRINT "I I"
|
||||
265 GOTO 280
|
||||
270 PRINT "I * * I"
|
||||
280 IF D=1 THEN 350
|
||||
290 IF D=2 THEN 330
|
||||
300 IF D=3 THEN 330
|
||||
310 PRINT "I * * I"
|
||||
320 GOTO 360
|
||||
330 PRINT "I * I"
|
||||
340 GOTO 360
|
||||
350 PRINT "I I"
|
||||
360 PRINT " ----- "
|
||||
370 PRINT
|
||||
375 IF N=2 THEN 500
|
||||
380 PRINT " +"
|
||||
381 PRINT
|
||||
400 A=D
|
||||
410 GOTO 100
|
||||
500 T=D+A
|
||||
510 PRINT " =";
|
||||
520 INPUT T1
|
||||
530 IF T1=T THEN 590
|
||||
540 PRINT "NO, COUNT THE SPOTS AND GIVE ANOTHER ANSWER."
|
||||
541 PRINT " =";
|
||||
550 INPUT T2
|
||||
560 IF T2=T THEN 590
|
||||
570 PRINT "NO, THE ANSWER IS";T
|
||||
580 GOTO 600
|
||||
590 PRINT "RIGHT!"
|
||||
600 PRINT
|
||||
601 PRINT "THE DICE ROLL AGAIN..."
|
||||
610 PRINT
|
||||
615 N=0
|
||||
620 GOTO 100
|
||||
999 END
|
||||
15
61_Math_Dice/pascal/README.md
Normal file
15
61_Math_Dice/pascal/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Pascal](https://en.wikipedia.org/wiki/Pascal_(programming_language))
|
||||
|
||||
##### Translator Notes:
|
||||
I tried to preserve as much of the original layout and flow of the code
|
||||
as possible. I added a procedure for the printing of the die-face; and
|
||||
another to read an integer from the player, as I was unhappy with the runtime
|
||||
error message spat out when a non-number is given to readln(<integer>).
|
||||
|
||||
I was torn between using the correct singular term "die" instead of "dice".
|
||||
In the end I used a (poor?) combination of both.
|
||||
|
||||
krt@krt.com.au 2020-10-12
|
||||
|
||||
129
61_Math_Dice/pascal/mathdice.pas
Normal file
129
61_Math_Dice/pascal/mathdice.pas
Normal file
@@ -0,0 +1,129 @@
|
||||
(*
|
||||
* Ported from mathdice.bas to Pascal by krt@krt.com.au
|
||||
*
|
||||
* Compile with Free Pascal (https://www.freepascal.org/) ~
|
||||
* fpc mathdice.pas
|
||||
*)
|
||||
|
||||
program MathDice;
|
||||
|
||||
|
||||
procedure printDice( face_value: integer );
|
||||
(* Prints a box with spots representing a die face for the user *)
|
||||
begin
|
||||
writeln( ' ----- ' );
|
||||
|
||||
if ( face_value = 1 ) then
|
||||
writeln( 'I I' )
|
||||
else if ( ( face_value = 2 ) or ( face_value = 3 ) ) then
|
||||
writeln( 'I * I' )
|
||||
else
|
||||
writeln( 'I * * I' );
|
||||
|
||||
if ( ( face_value = 2 ) or ( face_value = 4 ) ) then
|
||||
writeln( 'I I' )
|
||||
else if ( face_value = 6 ) then
|
||||
writeln( 'I * * I' )
|
||||
else
|
||||
writeln( 'I * I' );
|
||||
|
||||
if ( face_value = 1 ) then
|
||||
writeln( 'I I' )
|
||||
else if ( ( face_value = 2 ) or ( face_value = 3 ) ) then
|
||||
writeln( 'I * I' )
|
||||
else
|
||||
writeln( 'I * * I' );
|
||||
|
||||
writeln( ' ----- ' );
|
||||
end;
|
||||
|
||||
|
||||
procedure writeAtColumn( width: integer; words: string );
|
||||
(* Prints <width> worth of spaces before the <words> to justify the text *)
|
||||
var
|
||||
i: integer;
|
||||
begin
|
||||
for i := 1 to width do
|
||||
write( ' ' );
|
||||
writeln( words );
|
||||
end;
|
||||
|
||||
|
||||
function inputNumber(): integer;
|
||||
(* Get a number from the player with error checking.
|
||||
If they type a non-number, ask them again *)
|
||||
var
|
||||
player_input: string; (* The string entered by the player *)
|
||||
player_answer: integer; (* The converted value of the text *)
|
||||
input_error: integer; (* The letter's column that caused an error *)
|
||||
begin
|
||||
|
||||
input_error := 1;
|
||||
while ( input_error <> 0 ) do
|
||||
begin
|
||||
readln( player_input );
|
||||
|
||||
val( player_input, player_answer, input_error );
|
||||
|
||||
if ( input_error <> 0 ) then
|
||||
write( 'Please input a number: ' );
|
||||
end;
|
||||
|
||||
inputNumber := player_answer;
|
||||
end;
|
||||
|
||||
|
||||
|
||||
var
|
||||
dice1: integer; (* die 1 face value *)
|
||||
dice2: integer; (* die 2 face value *)
|
||||
answer: integer; (* the sum of the dice *)
|
||||
player_answer: integer; (* The value entered by the player *)
|
||||
begin
|
||||
writeAtColumn( 31, 'MATH DICE' );
|
||||
writeAtColumn( 15, 'CREATIVE COMPUTING MORRISTOWN, NEW JERSEY' );
|
||||
writeAtColumn( 15, '(Ported to Pascal Oct 2012 krt@krt.com.au)' );
|
||||
writeln( '' );
|
||||
writeln( '' );
|
||||
writeln( '' );
|
||||
|
||||
writeln( 'THIS PROGRAM GENERATES SUCCESSIVE PICTURES OF TWO DICE.' );
|
||||
writeln( 'WHEN TWO DICE AND AN EQUAL SIGN FOLLOWED BY A QUESTION' );
|
||||
writeln( 'MARK HAVE BEEN PRINTED, TYPE YOUR ANSWER AND THE RETURN KEY.' );
|
||||
writeln( 'TO CONCLUDE THE LESSON, TYPE CONTROL-C AS YOUR ANSWER.' );
|
||||
writeln( '' );
|
||||
writeln( '' );
|
||||
|
||||
while ( true ) do
|
||||
begin
|
||||
dice1 := Random( 6 ) + 1; (* Random number between 1 and 6 (including) *)
|
||||
dice2 := Random( 6 ) + 1; (* Random number between 1 and 6 (including) *)
|
||||
answer := dice1 + dice2;
|
||||
|
||||
(* Show the player two dice faces *)
|
||||
printDice( dice1 );
|
||||
writeln( ' +' );
|
||||
printDice( dice2 );
|
||||
|
||||
write( ' = ' );
|
||||
|
||||
player_answer := inputNumber();
|
||||
|
||||
if ( player_answer <> answer ) then
|
||||
begin
|
||||
(* Give the player a second chance at the answer... *)
|
||||
writeln( 'NO, COUNT THE SPOTS AND GIVE ANOTHER ANSWER.' );
|
||||
write( ' = ' );
|
||||
player_answer := inputNumber();
|
||||
end;
|
||||
|
||||
if ( player_answer <> answer ) then
|
||||
writeln( 'NO, THE ANSWER IS ', answer )
|
||||
else
|
||||
writeln( 'RIGHT!' );
|
||||
|
||||
writeln( '' );
|
||||
writeln( 'THE DICE ROLL AGAIN...' );
|
||||
end;
|
||||
end.
|
||||
|
||||
3
61_Math_Dice/perl/README.md
Normal file
3
61_Math_Dice/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
61_Math_Dice/python/README.md
Normal file
3
61_Math_Dice/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/)
|
||||
79
61_Math_Dice/python/mathdice.py
Normal file
79
61_Math_Dice/python/mathdice.py
Normal file
@@ -0,0 +1,79 @@
|
||||
from random import randint
|
||||
print("Math Dice")
|
||||
print("https://github.com/coding-horror/basic-computer-games")
|
||||
print()
|
||||
print("""This program generates images of two dice.
|
||||
When two dice and an equals sign followed by a question
|
||||
mark have been printed, type your answer, and hit the ENTER
|
||||
key.
|
||||
To conclude the program, type 0.
|
||||
""")
|
||||
|
||||
def print_dice(n):
|
||||
|
||||
def print_0():
|
||||
print("| |")
|
||||
def print_2():
|
||||
print("| * * |")
|
||||
|
||||
print(" ----- ")
|
||||
|
||||
if n in [4,5,6]:
|
||||
print_2()
|
||||
elif n in [2,3]:
|
||||
print("| * |")
|
||||
else:
|
||||
print_0()
|
||||
|
||||
if n in [1,3,5]:
|
||||
print("| * |")
|
||||
elif n in [2,4]:
|
||||
print_0()
|
||||
else:
|
||||
print_2()
|
||||
|
||||
if n in [4,5,6]:
|
||||
print_2()
|
||||
elif n in [2,3]:
|
||||
print("| * |")
|
||||
else:
|
||||
print_0()
|
||||
|
||||
print(" ----- ")
|
||||
|
||||
def main():
|
||||
|
||||
while True:
|
||||
d1 = randint(1,6)
|
||||
d2 = randint(1,6)
|
||||
guess = 13
|
||||
|
||||
print_dice(d1)
|
||||
print(" +")
|
||||
print_dice(d2)
|
||||
print(" =")
|
||||
|
||||
tries = 0
|
||||
while guess != (d1 + d2) and tries < 2:
|
||||
if tries == 1:
|
||||
print("No, count the spots and give another answer.")
|
||||
try:
|
||||
guess = int(input())
|
||||
except ValueError:
|
||||
print("That's not a number!")
|
||||
if guess == 0:
|
||||
exit()
|
||||
tries += 1
|
||||
|
||||
if guess != (d1 + d2):
|
||||
print(f"No, the answer is {d1 + d2}!")
|
||||
else:
|
||||
print("Correct!")
|
||||
|
||||
|
||||
print("The dice roll again....")
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
3
61_Math_Dice/ruby/README.md
Normal file
3
61_Math_Dice/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
61_Math_Dice/vbnet/README.md
Normal file
3
61_Math_Dice/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