This repository has been archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
308 lines (277 loc) · 11.4 KB
/
Main.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
{-
Copyright (c) 2015, Kristian Sällberg <[email protected]>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the author nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
-}
module Main (Move(..),
Context,
Player(..),
GameState(..),
handleAction,
main) where
--import AStar
import Block
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Trans.State.Lazy
import Data.List
import Data.Ord
import System.IO
import System.Random
{-| A Context is a StateT wrapper around the game's state,
allowing usage of the inner IO moand.
-}
type Context = StateT GameState IO
data Move =
StepDown |
StepLeft |
StepRight |
TurnLeft |
TurnRight |
Drop |
NoMoves deriving (Eq, Ord)
instance Show Move where
show StepDown = "down"
show StepLeft = "left"
show StepRight = "right"
show TurnLeft = "turnleft"
show TurnRight = "turnright"
show Drop = "drop"
show NoMoves = "no_moves"
data Player = Player {
playerName :: String,
rowPoints :: Int,
combo :: Int,
field :: Field
} deriving Show
data GameState = GameState {
timebank :: Int,
timePerMove :: Int,
players :: [Player],
myBot :: String,
fieldHeight :: Int,
fieldWidth :: Int,
gameRound :: Int,
thisPieceType :: Block,
nextPieceType :: Block,
thisPiecePosition :: (Int, Int)
} deriving Show
-- crude debugging flag
debug' :: Bool
debug' = False
debug :: String -> Context ()
debug x = when debug' (liftIO $ hPutStrLn stderr x)
debugIO :: IO () -> Context ()
debugIO x = when debug' (liftIO x)
{-| Get a list of moves required to get from point a to point b-}
p2p :: Pair -> Pair -> [Move]
p2p (x1, y1) (x2, y2) = xPath ++ [StepDown | _ <- [1..yDelta]]
where xDelta = x2 - x1
xPath = case xDelta > 0 of
True -> [StepRight | _ <- [1..xDelta]]
False -> [StepLeft | _ <- [1..(abs xDelta)]]
yDelta = abs $ y2 - y1
expandCoor :: Pair -> Pair -> [Pair]
expandCoor a@(x1, y1) b = [(x1, y1)] ++ p2p2' a b
where p2p2' :: Pair -> Pair -> [Pair]
p2p2' (x1, y1) (x2, y2)
| x1 == x2 && y1 == y2 = []
| x1 < x2 = [(x1+1,y1)] ++ p2p2' (x1+1,y1) (x2, y2)
| x1 > x2 = [(x1-1,y1)] ++ p2p2' (x1-1,y1) (x2, y2)
| y1 < y2 = [(x1,y1+1)] ++ p2p2' (x1,y1+1) (x2, y2)
coorToPath :: [Pair] -> [Move]
coorToPath (p:ps) = p2pLs' p ps
where p2pLs' :: Pair -> [Pair] -> [Move]
p2pLs' p [] = []
p2pLs' p ps = p2p p (head ps) ++ p2pLs' (head ps) (tail ps)
-- double list concated in order to apply the several alterPos returning pos
insertToField :: [(Field, [Pair], Int)] -> Field -> [(Pair, Field, Field, Int)]
insertToField xs f = concat [ [(p, insertBlock rot p f, rot, rotNum)
| p <- pos]
| (rot, pos, rotNum) <- xs]
optimizePath :: [Move] -> [Move]
optimizePath xs = case xs /= diff of
True -> diff ++ [Drop]
False -> xs
where diff = reverse $ dropWhile (== StepDown) (reverse xs)
keepOK :: [(Pair, Field, Field, Int)] -> [(Pair, Field, Field, Int)]
keepOK fs = filter isGrounded (filter noClashes fs)
where noClashes (_, f, _, _) = not $ elem 3 (concat f)
evalPath :: Pair -> Pair -> Field -> Field -> Bool
evalPath from to block field = and [not $ elem 3 (concat f) | f <- mapped]
where mapped = [insertBlock block p field | p <- (expandCoor from to)]
{-| Given an origin position, find the first possible location that
AStar can travel to, and return a list of moves representing the
path to that location.
-}
findPath :: Pair -> Field
-> [(Pair, Field, Field, Int)]
-> (Int, Field, Pair, [Move])
findPath _ _ [] = error "no possible moves"
findPath origin field ((p, f, bl, count):xs) =
case (evalPath origin p bl field) of
False -> findPath origin field xs
True -> (count, bl, p, coorToPath (expandCoor origin p))
{-| Handle the action given by the admin script!
Make use of already set game state. -}
handleAction :: Int -> Context (Field, Pair)
handleAction moves = do
state <- get
myPlayer <- getMyBot -- type Player
gen <- liftIO getStdGen
let myField = clearField oneToZeroNoBlack (field myPlayer)
unclearF = clearField oneToZero (field myPlayer)
-- idea: change mode/weight algoritm
-- (set scoringFun depending on something)
scoringFun = case (usedFieldHeight unclearF) <= 6 of
True -> avoidEmptys
False -> avoidEmptys--seekBottom
block = thisPieceType state
rots = allRotations block :: [(Field, Int)]
positions = [(rot,
allPositions (fieldWidth state) (fieldHeight state) rot,
numRot)
| (rot, numRot) <- rots] :: [(Field, [Pair], Int)]
allFields = keepOK $ insertToField positions myField
weighted = sortBy (comparing fst)
[(scoringFun f, x)
| x@(pos, f, b, _count) <- allFields]
(count, finalBl, pos, pairPath) = findPath (thisPiecePosition state)
myField
(reverse (map snd weighted))
movePlan = [TurnLeft | _ <- [1..count]] ++ pairPath
debug ("Rotations: " ++ show count)
debug ("Final block: ")
debugIO $ pretty finalBl
liftIO $ putStrLn $ formatMoves movePlan--(optimizePath movePlan)
return (finalBl, pos)
-------------
-- PARSING --
-------------
parse :: [String] -> Context ()
parse ["action", "moves", move] = do handleAction (read move :: Int)
return ()
parse ("update":xs) = parseUpdate xs
parse ("settings":xs) = parseSettings xs
parse _ = error "Unsupported command!"
parseSettings :: [String] -> Context ()
parseSettings ["timebank", time] = do
debug $ "Set timebank to: " ++ time
state <- get
put $ state{ timebank = (read time :: Int) }
parseSettings ["time_per_move", time] = do
state <- get
put $ state{ timePerMove = (read time :: Int) }
parseSettings ["player_names", names] = do
state <- get
let namesLs = splitBy ',' names
playersLs = foldl (\acc name -> acc ++ [Player{playerName = name,
rowPoints = 0,
combo = 0,
field = []
}])
[]
namesLs
put $ state{ players = playersLs }
parseSettings ["your_bot", botname] = do
state <- get
put $ state{ myBot = botname }
parseSettings ["field_width", width] = do
state <- get
put $ state{ fieldWidth = (read width :: Int) }
parseSettings ["field_height", height] = do
state <- get
put $ state{ fieldHeight = (read height :: Int) }
parseSettings _ = error "Unsupported setting received!"
{-| Update the game state with configurations received
with the update flag from the admin script -}
parseUpdate :: [String] -> Context ()
parseUpdate ["game", "round", roundVal] = do
state <- get
put $ state{ gameRound = (read roundVal :: Int) }
parseUpdate ["game", "this_piece_type", piece] = do
state <- get
put $ state{ thisPieceType = (read piece :: Block) }
parseUpdate ["game", "next_piece_type", piece] = do
state <- get
put $ state{ nextPieceType = (read piece :: Block) }
parseUpdate ["game", "this_piece_position", pos] = do
state <- get
let [x, y] = splitBy ',' pos
put $ state{ thisPiecePosition = (read x :: Int, read y :: Int) }
parseUpdate [playern, "row_points", rowPointsVal] = do
state <- get
let updatePls = [case playerName pl == playern of
True -> pl{rowPoints = read rowPointsVal :: Int}
False -> pl
| pl <- players state]
put $ state{ players = updatePls }
parseUpdate [playern, "combo", comboVal] = do
state <- get
let updatePls = [case playerName pl == playern of
True -> pl{combo = read comboVal :: Int}
False -> pl
| pl <- players state]
put $ state{ players = updatePls }
parseUpdate [playern, "field", fieldVal] = do
state <- get
let fieldParts = splitBy ';' fieldVal
fieldLs = [map (\x -> read x :: Int) (splitBy ',' fieldPart)
| fieldPart <- fieldParts]
updatePls = [case playerName pl == playern of
True -> pl{field = fieldLs}
False -> pl
| pl <- players state]
put $ state{ players = updatePls }
parseUpdate _ = error "Unsupported update received!"
loop :: Context ()
loop = do
line <- liftIO getLine
parse (words line)
liftIO (hFlush stdout)
eof <- liftIO isEOF
unless eof loop
main :: IO ()
main = do
hSetBuffering stdin LineBuffering
evalStateT loop $ GameState{timebank = 0,
timePerMove = 0,
players = [],
myBot = "not set",
fieldHeight = 20,
fieldWidth = 10,
gameRound = 0,
thisPieceType = I,
nextPieceType = O,
thisPiecePosition = (3, -1)}
-- helper functions
getMyBot :: Context Player
getMyBot = do
st <- get
case [pl | pl <- players st, playerName pl == myBot st] of
[] -> error "Bot not found!"
[bot] -> return $ bot
formatMoves :: [Move] -> String
formatMoves xs = tail $ foldl (\acc next -> acc ++ "," ++ show next) "" xs