Merge pull request #332 from domenic/acey-ducey-js-improvements

Improve Acey Ducey JavaScript port
This commit is contained in:
Jeff Atwood
2022-01-01 13:09:45 -08:00
committed by GitHub
3 changed files with 120 additions and 128 deletions

View File

@@ -1,9 +1,7 @@
<html> <!DOCTYPE html>
<head> <meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>ACEY DUCEY</title> <title>ACEY DUCEY</title>
</head>
<body>
<pre id="output" style="font-size: 12pt;"></pre> <pre id="output" style="font-size: 12pt;"></pre>
<script src="aceyducey.js"></script> <script type="module" src="aceyducey.js"></script>
</body>
</html>

View File

@@ -1,135 +1,100 @@
// ACEY DUCEY import { readLine, print, spaces } from "./io.js";
//
// Converted from BASIC to Javascript by Oscar Toledo G. (nanochess)
//
function print(str) const minFaceCard = 11;
{ const faceCards = {
document.getElementById("output").appendChild(document.createTextNode(str)); 11: "JACK",
12: "QUEEN",
13: "KING",
14: "ACE"
};
function randomCard() {
return Math.floor(Math.random() * 13 + 2);
} }
function input() function printCard(card) {
{ if (card < minFaceCard) {
var input_element; print(card);
var input_str; } else {
print(faceCards[card]);
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);
} }
}); print("\n");
});
} }
function tab(space) print(spaces(26) + "ACEY DUCEY CARD GAME\n");
{ print(spaces(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n");
var str = "";
while (space-- > 0)
str += " ";
return str;
}
print(tab(26) + "ACEY DUCEY CARD GAME\n");
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
print("\n");
print("\n");
print("ACEY-DUCEY IS PLAYED IN THE FOLLOWING MANNER\n"); print("ACEY-DUCEY IS PLAYED IN THE FOLLOWING MANNER\n");
print("THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP\n"); print("THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP\n");
print("YOU HAVE AN OPTION TO BET OR NOT BET DEPENDING\n"); print("YOU HAVE AN OPTION TO BET OR NOT BET DEPENDING\n");
print("ON WHETHER OR NOT YOU FEEL THE CARD WILL HAVE\n"); print("ON WHETHER OR NOT YOU FEEL THE CARD WILL HAVE\n");
print("A VALUE BETWEEN THE FIRST TWO.\n"); print("A VALUE BETWEEN THE FIRST TWO.\n");
print("IF YOU DO NOT WANT TO BET, INPUT A 0\n"); print("IF YOU DO NOT WANT TO BET, INPUT '0'\n");
function show_card(card) let currentMoney = 100;
{ while (true) {
if (card < 11) print(`YOU NOW HAVE ${currentMoney} DOLLARS.\n\n`);
print(card + "\n");
else if (card == 11)
print("JACK\n");
else if (card == 12)
print("QUEEN\n");
else if (card == 13)
print("KING\n");
else
print("ACE\n");
}
// Main program
async function main()
{
q = 100;
while (1) {
print("YOU NOW HAVE " + q + " DOLLARS.\n");
print("\n");
let card1, card2, currentBet;
do { do {
print("HERE ARE YOUR NEXT TWO CARDS: \n"); print("HERE ARE YOUR NEXT TWO CARDS: \n");
[card1, card2] = [randomCard(), randomCard()];
// Ensure we always show cards in order of lowest to highest, and we never
// get two of the same card.
do { do {
a = Math.floor(Math.random() * 13 + 2); card1 = randomCard();
b = Math.floor(Math.random() * 13 + 2); card2 = randomCard();
} while (a >= b) ; } while (card1 >= card2);
show_card(a);
show_card(b); printCard(card1);
printCard(card2);
print("\n"); print("\n");
while (1) {
print("\n"); while (true) {
print("WHAT IS YOUR BET"); print("\nWHAT IS YOUR BET? ");
m = parseInt(await input()); currentBet = parseInt(await readLine(), 10);
if (m > 0) {
if (m > q) { if (currentBet > 0) {
if (currentBet > currentMoney) {
print("SORRY, MY FRIEND, BUT YOU BET TOO MUCH.\n"); print("SORRY, MY FRIEND, BUT YOU BET TOO MUCH.\n");
print("YOU HAVE ONLY " + q + "DOLLARS TO BET.\n"); print(`YOU HAVE ONLY ${currentMoney} DOLLARS TO BET.\n`);
continue; continue;
} }
break; break;
} }
m = 0;
// Invalid bet value. Output an error message and reset to undefined to
// restart the loop with new cards.
currentBet = undefined;
print("CHICKEN!!\n"); print("CHICKEN!!\n");
print("\n"); print("\n");
break; break;
} }
} while (m == 0) ; } while (currentBet === undefined);
c = Math.floor(Math.random() * 13 + 2);
show_card(c); const actualCard = randomCard();
if (c > a && c < b) { print("\n\nHERE IS THE CARD WE DREW:\n")
printCard(actualCard);
print("\n\n");
if (actualCard > card1 && actualCard < card2) {
print("YOU WIN!!!\n"); print("YOU WIN!!!\n");
q = q + m; currentMoney += currentBet;
} else { } else {
print("SORRY, YOU LOSE\n"); print("SORRY, YOU LOSE\n");
if (m >= q) { if (currentBet < currentMoney) {
print("\n"); currentMoney -= currentBet;
print("\n"); } else {
print("SORRY, FRIEND, BUT YOU BLEW YOUR WAD.\n"); print("\n\nSORRY, FRIEND, BUT YOU BLEW YOUR WAD.\n\n\n");
print("\n");
print("\n");
print("TRY AGAIN (YES OR NO)"); print("TRY AGAIN (YES OR NO)");
a = await input(); const tryAgain = await readLine();
print("\n"); print("\n\n");
print("\n"); if (tryAgain.toLowerCase() === "yes") {
if (a == "YES") { currentMoney = 100;
q = 100;
} else { } else {
print("O.K., HOPE YOU HAD FUN!"); print("O.K., HOPE YOU HAD FUN!");
break; break;
} }
} else {
q = q - m;
}
} }
} }
} }
main();

View File

@@ -0,0 +1,29 @@
const outputEl = document.querySelector("#output");
export function print(string) {
outputEl.append(string);
}
export function readLine() {
return new Promise(resolve => {
const inputEl = document.createElement("input");
outputEl.append(inputEl);
inputEl.focus();
inputEl.addEventListener("keydown", event => {
if (event.key === "Enter") {
const result = inputEl.value;
inputEl.remove();
print(result);
print("\n");
resolve(result);
}
});
});
}
export function spaces(numberOfSpaces) {
return " ".repeat(numberOfSpaces);
}