-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.html
More file actions
120 lines (110 loc) · 4.14 KB
/
game.html
File metadata and controls
120 lines (110 loc) · 4.14 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Daksh Hande - Personal Portfolio Games</title>
<style>
/* Your existing CSS styles */
/* Additional styles for the game section */
section {
max-width: 800px;
margin: 20px auto;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
input, button {
margin-top: 10px;
}
#game {
display: flex;
justify-content: space-around;
margin-bottom: 20px;
}
button {
padding: 10px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<header>
<h1>Welcome to games!</h1>
</header>
<section>
<h2>Guess the Number Game</h2>
<p>Try to guess the number between 1 and 10:</p>
<input type="number" id="userGuess" placeholder="Enter your guess">
<button onclick="checkGuess()">Submit Guess</button>
<p id="result"></p>
</section>
<section>
<h2>Rock, Paper, Scissors Game</h2>
<div id="game">
<button onclick="playGame('rock')">Rock</button>
<button onclick="playGame('paper')">Paper</button>
<button onclick="playGame('scissors')">Scissors</button>
</div>
<p id="result"></p>
</section>
<footer>
<p>Feel free to play</p>
<span>© 2024 Daksh Hande. All Rights Reserved.</span>
</footer>
<script src="game.js"></script>
<script>
// Guess the Number Game
let secretNumber = Math.floor(Math.random() * 10) + 1; // Random number between 1 and 10
let attempts = 3;
function checkGuess() {
const userGuess = document.getElementById('userGuess').value;
const resultMessage = document.getElementById('result');
if (userGuess === '') {
resultMessage.textContent = 'Please enter a number!';
} else {
const guess = parseInt(userGuess);
if (!isNaN(guess)) {
attempts--;
if (guess === secretNumber) {
resultMessage.textContent = 'Congratulations! You guessed the correct number!';
resetGame();
} else if (attempts === 0) {
resultMessage.textContent = `Sorry, you're out of attempts. The correct number was ${secretNumber}.`;
resetGame();
} else {
resultMessage.textContent = `Wrong guess! ${attempts} attempt(s) remaining.`;
}
} else {
resultMessage.textContent = 'Invalid input. Please enter a valid number.';
}
}
}
function resetGame() {
secretNumber = Math.floor(Math.random() * 10) + 1;
attempts = 3;
document.getElementById('userGuess').value = '';
}
// Rock, Paper, Scissors Game
function playGame(playerChoice) {
const choices = ['rock', 'paper', 'scissors'];
const computerChoice = choices[Math.floor(Math.random() * 3)];
const resultMessage = document.getElementById('result');
if (playerChoice === computerChoice) {
resultMessage.textContent = `It's a tie! You both chose ${playerChoice}.`;
} else if (
(playerChoice === 'rock' && computerChoice === 'scissors') ||
(playerChoice === 'paper' && computerChoice === 'rock') ||
(playerChoice === 'scissors' && computerChoice === 'paper')
) {
resultMessage.textContent = `You win! ${playerChoice} beats ${computerChoice}.`;
} else {
resultMessage.textContent = `You lose! ${computerChoice} beats ${playerChoice}.`;
}
}
</script>
</body>
</html>