<commit_before>import { Component } from '@angular/core';
import { GridItem } from './grid-item';

@Component({
  selector: 'Minesweeper',
  templateUrl: './minesweeper.component.html',
  styleUrls: ['./minesweeper.component.css'],
})


export class MinesweeperComponent {

  private gridSize = 10;
  private numMines = 10;

  private itemQueue: GridItem[] = [];
  private processed: GridItem[] = [];
  private minePositions: number[] = [];

  grid: GridItem[] = [];

  constructor() {
    for (let i = 0; i < this.gridSize; i++) {
      // this.grid[i] = [];
      for (let j = 0; j < this.gridSize; j++) {
        this.grid[(i * this.gridSize) + j] = { row: i, col: j, isRevealed: false, mineNeighbours: 0 , isBomb: false, isFlagged: false};
      }
    }
    this.generateMines();
  }

  reveal(item: GridItem, event) {
    event.stopPropagation();
    item.isRevealed = true;
    if (item.isBomb) {
      console.log ('boom');
    }
    this.itemQueue.push(item);
    this.processQueue();
  }

  flag( item: GridItem, event) {
    event.stopPropagation();
    item.isFlagged = !item.isFlagged;
    return false;
  }

  outputGridType(item) {

      if (item.isRevealed && item.isBomb) {
        return '<i class="fa fa-bomb" aria-hidden="true"></i>';
      }


      if (item.isRevealed && !item.isBomb && item.mineNeighbours > 0 ) {
        return item.mineNeighbours;
      }

      if (!item.isRevealed && item.isFlagged) {
        return '<i class="fa fa-flag" aria-hidden="true"></i>';
      }

      if (!item.isRevealed || (item.isRevealed && !item.isBomb && item.mineNeighbours === 0 )) {
        return '<i class="fa fa-fw" aria-hidden="true"></i>';
      }

  }

  generateMines() {
    let minesPlaced = 0;

    while (minesPlaced < this.numMines) {
       const minePos = Math.floor(Math.random() * 99);
      if (!this.minePositions.some(x => x === minePos)) {
        this.minePositions.push(minePos);
        this.grid[ minePos].isBomb = true;
        minesPlaced++;
        this.updateNeighbours(minePos);
      }
    }
  }

 updateNeighbours(minePos) {

    const row = Math.floor(minePos / this.gridSize);
    const col  = minePos % this.gridSize;
     this.addOneToNeighbour(row - 1, col - 1);
     this.addOneToNeighbour(row - 1, col);
     this.addOneToNeighbour(row - 1, col + 1);

     this.addOneToNeighbour(row, col - 1);
     this.addOneToNeighbour(row, col + 1);

     this.addOneToNeighbour(row + 1, col - 1);
     this.addOneToNeighbour(row + 1, col);
     this.addOneToNeighbour(row + 1, col + 1);
 }

addOneToNeighbour( row, col) {
    if (row < 0 || row > 9) {
        return;
    }

    if (col < 0 || col > 9 ) {
      return;
    }
    this.grid[ row * this.gridSize + col].mineNeighbours += 1;
 }

  processQueue() {
    this.processed = [];
    while (this.itemQueue.length > 0) {
      const currentItem = this.itemQueue.pop();<commit_msg>Add bounds checking method (DRY)<commit_after>import { Component } from '@angular/core';
import { GridItem } from './grid-item';

@Component({
  selector: 'Minesweeper',
  templateUrl: './minesweeper.component.html',
  styleUrls: ['./minesweeper.component.css'],
})


export class MinesweeperComponent {

  private gridSize = 10;
  private numMines = 10;

  private itemQueue: GridItem[] = [];
  private processed: GridItem[] = [];
  private minePositions: number[] = [];

  grid: GridItem[] = [];

  constructor() {
    for (let i = 0; i < this.gridSize; i++) {
      // this.grid[i] = [];
      for (let j = 0; j < this.gridSize; j++) {
        this.grid[(i * this.gridSize) + j] = { row: i, col: j, isRevealed: false, mineNeighbours: 0 , isBomb: false, isFlagged: false};
      }
    }
    this.generateMines();
  }

  reveal(item: GridItem, event) {
    event.stopPropagation();
    item.isRevealed = true;
    if (item.isBomb) {
      console.log ('boom');
    }
    this.itemQueue.push(item);
    this.processQueue();
  }

  flag( item: GridItem, event) {
    event.stopPropagation();
    item.isFlagged = !item.isFlagged;
    return false;
  }

  outputGridType(item) {

      if (item.isRevealed && item.isBomb) {
        return '<i class="fa fa-bomb" aria-hidden="true"></i>';
      }


      if (item.isRevealed && !item.isBomb && item.mineNeighbours > 0 ) {
        return item.mineNeighbours;
      }

      if (!item.isRevealed && item.isFlagged) {
        return '<i class="fa fa-flag" aria-hidden="true"></i>';
      }

      if (!item.isRevealed || (item.isRevealed && !item.isBomb && item.mineNeighbours === 0 )) {
        return '<i class="fa fa-fw" aria-hidden="true"></i>';
      }

  }

  generateMines() {
    let minesPlaced = 0;

    while (minesPlaced < this.numMines) {
       const minePos = Math.floor(Math.random() * 99);
      if (!this.minePositions.some(x => x === minePos)) {
        this.minePositions.push(minePos);
        this.grid[ minePos].isBomb = true;
        minesPlaced++;
        this.updateNeighbours(minePos);
      }
    }
  }

 updateNeighbours(minePos) {

    const row = Math.floor(minePos / this.gridSize);
    const col  = minePos % this.gridSize;
     this.addOneToNeighbour(row - 1, col - 1);
     this.addOneToNeighbour(row - 1, col);
     this.addOneToNeighbour(row - 1, col + 1);

     this.addOneToNeighbour(row, col - 1);
     this.addOneToNeighbour(row, col + 1);

     this.addOneToNeighbour(row + 1, col - 1);
     this.addOneToNeighbour(row + 1, col);
     this.addOneToNeighbour(row + 1, col + 1);
 }

withinBounds( row, col ) {
    if (row < 0 || row > 9) {
        return false;
    }
    if (col < 0 || col > 9 ) {
      return false;
    }
    return true;

}
addOneToNeighbour( row, col) {
    if (!this.withinBounds(row, col)) {
      return;
    }

    this.grid[ row * this.gridSize + col].mineNeighbours += 1;
 }

  processQueue() {
    this.processed = [];
    while (this.itemQueue.length > 0) {
      const currentItem = this.itemQueue.pop();
{'fraction_non_alphanumeric': 0.16135593220338984, 'fraction_numerical': 0.010169491525423728, 'mean_word_length': 2.600366076876144, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '1664509', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2080, 'n_words': 432}