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("\n");
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) 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 let card1, card2, currentBet;
async function main() do {
{ print("HERE ARE YOUR NEXT TWO CARDS: \n");
q = 100; [card1, card2] = [randomCard(), randomCard()];
while (1) {
print("YOU NOW HAVE " + q + " DOLLARS.\n"); // Ensure we always show cards in order of lowest to highest, and we never
print("\n"); // get two of the same card.
do {
do { card1 = randomCard();
print("HERE ARE YOUR NEXT TWO CARDS: \n"); card2 = randomCard();
do { } while (card1 >= card2);
a = Math.floor(Math.random() * 13 + 2);
b = Math.floor(Math.random() * 13 + 2); printCard(card1);
} while (a >= b) ; printCard(card2);
show_card(a); print("\n");
show_card(b);
print("\n"); while (true) {
while (1) { print("\nWHAT IS YOUR BET? ");
print("\n"); currentBet = parseInt(await readLine(), 10);
print("WHAT IS YOUR BET");
m = parseInt(await input()); if (currentBet > 0) {
if (m > 0) { if (currentBet > currentMoney) {
if (m > q) { print("SORRY, MY FRIEND, BUT YOU BET TOO MUCH.\n");
print("SORRY, MY FRIEND, BUT YOU BET TOO MUCH.\n"); print(`YOU HAVE ONLY ${currentMoney} DOLLARS TO BET.\n`);
print("YOU HAVE ONLY " + q + "DOLLARS TO BET.\n"); continue;
continue;
}
break;
}
m = 0;
print("CHICKEN!!\n");
print("\n");
break;
}
} while (m == 0) ;
c = Math.floor(Math.random() * 13 + 2);
show_card(c);
if (c > a && c < b) {
print("YOU WIN!!!\n");
q = q + m;
} else {
print("SORRY, YOU LOSE\n");
if (m >= q) {
print("\n");
print("\n");
print("SORRY, FRIEND, BUT YOU BLEW YOUR WAD.\n");
print("\n");
print("\n");
print("TRY AGAIN (YES OR NO)");
a = await input();
print("\n");
print("\n");
if (a == "YES") {
q = 100;
} else {
print("O.K., HOPE YOU HAD FUN!");
break;
}
} else {
q = q - m;
}
} }
} break;
} }
main(); // Invalid bet value. Output an error message and reset to undefined to
// restart the loop with new cards.
currentBet = undefined;
print("CHICKEN!!\n");
print("\n");
break;
}
} while (currentBet === undefined);
const actualCard = randomCard();
print("\n\nHERE IS THE CARD WE DREW:\n")
printCard(actualCard);
print("\n\n");
if (actualCard > card1 && actualCard < card2) {
print("YOU WIN!!!\n");
currentMoney += currentBet;
} else {
print("SORRY, YOU LOSE\n");
if (currentBet < currentMoney) {
currentMoney -= currentBet;
} else {
print("\n\nSORRY, FRIEND, BUT YOU BLEW YOUR WAD.\n\n\n");
print("TRY AGAIN (YES OR NO)");
const tryAgain = await readLine();
print("\n\n");
if (tryAgain.toLowerCase() === "yes") {
currentMoney = 100;
} else {
print("O.K., HOPE YOU HAD FUN!");
break;
}
}
}
}

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);
}