{"size": 1527, "ext": "js", "max_stars_repo_path": "game/src/modules/game.js", "max_stars_repo_name": "SavvasStephanides/snake-game", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "game/src/modules/game.js", "max_issues_repo_name": "SavvasStephanides/snake-game", "max_issues_repo_issues_event_min_datetime": "2021-10-06T20:04:55.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T08:23:31.000Z", "max_forks_repo_path": "game/src/modules/game.js", "max_forks_repo_name": "SavvasStephanides/snake-game", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 3, "max_forks_count": null, "avg_line_length": 21.5070422535, "max_line_length": 101, "alphanum_fraction": 0.5389652914}
import Board from "./board"

export default function Game(){
    var board = new Board()
    var score = 0
    var isPaused = true

    board.snake.coordinates = [
        {x: 10, y: 10},
        {x: 11, y: 10},
        {x: 12, y: 10},
        {x: 13, y: 10}
    ]

    board.treat.coordinates = {x: 3, y: 3}

    function gameLoop(){
        if(isPaused === true){
            return
        }

        board.snake.moveSnake()
        if(snakeHasReachedTreat()){
            board.treat.changeTreatPosition()
            board.snake.expandSnake()
            this.score = this.score + 1
        }

        if(playerHasLost()){
            alert("Final score: " + this.score)
            window.location.reload()
        }
    }

    function snakeHasReachedTreat(){
        var snakeHead = board.snake.coordinates[0]
        return snakeHead.x === board.treat.coordinates.x && snakeHead.y === board.treat.coordinates.y
    }

    function playerHasLost(){
        return !board.snake.snakeIsWithinBoundaries({x: 20, y: 20}) 
                || board.snake.snakeHasRunIntoItself()
    }

    function setSnakeDirection(direction){
        board.snake.direction = direction
    }

    function resumeGame(){
        isPaused = false
    }

    function pauseGame(){
        isPaused = true
    }

    function getIsPaused(){
        return isPaused
    }

    return {
        score,
        board,
        gameLoop,
        setSnakeDirection,
        resumeGame,
        pauseGame,
        getIsPaused,
        isPaused
    }
}