hangman/script.js

228 lines
7.7 KiB
JavaScript
Raw Normal View History

2020-01-15 12:32:28 +00:00
/* Made by Rolf Martin Glomsrud from 13.01.2020 to 15.01.2020
Example of a project that is feasable for a student that has
had Informasjonsteknologi 2*/
2020-01-15 14:05:17 +00:00
var words = [
"scarf", "elbow", "cactus", "society", "aftermath", "fang", "bushes",
"furniture", "throat", "downtown", "rain", "soup", "regret", "tiger",
"vest", "line", "smash", "screw", "plate", "plate", "stage", "test", "boy",
"nest", "cherries", "cakes", "meat", "dolls", "spoon", "pot", "kiss", "measure",
"eyes", "shock", "smile", "scissors", "stomach", "ice", "root", "leather"
2020-01-15 12:32:28 +00:00
2020-01-15 14:05:17 +00:00
]
2020-01-15 12:32:28 +00:00
2020-01-15 14:05:17 +00:00
var randomNumber = Math.floor((Math.random()*40)+1);
var screenWidth = screen.width;
var screenHeight = screen.height;
2020-01-15 09:53:03 +00:00
var partsTracker = 0
2020-01-15 11:27:17 +00:00
var guessed = [];
var tries = 9;
2020-01-15 14:05:17 +00:00
var word = words[randomNumber];
var arrayWord = word.split("");
2020-01-15 09:53:03 +00:00
var whatTheUserSees = [];
var feil = [];
2020-01-15 12:32:28 +00:00
//makes an array of _ to show to the user
2020-01-15 09:53:03 +00:00
for (i=0; i < arrayWord.length; i++){
whatTheUserSees.push("_");
}
2020-01-15 09:53:03 +00:00
2020-01-15 12:32:28 +00:00
//converts the array from above to a string, then puts it into the HTML
2020-01-15 09:53:03 +00:00
var whatTheUserSeesWord = whatTheUserSees.join("");
document.getElementById("ord").innerHTML = whatTheUserSeesWord;
get("tries").innerHTML = tries;
2020-01-15 12:32:28 +00:00
//check if word is guessed already
2020-01-15 11:27:17 +00:00
function sjekkOrd(){
2020-01-15 14:05:17 +00:00
var guessOrd1 = get("myTextWord").value;
guessOrd = guessOrd1.toLowerCase();
2020-01-15 11:27:17 +00:00
get("myTextWord").innerHTML = "";
if (guessOrd == get("ord").innerHTML){
2020-01-16 08:55:30 +00:00
alert("The game is over!")
2020-01-15 11:27:17 +00:00
}else{
testWord();
}
}
2020-01-15 12:32:28 +00:00
2020-01-15 11:27:17 +00:00
function testWord(){
2020-01-15 12:32:28 +00:00
//check if user has more tries
2020-01-15 11:27:17 +00:00
if (tries <= 0){
2020-01-16 08:55:30 +00:00
alert("The game is over, please restart the page");
2020-01-15 12:32:28 +00:00
get("myTextWord").value = "";
}else if(get("ord").innerHTML==word){
2020-01-16 08:55:30 +00:00
alert("The game is over, please restart the page")
2020-01-15 12:32:28 +00:00
get("myTextWord").value="";
2020-01-15 11:27:17 +00:00
}else{
2020-01-15 12:32:28 +00:00
//grabs the guess from HTML
2020-01-15 14:05:17 +00:00
var guessedWord1 = get("myTextWord").value;
var guessedWord = guessedWord1.toLowerCase();
2020-01-15 12:32:28 +00:00
//adds it to the array of guessed words
2020-01-15 11:27:17 +00:00
guessed.push(guessedWord);
2020-01-15 12:32:28 +00:00
//checks if it was correct
2020-01-15 11:27:17 +00:00
if (word == guessedWord){
2020-01-16 08:55:30 +00:00
get("result").innerHTML = "Congratulations you won! ";
2020-01-15 11:27:17 +00:00
get("ord").innerHTML = word;
2020-01-15 12:32:28 +00:00
get("myTextWord").value = "";
//this plays if the guess was wrong
2020-01-15 12:01:42 +00:00
}else{
2020-01-15 12:32:28 +00:00
get("myTextWord").value = "";
//removes a try
2020-01-15 12:01:42 +00:00
tries -= 1;
2020-01-15 12:32:28 +00:00
//adds it to wrong words guessed array
2020-01-15 12:01:42 +00:00
feil.push(guessedWord);
2020-01-15 12:32:28 +00:00
//shows user the wrong word was guessed
2020-01-15 12:01:42 +00:00
var earlier = get("wrongLetters").innerHTML;
var nyInner = earlier + " " + guessedWord;
get("wrongLetters").innerHTML = nyInner;
2020-01-15 12:32:28 +00:00
//updates the amount of tries
2020-01-15 12:01:42 +00:00
get("tries").innerHTML = tries;
2020-01-15 12:32:28 +00:00
//draws a piece of hangman
2020-01-15 12:01:42 +00:00
draw();
2020-01-15 11:27:17 +00:00
}
2020-01-15 14:05:17 +00:00
if (tries == 0){
2020-01-16 08:55:30 +00:00
get("result").innerHTML = "Sorry, you lost. The word was" + word;
2020-01-15 14:05:17 +00:00
}else if(whatTheUserSeesWord == word){
2020-01-16 08:55:30 +00:00
get("result").innerHTML = "Congratulations you guessed the word!";
2020-01-15 14:05:17 +00:00
}
//removes the guess from the text input, ready for new guess
get("myText").value = "";
2020-01-15 11:27:17 +00:00
}
}
2020-01-15 12:32:28 +00:00
//checks if it is guessed
2020-01-15 11:27:17 +00:00
function sjekkBokstav(){
2020-01-15 14:05:17 +00:00
var guess1 = get("myText").value;
var guess = guess1.toLowerCase();
2020-01-15 11:27:17 +00:00
if (guessed.includes(guess)){
2020-01-16 08:55:30 +00:00
alert("You have already gussed that letter, please guess another one!");
2020-01-15 11:27:17 +00:00
}else{
testLetter();
}
}
function testLetter(){
2020-01-15 12:32:28 +00:00
//checks if the user is out of tries or they are done guessing
2020-01-15 11:27:17 +00:00
if (tries <= 0 || get("ord").innerHTML == word){
2020-01-16 08:55:30 +00:00
alert("The game is over, please restart the page");
2020-01-15 11:27:17 +00:00
get("myText").value = "";
}else{
2020-01-15 12:32:28 +00:00
//grabs the guess from HTML
2020-01-15 14:05:17 +00:00
var guess1 = get("myText").value;
var guess = guess1.toLowerCase();
2020-01-15 11:27:17 +00:00
guessed.push(guess);
2020-01-15 12:32:28 +00:00
//checks if the letter is in the word
2020-01-15 11:27:17 +00:00
if (arrayWord.includes(guess)){
2020-01-15 12:32:28 +00:00
//runs through the array of the word and changes the array of _ at the right position
2020-01-15 11:27:17 +00:00
for (i=0; i < arrayWord.length; i++){
if (guess == arrayWord[i]){
whatTheUserSees[i] = guess;
}
2020-01-15 09:53:03 +00:00
}
2020-01-15 12:32:28 +00:00
//if it is wrong it does the same as a wrong word guess at line 48
2020-01-15 11:27:17 +00:00
}else{
tries -= 1;
feil.push(guess);
var earlier = get("wrongLetters").innerHTML;
var nyInner = earlier + " " + guess;
get("wrongLetters").innerHTML = nyInner;
get("tries").innerHTML = tries;
draw();
2020-01-15 09:53:03 +00:00
}
2020-01-15 11:27:17 +00:00
var whatTheUserSeesWord = whatTheUserSees.join("");
document.getElementById("ord").innerHTML = whatTheUserSeesWord;
2020-01-15 12:32:28 +00:00
//checks if the user lost
2020-01-15 11:27:17 +00:00
if (tries == 0){
2020-01-16 08:55:30 +00:00
get("result").innerHTML = "Sorry, you lost. The word was " + word;
2020-01-15 11:27:17 +00:00
}else if(whatTheUserSeesWord == word){
2020-01-16 08:55:30 +00:00
get("result").innerHTML = "Congratulations you guessed the word!";
2020-01-15 11:27:17 +00:00
}
2020-01-15 12:32:28 +00:00
//removes the guess from the text input, ready for new guess
2020-01-15 11:27:17 +00:00
get("myText").value = "";
2020-01-14 12:04:21 +00:00
}
2020-01-15 11:27:17 +00:00
2020-01-14 12:04:21 +00:00
}
2020-01-15 12:32:28 +00:00
//initializing canvas
2020-01-13 18:16:42 +00:00
var c = get("myCanvas");
var ctx = c.getContext("2d");
2020-01-15 12:32:28 +00:00
//defining canvas size
ctx.canvas.width = 0.25 * screenWidth;
ctx.canvas.height = 0.25 * screenHeight;
2020-01-13 18:16:42 +00:00
ctx.moveTo(0, 0);
ctx.stroke();
2020-01-14 12:04:21 +00:00
function draw(){
if (partsTracker == 0){
partsTracker += 1
//draw platform
ctx.moveTo(0, 0);
ctx.moveTo(0.03*screenWidth, 0.23*screenHeight);
ctx.lineTo(0.08*screenWidth, 0.23*screenHeight);
ctx.stroke();
}else if(partsTracker == 1){
partsTracker += 1
//draw pole
ctx.moveTo(0.08*screenWidth, 0.23*screenHeight);
ctx.moveTo(0.055*screenWidth, 0.23*screenHeight);
ctx.lineTo(0.055*screenWidth, 0.05*screenHeight);
ctx.stroke()
}else if (partsTracker == 2){
partsTracker += 1
//draw linear pole
ctx.moveTo(0.055*screenWidth, 0.05*screenHeight);
ctx.lineTo(0.15*screenWidth, 0.05*screenHeight);
ctx.stroke();
}else if (partsTracker == 3){
partsTracker +=1
//draw rope
ctx.moveTo(0.15*screenWidth, 0.05*screenHeight);
ctx.lineTo(0.15*screenWidth, 0.1*screenHeight);
ctx.stroke();
}else if(partsTracker == 4){
//draw head
ctx.moveTo((0.15*screenWidth)+(0.012*screenWidth), 0.12*screenHeight);
ctx.arc(0.15*screenWidth, 0.12*screenHeight, 0.012*screenWidth,0, 2*Math.PI)
ctx.stroke();
partsTracker += 1
}else if(partsTracker == 5){
//draw body
ctx.moveTo(0.15*screenWidth, (0.12*screenHeight) + (0.021*screenHeight));
ctx.lineTo(0.15*screenWidth, 0.184*screenHeight);
ctx.stroke();
partsTracker += 1
}else if(partsTracker == 6){
//draw left leg
ctx.moveTo(0.15*screenWidth, 0.184*screenHeight);
ctx.lineTo(0.16*screenWidth, 0.21*screenHeight);
ctx.stroke();
partsTracker += 1
}else if(partsTracker == 7){
//draw right leg
ctx.moveTo(0.15*screenWidth, 0.184*screenHeight);
ctx.lineTo(0.14*screenWidth, 0.21*screenHeight);
ctx.stroke()
partsTracker += 1
}else if(partsTracker == 8){
// draw arms
ctx.moveTo(0.14*screenWidth, 0.16*screenHeight);
ctx.lineTo(0.16*screenWidth, 0.16*screenHeight);
ctx.stroke();
partsTracker += 1
}
}
2020-01-15 12:32:28 +00:00
//function to not write document.getElementById();
2020-01-13 18:16:42 +00:00
function get(a){
return document.getElementById(a);
2020-01-14 12:04:21 +00:00
}