Score
0
Watch 25 Star 104 Fork 12

Gitee星球的小活动 / 第 0 届 代码编故事接力赛

Join us
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
让我们点燃程序员写作之魂,一人用代码写一段故事玩玩吧~ spread retract

Clone or download
StoryForCoder 3.17 KB
Copy Edit Web IDE Raw Blame History
2gua authored 2020-03-26 15:12 . add StoryForCoder.
/*
故事主题:一个程序员大男孩与一位清秀女孩邂逅,然后他们交往了......
编程语言:Kotlin
by 2gua
*/
import java.beans.PropertyChangeListener
import java.beans.PropertyChangeSupport
import kotlin.properties.Delegates
import kotlin.reflect.KProperty
// 女孩心情监听 Helper 类
open class MoodChangeAware {
protected val changeSupport = PropertyChangeSupport(this)
fun addPropertyChangeListener(listener: PropertyChangeListener) {
changeSupport.addPropertyChangeListener(listener)
}
fun removePropertyChangeListener(listener: PropertyChangeListener) {
changeSupport.removePropertyChangeListener(listener)
}
}
// 程序员大男孩
class BoyCoder(val name: String, private val feature: String) {
// 邂逅的女孩
val girl = Girl("莉莉", "单身清秀")
// 主人公初始化
init {
println("""从前有个${feature}程序员大男孩,名叫 ${name}.""")
}
// 遇见一位女孩
fun metAGirl(): BoyCoder {
println("""他偶遇了一位${girl.feature}的女孩${girl.name},似曾相识,恍若隔世,一见钟情。""")
return this
}
// 交往了
fun date(): Girl {
girl.addPropertyChangeListener(
PropertyChangeListener { mood ->
when (mood.newValue) {
"感动" -> {
println("直到有一天,女孩感动了,因为男孩为她写了一段这样的代码:")
love()
}
else -> println("""如果他的女孩心情${mood.newValue},男孩也跟着${mood.newValue}。""")
}
}
)
println("于是,他们交往了......")
return girl
}
// 爱的示意,猜猜是什么?
fun love() {
(15 downTo -14 step 1).map { y ->
(-30..29).map { x ->
if (Math.pow(Math.pow(x * 0.05, 2.0) + Math.pow(y * 0.1, 2.0) - 1, 3.0) -
Math.pow(x * 0.05, 2.0) * Math.pow(y * 0.1, 3.0) <= 0)
"love".chunked(1)[Math.floorMod(x - y, 4)]
else " "
}.joinToString("")
}.forEach { println(it) }
}
}
// 女孩
class Girl(val name: String, val feature: String) : MoodChangeAware() {
private val observer = { prop: KProperty<*>, oldValue: String, newValue: String ->
changeSupport.firePropertyChange(prop.name, oldValue, newValue)
}
// 女孩心情
private var mood: String by Delegates.observable("", observer)
// 女孩的心情变化
fun girlsFeeling(mood: String): Girl {
this.mood = mood
return this
}
// 直到有一天......
fun untilOneDay() {
this.mood = "感动"
}
}
fun main() {
BoyCoder("隔壁小二", "帅气")
.metAGirl() // 邂逅女孩
.date() // 后来约会了
.girlsFeeling("高兴") // 女孩高兴了
.girlsFeeling("忧伤") // 女孩忧伤了
.untilOneDay() // 直到有一天......
}

Comment ( 1 )

Sign in for post a comment

1
https://gitee.com/Selected-Activities/Code-relay.git
git@gitee.com:Selected-Activities/Code-relay.git
Selected-Activities
Code-relay
第 0 届 代码编故事接力赛
master

Help Search

220309 7019f5b6 1899542 220318 4015cbb9 1899542