mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 15:16:33 -08:00
Merge pull request #409 from millwardesque/main
Refactoring for Javascript WAR
This commit is contained in:
@@ -1,60 +1,95 @@
|
|||||||
// WAR
|
// WAR
|
||||||
//
|
//
|
||||||
// Converted from BASIC to Javascript by Oscar Toledo G. (nanochess)
|
// Original conversion from BASIC to Javascript by Oscar Toledo G. (nanochess)
|
||||||
//
|
//
|
||||||
|
|
||||||
function print(str)
|
function print(str) {
|
||||||
{
|
|
||||||
document.getElementById("output").appendChild(document.createTextNode(str));
|
document.getElementById("output").appendChild(document.createTextNode(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
function input()
|
function tab(space) {
|
||||||
{
|
let str = "";
|
||||||
var input_element;
|
while (space-- > 0) {
|
||||||
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 += " ";
|
str += " ";
|
||||||
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
var a = [, "S-2","H-2","C-2","D-2","S-3","H-3","C-3","D-3",
|
function input() {
|
||||||
"S-4","H-4","C-4","D-4","S-5","H-5","C-5","D-5",
|
return new Promise(function (resolve) {
|
||||||
"S-6","H-6","C-6","D-6","S-7","H-7","C-7","D-7",
|
const input_element = document.createElement("INPUT");
|
||||||
"S-8","H-8","C-8","D-8","S-9","H-9","C-9","D-9",
|
|
||||||
"S-10","H-10","C-10","D-10","S-J","H-J","C-J","D-J",
|
|
||||||
"S-Q","H-Q","C-Q","D-Q","S-K","H-K","C-K","D-K",
|
|
||||||
"S-A","H-A","C-A","D-A"];
|
|
||||||
|
|
||||||
var l = [];
|
print("? ");
|
||||||
|
input_element.setAttribute("type", "text");
|
||||||
|
input_element.setAttribute("length", "50");
|
||||||
|
document.getElementById("output").appendChild(input_element);
|
||||||
|
input_element.focus();
|
||||||
|
input_element.addEventListener("keydown", function (event) {
|
||||||
|
if (event.keyCode == 13) {
|
||||||
|
const input_str = input_element.value;
|
||||||
|
document.getElementById("output").removeChild(input_element);
|
||||||
|
print(input_str);
|
||||||
|
print("\n");
|
||||||
|
resolve(input_str);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Main control section
|
async function askYesOrNo(question) {
|
||||||
async function main()
|
while (1) {
|
||||||
{
|
print(question);
|
||||||
|
const str = await input();
|
||||||
|
if (str == "YES") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (str == "NO") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
print("YES OR NO, PLEASE. ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function askAboutInstructions() {
|
||||||
|
const playerWantsInstructions = await askYesOrNo("DO YOU WANT DIRECTIONS");
|
||||||
|
if (playerWantsInstructions) {
|
||||||
|
print("THE COMPUTER GIVES YOU AND IT A 'CARD'. THE HIGHER CARD\n");
|
||||||
|
print("(NUMERICALLY) WINS. THE GAME ENDS WHEN YOU CHOOSE NOT TO\n");
|
||||||
|
print("CONTINUE OR WHEN YOU HAVE FINISHED THE PACK.\n");
|
||||||
|
}
|
||||||
|
print("\n");
|
||||||
|
print("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
function createGameDeck(cards, gameSize) {
|
||||||
|
const deck = [];
|
||||||
|
const deckSize = cards.length;
|
||||||
|
for (let j = 0; j < gameSize; j++) {
|
||||||
|
let card;
|
||||||
|
|
||||||
|
// Compute a new card index until we find one that isn't already in the new deck
|
||||||
|
do {
|
||||||
|
card = Math.floor(deckSize * Math.random());
|
||||||
|
} while (deck.includes(card));
|
||||||
|
deck.push(card);
|
||||||
|
}
|
||||||
|
return deck;
|
||||||
|
}
|
||||||
|
|
||||||
|
function computeCardValue(cardIndex) {
|
||||||
|
return Math.floor(cardIndex / 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
function printGameOver(playerScore, computerScore) {
|
||||||
|
print("\n");
|
||||||
|
print("\n");
|
||||||
|
print(`WE HAVE RUN OUT OF CARDS. FINAL SCORE: YOU: ${playerScore} THE COMPUTER: ${computerScore}\n`);
|
||||||
|
print("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
function printTitle() {
|
||||||
print(tab(33) + "WAR\n");
|
print(tab(33) + "WAR\n");
|
||||||
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||||
print("\n");
|
print("\n");
|
||||||
@@ -62,72 +97,65 @@ async function main()
|
|||||||
print("\n");
|
print("\n");
|
||||||
print("THIS IS THE CARD GAME OF WAR. EACH CARD IS GIVEN BY SUIT-#\n");
|
print("THIS IS THE CARD GAME OF WAR. EACH CARD IS GIVEN BY SUIT-#\n");
|
||||||
print("AS S-7 FOR SPADE 7. ");
|
print("AS S-7 FOR SPADE 7. ");
|
||||||
while (1) {
|
}
|
||||||
print("DO YOU WANT DIRECTIONS");
|
|
||||||
str = await input();
|
|
||||||
if (str == "YES") {
|
|
||||||
print("THE COMPUTER GIVES YOU AND IT A 'CARD'. THE HIGHER CARD\n");
|
|
||||||
print("(NUMERICALLY) WINS. THE GAME ENDS WHEN YOU CHOOSE NOT TO\n");
|
|
||||||
print("CONTINUE OR WHEN YOU HAVE FINISHED THE PACK.\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (str == "NO")
|
|
||||||
break;
|
|
||||||
print("YES OR NO, PLEASE. ");
|
|
||||||
}
|
|
||||||
print("\n");
|
|
||||||
print("\n");
|
|
||||||
|
|
||||||
a1 = 0;
|
function printCards(playerCard, computerCard) {
|
||||||
b1 = 0;
|
print("\n");
|
||||||
p = 0;
|
print(`YOU: ${playerCard}\tCOMPUTER: ${computerCard}\n`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const cards = [
|
||||||
|
"S-2", "H-2", "C-2", "D-2",
|
||||||
|
"S-3", "H-3", "C-3", "D-3",
|
||||||
|
"S-4", "H-4", "C-4", "D-4",
|
||||||
|
"S-5", "H-5", "C-5", "D-5",
|
||||||
|
"S-6", "H-6", "C-6", "D-6",
|
||||||
|
"S-7", "H-7", "C-7", "D-7",
|
||||||
|
"S-8", "H-8", "C-8", "D-8",
|
||||||
|
"S-9", "H-9", "C-9", "D-9",
|
||||||
|
"S-10", "H-10", "C-10", "D-10",
|
||||||
|
"S-J", "H-J", "C-J", "D-J",
|
||||||
|
"S-Q", "H-Q", "C-Q", "D-Q",
|
||||||
|
"S-K", "H-K", "C-K", "D-K",
|
||||||
|
"S-A", "H-A", "C-A", "D-A"
|
||||||
|
];
|
||||||
|
|
||||||
|
// Main control section
|
||||||
|
async function main() {
|
||||||
|
printTitle();
|
||||||
|
await askAboutInstructions();
|
||||||
|
|
||||||
|
let computerScore = 0;
|
||||||
|
let playerScore = 0;
|
||||||
|
|
||||||
// Generate a random deck
|
// Generate a random deck
|
||||||
for (j = 1; j <= 52; j++) {
|
const gameSize = cards.length; // Number of cards to shuffle into the game deck. Can be <= cards.length.
|
||||||
do {
|
const deck = createGameDeck(cards, gameSize);
|
||||||
l[j] = Math.floor(52 * Math.random()) + 1;
|
let shouldContinuePlaying = true;
|
||||||
for (k = 1; k < j; k++) {
|
|
||||||
if (l[k] == l[j]) // Already in deck?
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} while (j != 1 && k < j) ;
|
|
||||||
}
|
|
||||||
l[j] = 0; // Mark the end of the deck
|
|
||||||
|
|
||||||
while (1) {
|
while (deck.length > 0 && shouldContinuePlaying) {
|
||||||
m1 = l[++p]; // Take a card
|
const playerCard = deck.shift(); // Take a card
|
||||||
m2 = l[++p]; // Take a card
|
const computerCard = deck.shift(); // Take a card
|
||||||
print("\n");
|
printCards(cards[playerCard], cards[computerCard]);
|
||||||
print("YOU: " + a[m1] + "\tCOMPUTER: " + a[m2] + "\n");
|
|
||||||
n1 = Math.floor((m1 - 0.5) / 4);
|
const playerCardValue = computeCardValue(playerCard);
|
||||||
n2 = Math.floor((m2 - 0.5) / 4);
|
const computerCardValue = computeCardValue(computerCard);
|
||||||
if (n1 < n2) {
|
if (playerCardValue < computerCardValue) {
|
||||||
a1++;
|
computerScore++;
|
||||||
print("THE COMPUTER WINS!!! YOU HAVE " + b1 + " AND THE COMPUTER HAS " + a1 + "\n");
|
print("THE COMPUTER WINS!!! YOU HAVE " + playerScore + " AND THE COMPUTER HAS " + computerScore + "\n");
|
||||||
} else if (n1 > n2) {
|
} else if (playerCardValue > computerCardValue) {
|
||||||
b1++;
|
playerScore++;
|
||||||
print("YOU WIN. YOU HAVE " + b1 + " AND THE COMPUTER HAS " + a1 + "\n");
|
print("YOU WIN. YOU HAVE " + playerScore + " AND THE COMPUTER HAS " + computerScore + "\n");
|
||||||
} else {
|
} else {
|
||||||
print("TIE. NO SCORE CHANGE.\n");
|
print("TIE. NO SCORE CHANGE.\n");
|
||||||
}
|
}
|
||||||
if (l[p + 1] == 0) {
|
|
||||||
print("\n");
|
if (deck.length === 0) {
|
||||||
print("\n");
|
printGameOver(playerScore, computerScore);
|
||||||
print("WE HAVE RUN OUT OF CARDS. FINAL SCORE: YOU: " + b1 + " THE COMPUTER: " + a1 + "\n");
|
|
||||||
print("\n");
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
while (1) {
|
else {
|
||||||
print("DO YOU WANT TO CONTINUE");
|
shouldContinuePlaying = await askYesOrNo("DO YOU WANT TO CONTINUE");
|
||||||
str = await input();
|
|
||||||
if (str == "YES")
|
|
||||||
break;
|
|
||||||
if (str == "NO")
|
|
||||||
break;
|
|
||||||
print("YES OR NO, PLEASE. ");
|
|
||||||
}
|
}
|
||||||
if (str == "NO")
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
print("THANKS FOR PLAYING. IT WAS FUN.\n");
|
print("THANKS FOR PLAYING. IT WAS FUN.\n");
|
||||||
print("\n");
|
print("\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user