7 kyu Turn with a Compass
這題不算難,但是我用Lua在table宣告初使值,犯了錯誤,
如下:
> a = {'t' = 1}
stdin:1: '}' expected near '='
這邊't' = 1錯誤,
t應該不用宣告為string, 而是table中的key會自帶string,
宣正如下:
> a = {t = 1}
這樣就正確了。
另外這題我寫的程式碼不是很精簡,
但還是寫出來了,如下:
local function direction(facing, turn)
local d = {N = 0,
NE = 45,
E = 90,
SE= 135,
S = 180,
SW= 225,
W = 270,
NW= 315}
local t = (d[facing] + turn) % 360
if 0 == t then
return 'N'
elseif 45 == t then
return 'NE'
elseif 90 == t then
return 'E'
elseif 135 == t then
return 'SE'
elseif 180 == t then
return 'S'
elseif 225 == t then
return 'SW'
elseif 270 == t then
return 'W'
elseif 315 == t then
return 'NW'
end
end
return direction
沒有留言:
張貼留言