generated from cis3296f22/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwordle.js
122 lines (106 loc) · 3.54 KB
/
wordle.js
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
// objective: make a wordle program!!
const fs = require('node:fs');
const readline = require('readline');
const prompt = require('prompt-sync')();
const { OpenAI } = require("openai");
async function wordArray() {
const fileStream = fs.createReadStream('dictionary.txt');
// from https://stackoverflow.com/questions/6156501/read-a-file-one-line-at-a-time-in-node-js
const lineReader = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
const array = [];
for await (const line of lineReader) {
// wordles are five letter words
if (line.length == 5) {
array.push(line.toUpperCase());
}
}
return array;
}
async function guessMaker(array) {
let guess = prompt('Guess a five letter word: ').toUpperCase();
while (guess.length != 5) {
if (guess.length != 5) {
guess = prompt('Word not of correct length, please input again: ').toUpperCase();
}
else if (!array.includes(guess)) {
guess = prompt('Word not found, please input again: ').toUpperCase();
}
}
return guess;
}
async function getRandom5LetterWordFromChatgpt() {
const openai = new OpenAI({
apiKey: 'API_SECRET_KEY'
});
try {
const chatCompletion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [{"role": "user", "content": "Give me a random word that is exactly 5 letters long."}],
});
console.log(chatCompletion.choices[0].message);
// Extract the word from the response
const randomWord = chatCompletion.choices[0].message.content
// console.log("Generated word:", randomWord);
return randomWord.trim().toUpperCase()
} catch (error) {
console.error("Error generating word:", error);
}
}
// helper method to count number of G (correct) in a word
async function countCorrect(word) {
let gCount = 0;
for (let i = 0; i < word.length; i++) {
if (word.charAt(i) == 'G') {
gCount += 1;
}
}
return gCount;
}
// modify this for discord interface
// will have to modify in the future to accept shop parameters
async function main() {
const array = await wordArray();
// const randomWord = array[Math.floor(Math.random() * array.length)];
const randomWord = await getRandom5LetterWordFromChatgpt()
// console.log("retruned " + randomWord)
let numGuesses = 6;
let numLetters = 5;
while (numGuesses > 0) {
const guess = (await guessMaker(array)).toString();
// now compare word to guess
/*
for now:
'G' (green) = right letter, right place
'Y' (yellow) = right letter, wrong place
'X' (gray) = wrong letter
*/
let resultString = '';
for (let i = 0; i < guess.length; i++) {
if (guess.charAt(i) == randomWord.charAt(i)) {
resultString += 'G';
}
else if (randomWord.includes(guess.charAt(i))) {
resultString += 'Y';
}
else {
resultString += 'X';
}
}
console.log(resultString);
numLetters = 5 - (await countCorrect(resultString));
numGuesses = numGuesses - 1;
if (numLetters == 0) {
break;
}
}
if (numLetters == 0) {
console.log('you\'ve won, the word is ' + randomWord);
}
else {
console.log('you\'ve lost, the word is ' + randomWord);
}
}
main();