forked from esminc/mvc_kata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart_ui.html
182 lines (167 loc) · 4.44 KB
/
smart_ui.html
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
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>mvc_kata</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.15/jquery-ui.min.js"></script>
<script type="text/javascript">
<!--
function extend(super_class, initializer, prototype) {
var sub_class = function() {
super_class.apply(this, arguments);
initializer.apply(this, arguments);
};
sub_class.prototype = $.extend({}, super_class.prototype, prototype);
return sub_class;
}
Living = extend(Object, function() {
this.hp = this.max_hp;
}, {
damage: function(damage_point) {
this.hp = Math.max(this.hp - damage_point, 0);
},
cure: function(cure_point) {
this.hp = Math.min(this.hp + cure_point, this.max_hp);
}
});
Enemy = extend(Living, function() {
}, {
choose_command: function() {
return this.commands[Math.floor(Math.random() * this.commands.length)];
}
});
Slime = extend(Enemy, function(){
this.commands = [ new Attack(this) ];
}, {
name: 'SLIME',
max_hp: 10,
attack_power: 4,
exp: 1
});
Dragon = extend(Enemy, function(){
this.commands = [ new Attack(this) ];
}, {
name: 'DRAGON',
max_hp: 20,
attack_power: 8,
exp: 3000
});
Player = extend(Living, function(){
this.commands = [ new Attack(this), new Hoimi(this) ];
}, {
name: 'PLAYER',
max_hp: 10,
attack_power: 3,
choose_command: function() {
var message = MESSAGES['STATUS'].text(this.name, this.hp);
$.each(this.commands, function(i, command) {
message = message + "\n" + i + ": " + command.name;
});
for (;;) {
var s = prompt(message);
var i = parseInt(s);
if (!isNaN(i)) {
if (this.commands[i]) {
return this.commands[i];
}
}
}
}
});
Message = extend(Object, function(template){
this.template = template;
}, {
text: function() {
var s = this.template;
for(var i = 0; i < arguments.length; i++) {
s = s.replace('{'+i+'}', arguments[i]);
}
return s;
}
});
MESSAGES = {
WIN: new Message(
"{0}をたおした\n\n" +
"===========================\n" +
"かかったターン数{1}\n" +
"経験値{2}かくとく"),
LOOSE: new Message(
"{0}はたおれました\n\n" +
"===========================\n" +
"ゲームオーバー"),
ATTACK: new Message(
"{0}のこうげき\n" +
"{1}に{2}のダメージ"),
CURE: new Message(
"{0}はホイミをとなえた\n" +
"HPが{1}回復した"),
STATUS: new Message(
"{0}のHP: {1}"),
ENCOUNT: new Message(
"{0}があらわれた")
};
Command = extend(Object, function(commander){
this.commander = commander;
}, {});
Attack = extend(Command, function(){}, {
name: 'たたかう',
exec: function(target) {
var damage_point = Math.floor(Math.random() * this.commander.attack_power) + 1;
target.damage(damage_point);
return MESSAGES['ATTACK'].text(this.commander.name, target.name, damage_point);
}
});
Hoimi = extend(Command, function(){}, {
name: 'ホイミ',
exec: function(target) {
var cure_point = Math.floor(Math.random() * 8) + 1;
this.commander.cure(cure_point);
return MESSAGES['CURE'].text(this.commander.name, cure_point);
}
});
Battle = extend(Object, function() {
this.enemy_classes = [Slime, Dragon];
this.player = new Player();
this.turn_count = 0;
}, {
battle: function() {
for (;;) {
this.turn_count ++;
alert(this.player.choose_command().exec(this.enemy));
if (this.is_finished()) break;
alert(this.enemy.choose_command().exec(this.player));
if (this.is_finished()) break;
}
},
is_finished: function() {
if (this.player.hp <= 0) {
this.game_over();
return true;
} else if (this.enemy.hp <= 0) {
this.finish_battle();
return true;
}
return false;
},
finish_battle: function() {
alert(MESSAGES['WIN'].text(this.enemy.name, this.turn_count, this.enemy.exp));
},
game_over: function() {
alert(MESSAGES['LOOSE'].text(this.player.name));
},
encounter: function() {
this.enemy = new this.enemy_classes[Math.floor(Math.random() * this.enemy_classes.length)];
alert(MESSAGES['ENCOUNT'].text(this.enemy.name));
this.battle();
},
start: function() {
this.encounter();
}
});
//-->
</script>
</head>
<body>
<button onclick="new Battle().start();">start</button>
</body>
</html>