Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

just a learn..Any suggestions?...thanks... #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 39 additions & 69 deletions wordgame/src/App.vue
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
<div id="app"> <div id="app">
<h1>Guess the {{ currentWord.length }}-letter word</h1> <h1>Guess the {{ currentWord.length }}-letter word</h1>
<h3 style="margin-bottom: 40px">Choose from the letters below</h3> <h3 style="margin-bottom: 40px">Choose from the letters below</h3>

<!-- A circular progress indicator --> <!-- A circular progress indicator -->
<div class="progress-circle"> <div class="progress-circle">
<div class="pc-overlay">{{ progressPercent }}%</div> <div class="pc-overlay">{{ progressPercent }}%</div>
<div <div
class="pc-background" class="pc-background"
:style="{ :style="{
background: `conic-gradient(#ec826f ${degrees}deg, #ddd ${degrees}deg)`, background: `conic-gradient(#ec826f ${progressPercent * 3.6}deg, #ddd ${progressPercent * 3.6}deg)`,
}" }"
></div> ></div>
</div> </div>
Expand All @@ -27,7 +26,7 @@
class="word-display" class="word-display"
v-for="(l, i) in currentWord.split('')" v-for="(l, i) in currentWord.split('')"
:key="i" :key="i"
:value="getGuessedLetter(i)" :value="getGuessedLetter[i]"
/> />
</div> </div>


Expand All @@ -41,126 +40,97 @@
}" }"
></div> ></div>
<button <button
:class="getLetterButtonClass(l)" :class="getLetterButtonClass[l]"
v-for="l in alphabets" v-for="(l) in alphabets"
:key="l" :key="l"
:title="currentGuess.includes(l) ? `${l} already picked` : `Pick ${l}`" :title="currentGuess.includes(l) ? `${l} already picked` : `Pick ${l}`"
:style="{ cursor: currentGuess.includes(l) ? 'default' : 'pointer' }" :style="{ cursor: currentGuess.includes(l) ? 'default' : 'pointer' }"
@click="makeGuess(l)" @click="makeGuess(l)"
> >{{ l }}</button>
{{ l }} </div>
</button> <div
v-if="puzzleSolved"
>{{ `Congratulations! You found the word ${currentWord} in ${tries} tries! Click on the button below to begin a new game.` }}</div>
<div>
<button id="new_game" @click="loadGame">New Game</button>
</div> </div>
<div>{{ message }}</div>
<div><button id="new_game" @click="loadGame">New Game</button></div>
<div id="credits"> <div id="credits">
<div> <div>
Background Photo by Background Photo by
<a <a
target="_blank" target="_blank"
href="https://www.pexels.com/photo/date-arrow-calendar-time-5652114/" href="https://www.pexels.com/photo/date-arrow-calendar-time-5652114/"
> >Visual Tag Mx from Pexels</a>
Visual Tag Mx from Pexels
</a>
</div> </div>
<div> <div>
Word list from Word list from
<a <a
target="_blank" target="_blank"
href="https://www.ef.com/wwen/english-resources/english-vocabulary/top-3000-words/" href="https://www.ef.com/wwen/english-resources/english-vocabulary/top-3000-words/"
>https://www.ef.com/wwen/english-resources/english-vocabulary/top-3000-words/</a >https://www.ef.com/wwen/english-resources/english-vocabulary/top-3000-words/</a>
>
</div> </div>
</div> </div>
</div> </div>
</template> </template>


<script> <script>
import Constants from "./Constants"; import Constants from "./Constants";

export default { export default {
name: "WordGame", name: "WordGame",


data() { data() {
return { return {
currentWord: "",
currentGuess: [], currentGuess: [],
tries: 0, currentWord: "",
progress: 0, alphabets: Constants.ALPHABETS.split("")
message: "",
}; };
}, },


mounted() { mounted() {
this.loadGame(); this.loadGame()
}, },


computed: { computed: {
words() { progress() {
return Constants.WORD_LIST.split(","); return this.currentGuess.map(g => this.currentWord.split("").filter((e) => e === g).length).reduce((q, w) => q + w, 0)
},

alphabets() {
return Constants.ALPHABETS.split("");
}, },

progressPercent() { progressPercent() {
return Math.round((this.progress / this.currentWord.length) * 100); return Math.round((this.progress / this.currentWord.length) * 100);
}, },

degrees() {
return Math.round((this.progress / this.currentWord.length) * 360);
},

puzzleSolved() { puzzleSolved() {
return this.progress === this.currentWord.length; return this.progress === this.currentWord.length;
}, },
tries() {
return this.currentGuess.length
}, },

getGuessedLetter() {
methods: { return this.currentWord.split("").map((e) => {
reset() { if (this.currentGuess.includes(e)) {
this.currentWord = ""; return e;
this.currentGuess = [];
this.tries = 0;
this.progress = 0;
this.message = "";
},

loadGame() {
this.reset();

let rnd = Math.floor(Math.random() * this.words.length);
this.currentWord = this.words[rnd].toUpperCase();
},

getGuessedLetter(index) {
if (this.currentGuess.includes(this.currentWord[index])) {
return this.currentWord[index];
} }

return ""; return "";
})
},
getLetterButtonClass() {
return this.alphabets.reduce((all, letter) => {
if (this.currentGuess.includes(letter)) all[letter] = "letter-button-disabled";
else all[letter] = "letter-button";
return all
}, {})
},
}, },


getLetterButtonClass(letter) { methods: {
if (this.currentGuess.includes(letter)) return "letter-button-disabled"; loadGame() {
this.currentGuess = [];


return "letter-button"; const word = Constants.WORD_LIST.split(",")
const rnd = Math.floor(Math.random() * word.length);
this.currentWord = word[rnd].toUpperCase();
}, },

makeGuess(letter) { makeGuess(letter) {
if (this.currentGuess.includes(letter)) return; if (this.currentGuess.includes(letter)) return;
this.currentGuess.push(letter); this.currentGuess.push(letter);
this.tries++;

// this.currentWord.split("") -> converts the string to an array
// then the array is filtered upon which of its elements matches the letter
// the length of the filtered array is how many characters were guessed correctly
this.progress += this.currentWord.split("").filter((e) => e === letter).length;

if (this.puzzleSolved) {
// solved
this.message = `Congratulations! You found the word ${this.currentWord} in ${this.tries} tries! Click on the button below to begin a new game.`;
}
}, },
}, },
}; };
Expand Down