Browse Source

add second strategy

adaptedStrategy0
Zolfa 9 years ago
parent
commit
9deaefe0fd
2 changed files with 85 additions and 0 deletions
  1. +50
    -0
      src/Main2.hs
  2. +35
    -0
      src/Strategy2.hs

+ 50
- 0
src/Main2.hs View File

@ -0,0 +1,50 @@
{-# LANGUAGE DeriveGeneric #-}
module Main where
import GHC.Generics
import Data.Aeson
import Data.Aeson.Types
import System.Environment
import qualified Data.ByteString.Lazy.Char8 as BS
import System.IO
import Datatypes
import Opt
import JSONDeser
import Strategy2
import VM (Command, cmdToString)
data JSONSer = JSONSer { problemId :: Int,
seed :: Int,
tag :: String,
solution :: String
} deriving (Show, Generic)
instance FromJSON JSONSer
instance ToJSON JSONSer
main :: IO ()
main = do args <- getArgs
opt <- parseArgs args
file <- return ((optFile opt) !! 0)
str <- BS.readFile file
(id, gmseed) <- return (readInput str)
commandspoints <- return (map (\(seed,game) -> strat2 game) gmseed)
(commands,points) <- return $ unzip commandspoints
seeds <- return ((map (\(seed, _) -> seed)) gmseed)
writeFile ("scores") (scoredata id (fst $ unzip gmseed) points)
BS.putStrLn $ encode $ (packAll id seeds commands)
packAll :: Int -> [Int] -> [[Command]] -> [JSONSer]
packAll id seeds commandLists = zipWith (\x y -> JSONSer id x "lilik1" y) seeds commandStrings
where
commandStrings = map cmdToString commandLists
scoredata :: Int -> [Int] -> [Int] -> String
scoredata id seeds points = pretty
where
pretty = foldl (\x (a,b,c) -> (show a) ++ " " ++ (show b) ++ " " ++ (show c) ++ "\n" ++ x) "" zipdata
zipdata = zip3 ids seeds points
ids = replicate (length seeds) id

+ 35
- 0
src/Strategy2.hs View File

@ -0,0 +1,35 @@
module Strategy2 where
import Datatypes
import Test.QuickCheck (generate, elements)
import VM
import System.Random
pick :: Int -> [a] -> a
pick n vector = vector !! (n `mod` (length vector))
strat2 :: Game -> ([Command],Int)
strat2 game = if new_notes == GameOver
then (new_command : [], score new_game)
else (new_command : (fst $ strat2 new_game), snd $ strat2 new_game)
where
commands = [MoveSE, MoveSW, MoveE, MoveW, RotateClockwise, RotateCounterclockwise]
new_games = map (\cmd -> (step game cmd, cmd)) commands
ok_games = filter (is_ok.snd.fst) new_games
where is_ok (OK) = True
is_ok _ = False
locked_games = filter (is_locked.snd.fst) new_games
where is_locked (Lock _) = True
is_locked _ = False
over_games = filter (is_over.snd.fst) new_games
where is_over (GameOver) = True
is_over _ = False
((new_game,new_notes),new_command) = if (length ok_games) > 0
then pick 100 ok_games
else if (length locked_games) > 0
then pick 100 locked_games
else pick 100 over_games

Loading…
Cancel
Save