Repository navigation
Expand file tree
/
Copy pathwar.js
More file actions
164 lines (144 loc) · 4.75 KB
/
Copy pathwar.js
File metadata and controls
164 lines (144 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// WAR
//
// Original conversion from BASIC to Javascript by Oscar Toledo G. (nanochess)
//
function print(str) {
document.getElementById("output").appendChild(document.createTextNode(str));
}
function tab(space) {
let str = "";
while (space-- > 0) {
str += " ";
}
return str;
}
function input() {
return new Promise(function (resolve) {
const 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_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);
}
});
});
}
async function askYesOrNo(question) {
while (1) {
print(question);
const str = (await input()).toUpperCase();
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(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
print("\n");
print("\n");
print("\n");
print("THIS IS THE CARD GAME OF WAR. EACH CARD IS GIVEN BY SUIT-#\n");
print("AS S-7 FOR SPADE 7. ");
}
function printCards(playerCard, computerCard) {
print("\n");
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
const gameSize = cards.length; // Number of cards to shuffle into the game deck. Can be <= cards.length.
const deck = createGameDeck(cards, gameSize);
let shouldContinuePlaying = true;
while (deck.length > 0 && shouldContinuePlaying) {
const playerCard = deck.shift(); // Take a card
const computerCard = deck.shift(); // Take a card
printCards(cards[playerCard], cards[computerCard]);
const playerCardValue = computeCardValue(playerCard);
const computerCardValue = computeCardValue(computerCard);
if (playerCardValue < computerCardValue) {
computerScore++;
print("THE COMPUTER WINS!!! YOU HAVE " + playerScore + " AND THE COMPUTER HAS " + computerScore + "\n");
} else if (playerCardValue > computerCardValue) {
playerScore++;
print("YOU WIN. YOU HAVE " + playerScore + " AND THE COMPUTER HAS " + computerScore + "\n");
} else {
print("TIE. NO SCORE CHANGE.\n");
}
if (deck.length === 0) {
printGameOver(playerScore, computerScore);
}
else {
shouldContinuePlaying = await askYesOrNo("DO YOU WANT TO CONTINUE");
}
}
print("THANKS FOR PLAYING. IT WAS FUN.\n");
print("\n");
}
main();