-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPSwar.html
More file actions
235 lines (210 loc) · 6.4 KB
/
RPSwar.html
File metadata and controls
235 lines (210 loc) · 6.4 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html>
<head>
<title>ROCK PAPER SCISSORS WAR</title>
<style type="text/css">
p{
font-family: Helvetica;
margin: 1px;
font-size: 12px;
}
div{
display: inline-block;
float:left;
}
#hp{
margin-bottom: 4px;
}
#results{
margin-left: 40px;
}
aside{
border-right: 1px solid black;
display: inline-block;
float:left;
padding-right: 20px;
}
</style>
</head>
<body>
<h1>ROCK PAPER SCISSORS WAR</h1>
<aside>
<h3>Choose Your Monster</h3>
<form>
<label> RockyBalboa <input class = "mons" name = "monsters" type = "radio" value = "0"/></label><br/>
<label> PaperTiger <input class = "mons" name = "monsters" type = "radio" value = "1"/></label><br/>
<label> Cutty <input class = "mons" name = "monsters" type = "radio" value = "2"/></label><br/>
<label> RockLobster <input class = "mons" name = "monsters" type = "radio" value = "3"/></label><br/>
<label> PaperBoy <input class = "mons" name = "monsters" type = "radio" value = "4"/></label><br/>
<label> CutlassSupreme <input class = "mons" name = "monsters" type = "radio" value = "5"/></label><br/>
<label> Ragnarock <input class = "mons" name = "monsters" type = "radio" value = "6"/></label><br/>
<label> DarthPaper <input class = "mons" name = "monsters" type = "radio" value = "7"/></label><br/>
<label> TigerUpperCut <input class = "mons" name = "monsters" type = "radio" value = "8"/></label><br/>
<input type = "button" onCLick = "battler()" value = "FIGHT">
</form>
</aside>
<div id = "results"></div>
<script>
//constructor
function Monster(name){
this.name = name;
this.arsenal = ['rock','paper','scissors'];
this.maxHp = 30;
this.hp = 30;
this.attack = 10;
this.armor = 4;
this.used = false;
this.bonus = 0;
}
function isChecked(radios){
var x;
for(var i = 0; i < radios.length; i++){
if(radios[i].checked){
x = radios[i].value;
}
}
return x;
}
//creates array of monsters as objects
function monsterMaker(){
var monsters = [],
names = ['RockyBalboa','PaperTiger','Cutty','RockLobster','PaperBoy',
'CutlassSupreme','Ragnarock','DarthPaper','TigerUpperCut'];
for(var i = 0, len = names.length; i < len; i++){
var mons = new Monster(names[i]),
arsenal = mons.arsenal;
if(i < 3){
arsenal.push('scissors');
} else if(i < 6){
arsenal.push('paper');
} else {
arsenal.push('rock');
}
if(mons.name.indexOf('ut') != -1){
arsenal.push('scissors');
mons.attack += 6;
mons.type = 'scissors';
} else if (mons.name.indexOf('aper') != -1){
arsenal.push('paper');
mons.maxHp += 20;
mons.hp += 20;
//mons.attack +=2;
mons.type = 'paper'
} else {
arsenal.push('rock');
mons.armor += 3;
mons.type = 'rock';
}
monsters.push(mons);
}
return monsters;
}
//function for individual fights
function fight(player, opponent){
var msg = '<h4>' + player.name + ' vs ' + opponent.name + '</h4>';
if((player.type == 'rock' && opponent.type == 'scissors') || (player.type == 'scissors' && opponent.type == 'paper') || (player.type == 'paper' && opponent.type == 'scissors')){
player.bonus += 2;
}
if((opponent.type == 'rock' && player.type == 'scissors') || (opponent.type == 'scissors' && player.type == 'paper') || (opponent.type == 'paper' && player.type == 'scissors')){
opponent.bonus += 2;
}
do{
//assign strings from rock paper scissors array
var str1 = player.arsenal[Math.floor(Math.random()*5)],
str2 = opponent.arsenal[Math.floor(Math.random()*5)];
//compare strings deal damage
if((str1 == 'rock' && str2 == 'scissors') || (str1 == 'scissors' && str2 == 'paper') || (str1 == 'paper' && str2 == 'rock')){
var dmg = player.attack - (opponent.armor + opponent.bonus);
msg += '<p style = "color:green">' + 'You throw ' + str1 + ' and ' + opponent.name + ' throws ' + str2 + '! A hit! Your sword slices through the air striking your enemy for ' + dmg + ' damage!' + '</p>';
opponent.hp -= dmg;
} else if((str1 == 'rock' && str2 == 'paper') || (str1 == 'scissors' && str2 == 'rock') || (str1 == 'paper' && str2 == 'scissors')){
var dmg = opponent.attack - (player.armor + player.bonus);
msg += '<p style = "color: red">' + 'You throw ' + str1 + ' and ' + opponent.name + ' throws ' + str2 + '! Your enemy strikes! Their blade flashes past your shield and hits for ' + dmg + ' damage!' + '</p>';
player.hp -= dmg;
} else {
msg += '<p>' + 'You throw ' + str1 + ' and ' + opponent.name + ' throws ' + str2 + '! A draw! Your blades clash together and no damage is dealt.' + '</p>';
}
msg += '<p style="color:blueviolet" id="hp">' + 'You have ' + player.hp + ' hp and ' + opponent.name + ' has ' + opponent.hp + 'hp.' + '</p>';
}while (player.hp>0 && opponent.hp>0);
//set boolean for outer loop
if (player.hp > opponent.hp){
msg += '<p style="font-size: 18px; color:green">' + 'YOU WIN' + '</p>';
nextFight = true;
} else {
msg += '<p style="font-size: 18px; color:red">' + 'YOU LOSE' + '</p>';
nextFight = false;
}
//reset hp/bonus
player.hp = player.maxHp;
opponent.hp = opponent.maxHp;
player.bonus = 0;
opponent.bonus = 0;
return [nextFight, msg];
}
//function for whole match
function battler(){
function chooseOpponent(){
//set opponent from pool of monsters that haven't been picked
do{
opponent = monsters[Math.floor(Math.random()*9)]
}while(opponent.used);
opponent.used = true;
}
// pick fighter
var selection = document.querySelectorAll('.mons'),
p = isChecked(selection),
monsters = monsterMaker(),
player = monsters[p],
ret,
nextFight,
opponent,
msg,
results = document.getElementById('results');
player.used = true;
chooseOpponent();
//fights
ret = fight(player,opponent);
nextFight = ret[0];
chooseOpponent();
msg = ret[1];
if(nextFight){
ret = fight(player,opponent);
nextFight = ret[0];
msg += ret[1];
chooseOpponent();
}
if(nextFight){
ret = fight(player,opponent);
msg += ret[1];
}
//display results
results.innerHTML = msg;
}
function testAll(){
var mons = monsterMaker(),
pairs = [],
plyr,
enmy,
rets;
for (var i = 0; i < mons.length; i++) {
for (var j = i + 1; j < mons.length; j++) {
pairs.push([mons[i], mons[j]]);
};
};
for (var i = 0; i < pairs.length; i++) {
plyr = 0,
enmy = 0;
for (var j = 0; j < 5000; j++) {
rets = fight(pairs[i][0], pairs[i][1]);
if(rets[0]){
plyr++;
} else {
enmy++;
}
};
console.log(pairs[i][0].name + " vs " + pairs[i][1].name + " " + plyr + " : " + enmy);
};
}
</script>
</body>
</html>