diff --git a/css/style.css b/css/style.css new file mode 100644 index 0000000..3b4f9e6 --- /dev/null +++ b/css/style.css @@ -0,0 +1,69 @@ +body{ + background-color:#601155; + display: flex; + flex-direction: row; + height: 100vh; + padding: 0px; + margin: 0px; + color: white; + font-family: 'Chilanka', cursive; + } + .container{ + text-align: center; + display: flex; + flex-direction: column; + width: 75%; + padding: 30px; + } + .score{ + display: flex; + flex-direction: row; + justify-content: space-around; + align-items: center; + min-height: 150px; + font-size: 40px; + + } + .score div{ + min-width: 20%; + } + .arena{ + display: flex; + flex-direction: row; + justify-content: space-around; + align-items: center; + min-height: 450px; + font-weight: bold; + font-size: 50px; + } + .arena img{ + width: 350px; + } + + #rounds{ + background-color: #c8e615; + width: 18%; + height: 100%; + border-right: 20px dashed #601155; + padding: 50px 20px 10px 40px; + color: #601155; + } + #player, #computer{ + border: 4px solid #c8e615; + font-size: 30px; + padding: 20px 20px 10px 20px; + } + + button{ + background-color: transparent; + border: none; + cursor: pointer; + } + i{ + color: #c8e615; + margin: 5px 40px; + } + a{ + color: #c8e615; + font-size: 26px; + } diff --git a/game.js b/game.js index 3d7e01b..2bacd0e 100644 --- a/game.js +++ b/game.js @@ -1,3 +1,74 @@ -// Write all your code here -// Replace the contents of this file with your own code -console.log('Hello, world!'); +const choices = ['rock', 'paper', 'scissors']; + +function computerPlay() { + return choices[Math.floor(Math.random() * choices.length)]; +} + +function userPlay(playAgain){ + let msg = (playAgain) ? "Invalid input ...\n please chose one of : rock, paper or scissors":"Lets Play ...\n chose one of : rock, paper or scissors"; + let player = prompt(msg); + //check if player submited a good value, if yes return player input or prompt again + return (!choices.includes(player.toLowerCase())) ? userPlay(1): player.toLowerCase(); +} + +function playRound() { + let player = userPlay(); + let computer = computerPlay(); + let result = ""; + + if(player === computer){ + return "Deaw..!! Play again"; + } + + + let computerWin = "COMPUTER WIN :|"; + let playerWin = "YOU WIN :)"; + + if(player === "rock"){ + result = (computer === "scissors")? playerWin:computerWin; + }else if(player === "scissors"){ + result = (computer === "rock")? computerWin:playerWin; + }else if(player === "paper"){ + result = (computer === "rock")? playerWin:computerWin; + } + + let winer = (result === computer)? computer:player; + let loser = (result === computer)? player:computer; + + result = `${result} \n ${winer} beats ${loser}`; + return result; +} + +function game(rounds = 5){ + let report = { + rounds:[], + score:[] + }; + + report.score['player'] = 0; + report.score['computer'] = 0; + + let i = 0; + while(i < rounds){ + let round = playRound(); + console.log(round); + //add this round to the report + report.rounds.push(round); + + if(round !== "Deaw..!! Play again"){ + if(round.includes("YOU WIN")){ + report.score['player'] = report.score['player'] + 1; + }else{ + report.score['computer'] = report.score['computer'] + 1; + } + i++; + } + + } + report.finalResult = (report.score['computer'] > report.score['player'])? `Computer win ${report.score['computer']} of ${rounds}`:`You win ${report.score['player']} of ${rounds}`; + return report; + //this will return an pbject ,, includes the result of each game, and the score for both player and computer, and final result (who win) +} + + +console.log(game()); diff --git a/images/paper.png b/images/paper.png new file mode 100644 index 0000000..aaf25b1 Binary files /dev/null and b/images/paper.png differ diff --git a/images/rock.png b/images/rock.png new file mode 100644 index 0000000..b46e59a Binary files /dev/null and b/images/rock.png differ diff --git a/images/scissors.png b/images/scissors.png new file mode 100644 index 0000000..418b002 Binary files /dev/null and b/images/scissors.png differ diff --git a/index.html b/index.html index 947177f..f3f65c4 100644 --- a/index.html +++ b/index.html @@ -3,8 +3,60 @@ Rock, Paper, Scissors! - + + + + + + + + +
+

Rounds :

+ +
+ +
+ +
+
+ YOU : + 0 +
+ +
+ +
+ +
+ COUMPUTER : + 0 +
+
+ +
+ + + +
+ +
+
+ +
+
VS
+
+ +
+
+ + + + +
diff --git a/js/game.js b/js/game.js new file mode 100644 index 0000000..26024ff --- /dev/null +++ b/js/game.js @@ -0,0 +1,141 @@ +//LISTENER ,, to make action when a button clicked +document.getElementById('rock').addEventListener('click', function(e){ + let round = playRound('rock', computerPlay()); +}); +document.getElementById('paper').addEventListener('click', function(e){ + let round = playRound('paper', computerPlay()); +}); +document.getElementById('scissors').addEventListener('click', function(e){ + let round = playRound('scissors', computerPlay()); +}); + +//p = player score, c = computer score ,, just to count till 5 and one of them win +let p = 0 , c= 0; +const choices = ['rock', 'paper', 'scissors']; + +//add images as the choices of player and computer +function arenaImages(player, computer){ + //show images + document.getElementById('player-choice').innerHTML = ``; + document.getElementById('computer-choice').innerHTML = ``; +} + +//get random choice for computer play +function computerPlay() { + return choices[Math.floor(Math.random() * choices.length)]; +} + +//start a game and get winner and loser :) +function playRound(player, computer) { + arenaImages(player, computer) + //select the score element to change it after this round + let playerScore = document.querySelector('#player span'); + let computerScore = document.querySelector('#computer span'); + + + // let player = userPlay(); + // let computer = computerPlay(); + let result = ""; + + //check if both player and computer have the same choice + if(player === computer){ + document.getElementById('result').innerHTML = "Deaw.. 😑 ðŸ˜ķ !! Play again"; + + //create element to add it to the rounds element + let roundResult = document.createElement('li'); + roundResult.innerHTML = "Deaw.. 😑 ðŸ˜ķ !!"; + document.getElementById('rounds-list').appendChild(roundResult); + + //the retern was for the old version + return "Deaw.. 😑 ðŸ˜ķ !! Play again"; + } + + //defined the content for round result + let computerWin = "COMPUTER WIN ðŸ˜Ą ðŸĪŽ
"; + let playerWin = "YOU WIN ðŸĪŠ ðŸ’Š
"; + + //check who win ? + if(player === "rock"){ + result = (computer === "scissors")? playerWin:computerWin; + }else if(player === "scissors"){ + result = (computer === "rock")? computerWin:playerWin; + }else if(player === "paper"){ + result = (computer === "rock")? playerWin:computerWin; + } + + let winer = (result === computerWin)? computer:player; + let loser = (result === computerWin)? player:computer; + (result === playerWin) ? p++:c++; + + //increase the score for winner + playerScore.textContent = p; + computerScore.textContent = c; + if(p === 5 || c === 5){ + //finish the game if computer or player hits score 5 + finishTheGame(); + } + + result = `${result} \n ${winer} beats ${loser}`; + let roundResult = document.createElement('li'); + roundResult.innerHTML = result; + document.getElementById('rounds-list').appendChild(roundResult); + document.getElementById('result').innerHTML = result; + return result; +} + +//delete the buttons and show a link (play again) to restart the game +function finishTheGame() { + console.log(c); + console.log(p); + let text = "LOSER"; + if(p > c){ + text = "winner winner chicken dinner"; + } + document.querySelector('.buttons').innerHTML = `

${text}

play again

`; +} + + + + +//THIS WORKS WITH THE OLD VERSION .. +/* +function game(rounds = 5){ + let report = { + rounds:[], + score:[] + }; + + report.score['player'] = 0; + report.score['computer'] = 0; + + let i = 0; + while(i < rounds){ + let round = playRound(userPlay(), computerPlay()); + console.log(round); + //add this round to the report + report.rounds.push(round); + + if(round !== "Deaw..!! Play again"){ + if(round.includes("YOU WIN")){ + report.score['player'] = report.score['player'] + 1; + }else{ + report.score['computer'] = report.score['computer'] + 1; + } + i++; + } + } + report.finalResult = (report.score['computer'] > report.score['player'])? `Computer win ${report.score['computer']} of ${rounds}`:`You win ${report.score['player']} of ${rounds}`; + return report; + //this will return an pbject ,, includes the result of each game, and the score for both player and computer, and final result (who win) +} + + +function userPlay(playAgain){ + let msg = (playAgain) ? "Invalid input ...\n please chose one of : rock, paper or scissors":"Lets Play ...\n chose one of : rock, paper or scissors"; + let player = prompt(msg); + //check if player submited a good value, if yes return player input or prompt again + return (!choices.includes(player.toLowerCase())) ? userPlay(1): player.toLowerCase(); +} + + //console.log(game()); +*/