<commit_before>import drawHelper from './drawHelper';
import players from './playerConfig';
import computerAI from './computerAI';
import {showModal} from './modal';

export default class ChessBoard {
  constructor(n, mode) {
    this.num = n;
    this.gridWidth = 35;
    this.step = 0;
    this.canvas = document.getElementById('chessboard-canvas');
    this.regretButton = document.getElementById('regret');
    this.cancelRegretButton = document.getElementById('cancel-regret');
    this.canvas.width = this.gridWidth * this.num;
    this.canvas.height = this.gridWidth * this.num;
    this.brush = new drawHelper(this.canvas);
    this.mode = mode;
    this.history = [];
    if (this.isSingleMode()) {
      this.computerAI = new computerAI(n);
    }
    this.isLx = this.isLx.bind(this);
    this.isLy = this.isLy.bind(this);
    this.isX = this.isX.bind(this);
    this.isY = this.isY.bind(this);
    this.calculatePiecePosition = this.calculatePiecePosition.bind(this);
    this.regret = this.regret.bind(this);
    this.cancelRegret = this.cancelRegret.bind(this);
    this.initBoard();
  }

  isSingleMode() {
    return this.mode === 'single';
  }

  initBoard() {
    this.board = [];
    for (let x = 0; x < this.num; x++) {
      this.board[x] = [];
      for (let y = 0; y < this.num; y++) {
        this.board[x][y] = 0
      }
    }
    this.brush.drawBoard(this.num, this.gridWidth);
    this.bindEvents();
  }

  bindEvents() {
    this.canvas.addEventListener('click', this.calculatePiecePosition);
    this.regretButton.addEventListener('click', this.regret);
    this.cancelRegretButton.addEventListener('click', this.cancelRegret);
  }

  regret() {
    if (this.isSingleMode()) {
      this.resetPiece();
    }
    this.resetPiece();
  }

  cancelRegret() {
    let pieceComputer = this.history[this.step + 1];
    let piecePlayer = this.history[this.step + 2];
    if (this.isSingleMode()) {
      this.setPiece(pieceComputer.x, pieceComputer.y);
      this.setPiece(piecePlayer.x, piecePlayer.y);
    } else {
      this.setPiece(pieceComputer.x, pieceComputer.y);
    }
    this.cancelRegretButton.disabled = true;
  }

  resetPiece() {
    let piece = this.history[this.step];
    this.brush.clearPiece(piece.x, piece.y, this.gridWidth);
    this.board[piece.x][piece.y] = 0;
    this.step--;
    if (this.step === 0) this.regretButton.disabled = true;
    this.cancelRegretButton.disabled = false;
  }

  isPieceInBoard(x, y) {
    return (x > 0 && x < this.gridWidth * this.num - this.gridWidth / 2 ||<commit_msg>Cancel reset<commit_after>import drawHelper from './drawHelper';
import players from './playerConfig';
import computerAI from './computerAI';
import {showModal} from './modal';

export default class ChessBoard {
  constructor(n, mode) {
    this.num = n;
    this.gridWidth = 35;
    this.step = 0;
    this.canvas = document.getElementById('chessboard-canvas');
    this.regretButton = document.getElementById('regret');
    this.cancelRegretButton = document.getElementById('cancel-regret');
    this.canvas.width = this.gridWidth * this.num;
    this.canvas.height = this.gridWidth * this.num;
    this.resetStep = 0;
    this.brush = new drawHelper(this.canvas);
    this.mode = mode;
    this.history = [];
    if (this.isSingleMode()) {
      this.computerAI = new computerAI(n);
    }
    this.isLx = this.isLx.bind(this);
    this.isLy = this.isLy.bind(this);
    this.isX = this.isX.bind(this);
    this.isY = this.isY.bind(this);
    this.calculatePiecePosition = this.calculatePiecePosition.bind(this);
    this.regret = this.regret.bind(this);
    this.cancelRegret = this.cancelRegret.bind(this);
    this.initBoard();
  }

  isSingleMode() {
    return this.mode === 'single';
  }

  initBoard() {
    this.board = [];
    for (let x = 0; x < this.num; x++) {
      this.board[x] = [];
      for (let y = 0; y < this.num; y++) {
        this.board[x][y] = 0
      }
    }
    this.brush.drawBoard(this.num, this.gridWidth);
    this.bindEvents();
  }

  bindEvents() {
    this.canvas.addEventListener('click', this.calculatePiecePosition);
    this.regretButton.addEventListener('click', this.regret);
    this.cancelRegretButton.addEventListener('click', this.cancelRegret);
  }

  regret() {
    if (this.isSingleMode()) {
      this.resetPiece();
    }
    this.resetPiece();
  }

  cancelRegret() {
    let pieceComputer = this.history[this.step + 1];
    let piecePlayer = this.history[this.step + 2];
    if (this.isSingleMode()) {
      this.setPiece(pieceComputer.x, pieceComputer.y);
      this.setPiece(piecePlayer.x, piecePlayer.y);
      this.resetStep --;
    } else {
      this.setPiece(pieceComputer.x, pieceComputer.y);
    }
    this.resetStep--;
    if (this.resetStep == 0) this.cancelRegretButton.disabled = true;
  }

  resetPiece() {
    let piece = this.history[this.step];
    this.brush.clearPiece(piece.x, piece.y, this.gridWidth);
    this.board[piece.x][piece.y] = 0;
    this.step--;
    if (this.step === 0) this.regretButton.disabled = true;
    this.cancelRegretButton.disabled = false;
    this.resetStep++;
  }

  isPieceInBoard(x, y) {
    return (x > 0 && x < this.gridWidth * this.num - this.gridWidth / 2 ||
{'fraction_non_alphanumeric': 0.15446685878962535, 'fraction_numerical': 0.004995196926032661, 'mean_word_length': 3.054517133956386, 'pattern_counts': {'":': 0, '<': 9, '<?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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5070491', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1747, 'n_words': 311}