-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpizzaGuess.html
More file actions
80 lines (78 loc) · 2.34 KB
/
pizzaGuess.html
File metadata and controls
80 lines (78 loc) · 2.34 KB
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
<script>
//initialize variables
var toppings=['sausage','pepper','onion','octopus','mushroom','olive','tomato','pepperoni'];
var toppingString='sausage\npepper\nonion\noctopus\nmushroom\nolive\ntomato\npepperoni\n'
var answer=[];
//for loop selects random toppings to add to 'answer'
for (var i=0; i<toppings.length;i++){
x=Math.round(Math.random());
if (x==1){
answer.push(toppings[i]);
}
}
var running=true
var correct = false
var guessesRemaining=10
//This is the introductory line1. It will change in future prompts based on the previous guess
var line1='Guess what toppings I want on my pizza! The options are below.\nI may like none of them, all of them, or anything between!\nInput your guess with the toppings separated by single spaces.';
var line2='Guesses Remaining: '+guessesRemaining;
//this function turns a string into a list, with each element one of the words separated by spaces
function splitter(s){
var start=0;
var result=[];
var n=0;
while (n>-1){
n = s.indexOf(' ', start);
if (n!=-1){
result.push(s.substring(start,n));
}
else{
result.push(s.substring(start));
}
start=n+1;
}
return result;
}
//this function can check if there i a wrong topping or a missing topping
function wrongTopping(a,b){
found=false
for(var i=0;i<a.length;i++){
if (b.indexOf(a[i])==-1){
found=true
}
}
return found
}
var solved=false
//event handling loop
while (running==true){
//checks if it has been solved, and closes if so
if (solved){
alert('That\'s the pizza I wanted! You win.');
running=false;
}
//checks if the player has run out of guesses, and closes if so
else if (guessesRemaining==0){
alert('You ran out of guesses! You couldn\'n guess what pizza I like.\n We\'re not friends anymore');
running=false;
}
else{
//asks for guess and uses splitter function to turn it into a list
var guess=prompt(line1+'\n'+toppingString+'\n'+line2);
guess=splitter(guess);
//checks for wrong toppings
if (wrongTopping(guess, answer)){
line1='There\'s something on that I don\'t like!'
}
//if there are no wrong toppings, checks for missing toppings
else if (wrongTopping(answer, guess)){
line1='You\'re missing a topping that I want!'
}
else{
solved=true
}
guessesRemaining-=1
line2="Guesses Remaining: "+guessesRemaining
}
}
</script>