赞 | 1 |
VIP | 17 |
好人卡 | 3 |
积分 | 1 |
经验 | 49919 |
最后登录 | 2020-2-6 |
在线时间 | 898 小时 |
Lv1.梦旅人 矿工
- 梦石
- 0
- 星屑
- 134
- 在线时间
- 898 小时
- 注册时间
- 2012-10-5
- 帖子
- 1535
|
本帖最后由 876加几 于 2014-7-30 20:25 编辑
昨天写的VB.NET:
Public Class GameArray '变量 Private ary(13, 13) As Integer '调用 Public Sub New() Make() End Sub '调用数据 Public ReadOnly Property Data() As Integer(,) Get Return ary End Get End Property '制作 Sub Make() Dim x, y As Integer For x = 0 To 12 For y = 0 To 12 If x = 6 And y = 6 Then ary(x, y) = 2 ElseIf Int(Rnd() * 10) = 1 Then ary(x, y) = 1 Else ary(x, y) = 0 End If Next Next End Sub '注释 '可通行区域 = 0 '禁止通行区域 = 1 '老鼠 = 2 '是否相邻判断 Function adjoin(ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer) As Boolean If Not (Valid(x1, x2) = True And Valid(x2, y2) = True) Then Throw New Exception("存在无效坐标。") End If If Math.Abs(x2 - x1) = 1 Or Math.Abs(y2 - y1) = 1 Then Return True End If Dim num As Integer num = y1 Mod 2 If num = 0 Then '奇数行靠左 If Math.Abs(y2 - y1) = 1 And x1 - x2 = 1 Then Return True End If Else '偶数行靠右 If Math.Abs(y2 - y1) = 1 And x2 - x1 = 1 Then Return True End If End If Return False End Function '相邻可通行区域 Function AdjoinAndPossable(ByVal x As Integer, ByVal y As Integer) As Integer()() If Valid(x, y) = False Then Throw New Exception("无效坐标。") End If Dim x1, y1 As Integer Dim Coordinate As Integer()() = {} Dim SelfAry(2) As Integer For x1 = 0 To 12 For y1 = 0 To 12 SelfAry(0) = x1 SelfAry(1) = y1 If adjoin(x, y, x1, y1) = True And Me.Data(x1, y1) = 1 = False Then ArrayAdd(Coordinate, SelfAry) End If Next Next Return Coordinate End Function '有效 Function Valid(ByVal x As Integer, ByVal y As Integer) As Boolean Return (x >= 0 And x < 13 And y >= 0 And y < 13) End Function '清除老鼠 Sub MoveMouse() Dim x, y As Integer For x = 0 To 12 For y = 0 To 12 If ary(x, y) = 2 Then ary(x, y) = 0 Exit Sub End If Next Next End Sub '移动老鼠 Sub MoveMouse(ByVal x As Integer, ByVal y As Integer) If Valid(x, y) = False Then Throw New Exception("无效坐标。") End If Dim x1, y1 As Integer For x1 = 0 To 12 For y1 = 0 To 12 If ary(x1, y1) = 2 Then ary(x1, y1) = 0 End If Next Next ary(x, y) = 2 End Sub '添加障碍 (返回是否成功) Function AddBlock(ByVal x As Integer, ByVal y As Integer) As Boolean If Valid(x, y) = False Then Throw New Exception("无效坐标。") End If Dim x1, y1 As Integer Dim mouse(2) As Integer For x1 = 0 To 12 For y1 = 0 To 12 If ary(x1, y1) = 2 Then mouse(0) = x1 mouse(1) = y1 End If Next Next If (mouse(0) = x And mouse(1) = y) Or (Data(x, y) = 1) Then Return False End If ary(x, y) = 1 Return True End Function End Class
Public Class GameArray
'变量
Private ary(13, 13) As Integer
'调用
Public Sub New()
Make()
End Sub
'调用数据
Public ReadOnly Property Data() As Integer(,)
Get
Return ary
End Get
End Property
'制作
Sub Make()
Dim x, y As Integer
For x = 0 To 12
For y = 0 To 12
If x = 6 And y = 6 Then
ary(x, y) = 2
ElseIf Int(Rnd() * 10) = 1 Then
ary(x, y) = 1
Else
ary(x, y) = 0
End If
Next
Next
End Sub
'注释
'可通行区域 = 0
'禁止通行区域 = 1
'老鼠 = 2
'是否相邻判断
Function adjoin(ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer) As Boolean
If Not (Valid(x1, x2) = True And Valid(x2, y2) = True) Then
Throw New Exception("存在无效坐标。")
End If
If Math.Abs(x2 - x1) = 1 Or Math.Abs(y2 - y1) = 1 Then
Return True
End If
Dim num As Integer
num = y1 Mod 2
If num = 0 Then '奇数行靠左
If Math.Abs(y2 - y1) = 1 And x1 - x2 = 1 Then
Return True
End If
Else '偶数行靠右
If Math.Abs(y2 - y1) = 1 And x2 - x1 = 1 Then
Return True
End If
End If
Return False
End Function
'相邻可通行区域
Function AdjoinAndPossable(ByVal x As Integer, ByVal y As Integer) As Integer()()
If Valid(x, y) = False Then
Throw New Exception("无效坐标。")
End If
Dim x1, y1 As Integer
Dim Coordinate As Integer()() = {}
Dim SelfAry(2) As Integer
For x1 = 0 To 12
For y1 = 0 To 12
SelfAry(0) = x1
SelfAry(1) = y1
If adjoin(x, y, x1, y1) = True And Me.Data(x1, y1) = 1 = False Then
ArrayAdd(Coordinate, SelfAry)
End If
Next
Next
Return Coordinate
End Function
'有效
Function Valid(ByVal x As Integer, ByVal y As Integer) As Boolean
Return (x >= 0 And x < 13 And y >= 0 And y < 13)
End Function
'清除老鼠
Sub MoveMouse()
Dim x, y As Integer
For x = 0 To 12
For y = 0 To 12
If ary(x, y) = 2 Then
ary(x, y) = 0
Exit Sub
End If
Next
Next
End Sub
'移动老鼠
Sub MoveMouse(ByVal x As Integer, ByVal y As Integer)
If Valid(x, y) = False Then
Throw New Exception("无效坐标。")
End If
Dim x1, y1 As Integer
For x1 = 0 To 12
For y1 = 0 To 12
If ary(x1, y1) = 2 Then
ary(x1, y1) = 0
End If
Next
Next
ary(x, y) = 2
End Sub
'添加障碍 (返回是否成功)
Function AddBlock(ByVal x As Integer, ByVal y As Integer) As Boolean
If Valid(x, y) = False Then
Throw New Exception("无效坐标。")
End If
Dim x1, y1 As Integer
Dim mouse(2) As Integer
For x1 = 0 To 12
For y1 = 0 To 12
If ary(x1, y1) = 2 Then
mouse(0) = x1
mouse(1) = y1
End If
Next
Next
If (mouse(0) = x And mouse(1) = y) Or (Data(x, y) = 1) Then
Return False
End If
ary(x, y) = 1
Return True
End Function
End Class
以前写的Ruby:
module Function PI = 3.1415926535897932385 E = 2.7182818284590452354 BITS = 32 PHI = 1.6180339887498948482 TAU = 2 * PI ABTS = 32 GAMMA = 0.5772156649015328606 C2 = 0.6601618158468695739 def gamma(x) x = x.truncate == x ? x.truncate : x if x.is_a?(Integer) if x > 0 return fact(x) else raise ArgumentError.new("wrong number") end else if x > 0 num = sqrt(2 * PI / x) * (1.0 / E * sqrt(x + 1.0 / (12 * x - 1.0 / (10 * x)))) else num = gamma(1 - (n.abs - Integer(n.abs))) for t in 1..Integer(n.abs) + 1 i = 1 - (n.abs - Integer(n.abs)) - t num /= i end end end return num.truncate == num ? num.truncate : num rescue Infinity end def s_dec(x) a = x.to_s.scan(/./) dd = false for s in 0...a.size if dd break end for c in 0...a.size i = a[c] if i == "." a.shift dd = true break else a.shift break end end end return a.to_s end def about(x, y) num = Integer(x.abs) ary = s_dec(x).scan(/./) for i in 0...ary.size ns = ary[i] nt = ns.to_i ntc = nt ntc *= 10 ** (-i - 1) if i < y and not i - y == -1 num += ntc elsif i - y == -1 num += ntc if ary[i + 1].to_i >= 5 num += 10 ** (-y) end break else break end end if x < 0 num = -num end return num.truncate == num ? num.truncate : num end def fact(x) x = x.truncate == x ? x.truncate : x if x.is_a?(Integer) if x < 0 raise ArgumentError.new("wrong number") elsif x < 2 return 1 else return x * fact(x - 1) rescue Infinity end else return gamma(x + 1) end end def sqrt(x) if x < 0 raise ArgumentError.new("wrong number") end num = x ** 0.5 return num.truncate == num ? num.truncate : num end def sqr(x) return x ** 2 end def cube(x) return x ** 3 end def cuberoot(x) if x < 0 return -cuberoot(-x) end num = x ** (1.0 / 3) return num.truncate == num ? num.truncate : num end def root(x, y) if (x.abs != self and y % 2 == 0) or y == 0 raise ArgumentError.new("wrong number") end if x.abs != x num = (x * (-1)) ** (1.0 / y) return num.truncate == num ? -num.truncate : -num end num = x ** (1.0 / y) return num.truncate == num ? num.truncate : num end def sin(x) while x < 0 x += TAU end x %= TAU num = sum(0, BITS){|n| (-1) ** n * x ** (2 * n + 1) / fact(2 * n + 1).to_f} num = about(num, ABTS) return num.truncate == num ? num.truncate : num end def sind(x) return sin(x * PI / 180.0) end def sing(x) return sind(x * 0.9) end def cos(x) num = sqrt((1 - sqr(sin(x))).abs) if x % TAU >= PI num = -num end return num.truncate == num ? num.truncate : num end def cosd(x) return cos(x * PI / 180.0) end def cosg(x) return cosd(x * 0.9) end def tan(x) return sin(x) / cos(x) end def tand(x) return sind(x) / cosd(x) end def tang(x) return sing(x) / cosg(x) end def sum(min, max) ary = [] for i in min..max ary.push(yield(i)) end num = 0 for n in ary num += n end return num.truncate == num ? num.truncate : num rescue Nan end def multi(min, max) ary = [] for i in min..max ary.push(yield(i)) end num = ary[0] ary.shift for n in ary num *= n end return num.truncate == num ? num.truncate : num rescue Nan end def asin(x) if x < -1 or x > 1 raise ArgumentError.new("wrong number") end num = 2 * atan(x / (1 + sqrt(1 - sqr(x))).to_f) return num.truncate == num ? num.truncate : num end def acos(x) num = PI / 2 - asin(x) return num.truncate == num ? num.truncate : num end def atan(x) num = x / (1 + sqr(x)).to_f * sum(0, BITS){|n| n == 0 ? 1 : multi(1, n){|k| 2 * k * sqr(x) / ((2 * k + 1) * (1 + sqr(x))).to_f}} return num.truncate == num ? num.truncate : num end def asind(x) num = asin(x) * 180 / PI return num.truncate == num ? num.truncate : num end def acosd(x) num = 90 - asind(x) return num.truncate == num ? num.truncate : num end def atand(x) num = atan(x) * 180 / PI return num.truncate == num ? num.truncate : num end def erf(x) num = 2.0 / sqrt(PI) * sum(0, BITS){|n| (-1) ** n * x ** (2 * n + 1) / fact(n) * (2 * n + 1)} num = about(num, ABTS) return num.truncate == num ? num.truncate : num end def erfc(x) return 1 - erf(x) end def sgn(x) return x <=> 0 end def aerf(x) num = sum(0, BITS){|g| erf_c(g) / (2 * g + 1).to_f * (sqrt(PI) / 2 * x) ** (2 * g + 1)} return num.truncate == num ? num.truncate : num end def aerfc(x) return aerf(1 - x) end def ln(x) if x < 0 raise ArgumentError.new("wrong number") end y = (x - 1) / (x + 1) num = 2 * y * (1 + sum(1, BITS){|n| 1.0 / (2 * n + 1) * y ** (2 * n)}) return num.truncate == num ? num.truncate : num end def lg(x) if x < 0 raise ArgumentError.new("wrong number") end num = ln(x) / ln(10) return num.truncate == num ? num.truncate : num end def log(x, y) if x < 0 or (y <= 0 or y == 1) raise ArgumentError.new("wrong number") end num = lg(y) / lg(x) return num.truncate == num ? num.truncate : num end def sinh(x) num = (E ** x - E ** (-x)) / 2 return num.truncate == num ? num.truncate : num end def cosh(x) num = (E ** x + E ** (-x)) / 2 return num.truncate == num ? num.truncate : num end def tanh(x) num = sinh(x) / cosh(x) return num.truncate == num ? num.truncate : num end def asinh(x) num = ln(x + sqrt(sqr(x) + 1)) return num.truncate == num ? num.truncate : num end def acosh(x) num = ln(x + sqrt(sqr(x) - 1)) return num.truncate == num ? num.truncate : num end def atanh(x) num = ln(sqrt(1 - sqr(x)) / (1 - x)) return num.truncate == num ? num.truncate : num end def beta(x, y) num = (gamma(x) * gamma(y)) / gamma(x + y) return num.truncate == num ? num.truncate : num end def dms(x) ary = x.split(1, true) na = [ary[0].to_s, ".", ""] for i in ary[1] str = (i * 60).to_s na[2] += str end num = na.to_s.to_f return num.truncate == num ? num.truncate : num end def Int(x) return Integer(x) end def gen_e(x, y) num = fact(x) / sqrt(PI) * sum(0, BITS){|p| (-1) ** p * ((y ** (x * p + 1)).to_f / ((x * p + 1) * fact(p)))} return num.truncate == num ? num.truncate : num end def erf_c(x) if x == 0 return 1 end num = sum(0, x - 1){|m| erf_c(m) * erf_c(x - 1 - m) / ((m + 1) * (2 * m + 1)).to_f} return num.truncate == num ? num.truncate : num end def Frac(x) num = x - Int(x) return num.truncate == num ? num.truncate : num end def adms(x) ary = x.split(2, true) na = [ary[0].to_s, ".", ""] for i in ary[1] if i > 60 raise ArgumentError.new("wrong number") end n = i / 6 / 10 na.push(n) end num = na.to_s.to_f return num.truncate == num ? num.truncate : num end def atan2(y, x) num = atan(y) / atan(x) return num.truncate == num ? num.truncate : num end def atand2(y, x) num = atand(y) / atand(x) return num.truncate == num ? num.truncate : num end end class Object include Function end
module Function
PI = 3.1415926535897932385
E = 2.7182818284590452354
BITS = 32
PHI = 1.6180339887498948482
TAU = 2 * PI
ABTS = 32
GAMMA = 0.5772156649015328606
C2 = 0.6601618158468695739
def gamma(x)
x = x.truncate == x ? x.truncate : x
if x.is_a?(Integer)
if x > 0
return fact(x)
else
raise ArgumentError.new("wrong number")
end
else
if x > 0
num = sqrt(2 * PI / x) * (1.0 / E * sqrt(x + 1.0 / (12 * x - 1.0 / (10 * x))))
else
num = gamma(1 - (n.abs - Integer(n.abs)))
for t in 1..Integer(n.abs) + 1
i = 1 - (n.abs - Integer(n.abs)) - t
num /= i
end
end
end
return num.truncate == num ? num.truncate : num rescue Infinity
end
def s_dec(x)
a = x.to_s.scan(/./)
dd = false
for s in 0...a.size
if dd
break
end
for c in 0...a.size
i = a[c]
if i == "."
a.shift
dd = true
break
else
a.shift
break
end
end
end
return a.to_s
end
def about(x, y)
num = Integer(x.abs)
ary = s_dec(x).scan(/./)
for i in 0...ary.size
ns = ary[i]
nt = ns.to_i
ntc = nt
ntc *= 10 ** (-i - 1)
if i < y and not i - y == -1
num += ntc
elsif i - y == -1
num += ntc
if ary[i + 1].to_i >= 5
num += 10 ** (-y)
end
break
else
break
end
end
if x < 0
num = -num
end
return num.truncate == num ? num.truncate : num
end
def fact(x)
x = x.truncate == x ? x.truncate : x
if x.is_a?(Integer)
if x < 0
raise ArgumentError.new("wrong number")
elsif x < 2
return 1
else
return x * fact(x - 1) rescue Infinity
end
else
return gamma(x + 1)
end
end
def sqrt(x)
if x < 0
raise ArgumentError.new("wrong number")
end
num = x ** 0.5
return num.truncate == num ? num.truncate : num
end
def sqr(x)
return x ** 2
end
def cube(x)
return x ** 3
end
def cuberoot(x)
if x < 0
return -cuberoot(-x)
end
num = x ** (1.0 / 3)
return num.truncate == num ? num.truncate : num
end
def root(x, y)
if (x.abs != self and y % 2 == 0) or y == 0
raise ArgumentError.new("wrong number")
end
if x.abs != x
num = (x * (-1)) ** (1.0 / y)
return num.truncate == num ? -num.truncate : -num
end
num = x ** (1.0 / y)
return num.truncate == num ? num.truncate : num
end
def sin(x)
while x < 0
x += TAU
end
x %= TAU
num = sum(0, BITS){|n| (-1) ** n * x ** (2 * n + 1) / fact(2 * n + 1).to_f}
num = about(num, ABTS)
return num.truncate == num ? num.truncate : num
end
def sind(x)
return sin(x * PI / 180.0)
end
def sing(x)
return sind(x * 0.9)
end
def cos(x)
num = sqrt((1 - sqr(sin(x))).abs)
if x % TAU >= PI
num = -num
end
return num.truncate == num ? num.truncate : num
end
def cosd(x)
return cos(x * PI / 180.0)
end
def cosg(x)
return cosd(x * 0.9)
end
def tan(x)
return sin(x) / cos(x)
end
def tand(x)
return sind(x) / cosd(x)
end
def tang(x)
return sing(x) / cosg(x)
end
def sum(min, max)
ary = []
for i in min..max
ary.push(yield(i))
end
num = 0
for n in ary
num += n
end
return num.truncate == num ? num.truncate : num rescue Nan
end
def multi(min, max)
ary = []
for i in min..max
ary.push(yield(i))
end
num = ary[0]
ary.shift
for n in ary
num *= n
end
return num.truncate == num ? num.truncate : num rescue Nan
end
def asin(x)
if x < -1 or x > 1
raise ArgumentError.new("wrong number")
end
num = 2 * atan(x / (1 + sqrt(1 - sqr(x))).to_f)
return num.truncate == num ? num.truncate : num
end
def acos(x)
num = PI / 2 - asin(x)
return num.truncate == num ? num.truncate : num
end
def atan(x)
num = x / (1 + sqr(x)).to_f * sum(0, BITS){|n| n == 0 ? 1 : multi(1, n){|k| 2 * k * sqr(x) / ((2 * k + 1) * (1 + sqr(x))).to_f}}
return num.truncate == num ? num.truncate : num
end
def asind(x)
num = asin(x) * 180 / PI
return num.truncate == num ? num.truncate : num
end
def acosd(x)
num = 90 - asind(x)
return num.truncate == num ? num.truncate : num
end
def atand(x)
num = atan(x) * 180 / PI
return num.truncate == num ? num.truncate : num
end
def erf(x)
num = 2.0 / sqrt(PI) * sum(0, BITS){|n| (-1) ** n * x ** (2 * n + 1) / fact(n) * (2 * n + 1)}
num = about(num, ABTS)
return num.truncate == num ? num.truncate : num
end
def erfc(x)
return 1 - erf(x)
end
def sgn(x)
return x <=> 0
end
def aerf(x)
num = sum(0, BITS){|g| erf_c(g) / (2 * g + 1).to_f * (sqrt(PI) / 2 * x) ** (2 * g + 1)}
return num.truncate == num ? num.truncate : num
end
def aerfc(x)
return aerf(1 - x)
end
def ln(x)
if x < 0
raise ArgumentError.new("wrong number")
end
y = (x - 1) / (x + 1)
num = 2 * y * (1 + sum(1, BITS){|n| 1.0 / (2 * n + 1) * y ** (2 * n)})
return num.truncate == num ? num.truncate : num
end
def lg(x)
if x < 0
raise ArgumentError.new("wrong number")
end
num = ln(x) / ln(10)
return num.truncate == num ? num.truncate : num
end
def log(x, y)
if x < 0 or (y <= 0 or y == 1)
raise ArgumentError.new("wrong number")
end
num = lg(y) / lg(x)
return num.truncate == num ? num.truncate : num
end
def sinh(x)
num = (E ** x - E ** (-x)) / 2
return num.truncate == num ? num.truncate : num
end
def cosh(x)
num = (E ** x + E ** (-x)) / 2
return num.truncate == num ? num.truncate : num
end
def tanh(x)
num = sinh(x) / cosh(x)
return num.truncate == num ? num.truncate : num
end
def asinh(x)
num = ln(x + sqrt(sqr(x) + 1))
return num.truncate == num ? num.truncate : num
end
def acosh(x)
num = ln(x + sqrt(sqr(x) - 1))
return num.truncate == num ? num.truncate : num
end
def atanh(x)
num = ln(sqrt(1 - sqr(x)) / (1 - x))
return num.truncate == num ? num.truncate : num
end
def beta(x, y)
num = (gamma(x) * gamma(y)) / gamma(x + y)
return num.truncate == num ? num.truncate : num
end
def dms(x)
ary = x.split(1, true)
na = [ary[0].to_s, ".", ""]
for i in ary[1]
str = (i * 60).to_s
na[2] += str
end
num = na.to_s.to_f
return num.truncate == num ? num.truncate : num
end
def Int(x)
return Integer(x)
end
def gen_e(x, y)
num = fact(x) / sqrt(PI) * sum(0, BITS){|p| (-1) ** p * ((y ** (x * p + 1)).to_f / ((x * p + 1) * fact(p)))}
return num.truncate == num ? num.truncate : num
end
def erf_c(x)
if x == 0
return 1
end
num = sum(0, x - 1){|m| erf_c(m) * erf_c(x - 1 - m) / ((m + 1) * (2 * m + 1)).to_f}
return num.truncate == num ? num.truncate : num
end
def Frac(x)
num = x - Int(x)
return num.truncate == num ? num.truncate : num
end
def adms(x)
ary = x.split(2, true)
na = [ary[0].to_s, ".", ""]
for i in ary[1]
if i > 60
raise ArgumentError.new("wrong number")
end
n = i / 6 / 10
na.push(n)
end
num = na.to_s.to_f
return num.truncate == num ? num.truncate : num
end
def atan2(y, x)
num = atan(y) / atan(x)
return num.truncate == num ? num.truncate : num
end
def atand2(y, x)
num = atand(y) / atand(x)
return num.truncate == num ? num.truncate : num
end
end
class Object
include Function
end
(未包括number.split,本来写过的此方法) |
|