-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRPSGame.js
118 lines (105 loc) · 2.9 KB
/
RPSGame.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// let usermove='';
let computerMove='';
let win=0
let tie=0
let lose=0
let result=''
let gameplayed=0;
let gameHistory=[];
function generateComputerMove(){
const randomNumber=Math.floor(Math.random()*3);
if (randomNumber===0){
computerMove='Rock';
}
else if (randomNumber===1) {
computerMove='Paper';
}
else{
computerMove='Scissors';
}
console.log(`Your Move: ${usermove}\nComputer's Move: ${computerMove}`);
}
function evaluateMoves(){
if(usermove===computerMove)
{
tie+=1
result='Tie'
}
else if(usermove==="Rock" && computerMove==="Paper")
{
lose +=1
result = 'Lose'
}
else if(usermove==="Rock" && computerMove==="Scissors")
{
win+=1
result = 'Win'
}
else if(usermove==="Paper" && computerMove==="Rock")
{
win +=1
result = 'Win'
}
else if(usermove==="Paper" && computerMove==="Scissors")
{
lose +=1
result = 'Lose'
}
else if(usermove==="Scissors" && computerMove==="Rock")
{
lose +=1
result = 'Lose'
}
else if(usermove==="Scissors" && computerMove==="Paper")
{
win +=1
result = 'Win'
}
gameplayed=gameplayed+1;
console.log(`You : ${result}`);
console.log(win , lose , tie);
gameHistory.push({
userMove: usermove,
comMove: computerMove,
score: result
});
}
function renderGameSummary(){
document.getElementById('wins').innerHTML=win;
document.getElementById('loses').innerHTML=lose;
document.getElementById('ties').innerHTML=tie;
document.getElementById('gp').innerHTML=win+lose+tie;
}
function renderGameHistory() {
const tableBody = document.getElementById('his');
tableBody.innerHTML = "";
for (let i = 0; i < gameHistory.length; i++) {
const row = document.createElement("tr");
const numberCell = document.createElement("td");
numberCell.textContent = i + 1;
row.appendChild(numberCell);
const userMoveCell = document.createElement("td");
userMoveCell.textContent = gameHistory[i].userMove;
row.appendChild(userMoveCell);
const computerMoveCell = document.createElement("td");
computerMoveCell.textContent = gameHistory[i].computerMove;
row.appendChild(computerMoveCell);
const resultCell = document.createElement("td");
resultCell.textContent = gameHistory[i].result;
row.appendChild(resultCell);
tableBody.appendChild(row);
}
}
function persist()
{
}
function resetGame()
{
game={
win:0,
lose:0,
tie:0
}
renderGameSummary();
renderGameHistory();
}