|
| 1 | + |
| 2 | + function rpsGame(yourChoice) { |
| 3 | + console.log(yourChoice); |
| 4 | + |
| 5 | + var humanChoice, botChoice; |
| 6 | + humanChoice = yourChoice.id; |
| 7 | + var imgs = [document.getElementById("rock"), document.getElementById("paper"), document.getElementById("scissors")] |
| 8 | + botChoice = imgs[Math.floor(Math.random() * 3)]; |
| 9 | + botChoice = botChoice.id; |
| 10 | + console.log('Computer choice:', botChoice); |
| 11 | + results = decideWinner(humanChoice, botChoice); |
| 12 | + console.log(results); |
| 13 | + message = finalMessage(results); |
| 14 | + |
| 15 | + |
| 16 | + rpsFrontend(yourChoice.id, botChoice, message); |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | + function decideWinner(yourChoice, computerChoice) { |
| 21 | + var rpsDataBase = { |
| 22 | + 'rock': { 'scissors': 1, 'rock': 0.5, 'paper': 0 }, |
| 23 | + 'paper': { 'rock': 1, 'paper': 0.5, 'scissors': 0 }, |
| 24 | + 'scissors': { 'paper': 1, 'scissors': 0.5, 'rock': 0 }, |
| 25 | + } |
| 26 | + |
| 27 | + var yourScore = rpsDataBase[yourChoice][computerChoice]; |
| 28 | + var computerScore = rpsDataBase[computerChoice][yourChoice]; |
| 29 | + |
| 30 | + return [yourScore, computerScore]; |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + function finalMessage([yourScore, computerScore]) { |
| 35 | + if (yourScore === 0) { |
| 36 | + return { 'message': "You lost!", 'color': 'red' }; |
| 37 | + } else if (yourScore === 0.5) { |
| 38 | + return { 'message': "You tied!", 'color': 'yellow' }; |
| 39 | + } else { |
| 40 | + return { 'message': "You won!", 'color': 'green' }; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + function rpsFrontend(humanImageChoice, botImageChoice, finalMessage) { |
| 48 | + // console.log(yourChoice); |
| 49 | + var imagesDataBase = { |
| 50 | + 'rock': document.getElementById("rock").src, |
| 51 | + 'paper': document.getElementById("paper").src, |
| 52 | + 'scissors': document.getElementById("scissors").src |
| 53 | + }; |
| 54 | + |
| 55 | + document.getElementById("startingPoint").remove(); |
| 56 | + document.getElementById("rock").remove(); |
| 57 | + document.getElementById("paper").remove(); |
| 58 | + document.getElementById("scissors").remove(); |
| 59 | + |
| 60 | + var humanDiv = document.createElement('div'); |
| 61 | + var messageDiv = document.createElement('div'); |
| 62 | + var botDiv = document.createElement('div'); |
| 63 | + |
| 64 | + humanDiv.innerHTML = "<img src='" + imagesDataBase[humanImageChoice] + "' alt='error' width='150' height='150' style='box-shadow: 0px 10px 50px rgba(37, 50, 233, 1);'>" |
| 65 | + document.getElementById("flex-box-rps").appendChild(humanDiv); |
| 66 | + messageDiv.innerHTML = "<h1 style='color: " + finalMessage['color'] +"; font-size: 60px; padding: 30px; '>" + finalMessage['message'] + "</h1>"; |
| 67 | + document.getElementById("flex-box-rps").appendChild(messageDiv); |
| 68 | + botDiv.innerHTML = "<img src='" + imagesDataBase[botImageChoice] + "' alt='error' width='150' height='150' style='box-shadow: 0px 10px 50px rgba(243, 38, 24, 1);'>" |
| 69 | + document.getElementById("flex-box-rps").appendChild(botDiv); |
| 70 | + |
| 71 | + |
| 72 | + } |
| 73 | + |
0 commit comments