-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
128 lines (127 loc) · 5.71 KB
/
game.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
123
124
125
126
127
128
const gameState = require('./gameState');
const gamePage = require('./gamePage');
const words = require('./words');
const game = {
randomWord: function(wordsList) {
const arrayLength = wordsList.length;
const randomWordIndex = Math.floor(Math.random() * arrayLength);
return wordsList[randomWordIndex];
},
letterMatchCounter: function(usersGuess, answer) {
let guessedWords = usersGuess.toUpperCase().split('').sort().join('');
let randomWords = answer.toUpperCase().split('').sort().join('');
let matchCount = 0;
let guessIndex = 0;
let randomIndex = 0;
while(guessIndex < guessedWords.length && randomIndex < randomWords.length){
if(guessedWords[guessIndex] === randomWords[randomIndex]){
matchCount++;
guessIndex++;
randomIndex++;
} else if(guessedWords[guessIndex] < randomWords[randomIndex]){
guessIndex++;
} else{
randomIndex++;
}
}
return matchCount;
},
upperCaseWordList: function(wordList) {
let upperCasedWordList = [];
for (aWord in wordList) {
upperCasedWordList.push(wordList[aWord].toUpperCase());
}
return upperCasedWordList;
},
gameLogic: function(req) {
if(req.cookies === null || req.cookies.gamestate === undefined) {
// If cookie is not available then create a new game.
let uuid = game.generateNewGame();
return uuid;
}
let uuid = req.cookies.gamestate.uuid;
const {userguess} = req.body;
let caseAdjustedUserGuess = userguess.toUpperCase();
let currGameState = gameState.getGameState(uuid);
if (currGameState.wordsList.indexOf(caseAdjustedUserGuess) < 0) {
// User guess is not is the game word list
let messageToUser = "Invalid word, try again!";
gameState.storeGameState(uuid, 1, currGameState.wordsList, currGameState.answer, currGameState.guessedWordsList, messageToUser, currGameState.noOfTries);
// set Cookies
return uuid;
}
// Increment the tries count.
let noOfTries = currGameState.noOfTries + 1;
if (caseAdjustedUserGuess === currGameState.answer) {
// Correct word was guessed
let messageToUser = "Yay!!! you guessed it right in " + noOfTries + " attempts";
gameState.storeGameState(uuid, 2, currGameState.wordsList, currGameState.answer, currGameState.guessedWordsList, messageToUser, noOfTries);
return uuid;
} else {
// Wrong word was guessed
// Find matching letters.
let matchingLettersCount = game.letterMatchCounter(caseAdjustedUserGuess, currGameState.answer);
// Update Guessed Word List
currGameState.guessedWordsList.push({ userguess, matchingLettersCount})
// Build message for the player
let messageToUser = userguess + " is not the secret word.\nBut it has " + matchingLettersCount + " letters in common.";
// Update game state.
gameState.storeGameState(uuid, 1, currGameState.wordsList, currGameState.answer, currGameState.guessedWordsList, messageToUser, noOfTries);
return uuid;
}
},
// Generates a web page based on the current state
loadPage: function(req) {
if(req.cookies === null || req.cookies.gamestate === undefined){
let uuid = game.generateNewGame();
let userPage = game.generatePage(uuid);
return {uuid, userPage};
}
let uuid = req.cookies.gamestate.uuid;
let currGameState = gameState.getGameState(uuid);
if (currGameState === undefined || currGameState.state === undefined) {
// Extra check added to handle a wierd bug in Opera's incognito mode.
// When opening this game page on a new incognito window, the browser
// kept sending a stale cookie. Same uuid was in the cookie even when
// retrying on new incognito windows.
let uuid = game.generateNewGame();
let userPage = game.generatePage(uuid);
return {uuid, userPage};
}
let userPage = game.generatePage(uuid);
return {uuid, userPage};
},
generateNewGame: function() {
const wordsList = game.upperCaseWordList(words.wordsArray);
let answer = game.randomWord(wordsList);
let uuid = Math.floor(Math.random() * 1000000) + 1;
let state = 0;
let noOfTries = 0;
// Empty array because no guesses from user yet
let guessedWordsList = [];
// Store new game: uuid, state, wordsList, answer, guessedWordsList, messageToUser
gameState.storeGameState(uuid, state, wordsList, answer, guessedWordsList, "", noOfTries);
// set Cookies
return uuid;
},
generatePage: function(uuid) {
// Read current game state
let currGameState = gameState.getGameState(uuid);
if (currGameState.state === 0) {
// Load new game page
return gamePage.startGame(currGameState.wordsList);
}
else if (currGameState.state === 1) {
// Should update current game page
return gamePage.updateGame(currGameState.wordsList, currGameState.guessedWordsList, currGameState.messageToUser, currGameState.noOfTries);
}
else if (currGameState.state === 2) {
// User has guessed the game
return gamePage.endGame(currGameState.wordsList, currGameState.guessedWordsList, currGameState.messageToUser);
}
else {
// Corrupted state
}
},
}
module.exports = game;