Repository navigation
Expand file tree
/
Copy pathword.js
More file actions
148 lines (128 loc) · 5.56 KB
/
Copy pathword.js
File metadata and controls
148 lines (128 loc) · 5.56 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
// WORD
//
// Converted from BASIC to Javascript by Oscar Toledo G. (nanochess)
//
function print(str)
{
document.getElementById("output").appendChild(document.createTextNode(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);
}
});
});
}
function tab(space)
{
let str = "";
while (space-- > 0)
str += " ";
return str;
}
// These are the words that the game knows about> If you want a bigger challenge you could add more words to the array
const WORDS = ["DINKY", "SMOKE", "WATER", "GLASS", "TRAIN",
"MIGHT", "FIRST", "CANDY", "CHAMP", "WOULD",
"CLUMP", "DOPEY"];
const WORD_COUNT = WORDS.length;
// Main control section
async function main()
{
print(tab(33) + "WORD\n");
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
print("\n");
print("\n");
print("\n");
print("I AM THINKING OF A WORD -- YOU GUESS IT. I WILL GIVE YOU\n");
print("CLUES TO HELP YOU GET IT. GOOD LUCK!!\n");
print("\n");
print("\n");
outer: while (1) {
print("\n");
print("\n");
print("YOU ARE STARTING A NEW GAME...\n");
const secretWord = WORDS[Math.floor(Math.random() * WORD_COUNT)];
let guessCount = 0;
// This array holds the letters which have been found in the correct position across all guesses
// For instance if the word is "PLAIN" and the guesses so far are
// "SHALL" ("A" correct) and "CLIMB" ("L" correct) then it will hold "-LA--"
const knownLetters = [];
for (let i = 0; i < 5; i++)
knownLetters[i] = "-";
let guess = undefined;
while (1) {
print("GUESS A FIVE LETTER WORD");
guess = (await input()).toUpperCase();
guessCount++;
if (secretWord === guess) {
// The player has guessed correctly
break;
}
if (guess.charAt(0) === "?") {
// Player has given up
print("THE SECRET WORD IS " + secretWord + "\n");
print("\n");
// Start a new game by going to the start of the outer while loop
continue outer;
}
if (guess.length !== 5) {
print("YOU MUST GUESS A 5 LETTER WORD. START AGAIN.\n");
print("\n");
guessCount--;
continue;
}
// Two things happen in this double loop:
// 1. Letters which are in both the guessed and secret words are put in the lettersInCommon array
// 2. Letters which are in the correct position in the guessed word are added to the knownLetters array
let lettersInCommonCount = 0;
const lettersInCommon = [];
for (let i = 0; i < 5; i++) {// loop round characters in secret word
let secretWordCharacter = secretWord.charAt(i);
for (let j = 0; j < 5; j++) {// loop round characters in guessed word
let guessedWordCharacter = guess.charAt(j);
if (secretWordCharacter === guessedWordCharacter) {
lettersInCommon[lettersInCommonCount] = guessedWordCharacter;
if (i === j) {
// Letter is in the exact position so add to the known letters array
knownLetters[j] = guessedWordCharacter;
}
lettersInCommonCount++;
}
}
}
const lettersInCommonText = lettersInCommon.join("");
print("THERE WERE " + lettersInCommonCount + " MATCHES AND THE COMMON LETTERS WERE... " + lettersInCommonText + "\n");
const knownLettersText = knownLetters.join("");
print("FROM THE EXACT LETTER MATCHES, YOU KNOW............ " + knownLettersText + "\n");
if (knownLettersText === secretWord) {
guess = knownLettersText;
break;
}
if (lettersInCommonCount <= 1) {
print("\n");
print("IF YOU GIVE UP, TYPE '?' FOR YOUR NEXT GUESS.\n");
print("\n");
}
}
print("YOU HAVE GUESSED THE WORD. IT TOOK " + guessCount + " GUESSES!\n");
print("\n");
print("WANT TO PLAY AGAIN");
const playAgainResponse = (await input()).toUpperCase();
if (playAgainResponse !== "YES")
break;
}
}
main();