Skip to content

Further change Acey Ducey JavaScript #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions 01_Acey_Ducey/javascript/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": true,
"singleQuote": true
}
8 changes: 4 additions & 4 deletions 01_Acey_Ducey/javascript/aceyducey.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>ACEY DUCEY</title>

<pre id="output" style="font-size: 12pt;"></pre>
<script type="module" src="aceyducey.js"></script>
<pre id="output" style="font-size: 12pt"></pre>
<script src="aceyducey.js"></script>
294 changes: 205 additions & 89 deletions 01_Acey_Ducey/javascript/aceyducey.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,216 @@
import { readLine, print, spaces } from "./io.js";

const minFaceCard = 11;
const faceCards = {
11: "JACK",
12: "QUEEN",
13: "KING",
14: "ACE"
};

function randomCard() {
return Math.floor(Math.random() * 13 + 2);
// UTILITY VARIABLES

// By default:
// — Browsers have a window object
// — Node.js does not
// Checking for an undefined window object is a loose check
// to enable browser and Node.js support
const isRunningInBrowser = typeof window !== 'undefined';

// To easily validate input strings with utility functions
const validLowerCaseYesStrings = ['yes', 'y'];
const validLowerCaseNoStrings = ['no', 'n'];
const validLowerCaseYesAndNoStrings = [
...validLowerCaseYesStrings,
...validLowerCaseNoStrings,
];
// UTILITY VARIABLES

// Function to get a random number (card) 2-14 (ACE is 14)
function getRandomCard() {
// In our game, the value of ACE is greater than face cards;
// instead of having the value of ACE be 1, we’ll have it be 14.
// So, we want to shift the range of random numbers from 1-13 to 2-14
let min = 2;
let max = 14;
// Return random integer between two values, inclusive
return Math.floor(Math.random() * (max - min + 1) + min);
}

function newGameCards() {
let cardOne = getRandomCard();
let cardTwo = getRandomCard();
let cardThree = getRandomCard();
// We want:
// 1. cardOne and cardTwo to be different cards
// 2. cardOne to be lower than cardTwo
// So, while cardOne is greater than or equal too cardTwo
// we will continue to generate random cards.
while (cardOne >= cardTwo) {
cardOne = getRandomCard();
cardTwo = getRandomCard();
}
return [cardOne, cardTwo, cardThree];
}

function printCard(card) {
if (card < minFaceCard) {
print(card);
} else {
print(faceCards[card]);
}
print("\n");
// Function to get card value
function getCardValue(card) {
let faceOrAce = {
11: 'JACK',
12: 'QUEEN',
13: 'KING',
14: 'ACE',
};
// If card value matches a key in faceOrAce, use faceOrAce value;
// Else, return undefined and handle with the Nullish Coalescing Operator (??)
// and default to card value.
let cardValue = faceOrAce[card] ?? card;
return cardValue;
}

print(spaces(26) + "ACEY DUCEY CARD GAME\n");
print(spaces(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n");
print("ACEY-DUCEY IS PLAYED IN THE FOLLOWING MANNER\n");
print("THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP\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("A VALUE BETWEEN THE FIRST TWO.\n");
print("IF YOU DO NOT WANT TO BET, INPUT '0'\n");

let currentMoney = 100;
while (true) {
print(`YOU NOW HAVE ${currentMoney} DOLLARS.\n\n`);

let card1, card2, currentBet;
do {
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 {
card1 = randomCard();
card2 = randomCard();
} while (card1 >= card2);

printCard(card1);
printCard(card2);
print("\n");
print(spaces(26) + 'ACEY DUCEY CARD GAME');
print(spaces(15) + 'CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n');
print('ACEY-DUCEY IS PLAYED IN THE FOLLOWING MANNER');
print('THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP');
print('YOU HAVE AN OPTION TO BET OR NOT BET DEPENDING');
print('ON WHETHER OR NOT YOU FEEL THE CARD WILL HAVE');
print('A VALUE BETWEEN THE FIRST TWO.');
print("IF YOU DO NOT WANT TO BET, INPUT '0'");

main();

async function main() {
let bet;
let availableDollars = 100;

// Loop game forever
while (true) {
print("\nWHAT IS YOUR BET? ");
currentBet = parseInt(await readLine(), 10);

if (currentBet > 0) {
if (currentBet > currentMoney) {
print("SORRY, MY FRIEND, BUT YOU BET TOO MUCH.\n");
print(`YOU HAVE ONLY ${currentMoney} DOLLARS TO BET.\n`);
continue;
let [cardOne, cardTwo, cardThree] = newGameCards();

print(`YOU NOW HAVE ${availableDollars} DOLLARS.\n`);

print('HERE ARE YOUR NEXT TWO CARDS: ');
print(getCardValue(cardOne));
print(getCardValue(cardTwo));
print('');

// Loop until receiving a valid bet
let validBet = false;
while (!validBet) {
print('\nWHAT IS YOUR BET? ');
bet = parseInt(await input(), 10);
let minimumRequiredBet = 0;
if (bet > minimumRequiredBet) {
if (bet > availableDollars) {
print('SORRY, MY FRIEND, BUT YOU BET TOO MUCH.');
print(`YOU HAVE ONLY ${availableDollars} DOLLARS TO BET.`);
} else {
validBet = true;
}
} else {
// Does not meet minimum required bet
print('CHICKEN!!');
print('');
}
}
break;
}

// 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;

print('\n\nHERE IS THE CARD WE DREW: ');
print(getCardValue(cardThree));

// Determine if player won or lost
if (cardThree > cardOne && cardThree < cardTwo) {
print('YOU WIN!!!');
availableDollars = availableDollars + bet;
} else {
print('SORRY, YOU LOSE');

if (bet >= availableDollars) {
print('');
print('');
print('SORRY, FRIEND, BUT YOU BLEW YOUR WAD.');
print('');
print('');
print('TRY AGAIN (YES OR NO)');

let tryAgainInput = await input();

print('');
print('');

if (isValidYesNoString(tryAgainInput)) {
availableDollars = 100;
} else {
print('O.K., HOPE YOU HAD FUN!');
break;
}
} else {
availableDollars = availableDollars - bet;
}
}
}
}

// UTILITY FUNCTIONS
function isValidYesNoString(string) {
return validLowerCaseYesAndNoStrings.includes(string.toLowerCase());
}

function isValidYesString(string) {
return validLowerCaseYesStrings.includes(string.toLowerCase());
}

function isValidNoString(string) {
return validLowerCaseNoStrings.includes(string.toLowerCase());
}

function print(string) {
if (isRunningInBrowser) {
// Adds trailing newline to match console.log behavior
document
.getElementById('output')
.appendChild(document.createTextNode(string + '\n'));
} else {
console.log(string);
}
} 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;
}

function input() {
if (isRunningInBrowser) {
// Accept input from the browser DOM input
return new Promise((resolve) => {
const outputElement = document.querySelector('#output');
const inputElement = document.createElement('input');
outputElement.append(inputElement);
inputElement.focus();

inputElement.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
const result = inputElement.value;
inputElement.remove();
print(result);
print('');
resolve(result);
}
});
});
} 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;
}
// Accept input from the command line in Node.js
// See: https://nodejs.dev/learn/accept-input-from-the-command-line-in-nodejs
return new Promise(function (resolve) {
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
readline.question('', function (input) {
resolve(input);
readline.close();
});
});
}
}
}

function printInline(string) {
if (isRunningInBrowser) {
document
.getElementById('output')
.appendChild(document.createTextNode(string));
} else {
process.stdout.write(string);
}
}

function spaces(numberOfSpaces) {
return ' '.repeat(numberOfSpaces);
}

// UTILITY FUNCTIONS
29 changes: 0 additions & 29 deletions 01_Acey_Ducey/javascript/io.js

This file was deleted.