-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base.hs
57 lines (46 loc) · 1.58 KB
/
Base.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CPP #-}
module Base where
import Data.Char
import Foreign.C.Types
-- Describe the status of each letter in the guess
data Status =
Here -- Letter in correct place
| Elsewhere (Maybe Direction) -- Letter elsewhere in word, with optional direction
| Nowhere -- Letter is not in the word
deriving (Show, Eq)
-- Describe the position of a letter in a word
-- `Before` represents before and/or after
-- `After` represents strictly after
data Direction = Before | After
deriving (Show, Eq)
-- Names of various prompts
data Prompt = Guess | Start | Quit | Lose | Win
deriving (Show, Eq, Enum)
-- Give the string associated with the prompt
prompt :: Prompt -> String
prompt Lose = "Run out of guesses!"
prompt Win = "You got it. Well done!"
prompt Start = "Guess [any] or quit [q]? "
prompt Guess = "Ok. Enter your guess: "
prompt Quit = "Bye!"
-----------------------------------------------------------
-- No need to read past here ------------------------------
-----------------------------------------------------------
-- Multi-platform version of `getChar` which has a fix for a GHC bug with Windows cmd/Powershell
getChar' :: IO Char
getChar' = do
#ifdef mingw32_HOST_OS
-- Windows has to do things...
c <- c_getch
let c' = chr . fromEnum $ c
putChar c'
return c'
#else
-- Linux, Unix, Mac OS X can just use the normal getChar
getChar
#endif
#ifdef mingw32_HOST_OS
foreign import ccall unsafe "conio.h getch"
c_getch :: IO CInt
#endif