forked from haskell-game/tiny-games-hs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
play
executable file
·175 lines (165 loc) · 5.47 KB
/
play
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env bash
# A front end to run games easily.
set -e
# store args globally for the lol
args=("$@")
# All games in category then submission order, as in README.
# (leave 0th element empty)
games=(
""
prelude/guess1/guess1.hs
prelude/pure-doors/pure-doors.hs
prelude/fifteen/fifteen.hs
prelude/chess/chess.hs
prelude/sudoku/sudoku.hs
prelude/matchmaking/matchmaking.hs
prelude/tiny-brot/tiny-brot.hs
prelude/mini-othello/mini-othello.hs
prelude/one-dot/one-dot.hs
prelude/expressit/expressit.hs
prelude/life/life.hs
prelude/call-by-push-block/call-by-push-block.hs
prelude/companion/companion.hs
base/timing/timing.hs
base/shoot/shoot.hs
base/log2048/log2048.hs
base/rhythm/rhythm.hs
base/peyton-says/peyton-says.hs
base/acey-deucey/acey-deucey.hs
base/flower-seeds/flower-seeds.hs
base/lambda-ray/lambda-ray.hs
base/7up7down/7up7down.hs
base/snake/snake.hs
default/type-and-furious/type-and-furious.hs
default/shmupemup/shmupemup.hs
default/tsp/tsp.hs
default/lol/lol.hs
default/space-invaders/space-invaders.hs
hackage/guess2/guess2.hs
hackage/wordle/wordle.hs
hackage/ski/ski.hs
hackage/guesscolor/guesscolor.hs
hackage/bulls-n-cows/bulls-n-cows.hs
hackage/hallway-to-hell/hallway-to-hell.hs
hackage/1234-hero/1234-hero.hs
hackage/crappy-flappy/crappy-flappy.hs
hackage/pong/pong.hs
hackage/minesweeper/minesweeper.hs
hackage/pong2/pong2.hs
hackage/brickbreaker/brickbreaker.hs
hackage/lazy-march/lazy-march.hs
hackage/balances/balances.hs
hackage/vaders/vaders.hs
hackage/tetris/tetris.hs
hackage/short-guess/short-guess.hs
hackage/hexescape/hexescape.hs
hackage/snake-lemma/snake-lemma.hs
hackage/subpar-hexagon/subpar-hexagon.hs
)
# Games needing special invocation, in alphabetical order.
call-by-push-block() { runghc "$1"; }
fifteen() { runghc -XPatternSynonyms -XLambdaCase "$1"; }
brickbreaker() { stack script --compile --resolver lts-20 --package gloss "$1"; }
hallway-to-hell() { stack runghc --resolver lts-20 --package random --package rio "$1"; }
lambda-ray() { stack script --resolver lts-20 --optimize "$1"; }
life() { runghc "$1"; }
log2048() { runghc "$1"; }
lol() { stack script --resolver lts-20 --optimize --package process --package text -- "$1" "${args[@]}"; }
matchmaking() { runghc -cpp -DD='a=replicate;b=putStrLn;c=length;p=map;u=max(2)' "$1"; }
mini-othello() { stack script --resolver lts-20 --optimize "$1"; }
pong2() { stack runghc --resolver lts-20 --package ansi-terminal-game "$1"; }
shmupemup() { cd "$(dirname "$1")" || exit 1; stack runghc --resolver lts-20.12 --package ghc-9.2.6 --package parsec "$(basename "$1")"; }
short-guess() { stack runghc --resolver lts-20 --package random "$1"; }
space-invaders() { cd "$(dirname "$1")" || exit 1; runghc "$(basename "$1")"; }
sudoku() { runghc "$1"; }
tsp() { runghc "$1"; }
# Help
printFileWithTitle() {
printf '%0.1s' "="{1..80};echo
echo "= " "$1"
printf '%0.1s' "="{1..80};echo
cat "$1"
}
gamecat() { echo "$1" | cut -d/ -f1; }
gamename() { basename "$1" .hs; }
detectAndWarnAboutFreeglut() {
# NOTE: The detection logic requires gcc, without it will produce false alarm.
# But can you imagine the life of people without gcc?
which gcc &>/dev/null || return
(LIBRARY_PATH=$LD_LIBRARY_PATH gcc -lglut --shared -o /dev/null &>/dev/null) ||
cat <<HERE
Freeglut library not found! You should install freeglut in order to play some games.
Checkout the README freeglut section for more information.
HERE
}
help() {
cat <<HERE
--------------------------------------------------------
___ __
|__| _ _| _|| | . _ / _ _ _ _ | _ _ /|
| |(_|_)|((-|| | || )\/ \__)(_||||(- __)(_|||| |
--------------------------------------------------------
Here are the entries from HTGJ1, Feb 2023 !
This script can run each game for you, using ghc or stack.
If you don't have these yet, see https://www.haskell.org/get-started .
Usage: play [NUM|NAME|SUBSTR]
HERE
for k in "${!games[@]}"; do
[[ $k == 0 ]] && continue
g="${games[$k]}"
printf "%2d) %-20s %s\n" "$k" "$(gamename "$g")" "[$(gamecat "$g")]"
done
echo
detectAndWarnAboutFreeglut
}
# Select Game
declare -A gamesbyname
for g in "${games[@]}"; do
[[ -z $g ]] && continue
gamesbyname["$(gamename "$g")"]=$g
done
selectGame() {
local g
# select by num
if [[ "$1" =~ ^[0-9]+$ ]];then
g=${games[$1]}
fi
# select by game name
if [[ -z $g ]]; then g=${gamesbyname[$1]}; fi
# select by game name matching
if [[ -z $g ]]; then
for h in "${games[@]}"; do
if [[ $(gamename "$h") =~ $1 ]]; then g="$h"; break; fi
done
fi
# no match
if [[ -z $g ]]; then echo "$1 is unrecognised, run '$0' to see the list"; exit 1; fi
echo "$g"
}
# Main
cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
if [[ $# == 0 || "$1" == "-h" || "$1" == "--help" ]];then
help
elif [[ "$2" == "-h" || "$2" == "--help" ]];then
g=$(selectGame "$1")
gi=$(echo "$(dirname "$g")"/Import*.hs)
echo "$gi"
gh=$(echo "$(dirname "$g")"/README*)
{
if [[ -f "$gi" ]];then
printFileWithTitle "$gi"
fi
printFileWithTitle "$g"
if [[ -f "$gh" ]];then
printFileWithTitle "$gh"
fi
} | ${PAGPER:-less};
else
g=$(selectGame "$1")
gname=$(gamename "$g")
# run it through a launcher, or directly with shebang
if declare -f "$gname" >/dev/null
then "$gname" "$g"
else $g
fi
fi