Last active
July 29, 2016 21:43
-
-
Save FrankWu100/8126407 to your computer and use it in GitHub Desktop.
Linux & Ruby Mid 2 exam
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
期中補考-另補試題目(時間2013/12/23 22:00至23:00, 1個小時) | |
請設計一ruby程式,檔名為m2.rb,並完成以下需求: | |
這是一支模擬RPG遊戲的程式,地圖為橫向6格,縱向5格,如下圖: | |
(1) 圖中的T代表陷阱,每次走到陷阱時要扣除30點HP,圖中的B代表補血站,每次走到補血站可回復50點HP(最多補到100)。 | |
(2) 每走一步會耗費5點HP,但處於邊緣再往牆走時(不能移動)不扣HP。 | |
(3) 圖中波浪位置代表河流,走進河流時,角色即死亡,不能再移動。 | |
(4) 圖中的G位置代表成功到達點,當成功到達此位置時,請印出PASSED,角色亦不能再移動。 | |
(5) 每次移動要印出角色目前的位置與HP血量,預設血量為100,若血量扣完了請直接結束程式。 | |
(6) 輸入0時,可結束程式。 | |
如下例: | |
$ ./m2.rb | |
pos=0, HP=100 (印出目前位置與HP) | |
>8 (無法向上,留在原地) | |
pos=0, HP=100 (印出目前位置與HP) | |
>6 (輸入6代表向右) | |
pos=1, HP=65 (走到陷阱,HP扣30,走一步扣5) | |
>2 (向下) | |
pos=7, HP=60 (走一步扣5) | |
>2 (向下) | |
pos=13, HP=55 (走一步扣5) | |
>4 (向左) | |
pos=12, HP=100 (走一步扣5, 再吃補血包,補HP到100) | |
>6 (向右) | |
pos=13, HP=95 | |
>6 (向右) | |
pos=14, HP=0 (掉入河流, HP=0死亡) | |
>0 (輸入0,代表結束程式) | |
$ | |
請以pietty連線至 x.xxxx.xxx,使用自己的帳號登入,並在家目錄中設計m2.rb即可。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
class Person | |
attr_accessor :hp | |
attr_accessor :pos | |
def initialize | |
@hp = 100 | |
@pos = 0 | |
@passed = false | |
end | |
def moved | |
@hp=@hp-5 | |
end | |
def bump | |
@hp=@hp-20 | |
end | |
def haveHp | |
if @hp>0 | |
return true | |
else | |
puts "game over" | |
return false | |
end | |
end | |
def setHp(h) | |
@hp=h | |
end | |
def addHp(h) | |
@hp=@hp+h | |
if @hp>100 | |
@hp=100 | |
end | |
end | |
def setPassed(bool) | |
@passed = bool | |
end | |
def isPassed | |
if @passed | |
puts "PASSED" | |
end | |
return @passed | |
end | |
end | |
class Map | |
def initialize(height, width) | |
@height = height | |
@width = width | |
end | |
def setAddHealthPos(posArray) | |
@addHealthPos = posArray | |
end | |
def setTrapPos(posArray) | |
@trapPos = posArray | |
end | |
def setRiverPos(posArray) | |
@riverPos = posArray | |
end | |
def setGameOverPos(pos) | |
@gameOverPos = pos | |
end | |
def printLocation(pos) | |
for i in 0..@height*@width-1 | |
if i % @width == 0 | |
print "|" | |
end | |
if i==pos | |
print "*" | |
elsif @gameOverPos.include? i | |
print "G" | |
elsif @addHealthPos.include? i | |
print "B" | |
elsif @riverPos.include? i | |
print "~" | |
elsif @trapPos.include? i | |
print "T" | |
else | |
print " " | |
end | |
print "|" | |
if i % @width == @width-1 | |
print "\n" | |
end | |
end | |
end | |
def moveKey(p, key) | |
canMove=nil | |
arrow="" | |
case key | |
when "8" | |
canMove=moveUp(p) | |
arrow = "↑" | |
when "2" | |
canMove=moveDown(p) | |
arrow = "↓" | |
when "4" | |
canMove=moveLeft(p) | |
arrow = "←" | |
when "6" | |
canMove=moveRight(p) | |
arrow = "→" | |
when "7" | |
canMove=moveUpLeft(p) | |
arrow = "↖" | |
when "9" | |
canMove=moveUpRight(p) | |
arrow = "↗" | |
when "3" | |
canMove=moveDownRight(p) | |
arrow = "↘" | |
when "1" | |
canMove=moveDownLeft(p) | |
arrow = "↙" | |
end | |
if canMove==nil | |
msg = "Not direction key" | |
elsif canMove | |
p.moved | |
msg = "Moved " + arrow | |
spMsg = checkPos(p) | |
if spMsg != nil | |
msg = msg + ", " + spMsg | |
end | |
else | |
#p.bump | |
msg = "Bump" | |
end | |
return msg | |
end | |
def checkPos(p) | |
msg=nil | |
@gameOverPos.each { |x| | |
if (p.pos===x) | |
p.setPassed(true) | |
msg = "Game Over" | |
end | |
} | |
@addHealthPos.each { |x| | |
if (p.pos===x) | |
p.addHp(50) | |
msg = "Add HP" | |
end | |
} | |
@riverPos.each { |x| | |
if (p.pos===x) | |
p.setHp(0) | |
msg = "River" | |
end | |
} | |
@trapPos.each { |x| | |
if (p.pos===x) | |
p.addHp(-30) | |
msg = "Trap" | |
end | |
} | |
return msg | |
end | |
def canUp(pos) | |
if !((0..(@width-1))===pos) | |
return true | |
else | |
return false | |
end | |
end | |
def canDown(pos) | |
if !(((@width * (@height-1))..(@width*@height - 1))===pos) | |
return true | |
else | |
return false | |
end | |
end | |
def canLeft(pos) | |
if !(pos % @width == 0) | |
return true | |
else | |
return false | |
end | |
end | |
def canRight(pos) | |
if !(pos % @width == @width-1) | |
return true | |
else | |
return false | |
end | |
end | |
def moveUp(p) | |
if canUp(p.pos) | |
p.pos=p.pos-@width | |
return true | |
else | |
return false | |
end | |
end | |
def moveDown(p) | |
if canDown(p.pos) | |
p.pos=p.pos+@width | |
return true | |
else | |
return false | |
end | |
end | |
def moveLeft(p) | |
if canLeft(p.pos) | |
p.pos=p.pos-1 | |
return true | |
else | |
return false | |
end | |
end | |
def moveRight(p) | |
if canRight(p.pos) | |
p.pos=p.pos+1 | |
return true | |
else | |
return false | |
end | |
end | |
def moveUpLeft(p) | |
if canUp(p.pos) && canLeft(p.pos) | |
p.pos=p.pos-(@width+1) | |
return true | |
else | |
return false | |
end | |
end | |
def moveUpRight(p) | |
if canUp(p.pos) && canRight(p.pos) | |
p.pos=p.pos-(@width-1) | |
return true | |
else | |
return false | |
end | |
end | |
def moveDownRight(p) | |
if canDown(p.pos) && canRight(p.pos) | |
p.pos=p.pos+(@width+1) | |
return true | |
else | |
return false | |
end | |
end | |
def moveDownLeft(p) | |
if canDown(p.pos) && canLeft(p.pos) | |
p.pos=p.pos+(@width-1) | |
return true | |
else | |
return false | |
end | |
end | |
end | |
p=Person.new | |
m=Map.new(5,6) | |
m.setGameOverPos([26]) | |
m.setAddHealthPos([12]) | |
m.setTrapPos([1, 10]) | |
m.setRiverPos([14, 15, 16, 17]) | |
puts "Game Start" | |
printf "pos=%d, hp=%d\n", p.pos, p.hp | |
m.printLocation(p.pos) | |
key=nil | |
while(key!="0" && p.haveHp && !p.isPassed ) | |
print "Direction: " | |
key=gets.chop | |
pos=p.pos | |
movedMsg=m.moveKey(p, key) | |
m.printLocation(p.pos) | |
printf "%s, pos=%d, hp=%d\n", movedMsg, p.pos, p.hp | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment