-
Notifications
You must be signed in to change notification settings - Fork 1
/
general-class.rb
67 lines (62 loc) · 1.33 KB
/
general-class.rb
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
class Player
attr_accessor :name, :hp, :max_hp, :mp, :max_mp, :attack, :block, :agility, :money, :exp, :item_list
def initialize(name)
@name = name
@max_hp = 100
@hp = 100
@max_mp = 60
@mp = 60
@attack = 20
@block = 20
@agility = 20
@money = 1000
@exp = 0
@item_list = []
end
end
class Text
class << self
def insert(text, num: 1, ins: " ")
text = text.to_s
width_ = width(text)
if num == 1
return (width_ % 2) == 1 ? text + ins : text
else
re = text
(num - width_).times do
re = ins + re
end
return re
end
end
def add(text, num: 1, add: " ")
text = text.to_s
width_ = width(text)
if num == 1
return (width_ % 2) == 1 ? text + add : text
else
re = text
(num - width_).times do
re += add
end
return re
end
end
def width(text)
text = text.to_s
width = 0
text.scan(/./) do |i|
if /\A[ぁ-んー-]+\z/ =~ i# 全角ひらがな
width += 2
elsif /\A[ァ-ン゙゚]+\z/ =~ i# 半角型カタカナ
width += 1
elsif /\A[ -~。-゜]+\z/ =~ i# 半角
width += 1
else
width += 2
end
end
return width
end
end
end