瀏覽代碼

Create Grid data type

DricomDragon 5 年之前
父節點
當前提交
2e0a3a9589
共有 1 個文件被更改,包括 35 次插入0 次删除
  1. 35 0
      tp/mine-sweeper/main.hs

+ 35 - 0
tp/mine-sweeper/main.hs

@@ -0,0 +1,35 @@
+-- Mine Sweeper Lab
+
+-- Import section
+
+import Data.Char
+
+
+-- Types
+
+data Cell   = Covered Int Bool Bool -- number of bombs around ; has bomb ; has flag
+            | Uncovered Int -- number of bombs around
+            | Selected
+
+data Grid = Grid [[Cell]]
+
+
+-- Methods
+
+instance Show Cell where
+    show ( Covered _ _ hasFlag ) = if hasFlag then "[F]" else "[ ]"
+    show ( Uncovered n ) = ['(', intToDigit n, ')']
+    show Selected = ">x<"
+
+instance Show Grid where
+     show (Grid a) = unlines $ map (unwords . map show) a
+
+-- Functions
+
+
+-- Testing data
+
+tinyGrid = Grid [
+                    [Covered 2 True False, Uncovered 2, Covered 2 True False],
+                    [Selected, Uncovered 0, Covered 2 True True]
+                ]