Skip to content

Commit 6c22abe

Browse files
committed
Snake, Water , Gun Game using JS
1 parent 8e67895 commit 6c22abe

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

30_Excersice 2/index.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width">
7+
<title>Excersice 2 - Snake Water Gun Game</title>
8+
<link href="style.css" rel="stylesheet" type="text/css" />
9+
</head>
10+
11+
<body>
12+
<h1>Snake Water Gun Game</h1>
13+
<script src="script.js"></script>
14+
15+
<!-- <script>
16+
console.log("Also use this method to create script file")
17+
</script> -->
18+
19+
20+
</body>
21+
22+
</html>

30_Excersice 2/script.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
const snakeWaterGun = (user, comp) => {
3+
if(user == comp){
4+
return 0
5+
}
6+
/* Case covered --> sg gs sw ws gw wg */
7+
if(user == 's' && comp == 'g'){
8+
return -1
9+
}
10+
else if(user == 'g' && comp == 's'){
11+
return 1
12+
}
13+
14+
if(user == 's' && comp == 'w'){
15+
return -1
16+
}
17+
else if(user == 'w' && comp == 's'){
18+
return 1
19+
}
20+
21+
if(user == 'g' && comp == 'w'){
22+
return -1
23+
}
24+
else if(user == 'w' && comp == 'g'){
25+
return 1
26+
}
27+
}
28+
29+
let playAgain = true;
30+
do{
31+
let comp = Math.floor((Math.random()*100)+1)
32+
if(comp<33){
33+
comp = 's'
34+
}
35+
else if(comp>33 && comp<66){
36+
comp = 'w'
37+
}
38+
else{
39+
comp = 'g'
40+
}
41+
42+
43+
let user = prompt("Enter 's' for snake, 'w' for water and 'g' for gun")
44+
let result = snakeWaterGun(user, comp)
45+
console.log(`You chose ${user} and computer chose ${comp}`)
46+
if(result==0){
47+
alert("Game Draw! Try Again")
48+
}
49+
else if(result==1){
50+
alert("You win!")
51+
}
52+
else if(result==-1){
53+
alert("You lose!")
54+
}
55+
else{
56+
alert("Please enter a valid keyword!")
57+
}
58+
59+
playAgain = confirm("Do you want to play again?")
60+
61+
}while(playAgain)

30_Excersice 2/style.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
html, body {
2+
height: 100%;
3+
width: 100%;
4+
background: aquamarine;
5+
}
6+
7+
h1{
8+
font-family: Arial, Helvetica, sans-serif;
9+
text-align: center;
10+
}

0 commit comments

Comments
 (0)