<commit_before>import { Component, ViewChild, ElementRef, Renderer  } from '@angular/core';
import { HostListener, NgZone } from '@angular/core';

class Direction {
  static UP    = 'UP';
  static RIGHT = 'RIGHT';
  static DOWN  = 'DOWN';
  static LEFT  = 'LEFT';

  static fromKey(keyCode: number): string {
    if (keyCode === 37) {
      return Direction.LEFT;
    } else if (keyCode === 38) {
      return Direction.UP;
    } else if (keyCode === 39) {
      return Direction.RIGHT;
    } else if (keyCode === 40) {
      return Direction.DOWN;
    } else {
      return null;
    }
  }

  static opposites(d1: string, d2: string): boolean {
    return (d1 === Direction.LEFT && d2 === Direction.RIGHT) ||
      (d1 === Direction.RIGHT && d2 === Direction.LEFT) ||
      (d1 === Direction.UP && d2 === Direction.DOWN) ||
      (d1 === Direction.DOWN && d2 === Direction.UP);
  }
}

@Component({
  selector: 'my-snake-grid',
  template: `
      <canvas #canvas></canvas>
  `
})
export class MySnakeGridComponent {
  @ViewChild('canvas')
  canvasRef: ElementRef;
  private canvas: any;

  private x: number;
  private y: number;
  private running: boolean;
  private direction: string;

  constructor(private ngZone: NgZone) {
    this.x = 0;
    this.y = 0;
    this.direction = Direction.RIGHT;
  }

  private getNextX(): number {
    if (this.direction === Direction.LEFT) {
      this.x--;
      if (this.x < 0) {
        this.x = 200;
      }
    } else if (this.direction === Direction.RIGHT) {
      this.x++;
      if (this.x > 200) {
        this.x = 0;
      }
    }
    return this.x;
  }

  private getNextY(): number {
    if (this.direction === Direction.UP) {
      this.y--;
      if (this.y < 0) {
        this.y = 200;
      }
    } else if (this.direction === Direction.DOWN) {
      this.y++;
      if (this.y > 200) {
        this.y = 0;
      }
    }
    return this.y;
  }

  private setDirection(d: string) {
    if (d && ! Direction.opposites(d, this.direction)) {
      this.direction = d;
    }
  }

  ngAfterViewInit() {
    console.log('Initialize grid');
    this.canvas = this.canvasRef.nativeElement;
    this.canvas.width = 200;
    this.canvas.height = 200;
    //this.draw();
    this.running = true;
    this.ngZone.runOutsideAngular(() => this.paintLoop());
  }

  ngOnDestroy() {
    this.running = false;
  }

  paintLoop() {
    if (! this.running) {
      return;
    }

    if (this.canvas.getContext) {
      let ctx = this.canvas.getContext('2d');

      // Paint current frame
      ctx.fillRect(this.getNextX(), this.getNextY(), 10, 10);
    }

    // Schedule next frame
   requestAnimationFrame(() => this.paintLoop());
  }

  draw() {
    console.log('Draw grid');
    if (this.canvas.getContext) {
      let ctx = this.canvas.getContext('2d');
      let w = this.canvas.width;
      let h = this.canvas.height;

      ctx.beginPath();
      // Outer circle
      ctx.arc(w * .5, h * .5, w * .4, 0, Math.PI * 2, true);<commit_msg>remove unused import<commit_after>import { Component, ViewChild, ElementRef } from '@angular/core';
import { HostListener, NgZone } from '@angular/core';

class Direction {
  static UP    = 'UP';
  static RIGHT = 'RIGHT';
  static DOWN  = 'DOWN';
  static LEFT  = 'LEFT';

  static fromKey(keyCode: number): string {
    if (keyCode === 37) {
      return Direction.LEFT;
    } else if (keyCode === 38) {
      return Direction.UP;
    } else if (keyCode === 39) {
      return Direction.RIGHT;
    } else if (keyCode === 40) {
      return Direction.DOWN;
    } else {
      return null;
    }
  }

  static opposites(d1: string, d2: string): boolean {
    return (d1 === Direction.LEFT && d2 === Direction.RIGHT) ||
      (d1 === Direction.RIGHT && d2 === Direction.LEFT) ||
      (d1 === Direction.UP && d2 === Direction.DOWN) ||
      (d1 === Direction.DOWN && d2 === Direction.UP);
  }
}

@Component({
  selector: 'my-snake-grid',
  template: `
      <canvas #canvas></canvas>
  `
})
export class MySnakeGridComponent {
  @ViewChild('canvas')
  canvasRef: ElementRef;
  private canvas: any;

  private x: number;
  private y: number;
  private running: boolean;
  private direction: string;

  constructor(private ngZone: NgZone) {
    this.x = 0;
    this.y = 0;
    this.direction = Direction.RIGHT;
  }

  private getNextX(): number {
    if (this.direction === Direction.LEFT) {
      this.x--;
      if (this.x < 0) {
        this.x = 200;
      }
    } else if (this.direction === Direction.RIGHT) {
      this.x++;
      if (this.x > 200) {
        this.x = 0;
      }
    }
    return this.x;
  }

  private getNextY(): number {
    if (this.direction === Direction.UP) {
      this.y--;
      if (this.y < 0) {
        this.y = 200;
      }
    } else if (this.direction === Direction.DOWN) {
      this.y++;
      if (this.y > 200) {
        this.y = 0;
      }
    }
    return this.y;
  }

  private setDirection(d: string) {
    if (d && ! Direction.opposites(d, this.direction)) {
      this.direction = d;
    }
  }

  ngAfterViewInit() {
    console.log('Initialize grid');
    this.canvas = this.canvasRef.nativeElement;
    this.canvas.width = 200;
    this.canvas.height = 200;
    this.running = true;
    this.ngZone.runOutsideAngular(() => this.paintLoop());
  }

  ngOnDestroy() {
    this.running = false;
  }

  paintLoop() {
    if (! this.running) {
      return;
    }

    if (this.canvas.getContext) {
      let ctx = this.canvas.getContext('2d');

      // Paint current frame
      ctx.fillRect(this.getNextX(), this.getNextY(), 10, 10);
    }

    // Schedule next frame
   requestAnimationFrame(() => this.paintLoop());
  }

  drawSmiley() {
    console.log('Draw grid');
    if (this.canvas.getContext) {
      let ctx = this.canvas.getContext('2d');
      let w = this.canvas.width;
      let h = this.canvas.height;

      ctx.beginPath();
      // Outer circle
      ctx.arc(w * .5, h * .5, w * .4, 0, Math.PI * 2, true);
{'fraction_non_alphanumeric': 0.15962994112699747, 'fraction_numerical': 0.017830109335576116, 'mean_word_length': 2.279646993932708, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '1659724', 'n_tokens_mistral': 2122, 'n_tokens_neox': 2019, 'n_words': 473}