From ad6e10e5cc3c22dd297c01cf472800ac092e0ca5 Mon Sep 17 00:00:00 2001 From: Gurupreet Singh Date: Tue, 7 Feb 2023 16:54:19 +0530 Subject: [PATCH 1/2] Implementing a rps game which is based on javascript --- Rock_paper_scissors/index.html | 53 ++++++++++++++++++++++++++++++++++ Rock_paper_scissors/main.js | 28 ++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 Rock_paper_scissors/index.html create mode 100644 Rock_paper_scissors/main.js diff --git a/Rock_paper_scissors/index.html b/Rock_paper_scissors/index.html new file mode 100644 index 0000000..4119e26 --- /dev/null +++ b/Rock_paper_scissors/index.html @@ -0,0 +1,53 @@ + + + + + Rock,Paper and Scissor + + + + + + + + + +
+ +
+
+ +
+
+

Result

+ + + + + + + + + + + + + + + +
WinLoseTie
000
+
+
+
+ + + +
+
+ + + \ No newline at end of file diff --git a/Rock_paper_scissors/main.js b/Rock_paper_scissors/main.js new file mode 100644 index 0000000..6d78591 --- /dev/null +++ b/Rock_paper_scissors/main.js @@ -0,0 +1,28 @@ +const arr = ['rock', 'paper', 'scissor']; +let wins = 0; +let losses = 0; +let ties = 0; + +function ABC(value) { + var rand = arr[Math.floor(Math.random() * 3)]; + document.getElementById("print_result").innerHTML = `You choose ${value}, computer choose ${rand}.

`; + + if (value == 'rock' && rand == "scissor" || + value == "paper" && rand == "rock" || + value == "scissor" && rand == "paper") { + document.getElementById("win").innerHTML = wins += 1; + document.getElementById("print_result").innerHTML += `You Win`; + + } else if (rand == 'rock' && value == "scissor" || + rand == "paper" && value == "rock" || + rand == "scissor" && value == "paper") { + document.getElementById("lose").innerHTML = losses += 1; + document.getElementById("print_result").innerHTML += `Computer Win`; + + } else { + document.getElementById("tie").innerHTML = ties += 1; + document.getElementById("print_result").innerHTML += `it's a Tie`; + + } + +} \ No newline at end of file From ebc14c46fd595c1fb5abda1e8a7e662c834ff648 Mon Sep 17 00:00:00 2001 From: Gurupreet Singh Date: Mon, 13 Feb 2023 17:10:46 +0530 Subject: [PATCH 2/2] Improving the code on basis of PR commits --- Rock_paper_scissors/index.html | 6 +++--- Rock_paper_scissors/main.js | 30 +++++++++--------------------- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/Rock_paper_scissors/index.html b/Rock_paper_scissors/index.html index 4119e26..27ec935 100644 --- a/Rock_paper_scissors/index.html +++ b/Rock_paper_scissors/index.html @@ -43,9 +43,9 @@

Result

- - - + + +
diff --git a/Rock_paper_scissors/main.js b/Rock_paper_scissors/main.js index 6d78591..da8493a 100644 --- a/Rock_paper_scissors/main.js +++ b/Rock_paper_scissors/main.js @@ -3,26 +3,14 @@ let wins = 0; let losses = 0; let ties = 0; -function ABC(value) { +function gameState(value) { var rand = arr[Math.floor(Math.random() * 3)]; document.getElementById("print_result").innerHTML = `You choose ${value}, computer choose ${rand}.

`; - - if (value == 'rock' && rand == "scissor" || - value == "paper" && rand == "rock" || - value == "scissor" && rand == "paper") { - document.getElementById("win").innerHTML = wins += 1; - document.getElementById("print_result").innerHTML += `You Win`; - - } else if (rand == 'rock' && value == "scissor" || - rand == "paper" && value == "rock" || - rand == "scissor" && value == "paper") { - document.getElementById("lose").innerHTML = losses += 1; - document.getElementById("print_result").innerHTML += `Computer Win`; - - } else { - document.getElementById("tie").innerHTML = ties += 1; - document.getElementById("print_result").innerHTML += `it's a Tie`; - - } - -} \ No newline at end of file + let you_win = ((value == 'rock' && rand == "scissor") || (value == "paper" && rand == "rock") || (value == "scissor" && rand == "paper")) + let computer_win = (value == 'rock' && value == "scissor" || rand == "paper" && value == "rock" || rand == "scissor" && value == "paper") + document.getElementById("print_result").innerHTML += (you_win) ? `You Win` : (value === rand) ? `it's a Tie` : `Computer Win` + incrementScore(you_win,computer_win) +} +const incrementScore = ((user,computer) => { + user ? document.getElementById("win").innerHTML = wins += 1 : computer ? document.getElementById("lose").innerHTML = losses += 1 : document.getElementById("tie").innerHTML = ties +=1; + }); \ No newline at end of file