<commit_before>{-# LANGUAGE OverloadedStrings #-}
module Main where

import Control.Monad
import Linear
import Linear.Affine ( Point(P) )
import SDL (($=))
import qualified SDL
import Data.IORef
import Data.Word
import Data.Maybe
import System.Random

data Direction = L | U | R | D
               deriving(Show)

-- turn an event into a possible direction if its an arrow key
--key2Code :: SDL.EventPayload -> Maybe Int32
key2Code (SDL.KeyboardEvent ked) = Just $ SDL.unwrapScancode $ SDL.keysymScancode $ SDL.keyboardEventKeysym ked
key2Code _ = Nothing

key2Direction :: SDL.EventPayload -> Maybe Direction
key2Direction (SDL.KeyboardEvent ked)
  | sym == 80 = Just L
  | sym == 82 = Just U
  | sym == 79 = Just R
  | sym == 81 = Just D
  where sym = SDL.unwrapScancode $ SDL.keysymScancode $ SDL.keyboardEventKeysym ked
key2Direction _ = Nothing


main :: IO ()
main = do
  SDL.initialize [ SDL.InitEverything ]

  let winConfig = SDL.defaultWindow { SDL.windowPosition = SDL.Absolute (P (V2 100 100))
                                    , SDL.windowInitialSize = V2 640 480 }

      rdrConfig = SDL.RendererConfig { SDL.rendererType = SDL.AcceleratedVSyncRenderer
                                     , SDL.rendererTargetTexture = True }

  let gridSize = 25

  window <- SDL.createWindow "Hello world" winConfig
  renderer <- SDL.createRenderer window (-1) rdrConfig

  lstRef <- newIORef (0 :: Word32)
  posRef <- newIORef [(0, 0)]
  dirRef <- newIORef R
  foodRef <- newIORef Nothing

  let loop = do
        let collectEvents = do
              e <- SDL.pollEvent
              case e of
                Nothing -> return []
                Just e' -> (e' :) <$> collectEvents
        events <- collectEvents

        let quit = any (== SDL.QuitEvent) $ map SDL.eventPayload events
        let dirs = mapMaybe key2Direction $ map SDL.eventPayload events

        forM_ dirs $ writeIORef dirRef
        currentDir <- readIORef dirRef

        -- putStrLn $ show dirs -- $ map key2Code $ map SDL.eventPayload events

        lst <- readIORef lstRef
        ts <- SDL.ticks
        if ts > lst + 400
          then do writeIORef lstRef ts
                  modifyIORef posRef (\x -> case x of
                                        s:ss -> case currentDir of
                                          L -> (fst s - gridSize, snd s):ss
                                          R -> (fst s + gridSize, snd s):ss
                                          U -> (fst s, snd s - gridSize):ss
                                          D -> (fst s, snd s + gridSize):ss
                                        _ -> [])
          else do return ()

        food <- readIORef foodRef
        when (isNothing food) $ do
          rndx <- getStdRandom (randomR (0, 10))
          rndy <- getStdRandom (randomR (0, 10))
          writeIORef foodRef $ Just (rndx, rndy)

        -- clear the drawing
        SDL.rendererDrawColor renderer $= V4 0 0 0 255
        SDL.clear renderer

        -- draw a food
        food <- readIORef foodRef
        when (isJust food) $ do<commit_msg>drop last element of snake as it moves<commit_after>{-# LANGUAGE OverloadedStrings #-}
module Main where

import Control.Monad
import Linear
import Linear.Affine ( Point(P) )
import SDL (($=))
import qualified SDL
import Data.IORef
import Data.Word
import Data.Maybe
import System.Random

data Direction = L | U | R | D
               deriving(Show)

-- turn an event into a possible direction if its an arrow key
--key2Code :: SDL.EventPayload -> Maybe Int32
key2Code (SDL.KeyboardEvent ked) = Just $ SDL.unwrapScancode $ SDL.keysymScancode $ SDL.keyboardEventKeysym ked
key2Code _ = Nothing

key2Direction :: SDL.EventPayload -> Maybe Direction
key2Direction (SDL.KeyboardEvent ked)
  | sym == 80 = Just L
  | sym == 82 = Just U
  | sym == 79 = Just R
  | sym == 81 = Just D
  where sym = SDL.unwrapScancode $ SDL.keysymScancode $ SDL.keyboardEventKeysym ked
key2Direction _ = Nothing


main :: IO ()
main = do
  SDL.initialize [ SDL.InitEverything ]

  let winConfig = SDL.defaultWindow { SDL.windowPosition = SDL.Absolute (P (V2 100 100))
                                    , SDL.windowInitialSize = V2 640 480 }

      rdrConfig = SDL.RendererConfig { SDL.rendererType = SDL.AcceleratedVSyncRenderer
                                     , SDL.rendererTargetTexture = True }

  let gridSize = 25

  window <- SDL.createWindow "Hello world" winConfig
  renderer <- SDL.createRenderer window (-1) rdrConfig

  lstRef <- newIORef (0 :: Word32)
  posRef <- newIORef [(0, 0)]
  dirRef <- newIORef R
  foodRef <- newIORef Nothing

  let loop = do
        let collectEvents = do
              e <- SDL.pollEvent
              case e of
                Nothing -> return []
                Just e' -> (e' :) <$> collectEvents
        events <- collectEvents

        let quit = any (== SDL.QuitEvent) $ map SDL.eventPayload events
        let dirs = mapMaybe key2Direction $ map SDL.eventPayload events

        forM_ dirs $ writeIORef dirRef
        currentDir <- readIORef dirRef

        -- putStrLn $ show dirs -- $ map key2Code $ map SDL.eventPayload events

        lst <- readIORef lstRef
        ts <- SDL.ticks
        if ts > lst + 400
          then do writeIORef lstRef ts
                  modifyIORef posRef (\x -> case x of
                                        s:ss -> init ((case currentDir of
                                          L -> (fst s - gridSize, snd s)
                                          R -> (fst s + gridSize, snd s)
                                          U -> (fst s, snd s - gridSize)
                                          D -> (fst s, snd s + gridSize)):s:ss)
                                        _ -> [])
          else do return ()

        food <- readIORef foodRef
        when (isNothing food) $ do
          rndx <- getStdRandom (randomR (0, 10))
          rndy <- getStdRandom (randomR (0, 10))
          writeIORef foodRef $ Just (rndx, rndy)

        -- clear the drawing
        SDL.rendererDrawColor renderer $= V4 0 0 0 255
        SDL.clear renderer

        -- draw a food
        food <- readIORef foodRef
        when (isJust food) $ do
{'fraction_non_alphanumeric': 0.08833522083805209, 'fraction_numerical': 0.01812004530011325, 'mean_word_length': 1.7078405606657907, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3799816', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1721, 'n_words': 577}