Skip to content

Commit

Permalink
Final push
Browse files Browse the repository at this point in the history
  • Loading branch information
emrebilge committed Sep 21, 2023
1 parent 69c9680 commit 294934d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
36 changes: 30 additions & 6 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ app.use(express.json());

const baseURL = 'https://random-word-api.herokuapp.com/word';

let round = 1;
let score = 0;
let guesses = 0;
let correct = 0;
let currentWord = '';
let correctWords = [];
let skipped = 0;

let accuracy = 0;

Expand Down Expand Up @@ -44,19 +46,19 @@ app.get('/get_word', async (req, res) => {
const randomWord = await fetchRandomWord(); // Fetch a random word using the API
const scrambledWord = scrambleWord(randomWord); // Scramble the word
currentWord = randomWord;
res.json({ scrambledWord });
res.send({ scrambledWord });
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Error fetching a random word from the API' });
}
});

app.post('/validate', (req, res) => {
console.log(currentWord);
console.log(req.body.guess);
try {
const userGuess = req.body.guess;
guesses += 1;
accuracy = correct/guesses * 100;
accuracy = ((correct / guesses) * 100).toFixed(2); // Convert to string
console.log(currentWord[0]);
console.log(accuracy);
console.log(correct/guesses * 100);
Expand All @@ -68,11 +70,12 @@ app.post('/validate', (req, res) => {
score += 10;
correct += 1;
correctWords.push(currentWord[0]);
accuracy = ((correct/guesses) * 100).toFixed(2);
accuracy = ((correct / guesses) * 100).toFixed(2); // Convert to string
res.status(200).send({
score,
message: 'Correct guess', accuracy
});
round = round + 1;
} else {
res.status(200).send({
score,
Expand All @@ -85,11 +88,33 @@ app.post('/validate', (req, res) => {
}
});

app.post('/skip_word', async (req, res) => {
try {
// Make an HTTP GET request to the /get_word endpoint
const response = await axios.get('http://localhost:5551/get_word');

// Extract the scrambled word from the response
const scrambledWord = response.data.scrambledWord;

skipped += 1;
// Update the currentWord and increase guesses

guesses += 1;
accuracy = ((correct / guesses) * 100).toFixed(2); // Convert to string

res.status(200).send({ scrambledWord, accuracy});
} catch (error) {
console.error('Error in /skip_word:', error);
res.status(500).json({ message: 'Internal Server Error' });
}
});


app.get('/correct_words', (req, res) => {
res.send({ correctWords });
});


// end point to reset the game after the game is over
app.post('/reset_game', (req, res) => {
score = 0;
Expand All @@ -101,12 +126,11 @@ app.post('/reset_game', (req, res) => {

// Retrieve the accuracy
app.get('/accuracy', (req, res) => {
accuracy = correct/guesses * 100;
accuracy = ((correct / guesses) * 100).toFixed(2); // Convert to string
res.send({ accuracy });
});



app.listen(PORT, () => {
console.log(`Backend is running on http://localhost:${PORT}`);
});
2 changes: 1 addition & 1 deletion frontend/word-guessing-game/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
name="Ekreb word scrambling game"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo1.jpg" />
Expand Down

0 comments on commit 294934d

Please sign in to comment.