Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
硅谷海盗
kotlin
提交
d549e9d3
K
kotlin
项目概览
硅谷海盗
/
kotlin
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kotlin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d549e9d3
编写于
4月 16, 2012
作者:
D
Dmitry Jemerov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update to current Kotlin
上级
338de15f
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
30 addition
and
34 deletion
+30
-34
examples/src/LambdaTheGathering.kt
examples/src/LambdaTheGathering.kt
+30
-34
未找到文件。
examples/src/LambdaTheGathering.
jetl
→
examples/src/LambdaTheGathering.
kt
浏览文件 @
d549e9d3
namespace LambdaTheGathering;
package
LambdaTheGathering
class GameError(msg: String): Exception(msg) {
}
/**
* Incomplete solution to the IFCP Contest 2011
* @author yole
*/
fun print(s: String) {
System.out?.println(s)
class
GameError
(
msg
:
String
):
Exception
(
msg
)
{
}
class Function(val name: String) {
fun apply(x: Value, proponent: Player): Value
abstract
class
Function
(
val
name
:
String
)
{
abstract
fun
apply
(
x
:
Value
,
proponent
:
Player
):
Value
fun
toString
()
=
name
fun asNumber(): Int { throw GameError("could not get numeric value of a function") }
open
fun
asNumber
():
Int
{
throw
GameError
(
"could not get numeric value of a function"
)
}
}
object
Identity
:
Function
(
"I"
)
{
fun apply(x: Value, proponent: Player): Value = x
override
fun
apply
(
x
:
Value
,
proponent
:
Player
):
Value
=
x
}
object
IdentityValue
:
Value
(
Identity
)
object
Zero
:
Function
(
"zero"
)
{
fun apply(x: Value, proponent: Player): Value = Value(0)
fun asNumber(): Int = 0
override
fun
apply
(
x
:
Value
,
proponent
:
Player
):
Value
=
Value
(
0
)
override
fun
asNumber
():
Int
=
0
}
object
Succ
:
Function
(
"succ"
)
{
fun apply(x: Value, proponent: Player): Value = Value(x.asNumber() + 1)
override
fun
apply
(
x
:
Value
,
proponent
:
Player
):
Value
=
Value
(
x
.
asNumber
()
+
1
)
}
object
Get
:
Function
(
"get"
)
{
fun apply(x: Value, proponent: Player): Value = proponent.getSlot(x.asNumber()).field
override
fun
apply
(
x
:
Value
,
proponent
:
Player
):
Value
=
proponent
.
getSlot
(
x
.
asNumber
()).
field
}
object
Dbl
:
Function
(
"dbl"
)
{
fun apply(x: Value, proponent: Player): Value = Value(x.asNumber() * 2)
override
fun
apply
(
x
:
Value
,
proponent
:
Player
):
Value
=
Value
(
x
.
asNumber
()
*
2
)
}
class
HelpFunc2
(
val
i
:
Value
,
val
j
:
Value
):
Function
(
"help("
+
i
+
")("
+
j
+
")"
)
{
fun apply(x: Value, proponent: Player): Value {
override
fun
apply
(
x
:
Value
,
proponent
:
Player
):
Value
{
val
n
=
x
.
asNumber
()
proponent
.
getSlot
(
i
.
asNumber
()).
decreaseVitality
(
n
)
proponent
.
getSlot
(
j
.
asNumber
()).
increaseVitality
(
n
*
11
/
10
);
...
...
@@ -46,23 +47,23 @@ class HelpFunc2(val i: Value, val j: Value): Function("help(" + i + ")(" + j + "
}
class
HelpFunc
(
val
i
:
Value
):
Function
(
"help("
+
i
+
")"
)
{
fun apply(x: Value, proponent: Player): Value = Value(HelpFunc2(i, x))
override
fun
apply
(
x
:
Value
,
proponent
:
Player
):
Value
=
Value
(
HelpFunc2
(
i
,
x
))
}
object
Help
:
Function
(
"help"
)
{
fun apply(x: Value, proponent: Player): Value = Value(HelpFunc(x))
override
fun
apply
(
x
:
Value
,
proponent
:
Player
):
Value
=
Value
(
HelpFunc
(
x
))
}
class
KFunc
(
val
arg
:
Value
):
Function
(
"K("
+
arg
+
")"
)
{
fun apply(x: Value, proponent: Player): Value = arg
override
fun
apply
(
x
:
Value
,
proponent
:
Player
):
Value
=
arg
}
object
K
:
Function
(
"K"
)
{
fun apply(x: Value, proponent: Player): Value = Value(KFunc(x))
override
fun
apply
(
x
:
Value
,
proponent
:
Player
):
Value
=
Value
(
KFunc
(
x
))
}
class
SFunc2
(
val
f
:
Value
,
val
g
:
Value
):
Function
(
"S("
+
f
+
")("
+
g
+
")"
)
{
fun apply(x: Value, proponent: Player): Value {
override
fun
apply
(
x
:
Value
,
proponent
:
Player
):
Value
{
val
h
=
f
.
apply
(
x
,
proponent
)
val
y
=
g
.
apply
(
x
,
proponent
)
val
z
=
h
.
apply
(
y
,
proponent
)
...
...
@@ -71,14 +72,14 @@ class SFunc2(val f: Value, val g: Value): Function("S(" + f + ")(" + g + ")") {
}
class
SFunc
(
val
f
:
Value
):
Function
(
"S("
+
f
+
")"
)
{
fun apply(x: Value, proponent: Player): Value = Value(SFunc2(f, x))
override
fun
apply
(
x
:
Value
,
proponent
:
Player
):
Value
=
Value
(
SFunc2
(
f
,
x
))
}
object
S
:
Function
(
"S"
)
{
fun apply(x: Value, proponent: Player): Value = Value(SFunc(x))
override
fun
apply
(
x
:
Value
,
proponent
:
Player
):
Value
=
Value
(
SFunc
(
x
))
}
class Value(val n: Int, val f: Function, val isFunction: Boolean) {
open
class
Value
(
val
n
:
Int
,
val
f
:
Function
,
val
isFunction
:
Boolean
)
{
this
(
n
:
Int
):
this
(
if
(
n
>
65535
)
65535
else
n
,
Identity
,
false
)
{}
this
(
f
:
Function
):
this
(
0
,
f
,
true
)
{}
...
...
@@ -132,14 +133,9 @@ class Slot() {
}
class
Player
()
{
var opponent: Player
var
opponent
:
Player
?
=
null
val slots: Array<Slot> = Array<Slot>(256)
{
for(i in 0..255) {
slots[i] = Slot()
}
}
val
slots
:
Array
<
Slot
>
=
Array
<
Slot
>(
256
,
{
i
->
Slot
()
})
fun
getSlot
(
i
:
Int
):
Slot
{
if
(
i
<
0
||
i
>
255
)
throw
GameError
(
"invalid slot index"
)
...
...
@@ -148,21 +144,21 @@ class Player() {
}
fun
printSlots
()
{
print("(slots {10000,I} are omitted)")
print
ln
(
"(slots {10000,I} are omitted)"
)
for
(
val
i
in
0
..
255
)
{
if
(!
slots
[
i
].
isDefault
())
{
print("" + i + "=" + slots[i].toString())
print
ln
(
""
+
i
+
"="
+
slots
[
i
].
toString
())
}
}
}
fun
applyCardToSlot
(
index
:
Int
,
card
:
Function
)
{
print("applying card " + card + " to slot " + index)
print
ln
(
"applying card "
+
card
+
" to slot "
+
index
)
slots
[
index
].
field
=
card
.
apply
(
slots
[
index
].
field
,
this
)
}
fun
applySlotToCard
(
index
:
Int
,
card
:
Function
)
{
print("applying slot " + index + " to card " + card)
print
ln
(
"applying slot "
+
index
+
" to card "
+
card
)
slots
[
index
].
field
=
slots
[
index
].
field
.
apply
(
Value
(
card
),
this
)
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录