File tree Expand file tree Collapse file tree 6 files changed +95
-0
lines changed
RockPaperScissorsGame/AckermanLevi1 Expand file tree Collapse file tree 6 files changed +95
-0
lines changed Original file line number Diff line number Diff line change 1+ body {
2+ font-family : Arial, sans-serif;
3+ text-align : center;
4+ }
5+
6+ h1 {
7+ margin-top : 20px ;
8+ }
9+
10+ .choice {
11+ width : 100px ;
12+ height : 100px ;
13+ cursor : pointer;
14+ margin : 10px ;
15+ }
16+
17+ # game {
18+ display : flex;
19+ justify-content : center;
20+ }
21+
22+ # player , # computer {
23+ width : 200px ;
24+ }
25+
26+ # result {
27+ margin-top : 20px ;
28+ }
29+ # computer {
30+ display : none; /* Initially hidden */
31+ }
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6+ < title > Rock, Paper, Scissors Game</ title >
7+ < link rel ="stylesheet " href ="./index.css ">
8+ </ head >
9+ < body >
10+ < h1 > Rock, Paper, Scissors Game</ h1 >
11+ < div id ="game ">
12+ < div id ="player ">
13+ < h2 > You</ h2 >
14+ < img src ="rock.png " alt ="Rock " id ="rock " class ="choice ">
15+ < img src ="paper.png " alt ="Paper " id ="paper " class ="choice ">
16+ < img src ="scissors.png " alt ="Scissors " id ="scissors " class ="choice ">
17+ </ div >
18+ < div id ="computer ">
19+ < h2 > Computer</ h2 >
20+ < img src ="question.png " alt ="Question " id ="computer-choice " class ="choice ">
21+ </ div >
22+ < div id ="result ">
23+ < p > Choose your weapon!</ p >
24+ </ div >
25+ </ div >
26+ < script src ="./index.js "> </ script >
27+ </ body >
28+ </ html >
Original file line number Diff line number Diff line change 1+ const choices = [ "rock" , "paper" , "scissors" ] ;
2+
3+ function getResult ( player , computer ) {
4+ if ( player === computer ) return "It's a draw!" ;
5+ if (
6+ ( player === "rock" && computer === "scissors" ) ||
7+ ( player === "scissors" && computer === "paper" ) ||
8+ ( player === "paper" && computer === "rock" )
9+ ) {
10+ return "You win!" ;
11+ }
12+ return "Computer wins!" ;
13+ }
14+
15+ function displayResult ( result ) {
16+ const resultElement = document . getElementById ( "result" ) ;
17+ resultElement . textContent = result ; // Set the result text
18+ }
19+
20+ document . querySelectorAll ( ".choice" ) . forEach ( ( element ) => {
21+ element . addEventListener ( "click" , ( ) => {
22+ const playerChoice = element . id ;
23+ const computerChoice = choices [ Math . floor ( Math . random ( ) * 3 ) ] ;
24+ const computerChoiceImage = document . getElementById ( "computer-choice" ) ;
25+
26+ // Show the computer's choice element
27+ document . getElementById ( "computer" ) . style . display = "block" ;
28+
29+ // Update the computer's choice image
30+ computerChoiceImage . src = `${ computerChoice } .png` ;
31+
32+ const result = getResult ( playerChoice , computerChoice ) ;
33+
34+ displayResult ( result ) ;
35+ } ) ;
36+ } ) ;
You can’t perform that action at this time.
0 commit comments