├── index.html
├── README.md
└── app.js
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Rock Paper Scissors
9 |
10 |
11 |
12 |
36 |
37 |
38 | CLICK TO START
39 |
40 |
41 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Rock-Paper-Scissors Game
2 |
3 | Rock Paper Scissors is a classic hand game played by two people. The rules are simple: each player chooses either rock, paper, or scissors, and the winner is determined by the rules below:
4 |
5 | 1. Rock beats scissors (rock crushes scissors)
6 | 2. Scissors beats paper (scissors cut paper)
7 | 3. Paper beats rock (paper covers rock).
8 |
9 | To create a Rock Paper Scissors game in JavaScript, we can use the prompt function to ask the player for their choice and the alert function to display the results. First, we can create a function that takes in the player's choice as an argument and determines the outcome based on the rules above. Then, we can use prompt to ask the player for their choice and store it in a variable. Next, we can use Math.random to generate a random number for the computer's choice. Finally, we can use alert to display the results of the game.
10 |
11 |
12 | ## Live Preview
13 | You can preview the project with [THIS LINK](https://calebchris000.github.io/Rock-Paper-Scissors/)
14 |
15 | ## Support
16 | If you like what you see, you don't need to donate anything, for now. You can appreciate the work by starring this repository and following me on Github.
17 |
--------------------------------------------------------------------------------
/app.js:
--------------------------------------------------------------------------------
1 |
2 | //Initialisation of user and computer scores.
3 | let humanScore = 0; let computerScore = 0
4 |
5 | //The function that runs the number of games
6 | function loopedRound() {
7 | let numOfGame = (prompt("How many game?"))
8 |
9 | //If the user clicks cancel
10 | if (numOfGame === null) {
11 | alert("See you :)")
12 | }
13 | else {
14 | for (let i = 0; i < numOfGame; i++) {
15 | userPlay()
16 | }
17 | whoWon()
18 | humanScore = computerScore = 0
19 | }
20 |
21 | }
22 |
23 | //Checks the differences of the scores
24 | function whoWon() {
25 |
26 | humanScore > computerScore ? alert(`You won by ${humanScore} : ${computerScore}`) :
27 | computerScore > humanScore ? alert(`You lost by ${computerScore} : ${humanScore}`) :
28 | alert("There is no winner")
29 |
30 | }
31 |
32 | //The computer play function itself. It's set at random
33 | function computerPlay() {
34 | let hands = ['Rock', 'Paper', 'Scissors'];
35 | let randomPick = Math.floor(Math.random() * 3);
36 | return hands[randomPick]
37 | }
38 |
39 | //The function that prompt the user of their hand
40 | function userPlay() {
41 | let user = prompt("What is your hand? (Rock = 1, Paper = 2, Scissors = 3)");
42 | return playRound(user, computerPlay())
43 |
44 | }
45 |
46 | //The main RPS function. It checks for the winner of each round.
47 | function playRound(user, computer) {
48 |
49 | const hands = {
50 | 1: 'Rock',
51 | 2: 'Paper',
52 | 3: 'Scissors'
53 | }
54 |
55 | let userlowercased = hands[user].toLowerCase();
56 | let computerLowerCased = computer.toLowerCase();
57 |
58 | if (userlowercased == computerLowerCased) {
59 | alert("Its a tie")
60 | return
61 | }
62 |
63 | if (userlowercased == "rock" && computerLowerCased == "scissors") {
64 | alert(`You Win! ${hands[user]} beats ${computer}`)
65 | humanScore += 1
66 | return
67 | }
68 | if (userlowercased == "paper" && computerLowerCased == "rock") {
69 | alert(`You Win! ${hands[user]} beats ${computer}`)
70 | humanScore += 1
71 | return
72 | }
73 | if (userlowercased == "scissors" && computerLowerCased == "paper") {
74 | alert(`You Win! ${hands[user]} beats ${computer}`)
75 | humanScore += 1
76 | return
77 | }
78 |
79 | else {
80 | alert(`You Lose! ${computer} beats ${hands[user]}`)
81 | computerScore += 1
82 | return
83 | }
84 |
85 | }
86 |
87 | document.addEventListener('keydown', function (event) {
88 | if (event.key == "Enter") {
89 | loopedRound()
90 | }
91 | })
--------------------------------------------------------------------------------