Skip to content

Commit da18ed9

Browse files
add main game
1 parent f3bc6fa commit da18ed9

3 files changed

Lines changed: 167 additions & 5 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Rock Paper Scissors Game
2+
3+
## Overview
4+
5+
This is a simple Rock Paper Scissors game implemented in JavaScript, HTML, and CSS. The game allows a user to play against the computer by choosing between Rock, Paper, and Scissors. The winner is determined based on the classic rules of the game.
6+
7+
## Features
8+
9+
- User vs. Computer: Play the classic Rock Paper Scissors game against the computer.
10+
- Visual Feedback: The game provides visual feedback with images representing the player's and computer's choices.
11+
- Score Tracking: The game keeps track of the player's score, updating it based on wins and losses.
12+
Responsive Design: The interface is designed to be responsive and accessible on various devices.
13+
14+
## How to Play
15+
16+
1 Open the game in a web browser.
17+
2 Click on one of the available choices: Rock, Paper, or Scissors.
18+
3 The computer randomly selects its choice.
19+
4 The winner is determined based on the game rules.
20+
5 The result is displayed along with an updated score.
21+
22+
## Setup
23+
24+
No special setup is required to run the game. Simply open the index.html file in a web browser.
25+
26+
## Technologies Used
27+
28+
- HTML
29+
- Tailwind CSS
30+
- JavaScript
31+
32+
### Project Structure
33+
34+
- index.html: The main HTML file containing the game structure.
35+
- style.css: The CSS file for styling the game.
36+
- main.js: The JavaScript file containing the game logic.

Projects/RockPaperScsissors/index.html

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,66 @@
1818
<span class="font-bold text-4xl text-blue-400" id="score">0</span>
1919
</div>
2020
</div>
21-
<div class="justify-center flex">
21+
<div class="justify-center flex" id="game">
2222
<div
2323
class="relative bg-[url('./assets/svg/bg-triangle.svg')] h-[300px] w-[305px] bg-no-repeat bg-center"
2424
>
2525
<div
26+
onclick="Main.play(PAPER)"
2627
class="bg-white absolute left-0 top-[-10%] rounded-full w-[100px] h-[100px] flex hover:shadow-[0_1px_20px_10px_#f8f8f8]"
2728
>
2829
<img src="./assets/svg/icon-paper.svg" class="m-auto" />
2930
</div>
3031
<div
32+
onclick="Main.play(SCISSORS)"
3133
class="bg-white absolute right-0 top-[-10%] rounded-full w-[100px] h-[100px] flex hover:shadow-[0_1px_20px_10px_#f8f8f8]"
3234
>
3335
<img src="./assets/svg/icon-scissors.svg" class="m-auto" />
3436
</div>
3537
<div
38+
onclick="Main.play(ROCK)"
3639
class="bg-white absolute left-[34%] bottom-0 rounded-full w-[100px] h-[100px] flex hover:shadow-[0_1px_20px_10px_#f8f8f8]"
3740
>
3841
<img src="./assets/svg/icon-rock.svg" class="m-auto" />
3942
</div>
4043
</div>
4144
</div>
42-
<button
43-
class="bg-white w-[100px] border-0 text-sky-500 rounded-lg m-auto pointer"
45+
<div
46+
class="flex justify-between w-[100%] md:w-[80%] m-auto hidden"
47+
id="result"
4448
>
45-
Reset game
46-
</button>
49+
<div class="flex-col flex gap-5 items-center">
50+
<span class="text:xl md:text-2xl font-bold text-white"
51+
>You picked</span
52+
>
53+
<div class="bg-white rounded-full w-[100px] h-[100px] flex">
54+
<img class="m-auto" id="player" />
55+
</div>
56+
</div>
57+
<div class="flex flex-col items-center gap-4">
58+
<span
59+
class="text:xl md:text-2xl font-bold text-white"
60+
id="result-text"
61+
>You win</span
62+
>
63+
<button
64+
onclick="Main.playAgain()"
65+
class="bg-white w-[100px] border-0 text-sky-500 rounded-lg pointer"
66+
>
67+
Play again
68+
</button>
69+
</div>
70+
<div class="flex-col flex items-center gap-5">
71+
<span class="text:xl md:text-2xl font-bold text-white"
72+
>Computer pick</span
73+
>
74+
<div class="bg-white rounded-full w-[100px] h-[100px] flex">
75+
<img id="computer" class="m-auto" />
76+
</div>
77+
</div>
78+
</div>
4779
</div>
4880
</div>
4981
</body>
82+
<script src="./main.js"></script>
5083
</html>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
const PAPER = 1;
2+
const SCISSORS = 2;
3+
const ROCK = 3;
4+
5+
const gameElements = {
6+
resultHtml: document.getElementById("result"),
7+
gameHtml: document.getElementById("game"),
8+
playerHtml: document.getElementById("player"),
9+
computerHtml: document.getElementById("computer"),
10+
resultTextHtml: document.getElementById("result-text"),
11+
scoreHtml: document.getElementById("score"),
12+
};
13+
14+
const Main = {
15+
score: 0,
16+
player: null,
17+
computer: null,
18+
result: "",
19+
20+
play(type) {
21+
console.log("ok");
22+
this.computer = this.getRandomItem([PAPER, SCISSORS, ROCK]);
23+
this.player = type;
24+
25+
this.updateImages(type, this.computer);
26+
this.calculateResult();
27+
this.displayResult();
28+
},
29+
30+
getRandomItem(items) {
31+
return items[Math.floor(Math.random() * items.length)];
32+
},
33+
34+
updateImages(playerType, computerType) {
35+
gameElements.playerHtml.src = `./assets/svg/icon-${this.getTypeName(
36+
playerType
37+
)}.svg`;
38+
gameElements.computerHtml.src = `./assets/svg/icon-${this.getTypeName(
39+
computerType
40+
)}.svg`;
41+
},
42+
43+
getTypeName(type) {
44+
switch (type) {
45+
case PAPER:
46+
return "paper";
47+
case SCISSORS:
48+
return "scissors";
49+
case ROCK:
50+
return "rock";
51+
default:
52+
return "";
53+
}
54+
},
55+
56+
calculateResult() {
57+
if (this.player === this.computer) {
58+
this.result = "Draw";
59+
} else if (
60+
(this.player === PAPER && this.computer === ROCK) ||
61+
(this.player === SCISSORS && this.computer === PAPER) ||
62+
(this.player === ROCK && this.computer === SCISSORS)
63+
) {
64+
this.result = "You win";
65+
this.score++;
66+
} else {
67+
this.result = "You Lose";
68+
this.score--;
69+
}
70+
},
71+
72+
displayResult() {
73+
this.hideElement(gameElements.gameHtml);
74+
this.showElement(gameElements.resultHtml);
75+
gameElements.resultTextHtml.textContent = this.result;
76+
gameElements.scoreHtml.textContent = this.score;
77+
},
78+
79+
hideElement(element) {
80+
element.classList.add("hidden");
81+
element.classList.remove("block");
82+
},
83+
84+
showElement(element) {
85+
element.classList.remove("hidden");
86+
element.classList.add("block");
87+
},
88+
89+
playAgain() {
90+
this.showElement(gameElements.gameHtml);
91+
this.hideElement(gameElements.resultHtml);
92+
},
93+
};

0 commit comments

Comments
 (0)