-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
153 lines (152 loc) · 4.67 KB
/
app.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
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
const app = Vue.createApp({
data: function (){
return {
game: {
guess: "",
words: ["Fruits", "Simba", "Object", "Programming", "Screen"],
points : 0,
strikes : 0,
scrambledWord: "",
wordNotScrumbled : "",
pickedIndex: 0,
passes: 3,
isVisible: false,
playAgainVisible: false,
messageBody: "",
alertClass: "",
disabled: 0,
won: false,
lost: false
}
};
},
mounted: function (){
if( localStorage.getItem("scrumble") ){
this.game = JSON.parse( localStorage.getItem("scrumble") ); //this.game = JSON.parse(localStorage.getItem("scrumble"));
}else{
this.generateNewWord();
}
},
watch: {
'game.points': function (newVal){
if(newVal >= 3 && this.game.words.length < 1 ) {
this.wonGame();
}
},
'game.strikes': function (newVal){
if( newVal >= 3 ){
this.lostGame();
}
}
},
methods: {
lostGame: function(){
this.lost = true;
this.game.messageBody = "😭😭Sorry😭😭 you lost the game! Try again!";
this.isVisible = true;
},
wonGame: function () {
this.won = true;
this.game.messageBody = "🌟🌟Congratulations🌟🌟 you won the game! Want to try again ? ";
this.isVisible = true;
},
passWord: function ( ) {
if ( this.game.passes < 1 ){
alert("You can't pass anymore !");
return;
}
else{
this.game.passes--;
this.game.words.splice(this.game.pickedIndex, 1);
this.generateNewWord();
}
},
initGameState: function ( ){
let game = {
guess: "",
words: ["Fruits", "Simba", "Object", "Programming", "Screen"],
points : 0,
strikes : 0,
scrambledWord: "",
wordNotScrumbled : "",
pickedIndex: 0,
passes: 3,
isVisible: false,
playAgainVisible: false,
messageBody: "",
alertClass: "",
disabled: 0,
won: false
}
this.game = game;
},
playAgain: function ( ) {
localStorage.removeItem("scrumble");
this.initGameState();
this.generateNewWord();
},
updateStorage: function( ) {
localStorage.setItem("scrumble", JSON.stringify(this.game));
},
generateNewWord: function( ) {
this.game.pickedIndex = this.pickRandomWordsIndex( this.game.words );
this.game.wordNotScrumbled = this.game.words[this.game.pickedIndex];
this.game.scrambledWord = this.shuffle( this.game.words[this.game.pickedIndex] ).toLowerCase();
this.updateStorage();
},
pickRandomWordsIndex: function( array ) {
let indexOfWord = Math.floor(Math.random() * array.length);
return indexOfWord;
},
shuffle : function( src ) {
const copy = [...src]
const length = copy.length
for (let i = 0; i < length; i++) {
const x = copy[i]
const y = Math.floor(Math.random() * length)
const z = copy[y]
copy[i] = z
copy[y] = x
}
if (typeof src === 'string') {
return copy.join('')
}
return copy
},
guessWord: function() {
if( this.game.strikes < 3 ){
if( this.game.guess.toLowerCase().trim() === this.game.wordNotScrumbled.toLowerCase() ){
this.game.points++;
this.game.messageBody = "Correct you have guessed it, Next!"
this.game.alertClass = "alert alert-primary"
this.game.isVisible = true;
setTimeout(function () { vm.$data.game.isVisible = false; }, 5000)
this.game.words.splice(this.game.pickedIndex, 1);
this.game.guess = ""
this.updateStorage();
if(this.game.words.length > 0) {
this.generateNewWord();
}
else {
this.game.playAgainVisible = true;
this.game.disabled = 1;
}
} else if ( this.game.strikes <= 3 ) {
this.game.strikes++;
this.updateStorage();
this.game.messageBody = "Wrong! Give it another shot.";
this.game.isVisible = true;
this.game.alertClass = "alert alert-danger";
setTimeout(function () { vm.$data.game.isVisible = false }, 5000);
this.updateStorage();
}
}
else{
this.game.playAgainVisible = true;
this.game.disabled = 1;
this.updateStorage();
}
}
}
});
const vm = app.mount('#app')