mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-12 15:50:20 -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
22_Change/README.md
Normal file
7
22_Change/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
### Change
|
||||
|
||||
As published in Basic Computer Games (1978)
|
||||
https://www.atariarchives.org/basicgames/showpage.php?page=39
|
||||
|
||||
Downloaded from Vintage Basic at
|
||||
http://www.vintage-basic.net/games.html
|
||||
51
22_Change/change.bas
Normal file
51
22_Change/change.bas
Normal file
@@ -0,0 +1,51 @@
|
||||
2 PRINT TAB(33);"CHANGE"
|
||||
4 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
|
||||
5 PRINT:PRINT:PRINT
|
||||
6 PRINT "I, YOUR FRIENDLY MICROCOMPUTER, WILL DETERMINE"
|
||||
8 PRINT "THE CORRECT CHANGE FOR ITEMS COSTING UP TO $100."
|
||||
9 PRINT:PRINT
|
||||
10 PRINT "COST OF ITEM";:INPUT A:PRINT "AMOUNT OF PAYMENT";:INPUT P
|
||||
20 C=P-A:M=C:IF C<>0 THEN 90
|
||||
25 PRINT "CORRECT AMOUNT, THANK YOU."
|
||||
30 GOTO 400
|
||||
90 IF C>0 THEN 120
|
||||
95 PRINT "SORRY, YOU HAVE SHORT-CHANGED ME $";A-P
|
||||
100 GOTO 10
|
||||
120 PRINT "YOUR CHANGE, $";C
|
||||
130 D=INT(C/10)
|
||||
140 IF D=0 THEN 155
|
||||
150 PRINT D;"TEN DOLLAR BILL(S)"
|
||||
155 C=M-(D*10)
|
||||
160 E=INT(C/5)
|
||||
170 IF E=0 THEN 185
|
||||
180 PRINT E;"FIVE DOLLARS BILL(S)"
|
||||
185 C=M-(D*10+E*5)
|
||||
190 F=INT(C)
|
||||
200 IF F=0 THEN 215
|
||||
210 PRINT F;"ONE DOLLAR BILL(S)"
|
||||
215 C=M-(D*10+E*5+F)
|
||||
220 C=C*100
|
||||
225 N=C
|
||||
230 G=INT(C/50)
|
||||
240 IF G=0 THEN 255
|
||||
250 PRINT G;"ONE HALF DOLLAR(S)"
|
||||
255 C=N-(G*50)
|
||||
260 H=INT(C/25)
|
||||
270 IF H=0 THEN 285
|
||||
280 PRINT H;"QUARTER(S)"
|
||||
285 C=N-(G*50+H*25)
|
||||
290 I=INT(C/10)
|
||||
300 IF I=0 THEN 315
|
||||
310 PRINT I;"DIME(S)"
|
||||
315 C=N-(G*50+H*25+I*10)
|
||||
320 J=INT(C/5)
|
||||
330 IF J=0 THEN 345
|
||||
340 PRINT J;"NICKEL(S)"
|
||||
345 C=N-(G*50+H*25+I*10+J*5)
|
||||
350 K=INT(C+.5)
|
||||
360 IF K=0 THEN 380
|
||||
370 PRINT K;"PENNY(S)"
|
||||
380 PRINT "THANK YOU, COME AGAIN."
|
||||
390 PRINT:PRINT
|
||||
400 GOTO 10
|
||||
410 END
|
||||
3
22_Change/csharp/README.md
Normal file
3
22_Change/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/)
|
||||
3
22_Change/java/README.md
Normal file
3
22_Change/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/)
|
||||
187
22_Change/java/src/Change.java
Normal file
187
22_Change/java/src/Change.java
Normal file
@@ -0,0 +1,187 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of Change
|
||||
* <p>
|
||||
* Based on the Basic game of Change here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/22%20Change/change.bas
|
||||
* <p>
|
||||
* Note: The idea was to create a version of the 1970's Basic game in Java, without introducing
|
||||
* new features - no additional text, error checking, etc has been added.
|
||||
*/
|
||||
public class Change {
|
||||
|
||||
// Used for keyboard input
|
||||
private final Scanner kbScanner;
|
||||
|
||||
private enum GAME_STATE {
|
||||
START_GAME,
|
||||
INPUT,
|
||||
CALCULATE,
|
||||
END_GAME,
|
||||
GAME_OVER
|
||||
}
|
||||
|
||||
// Current game state
|
||||
private GAME_STATE gameState;
|
||||
|
||||
// Amount of change needed to be given
|
||||
private double change;
|
||||
|
||||
public Change() {
|
||||
kbScanner = new Scanner(System.in);
|
||||
|
||||
gameState = GAME_STATE.START_GAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main game loop
|
||||
*/
|
||||
public void play() {
|
||||
|
||||
do {
|
||||
switch (gameState) {
|
||||
case START_GAME:
|
||||
intro();
|
||||
gameState = GAME_STATE.INPUT;
|
||||
break;
|
||||
|
||||
case INPUT:
|
||||
|
||||
double costOfItem = displayTextAndGetNumber("COST OF ITEM ");
|
||||
double amountPaid = displayTextAndGetNumber("AMOUNT OF PAYMENT ");
|
||||
change = amountPaid - costOfItem;
|
||||
if (change == 0) {
|
||||
// No change needed
|
||||
System.out.println("CORRECT AMOUNT, THANK YOU.");
|
||||
gameState = GAME_STATE.END_GAME;
|
||||
} else if (change < 0) {
|
||||
System.out.println("YOU HAVE SHORT-CHANGES ME $" + (costOfItem - amountPaid));
|
||||
// Don't change game state so it will loop back and try again
|
||||
} else {
|
||||
// Change needed.
|
||||
gameState = GAME_STATE.CALCULATE;
|
||||
}
|
||||
break;
|
||||
|
||||
case CALCULATE:
|
||||
System.out.println("YOUR CHANGE, $" + change);
|
||||
calculateChange();
|
||||
gameState = GAME_STATE.END_GAME;
|
||||
break;
|
||||
|
||||
case END_GAME:
|
||||
System.out.println("THANK YOU, COME AGAIN");
|
||||
System.out.println();
|
||||
gameState = GAME_STATE.INPUT;
|
||||
}
|
||||
} while (gameState != GAME_STATE.GAME_OVER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate and output the change required for the purchase based on
|
||||
* what money was paid.
|
||||
*/
|
||||
private void calculateChange() {
|
||||
|
||||
double originalChange = change;
|
||||
|
||||
int tenDollarBills = (int) change / 10;
|
||||
if (tenDollarBills > 0) {
|
||||
System.out.println(tenDollarBills + " TEN DOLLAR BILL(S)");
|
||||
}
|
||||
change = originalChange - (tenDollarBills * 10);
|
||||
|
||||
int fiveDollarBills = (int) change / 5;
|
||||
if (fiveDollarBills > 0) {
|
||||
System.out.println(fiveDollarBills + " FIVE DOLLAR BILL(S)");
|
||||
}
|
||||
change = originalChange - (tenDollarBills * 10 + fiveDollarBills * 5);
|
||||
|
||||
int oneDollarBills = (int) change;
|
||||
if (oneDollarBills > 0) {
|
||||
System.out.println(oneDollarBills + " ONE DOLLAR BILL(S)");
|
||||
}
|
||||
change = originalChange - (tenDollarBills * 10 + fiveDollarBills * 5 + oneDollarBills);
|
||||
|
||||
change = change * 100;
|
||||
double cents = change;
|
||||
|
||||
int halfDollars = (int) change / 50;
|
||||
if (halfDollars > 0) {
|
||||
System.out.println(halfDollars + " ONE HALF DOLLAR(S)");
|
||||
}
|
||||
change = cents - (halfDollars * 50);
|
||||
|
||||
int quarters = (int) change / 25;
|
||||
if (quarters > 0) {
|
||||
System.out.println(quarters + " QUARTER(S)");
|
||||
}
|
||||
|
||||
change = cents - (halfDollars * 50 + quarters * 25);
|
||||
|
||||
int dimes = (int) change / 10;
|
||||
if (dimes > 0) {
|
||||
System.out.println(dimes + " DIME(S)");
|
||||
}
|
||||
|
||||
change = cents - (halfDollars * 50 + quarters * 25 + dimes * 10);
|
||||
|
||||
int nickels = (int) change / 5;
|
||||
if (nickels > 0) {
|
||||
System.out.println(nickels + " NICKEL(S)");
|
||||
}
|
||||
|
||||
change = cents - (halfDollars * 50 + quarters * 25 + dimes * 10 + nickels * 5);
|
||||
|
||||
int pennies = (int) (change + .5);
|
||||
if (pennies > 0) {
|
||||
System.out.println(pennies + " PENNY(S)");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void intro() {
|
||||
System.out.println(simulateTabs(33) + "CHANGE");
|
||||
System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println();
|
||||
System.out.println("I, YOUR FRIENDLY MICROCOMPUTER, WILL DETERMINE");
|
||||
System.out.println("THE CORRECT CHANGE FOR ITEMS COSTING UP TO $100.");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
/*
|
||||
* Print a message on the screen, then accept input from Keyboard.
|
||||
* Converts input to a Double
|
||||
*
|
||||
* @param text message to be displayed on screen.
|
||||
* @return what was typed by the player.
|
||||
*/
|
||||
private double displayTextAndGetNumber(String text) {
|
||||
return Double.parseDouble(displayTextAndGetInput(text));
|
||||
}
|
||||
|
||||
/*
|
||||
* Print a message on the screen, then accept input from Keyboard.
|
||||
*
|
||||
* @param text message to be displayed on screen.
|
||||
* @return what was typed by the player.
|
||||
*/
|
||||
private String displayTextAndGetInput(String text) {
|
||||
System.out.print(text);
|
||||
return kbScanner.next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate the old basic tab(xx) command which indented text by xx spaces.
|
||||
*
|
||||
* @param spaces number of spaces required
|
||||
* @return String with number of spaces
|
||||
*/
|
||||
private String simulateTabs(int spaces) {
|
||||
char[] spacesTemp = new char[spaces];
|
||||
Arrays.fill(spacesTemp, ' ');
|
||||
return new String(spacesTemp);
|
||||
}
|
||||
}
|
||||
6
22_Change/java/src/ChangeGame.java
Normal file
6
22_Change/java/src/ChangeGame.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public class ChangeGame {
|
||||
public static void main(String[] args) {
|
||||
Change change = new Change();
|
||||
change.play();
|
||||
}
|
||||
}
|
||||
3
22_Change/javascript/README.md
Normal file
3
22_Change/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
22_Change/javascript/change.html
Normal file
9
22_Change/javascript/change.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>CHANGE</title>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="output" style="font-size: 12pt;"></pre>
|
||||
<script src="change.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
107
22_Change/javascript/change.js
Normal file
107
22_Change/javascript/change.js
Normal file
@@ -0,0 +1,107 @@
|
||||
// CHANGE
|
||||
//
|
||||
// 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) + "CHANGE\n");
|
||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
print("I, YOUR FRIENDLY MICROCOMPUTER, WILL DETERMINE\n");
|
||||
print("THE CORRECT CHANGE FOR ITEMS COSTING UP TO $100.\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
while (1) {
|
||||
print("COST OF ITEM");
|
||||
a = parseFloat(await input());
|
||||
print("AMOUNT OF PAYMENT");
|
||||
p = parseFloat(await input());
|
||||
c = p - a;
|
||||
m = c;
|
||||
if (c == 0) {
|
||||
print("CORRECT AMOUNT, THANK YOU.\n");
|
||||
} else {
|
||||
print("YOUR CHANGE, $" + c + "\n");
|
||||
d = Math.floor(c / 10);
|
||||
if (d)
|
||||
print(d + " TEN DOLLAR BILL(S)\n");
|
||||
c -= d * 10;
|
||||
e = Math.floor(c / 5);
|
||||
if (e)
|
||||
print(e + " FIVE DOLLAR BILL(S)\n");
|
||||
c -= e * 5;
|
||||
f = Math.floor(c);
|
||||
if (f)
|
||||
print(f + " ONE DOLLAR BILL(S)\n");
|
||||
c -= f;
|
||||
c *= 100;
|
||||
g = Math.floor(c / 50);
|
||||
if (g)
|
||||
print(g + " ONE HALF DOLLAR(S)\n");
|
||||
c -= g * 50;
|
||||
h = Math.floor(c / 25);
|
||||
if (h)
|
||||
print(h + " QUARTER(S)\n");
|
||||
c -= h * 25;
|
||||
i = Math.floor(c / 10);
|
||||
if (i)
|
||||
print(i + " DIME(S)\n");
|
||||
c -= i * 10;
|
||||
j = Math.floor(c / 5);
|
||||
if (j)
|
||||
print(j + " NICKEL(S)\n");
|
||||
c -= j * 5;
|
||||
k = Math.floor(c + 0.5);
|
||||
if (k)
|
||||
print(k + " PENNY(S)\n");
|
||||
print("THANK YOU, COME AGAIN.\n");
|
||||
print("\n");
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
3
22_Change/pascal/README.md
Normal file
3
22_Change/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
22_Change/perl/README.md
Normal file
3
22_Change/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
22_Change/python/README.md
Normal file
3
22_Change/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/)
|
||||
113
22_Change/python/change.py
Normal file
113
22_Change/python/change.py
Normal file
@@ -0,0 +1,113 @@
|
||||
"""
|
||||
CHANGE
|
||||
|
||||
Change calculator
|
||||
|
||||
Port by Dave LeCompte
|
||||
"""
|
||||
|
||||
PAGE_WIDTH = 64
|
||||
|
||||
|
||||
def print_centered(msg):
|
||||
spaces = " " * ((PAGE_WIDTH - len(msg)) // 2)
|
||||
print(spaces + msg)
|
||||
|
||||
|
||||
def print_header(title):
|
||||
print_centered(title)
|
||||
print_centered("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY")
|
||||
print()
|
||||
print()
|
||||
print()
|
||||
|
||||
|
||||
def print_introduction():
|
||||
print("I, YOUR FRIENDLY MICROCOMPUTER, WILL DETERMINE")
|
||||
print("THE CORRECT CHANGE FOR ITEMS COSTING UP TO $100.")
|
||||
print()
|
||||
print()
|
||||
|
||||
|
||||
def pennies_to_dollar_string(p):
|
||||
d = p / 100
|
||||
ds = f"${d:0.2f}"
|
||||
return ds
|
||||
|
||||
|
||||
def compute_change():
|
||||
print("COST OF ITEM?")
|
||||
cost = float(input())
|
||||
print("AMOUNT OF PAYMENT?")
|
||||
payment = float(input())
|
||||
|
||||
change_in_pennies = round((payment - cost) * 100)
|
||||
if change_in_pennies == 0:
|
||||
print("CORRECT AMOUNT, THANK YOU.")
|
||||
return
|
||||
|
||||
if change_in_pennies < 0:
|
||||
short = -change_in_pennies / 100
|
||||
|
||||
print(f"SORRY, YOU HAVE SHORT-CHANGED ME ${short:0.2f}")
|
||||
print()
|
||||
return
|
||||
|
||||
print(f"YOUR CHANGE, {pennies_to_dollar_string(change_in_pennies)}")
|
||||
|
||||
d = change_in_pennies // 1000
|
||||
if d > 0:
|
||||
print(f"{d} TEN DOLLAR BILL(S)")
|
||||
change_in_pennies -= d * 1000
|
||||
|
||||
e = change_in_pennies // 500
|
||||
if e > 0:
|
||||
print(f"{e} FIVE DOLLAR BILL(S)")
|
||||
change_in_pennies -= e * 500
|
||||
|
||||
f = change_in_pennies // 100
|
||||
if f > 0:
|
||||
print(f"{f} ONE DOLLAR BILL(S)")
|
||||
change_in_pennies -= f * 100
|
||||
|
||||
g = change_in_pennies // 50
|
||||
if g > 0:
|
||||
print("ONE HALF DOLLAR")
|
||||
change_in_pennies -= g * 50
|
||||
|
||||
h = change_in_pennies // 25
|
||||
if h > 0:
|
||||
print(f"{h} QUARTER(S)")
|
||||
change_in_pennies -= h * 25
|
||||
|
||||
i = change_in_pennies // 10
|
||||
if i > 0:
|
||||
print(f"{i} DIME(S)")
|
||||
change_in_pennies -= i * 10
|
||||
|
||||
j = change_in_pennies // 5
|
||||
if j > 0:
|
||||
print(f"{j} NICKEL(S)")
|
||||
change_in_pennies -= j * 5
|
||||
|
||||
if change_in_pennies > 0:
|
||||
print(f"{change_in_pennies} PENNY(S)")
|
||||
|
||||
|
||||
def print_thanks():
|
||||
print("THANK YOU, COME AGAIN.")
|
||||
print()
|
||||
print()
|
||||
|
||||
|
||||
def main():
|
||||
print_header("CHANGE")
|
||||
print_introduction()
|
||||
|
||||
while True:
|
||||
compute_change()
|
||||
print_thanks()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
3
22_Change/ruby/README.md
Normal file
3
22_Change/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
22_Change/vbnet/README.md
Normal file
3
22_Change/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