赞 | 1 |
VIP | 17 |
好人卡 | 3 |
积分 | 1 |
经验 | 49919 |
最后登录 | 2020-2-6 |
在线时间 | 898 小时 |
Lv1.梦旅人 矿工
- 梦石
- 0
- 星屑
- 134
- 在线时间
- 898 小时
- 注册时间
- 2012-10-5
- 帖子
- 1535
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
本帖最后由 876加几 于 2013-12-20 23:51 编辑
我为了将[7, 7, 7, 7, 6]这类的数组消灭掉全部相同的,费了我很大的功夫。
于是,我好不容易才写好了Array方法新增,从此消灭重复单元不再费劲了。
脚本:
=begin =============================================================================== ■ 使用方法: ------------------------------------------------------------------------------- ● number 则返回某个单元名称重复的数量。 例:[7, 7, 7, 7, 6, 6, 5].number(7) => 4 ● aftftn 也是返回重复的数量,但与 number 有所不同。 例:[7, 7, 7, 7, 6, 6, 5].aftftn(4) => 2 也就是说就是数组第几个单元的名称重复的数量。 ● delete_same 则删除单元里指定名称重复的部分,具有破坏性。 例:a = [7, 7, 7, 7, 6, 6, 5] a.delete_same(7) p a => [7, 6, 6, 5] ● same? 则返回是否是重复的单元。 例:a = [7, 7, 7, 7, 6, 6, 5] p a.same?(4) => false p a.same?(3) => true ● das 和 das! 都是删除数组中所有重复的单元,但是 das 只会返回删除后的数组,das! 具有破坏性。 例:a = [7, 7, 7, 7, 6, 6, 5] p a.das => [7, 6, 5] p a => [7, 7, 7, 7, 6, 6, 5] # 没有变化 a.das! p a => [7, 6, 5] # 之后没法再消了,总是返回原来的数组。 ● aftft 和 index 都是寻找第一个出现的单元,但有所不同。 例:a = [7, 7, 7, 7, 6, 6, 5] p a.aftft(2) => 0 等同于 p a.index(a[2]) => 0 ● dasn 和 dasn! 都是先删除重复的单元再计算出重复单元的数量,但是 dasn 只会返 回改变后的数组,dasn! 具有破坏性! 例:a = [7, 7, 7, 7, 6, 6, 5] p a.dasn => [4, 2, 1] p a => [7, 7, 7, 7, 6, 6, 5] # 没有变化 a.dasn! p a => [4, 2, 1] a.dasn! p a => [1, 1, 1] a.dasn! p a => [3] a.dasn! p a => [1] # 之后永远就会出现 [1] 了。 ● size 可以自行更改了,请看例子: 例:a = [7, 7, 7, 7, 6, 6, 5] p a.size => 7 a.size = 4 p a => [7, 7, 7, 7] p a.size => 4 a.size **= 2 p a => [7, 7, 7, 7, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil] # size 可以做任何运算。 ● fill 就是规定长度往后面 push 相应的字串,具有破坏性。 例:a = [7, 7, 7, 7, 6, 6, 5] a.fill(3) p a => [7, 7, 7, 7, 6, 6, 5, nil, nil, nil] a.fill(2, "我们") => [7, 7, 7, 7, 6, 6, 5, nil, nil, nil, "我们", "我们"] =============================================================================== =end class Array def number(nth_name) if self.index(nth_name) == nil return 0 end num = 1 for i in 0...self.size if self[i] == nth_name and self.index(nth_name) != i num += 1 end end return num end def aftftn(index) num = 1 aftft = 0 for i in 0...self.size if self[i] == self[index] aftft = i break end end for j in 0...self.size if self[j] == self[aftft] and aftft != j num += 1 end end return num end def delete_same(name) if self.number(name) == 0 return self end for i in 1..self.number(name) for d in 0...self.size if self[d] == name and self.index(name) != d self.delete_at(d) break end end end return self end def same?(index) return (self.aftft(index) != index) end def das # (delete_all_same) ary = self.dup for j in 0...self.size ary.delete_same(self[j]) end return ary end def das! ary = self.dup for j in 0...ary.size self.delete_same(ary[j]) end return self end def aftft(index) num = nil for j in 0...self.size if self[index] == self[j] num = j break end end return num end def dasn # (delete_all_same_number) pushing = [] ary = self.dup a = self.das for i in 0...a.size pushing[i] = ary.number(a[i]) end return pushing end def dasn! ary = self.dup a = self.das self.clear for i in 0...a.size self[i] = ary.number(a[i]) end return self end def size=(size) if self.size == size return end if self.size > size s = self.size for i in 0..self.size-size self.delete_at(s-i) end return end for i in self.size...size self[i] = nil end end def fill(size, text = nil) size.times do self.push(text) end end end
=begin
===============================================================================
■ 使用方法:
-------------------------------------------------------------------------------
● number 则返回某个单元名称重复的数量。
例:[7, 7, 7, 7, 6, 6, 5].number(7) => 4
● aftftn 也是返回重复的数量,但与 number 有所不同。
例:[7, 7, 7, 7, 6, 6, 5].aftftn(4) => 2
也就是说就是数组第几个单元的名称重复的数量。
● delete_same 则删除单元里指定名称重复的部分,具有破坏性。
例:a = [7, 7, 7, 7, 6, 6, 5]
a.delete_same(7)
p a => [7, 6, 6, 5]
● same? 则返回是否是重复的单元。
例:a = [7, 7, 7, 7, 6, 6, 5]
p a.same?(4) => false
p a.same?(3) => true
● das 和 das! 都是删除数组中所有重复的单元,但是 das 只会返回删除后的数组,das!
具有破坏性。
例:a = [7, 7, 7, 7, 6, 6, 5]
p a.das => [7, 6, 5]
p a => [7, 7, 7, 7, 6, 6, 5] # 没有变化
a.das!
p a => [7, 6, 5]
# 之后没法再消了,总是返回原来的数组。
● aftft 和 index 都是寻找第一个出现的单元,但有所不同。
例:a = [7, 7, 7, 7, 6, 6, 5]
p a.aftft(2) => 0
等同于
p a.index(a[2]) => 0
● dasn 和 dasn! 都是先删除重复的单元再计算出重复单元的数量,但是 dasn 只会返
回改变后的数组,dasn! 具有破坏性!
例:a = [7, 7, 7, 7, 6, 6, 5]
p a.dasn => [4, 2, 1]
p a => [7, 7, 7, 7, 6, 6, 5] # 没有变化
a.dasn!
p a => [4, 2, 1]
a.dasn!
p a => [1, 1, 1]
a.dasn!
p a => [3]
a.dasn!
p a => [1]
# 之后永远就会出现 [1] 了。
● size 可以自行更改了,请看例子:
例:a = [7, 7, 7, 7, 6, 6, 5]
p a.size => 7
a.size = 4
p a => [7, 7, 7, 7]
p a.size => 4
a.size **= 2
p a => [7, 7, 7, 7, nil, nil, nil, nil,
nil, nil, nil, nil, nil, nil,
nil, nil]
# size 可以做任何运算。
● fill 就是规定长度往后面 push 相应的字串,具有破坏性。
例:a = [7, 7, 7, 7, 6, 6, 5]
a.fill(3)
p a => [7, 7, 7, 7, 6, 6, 5, nil, nil,
nil]
a.fill(2, "我们") => [7, 7, 7, 7, 6, 6, 5, nil, nil,
nil, "我们", "我们"]
===============================================================================
=end
class Array
def number(nth_name)
if self.index(nth_name) == nil
return 0
end
num = 1
for i in 0...self.size
if self[i] == nth_name and self.index(nth_name) != i
num += 1
end
end
return num
end
def aftftn(index)
num = 1
aftft = 0
for i in 0...self.size
if self[i] == self[index]
aftft = i
break
end
end
for j in 0...self.size
if self[j] == self[aftft] and aftft != j
num += 1
end
end
return num
end
def delete_same(name)
if self.number(name) == 0
return self
end
for i in 1..self.number(name)
for d in 0...self.size
if self[d] == name and self.index(name) != d
self.delete_at(d)
break
end
end
end
return self
end
def same?(index)
return (self.aftft(index) != index)
end
def das # (delete_all_same)
ary = self.dup
for j in 0...self.size
ary.delete_same(self[j])
end
return ary
end
def das!
ary = self.dup
for j in 0...ary.size
self.delete_same(ary[j])
end
return self
end
def aftft(index)
num = nil
for j in 0...self.size
if self[index] == self[j]
num = j
break
end
end
return num
end
def dasn # (delete_all_same_number)
pushing = []
ary = self.dup
a = self.das
for i in 0...a.size
pushing[i] = ary.number(a[i])
end
return pushing
end
def dasn!
ary = self.dup
a = self.das
self.clear
for i in 0...a.size
self[i] = ary.number(a[i])
end
return self
end
def size=(size)
if self.size == size
return
end
if self.size > size
s = self.size
for i in 0..self.size-size
self.delete_at(s-i)
end
return
end
for i in self.size...size
self[i] = nil
end
end
def fill(size, text = nil)
size.times do
self.push(text)
end
end
end
有了这个,还可以美化一下战斗结束提示窗口。
#============================================================================== # ■ Window_BattleResult #------------------------------------------------------------------------------ # 战斗结束时、显示获得的 EXP 及金钱的窗口。 #============================================================================== class Window_BattleResult < Window_Base #-------------------------------------------------------------------------- # ● 初始化对像 # exp : EXP # gold : 金钱 # treasures : 宝物 #-------------------------------------------------------------------------- def initialize(exp, gold, treasures) [url=home.php?mod=space&uid=13302]@exp[/url] = exp [url=home.php?mod=space&uid=236945]@gold[/url] = gold @treasures = treasures.das @treasures_num = treasures.dasn super(160, 0, 320, @treasures.size * 32 + 64) self.contents = Bitmap.new(width - 32, height - 32) self.y = 160 - height / 2 self.back_opacity = 160 self.visible = false refresh end #-------------------------------------------------------------------------- # ● 刷新 #-------------------------------------------------------------------------- def refresh self.contents.clear x = 4 self.contents.font.color = normal_color cx = contents.text_size(@exp.to_s).width self.contents.draw_text(x, 0, cx, 32, @exp.to_s) x += cx + 4 self.contents.font.color = system_color cx = contents.text_size("EXP").width self.contents.draw_text(x, 0, 64, 32, "EXP") x += cx + 16 self.contents.font.color = normal_color cx = contents.text_size(@gold.to_s).width self.contents.draw_text(x, 0, cx, 32, @gold.to_s) x += cx + 4 self.contents.font.color = system_color self.contents.draw_text(x, 0, 128, 32, $data_system.words.gold) y = 32 for i in 0...@treasures.size item = @treasures[i] draw_item_name(item, 4, y) self.contents.draw_text(320 - 32 - 48, y, 48, 32, @treasures_num[i].to_s, 2) self.contents.draw_text(320 - 32 - 96, y, 48, 32, "×", 1) y += 32 end end end
#==============================================================================
# ■ Window_BattleResult
#------------------------------------------------------------------------------
# 战斗结束时、显示获得的 EXP 及金钱的窗口。
#==============================================================================
class Window_BattleResult < Window_Base
#--------------------------------------------------------------------------
# ● 初始化对像
# exp : EXP
# gold : 金钱
# treasures : 宝物
#--------------------------------------------------------------------------
def initialize(exp, gold, treasures)
[url=home.php?mod=space&uid=13302]@exp[/url] = exp
[url=home.php?mod=space&uid=236945]@gold[/url] = gold
@treasures = treasures.das
@treasures_num = treasures.dasn
super(160, 0, 320, @treasures.size * 32 + 64)
self.contents = Bitmap.new(width - 32, height - 32)
self.y = 160 - height / 2
self.back_opacity = 160
self.visible = false
refresh
end
#--------------------------------------------------------------------------
# ● 刷新
#--------------------------------------------------------------------------
def refresh
self.contents.clear
x = 4
self.contents.font.color = normal_color
cx = contents.text_size(@exp.to_s).width
self.contents.draw_text(x, 0, cx, 32, @exp.to_s)
x += cx + 4
self.contents.font.color = system_color
cx = contents.text_size("EXP").width
self.contents.draw_text(x, 0, 64, 32, "EXP")
x += cx + 16
self.contents.font.color = normal_color
cx = contents.text_size(@gold.to_s).width
self.contents.draw_text(x, 0, cx, 32, @gold.to_s)
x += cx + 4
self.contents.font.color = system_color
self.contents.draw_text(x, 0, 128, 32, $data_system.words.gold)
y = 32
for i in 0...@treasures.size
item = @treasures[i]
draw_item_name(item, 4, y)
self.contents.draw_text(320 - 32 - 48, y, 48, 32, @treasures_num[i].to_s, 2)
self.contents.draw_text(320 - 32 - 96, y, 48, 32, "×", 1)
y += 32
end
end
end
|
评分
-
查看全部评分
|