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</ title >
7+ < style >
8+ body {
9+ display : flex;
10+ flex-direction : column;
11+ justify-content : center;
12+ align-items : center;
13+ align-content : center;
14+ background-color : rgb (107 , 141 , 171 );
15+ background-repeat : no-repeat;
16+ /* background-size: cover; */
17+ background-position-y : vertical ;
18+ background-image : url ("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQTD6rfzq2lVRsq2pF7_gNAwU_dCOkeUzTmpw&s" );
19+ }
20+ </ style >
21+ </ head >
22+ < body >
23+ < h1 style ="color: rgb(11, 42, 69); font-size: 70px; "> Rock-Paper-Scissor</ h1 >
24+
25+ < p style ="color: rgb(150, 29, 29); font-size: 25px; "> Select either rock, paper, or scissors to play the game!</ p >
26+
27+ < p style ="color: rgb(150, 29, 29); font-size: 25px; "> < strong > Rules:</ strong > “Rock breaks scissors, scissors cuts paper, paper covers rock.” </ p >
28+ < div class ='score '>
29+
30+ </ div >
31+ < div class ='message '>
32+
33+ </ div >
34+ < button type ='button ' style ="background-color: rgb(89, 142, 142); height: 50px; width:110px ; font-size: 20px; "> < strong > Rock</ strong > </ button >
35+ < button type ='button ' style ="background-color: rgb(89, 142, 142); height: 50px; width:110px ; font-size: 20px; "> < strong > Paper</ strong > </ button >
36+ < button type ='button ' style ="background-color: rgb(89, 142, 142); height: 50px; width:110px ; font-size: 20px; "> < strong > Scissors</ strong > </ button >
37+ < script >
38+
39+ const message = document . querySelector ( '.message' ) ;
40+ const score = document . querySelector ( '.score' ) ;
41+ const buttons = document . querySelectorAll ( 'button' ) ;
42+ const winnerScores = [ 0 , 0 ] ;
43+
44+ //add event listeners to buttons
45+ for ( let i = 0 ; i < buttons . length ; i ++ ) {
46+ buttons [ i ] . addEventListener ( 'click' , playGame ) ;
47+ }
48+
49+ function playGame ( e ) {
50+ //setup player's selection
51+ let playerSelection = e . target . innerText ;
52+ //setup a random number to select for computer
53+ //selects a number between 0 and 1 (1 not-inclusive)
54+ let computerSelection = Math . random ( ) ;
55+
56+ //if computerSelection is less than .34, computer selects Rock
57+ if ( computerSelection < .34 ) {
58+ computerSelection = 'Rock' ;
59+ } else if ( computerSelection <= .67 ) {
60+ computerSelection = 'Paper' ;
61+ } else {
62+ computerSelection = 'Scissors' ;
63+ }
64+
65+ //setup a function to compare winners and return result
66+ let result = checkWinner ( playerSelection , computerSelection ) ;
67+
68+ //output scores to the DOM
69+ if ( result === 'Player' ) {
70+ result += '<strong style="font-size:35px; color:rgb(150, 29, 29)"> wins!<strong>' ;
71+ //update score
72+ winnerScores [ 0 ] ++ ;
73+ }
74+
75+ if ( result === 'Computer' ) {
76+ result += ' <strong style="font-size:35px; color:rgb(150, 29, 29)">wins!<strong/>' ;
77+ winnerScores [ 1 ] ++ ;
78+ }
79+
80+ if ( result === 'Draw' ) {
81+ result += '<strong style="font-size:35px; color:rgb(150, 29, 29)"> It\'s a tie!<strong/>'
82+ }
83+
84+ //output score into Score DIV
85+ score . innerHTML = 'Player: [ ' + winnerScores [ 0 ] + ' ] Computer: [ ' + winnerScores [ 1 ] + ' ]' ;
86+
87+ //output player and computer's selections
88+ messenger ( 'Player: <strong>' + playerSelection + '</strong> Computer: <strong>' + computerSelection + '</strong><br>' + result ) ;
89+ }
90+
91+ function messenger ( selectionMessage ) {
92+ message . innerHTML = selectionMessage ;
93+ }
94+
95+ function checkWinner ( player , computer ) {
96+ if ( player === computer ) {
97+ return 'Draw' ;
98+ }
99+
100+ if ( player === 'Rock' ) {
101+ if ( computer === 'Paper' ) {
102+ return 'Computer' ;
103+ } else {
104+ return 'Player' ;
105+ }
106+ }
107+
108+ if ( player === 'Paper' ) {
109+ if ( computer === 'Scissors' ) {
110+ return 'Computer' ;
111+ } else {
112+ return 'Player' ;
113+ }
114+ }
115+
116+ if ( player === 'Scissors' ) {
117+ if ( computer === 'Rock' ) {
118+ return 'Computer' ;
119+ } else {
120+ return 'Player' ;
121+ }
122+ }
123+
124+ }
125+ </ script >
126+ </ body >
127+ </ html >
0 commit comments