Serie: Angelia
java
46 líneas
· Actualizado 2026-08-13
Direction.java
Angelia/Java/Lab_3/code/Direction.java
/**
* Direction.java —— 方向列舉(enum)
* Lab 3: Mountain Climbing(登山實驗)
*
* 這個檔案定義了在高度圖(heightmap)上移動時使用的五個「方向值」。
*
* 【重要】在課程的原始儲存庫中,這個檔案已經提供,
* 而且講義明確要求「不要修改這個檔案」。
* 這裡為了教材的完整性重新列出,並補上教學用註解。
*
* ─────────────────────────────────────────────
* 為什麼要用 enum,而不是用 int 常數(0 代表北、1 代表東……)?
* ─────────────────────────────────────────────
* 1. 型別安全(type safety):
* 編譯器保證 Direction 型別的變數只可能是這五個值之一,
* 絕對不會出現 dir = 7 這種毫無意義的狀態。
*
* 2. 可讀性:
* Direction.NORTH 一看就懂;數字 0 則要查文件才知道意思。
*
* 3. 搭配 switch 使用時,若漏寫某個分支,
* 現代的 switch 運算式(Java 14+)會直接編譯錯誤,幫你抓漏。
*
* enum 的每個值(NORTH、EAST……)其實都是 Direction 型別的
* 「唯一物件」,可以直接用 == 比較,也可以放進 switch。
*/
public enum Direction {
/** 北(North):往「上」一列移動,也就是列索引減 1(y - 1)。 */
NORTH,
/** 東(East):往「右」一行移動,也就是行索引加 1(x + 1)。 */
EAST,
/** 南(South):往「下」一列移動,也就是列索引加 1(y + 1)。 */
SOUTH,
/** 西(West):往「左」一行移動,也就是行索引減 1(x - 1)。 */
WEST,
/**
* 山頂(Peak):目前所在的格子比東西南北四個鄰居都「嚴格更高」,
* 不需要再移動了——你已經站在山頂上!
*/
PEAK
}
Artículos relacionados
Angelia
haskell
EN
Actualizado 2026-02-03
Adding.hs
Adding.hs — haskell source code from the Angelia learning materials (Angelia/Haskell/Adding.hs).
Leer artículo →
Angelia
haskell
EN
Actualizado 2026-02-03
Area.hs
Area.hs — haskell source code from the Angelia learning materials (Angelia/Haskell/Area.hs).
Leer artículo →
Angelia
haskell
EN
Actualizado 2026-02-03
Chess.hs
Chess.hs — haskell source code from the Angelia learning materials (Angelia/Haskell/Chess.hs).
Leer artículo →
Angelia
haskell
EN
Actualizado 2026-02-03
ChessGraphics.hs
ChessGraphics.hs — haskell source code from the Angelia learning materials (Angelia/Haskell/ChessGraphics.hs).
Leer artículo →
Angelia
haskell
EN
Actualizado 2026-02-03
Examples.hs
Examples.hs — haskell source code from the Angelia learning materials (Angelia/Haskell/Examples.hs).
Leer artículo →
Angelia
haskell
EN
Actualizado 2026-02-03
Fibonacci.hs
Fibonacci.hs — haskell source code from the Angelia learning materials (Angelia/Haskell/Fibonacci.hs).
Leer artículo →