Sun 14 July 2019
By Haven
In Python .
tags: python game
我在7年后等着你 真是一款非常震撼的故事性手游,今天南瓜又和我讨论了一下最终结局里的关于盒子怎么开的问题。
题目如下:
春人和葵分别在不同的房间。他们每人面前有一个盒子,以及自己和对方的两套操作系统,每套操作系统上有红蓝两个按钮。
盒子操作规则:
1. 解锁盒子:
红蓝两个按键,先按下的颜色为错误,后按下的颜色为正确。
2. 打开盒子:
只有解锁后才能打开盒子,打开任何一个盒子之后,便无法再进行此操作。
打开盒子的人才能存活,为了简化流程,我们在这里把单个人存活条件设置为打开盒子。
双活状态:要二人同时长安对方盒子的两个按钮。
故事中有三条时间线:
0周目: 春人先按了自己盒子(必然错误,假设是蓝色),自己的盒子锁了,然后用红色按钮给葵解锁了盒子,自己狗带;葵什么都不知道,打开盒子后进入1周目。
1周目: 葵穿越回来后比主角先操作,用了同样的方法帮主角解锁了盒子。并且一直按着自己盒子的两个按钮,自己狗带。春人打开盒子后进入2周目。
2周目: 春人回到了葵按下她的盒子的两个按钮以及自己打开盒子之前的时间点,这时候他直接按了双活解法,两人都活下来了。
设置类和属性如下:
盒子Box 有correctColor属性,为Blue or Red两种情况,可以再最后优化的时候将红蓝设为随机数增加趣味性。是否lock,是否open,如果unlock了就可以open了。
春人Haruto和葵Aoi都属于Human类,这里我们只关心他们是否alive。Alive与否受两个盒子的按钮与状态影响。
Solution类包含了0-2周目的所有操作情况,0周目时春人先按的颜色之外的另一种颜色为葵打开盒子的颜色,1周目亦然。2周目实际上只关心两个盒子的四个按钮是否都按下。
写代码的过程种意识到卧槽这可是个穿越游戏,也就是说他们穿越回以前节点的时候,盒子的状态有可能是未改变过的,所以应当把盒子也作为Human的一个属性,就能保留盒子状态了。
代码如下:
import random
class Box ( object ):
def __init__ ( self ):
self . lock = False
self . open = False
self . correctColor = None
self . blue = False
self . red = False
self . button = None
def pressButton ( self , button ):
self . button = button
if button == "Blue" :
self . blue = True
self . lock = True
self . correctColor = "Red"
else :
self . red = True
self . lock = True
self . correctColor = "Blue"
def returnButton ( self ):
return self . button
def setButton ( self , correctColor ):
self . correctColor = correctColor
self . lock = False
def openBox ( self ):
if self . lock == False :
self . open = True
def printButton ( self ):
if self . blue and self . red :
return " pressed both buttons Blue and Red!"
elif self . blue :
return " pressed any button Blue."
else :
return " pressed any button Red."
def printCorrectButton ( self ):
return "pressed the correct button " + self . correctColor + "."
def printOpen ( self ):
return " opened the box."
class Human ( object ):
def __init__ ( self , name ):
self . name = name
self . alive = True
self . box = None
def change ( self , box ):
self . box = box
if box . open :
self . alive = True
else :
self . alive = False
class areTheyAlive ( object ):
def printMessage ( self , human1 , human2 ):
if human1 . box . blue and human1 . box . red and human2 . box . blue and human2 . box . red :
print "Both of " + human1 . name + " and " + human2 . name + " are alive!"
else :
if human1 . alive :
print human1 . name + " is alive."
else :
print human1 . name + " is dead..."
if human2 . alive :
print human2 . name + " is alive."
else :
print human2 . name + " is dead..."
class Solution ( object ):
def __init__ ( self ):
self . lock = False
self . correct = None
def operateBox ( self ):
haruto = Human ( "Haruto" )
aoi = Human ( "Aoi" )
buttons = [ "Blue" , "Red" ]
zhoumu = 0
while zhoumu < 3 :
print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
print "In zhoumu " + str ( zhoumu ) + ":"
if zhoumu == 0 :
Haruto_Box = Box ()
button = buttons [ random . randint ( 0 , 1 )]
Haruto_Box . pressButton ( button )
print haruto . name + Haruto_Box . printButton ()
haruto . change ( Haruto_Box )
Aoi_Box = Box ()
Aoi_Box . setButton ( Haruto_Box . correctColor )
print aoi . name + " " + Aoi_Box . printCorrectButton ()
Aoi_Box . openBox ()
print aoi . name + Aoi_Box . printOpen ()
aoi . change ( Aoi_Box )
if zhoumu == 1 :
Aoi_Box = Box ()
button = buttons [ random . randint ( 0 , 1 )]
Aoi_Box . pressButton ( button )
print aoi . name + Aoi_Box . printButton ()
aoi . change ( Aoi_Box )
aoi . box . blue = True
aoi . box . red = True
Haruto_Box = Box ()
Haruto_Box . setButton ( Aoi_Box . correctColor )
print haruto . name + " " + Haruto_Box . printCorrectButton ()
Haruto_Box . openBox ()
print haruto . name + Haruto_Box . printOpen ()
haruto . change ( Haruto_Box )
print aoi . name + aoi . box . printButton ()
areTheyAlive () . printMessage ( haruto , aoi )
zhoumu += 1
continue
if zhoumu == 2 :
haruto . box . blue = True
haruto . box . red = True
print haruto . name + haruto . box . printButton ()
areTheyAlive () . printMessage ( haruto , aoi )
zhoumu += 1
if __name__ == "__main__" :
answer = Solution ()
answer . operateBox ()
运行结果如下:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
In zhoumu 0 :
Haruto pressed any button Blue .
Aoi pressed the correct button Red .
Aoi opened the box .
Haruto is dead ...
Aoi is alive .
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
In zhoumu 1 :
Aoi pressed any button Red .
Haruto pressed the correct button Blue .
Haruto opened the box .
Aoi pressed both buttons Blue and Red !
Haruto is alive .
Aoi is dead ...
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
In zhoumu 2 :
Haruto pressed both buttons Blue and Red !
Both of Haruto and Aoi are alive !
就联系一下对于实际问题的时候怎么样设计class和attributes。
虽然约定梗和穿越梗快被日帝玩烂了,但是凭借极其优秀的脚本,这个仅靠像素小人完成的故事还是给了我极大的震撼。哎,哭成狗了。。。霓虹人民放飞起来真是阔怕。。。南瓜推荐必出精品。
Proudly powered by Pelican , which takes great advantage of Python .
The theme is by Smashing Magazine , thanks!