Skip to content

Commit

Permalink
the code has been updated to make it more readable
Browse files Browse the repository at this point in the history
and efficient..
  • Loading branch information
shahidafridi-321 committed Mar 21, 2024
1 parent 0f23d35 commit c233a14
Showing 1 changed file with 65 additions and 51 deletions.
116 changes: 65 additions & 51 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,94 @@
</head>
<body>
<script>
let player_wins_counter = 0;
let computer_wins_counter = 0;
playGame();
let player_wins_counter = 0; // thia is counter for player win,when player wins it is incremented by one for scores
let computer_wins_counter = 0; // thia is counter for win,when computer wins it is incremented by one for keeping scores
let choices = ["rock","paper","scissor"]; //choices for machine to choice from

playGame();
getFinalResult();



// FUNCTION FOR PLAYING THE GAME FOR FIVE ROUNDS
function playGame()
{
for (let i = 0; i<5;i++)
{

let playerSelection = prompt("Rock,Paper,Scissor ").toLowerCase();
let choices = ["rock","paper","scissor"];

let computerSelection = getComputedChoice(choices);
console.log(playRound(playerSelection,computerSelection));

function playRound(player_one,player_two)
{

if (player_one===player_two)
{
let messege = "The Round is Tie!";
let result = messege + " the score is " + player_wins_counter +":"+computer_wins_counter;
return result;
}
else if ((player_one === "rock" && player_two === "scissor") || (player_one === "scissor" && player_two === "paper") || (player_one === "paper" && player_two === "rock") )
{
let messege = "the player wins the round";
player_wins_counter++;
let result = messege + " the score is " + player_wins_counter +":"+computer_wins_counter;
return result;
}
else if ((player_two === "rock" && player_one === "scissor") || (player_two === "scissor" && player_one === "paper") || (player_two === "paper" && player_one === "rock") )
{
let messege = "the Computer wins the round";
computer_wins_counter++;
let result = messege + " the score is " + player_wins_counter +":"+computer_wins_counter;
return result;
}
else
{
let messege = "incorrect choice";
i--;
return messege;
}

console.log(playRound(playerSelection,computerSelection));
}



function getComputedChoice(ch)
{
let index_of_choice = Math.floor(Math.random()*3);
let choisen = ch[index_of_choice];
return choisen;
}

}
}
if(player_wins_counter > computer_wins_counter)
// FUNCTION THAT SHOWS AT THE END WHO WHEN THE GAME PLAYER OR COMPUTER WITH SCORES
function getFinalResult()
{
console.log("You Have Won the Game Congratulations! with the Score "+ player_wins_counter +":"+computer_wins_counter);
if(player_wins_counter > computer_wins_counter)
{
alert("You Have Won the Game Congratulations! with the Score "+ player_wins_counter +":"+computer_wins_counter);
}
else if (player_wins_counter < computer_wins_counter)
{
console.log("Computer Have Won the Game , Try Again... Scores are "+ player_wins_counter +":"+computer_wins_counter);
alert("Computer Have Won the Game , Try Again... Scores are "+ player_wins_counter +":"+computer_wins_counter);

}
else
{
console.log("the Game is tied , with the Scores "+ player_wins_counter +":"+computer_wins_counter);
alert("the Game is tied , with the Scores "+ player_wins_counter +":"+computer_wins_counter);

}
}

// fuction for computer to make random choices
function getComputedChoice(ch)
{
let index_of_choice = Math.floor(Math.random()*3);
let choisen = ch[index_of_choice];
return choisen;
}




// function that checks who wins player or computer,takes to parameters a player_choice and a computer_choice
function playRound(player_one,player_two)
{
if (player_one===player_two)
{
let messege = "The Round is Tie! ";
let result = messege + " " + player_one.toUpperCase() +" = "+ player_two.toUpperCase() +" the score is "+ player_wins_counter +":"+computer_wins_counter;
return result;
}
else if ((player_one === "rock" && player_two === "scissor") ||
(player_one === "scissor" && player_two === "paper")||
(player_one === "paper" && player_two === "rock")
)
{
let messege = "the player wins the round ";
player_wins_counter++;
let result = messege + player_one.toUpperCase() +" beats "+ player_two.toUpperCase()+ " the score is " + player_wins_counter +":"+computer_wins_counter;
return result;
}
else if ((player_two === "rock" && player_one === "scissor") ||
(player_two === "scissor" && player_one === "paper")||
(player_two === "paper" && player_one === "rock")
)
{
let messege = "the Computer wins the round ";
computer_wins_counter++;
let result = messege + player_two.toUpperCase() +" beats "+ player_one.toUpperCase()+ " the score is " + player_wins_counter +":"+computer_wins_counter;
return result;
}
else
{
let messege = "incorrect choice";
i--;
return messege;
}

}
</script>
</body>
</html>

0 comments on commit c233a14

Please sign in to comment.