<commit_before>const {Range, Record} = require('immutable');

const Vector = require('../math/vector');

const GameState = require('./gameState');

const EMPTY = 0;
const SNAKE = 1;
const GOAL = 2;

// Body parts added for each food item
const GROWTH_RATE = 5; // parts per goal

const SnakeGameRecord = Record({
  rows: undefined,
  cols: undefined,
  tiles: undefined,
  goal: undefined,
  snakeSize: 0,
  remainingGrowth: 0,
  state: new GameState(),
});

class SnakeGame extends SnakeGameRecord {
  static fromDimensions({rows, cols}) {
    return new SnakeGame({
      rows: rows,
      cols: cols,
      tiles: Range(0, rows).map(() => (
        Range(0, cols).map(() => EMPTY).toList()
      )).toList(),
    }).placeSnake({
      x: Math.floor(cols / 2),
      y: Math.floor(rows / 2),
    }).placeRandomGoal();
  }

  get isFull() {
    return this.snakeSize >= this.rows * this.cols;
  }

  isSnake({x, y}) {
    return this.getTile({x, y}) === SNAKE;
  }

  getTile({x, y}) {
    return this.tiles.get(y).get(x);
  }

  setTile({x, y}, value) {
    return this.update('tiles', (tiles) => (
      tiles.update(y, (row) => row.set(x, value))
    ));
  }

  placeSnake({x, y}) {
    if (this.isSnake({x, y})) {<commit_msg>Changed internal representation of tiles to something easier to work with in immutable js<commit_after>const {Range, Record} = require('immutable');

const Vector = require('../math/vector');

const GameState = require('./gameState');

const EMPTY = 0;
const SNAKE = 1;
const GOAL = 2;

// Body parts added for each food item
const GROWTH_RATE = 5; // parts per goal

const SnakeGameRecord = Record({
  rows: undefined,
  cols: undefined,
  tiles: undefined,
  goal: undefined,
  snakeSize: 0,
  remainingGrowth: 0,
  state: new GameState(),
});

class SnakeGame extends SnakeGameRecord {
  static fromDimensions({rows, cols}) {
    return new SnakeGame({
      rows: rows,
      cols: cols,
      tiles: Range(0, rows * cols).map(() => EMPTY).toList(),
    }).placeSnake({
      x: Math.floor(cols / 2),
      y: Math.floor(rows / 2),
    }).placeRandomGoal();
  }

  get isFull() {
    return this.snakeSize >= this.rows * this.cols;
  }

  isSnake({x, y}) {
    return this.getTile({x, y}) === SNAKE;
  }

  getTile({x, y}) {
    return this.tiles.get(this.tileIndexFromPosition({x, y}));
  }

  setTile({x, y}, value) {
    return this.update('tiles', (tiles) => (
      tiles.set(this.tileIndexFromPosition({x, y}), value)
    ));
  }

  *getRows() {
    return Range(0, this.rows).map((i) => (
      this.tiles.slice(i * this.cols, (i + 1) * this.cols)
    ));
  }

  tileIndexFromPosition({x, y}) {
    return y * this.rows + x;
  }

  placeSnake({x, y}) {
    if (this.isSnake({x, y})) {
{'fraction_non_alphanumeric': 0.17357798165137614, 'fraction_numerical': 0.007706422018348624, 'mean_word_length': 3.0445103857566767, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '7427558', 'n_tokens_mistral': 1009, 'n_tokens_neox': 984, 'n_words': 246}