<commit_before>/**
 * Canvas class
 *
 * @constructor
 */
var Canvas = function () {

    // listen events
    _.listenTo({
        'frame:change': 'draw',
        'food:eaten':   'setFood',
        'change:speed': 'speedUpSnake',
        'change:score': 'updateScore',
        'game:over':    'gameOver'
    }, this);

    // config
    this.frame = 0;
    this.speed = 10;
    this.food = {
        x: null,
        y: null
    };
    this.gameScore = 0;
    this.highScore = localStorage.getItem('highScore') || 0;
    this.scores = {};

    // canvas
    this.canvas = document.createElement('canvas');
    // attrs
    this.canvas.width = 400;
    this.canvas.height = 300;
    this.canvas.className = 'board';

    // context
    this.ctx = this.canvas.getContext('2d');

    // new snake for the board
    this.snake = new Snake(this.ctx, this.food);
};


/**
 * drawScoreBoard
 *
 * @returns {Canvas}
 */
Canvas.prototype.drawBoard = function () {

    var list = document.createElement('ul');

    list.className = 'score';

    _.forEach(['highScore', 'gameScore'], function (key) {

        this.scores[key] = document.createElement('li');
        this.scores[key].className = key;
        this.scores[key].innerHTML = 0;

        list.appendChild(this.scores[key]);
    }, this);

    this.scores.highScore.innerHTML = 'High score: ' + this.highScore;

    document.body.appendChild(list);
    document.body.appendChild(this.canvas);

    return this;
};

/**
 * clear
 */
Canvas.prototype.clear = function () {
    this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
};


/**
 * draw
 */
Canvas.prototype.draw = function () {

    this.frame += 1;

    if (this.frame >= this.speed) {
        this.frame = 0;

        this.clear();
        this.snake.move();

        this.drawFood()
    }
};


/**
 * setFood
 *
 * @returns {Canvas}
 */
Canvas.prototype.setFood = function () {

    var coordinates = {
        x: _.random(390, 10),
        y: _.random(140, 10)
    };

    while (this.snake.isInSnakeTail(coordinates)) {
        coordinates.x = _.random(390, 10);
        coordinates.y = _.random(140, 10);
    }

    this.food.x = coordinates.x;
    this.food.y = coordinates.y;

    return this.drawFood();
};


/**
 * drawFood
 *
 * @returns {Canvas}
 */
Canvas.prototype.drawFood = function () {

    this.ctx.beginPath();
    this.ctx.rect(this.food.x, this.food.y, 10, 10);
    this.ctx.fillStyle = 'black';
    this.ctx.fill();

    return this;
};


/**
 * speedUpSnake
 * speeds up the refresh rate and in turn speeds up the snake
 */
Canvas.prototype.speedUpSnake = function () {
    this.speed -= 0.1;
};


/**
 * updateScore
 *
 * @param type {String}<commit_msg>Changing food to replicate the original style<commit_after>/**
 * Canvas class
 *
 * @constructor
 */
var Canvas = function () {

    // listen events
    _.listenTo({
        'frame:change': 'draw',
        'food:eaten':   'setFood',
        'change:speed': 'speedUpSnake',
        'change:score': 'updateScore',
        'game:over':    'gameOver'
    }, this);

    // config
    this.frame = 0;
    this.speed = 10;
    this.food = {
        x: null,
        y: null
    };
    this.gameScore = 0;
    this.highScore = localStorage.getItem('highScore') || 0;
    this.scores = {};

    // canvas
    this.canvas = document.createElement('canvas');
    // attrs
    this.canvas.width = 400;
    this.canvas.height = 300;
    this.canvas.className = 'board';

    // context
    this.ctx = this.canvas.getContext('2d');

    // new snake for the board
    this.snake = new Snake(this.ctx, this.food);
};


/**
 * drawScoreBoard
 *
 * @returns {Canvas}
 */
Canvas.prototype.drawBoard = function () {

    var list = document.createElement('ul');

    list.className = 'score';

    _.forEach(['highScore', 'gameScore'], function (key) {

        this.scores[key] = document.createElement('li');
        this.scores[key].className = key;
        this.scores[key].innerHTML = 0;

        list.appendChild(this.scores[key]);
    }, this);

    this.scores.highScore.innerHTML = 'High score: ' + this.highScore;

    document.body.appendChild(list);
    document.body.appendChild(this.canvas);

    return this;
};

/**
 * clear
 */
Canvas.prototype.clear = function () {
    this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
};


/**
 * draw
 */
Canvas.prototype.draw = function () {

    this.frame += 1;

    if (this.frame >= this.speed) {
        this.frame = 0;

        this.clear();
        this.snake.move();

        this.drawFood()
    }
};


/**
 * setFood
 *
 * @returns {Canvas}
 */
Canvas.prototype.setFood = function () {

    var coordinates = {
        x: _.random(390, 10),
        y: _.random(140, 10)
    };

    while (this.snake.isInSnakeTail(coordinates)) {
        coordinates.x = _.random(390, 10);
        coordinates.y = _.random(140, 10);
    }

    this.food.x = coordinates.x;
    this.food.y = coordinates.y;

    return this.drawFood();
};


/**
 * drawFood
 *
 * @returns {Canvas}
 */
Canvas.prototype.drawFood = function () {

    var draw = false,
        x,
        y,
        i;

    for (i = 0; i < 9; i += 1) {

        y = Math.floor(i / 3) * 3;
        x = (i % 3) * 3;

        if (draw) {
            this.ctx.beginPath();
            this.ctx.rect((this.food.x + x), (this.food.y + y), 3, 3);
            this.ctx.fillStyle = 'black';
            this.ctx.fill();
        }

        draw = !draw;
    }

    return this;
};


/**
 * speedUpSnake
 * speeds up the refresh rate and in turn speeds up the snake
 */
Canvas.prototype.speedUpSnake = function () {
    this.speed -= 0.1;
};


/**
 * updateScore
 *
 * @param type {String}
{'fraction_non_alphanumeric': 0.1671353251318102, 'fraction_numerical': 0.015992970123022848, 'mean_word_length': 2.1918115535614136, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7166707', 'n_tokens_mistral': 2004, 'n_tokens_neox': 1846, 'n_words': 376}