Skip to content

Commit ca7a2aa

Browse files
committed
Adding my Java-Redo lab
1 parent 6cbae2e commit ca7a2aa

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

Code/renan/java/RPS/app.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const computerChoiceDisplay = document.getElementById('computer-choice')
2+
const playerChoiceDisplay = document.getElementById('player-choice')
3+
const resultDisplay = document.getElementById('result')
4+
const choices = document.querySelectorAll('button')
5+
let playerChoice
6+
let computerChoice
7+
let result
8+
9+
// console.log(computerChoiceDisplay)
10+
// console.log(computerChoiceDisplay.id)
11+
//console.log(choices)
12+
13+
14+
15+
console.log(playerChoiceDisplay.innerHTML, 'before')
16+
//listeners
17+
choices.forEach(choice=>choice.addEventListener('click', (e)=>{
18+
playerChoice = e.target.id
19+
playerChoiceDisplay.innerHTML = playerChoice
20+
21+
createComputerChoice()
22+
getResult()
23+
//console.log(playerChoiceDisplay, 'after')
24+
}))
25+
26+
function createComputerChoice(){
27+
const randomNumber = Math.floor(Math.random() *choices.length) +1 //3 +1
28+
console.log(randomNumber)
29+
30+
if(randomNumber === 1){
31+
computerChoice = 'rock'
32+
}
33+
if(randomNumber=== 2){
34+
computerChoice = 'scissors'
35+
}
36+
if(randomNumber=== 3){
37+
computerChoice = 'paper'
38+
}
39+
40+
computerChoiceDisplay.innerHTML = computerChoice
41+
42+
}
43+
function getResult(){
44+
45+
if(computerChoice===playerChoice){
46+
result = 'We have a tie!'
47+
}
48+
if(computerChoice=== 'rock' && playerChoice ==='paper'){
49+
result = 'You won the game'
50+
}
51+
if(computerChoice=== 'rock' && playerChoice ==='scissors'){
52+
result = 'You lost the game'
53+
}
54+
if(computerChoice=== 'paper' && playerChoice ==='scissors'){
55+
result = 'You won the game'
56+
}
57+
if(computerChoice=== 'paper' && playerChoice ==='rock'){
58+
result = 'You lost the game'
59+
}
60+
if(computerChoice=== 'scissors' && playerChoice ==='rock'){
61+
result = 'You won the game'
62+
}
63+
if(computerChoice=== 'scissors' && playerChoice ==='paper'){
64+
result = 'You lost the game'
65+
}
66+
67+
resultDisplay.innerHTML = result
68+
}
69+
70+
createComputerChoice()

Code/renan/java/RPS/index.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>RPS</title>
8+
<script defer src="app.js"></script>
9+
10+
</head>
11+
<body>
12+
<h2>Computer Choice: <span id="computer-choice"></span></h2>
13+
<h2>Player Choice: <span id="player-choice"></span></h2>
14+
<h2>Result: <span id="result"></span></h2>
15+
16+
17+
<button id="rock">Rock</button>
18+
<button id="paper">Paper</button>
19+
<button id="scissors">Scissors</button>
20+
21+
</body>
22+
</html>

0 commit comments

Comments
 (0)