<commit_before>"use strict"
/*
Handle the event attatched to .board DOM element
Add clicked item to a size 2 queue and swap if move is legal
*/

import { updateBoardTemplate } from "script/view/boardView"
import { prepareBoard } from "script/controller/boardController"

let tappedQueue = []
let swapEventHandler = function (e) {
    if (e.target !== e.currentTarget) {
        let tappedItem = e.target
        if (tappedItem.classList.contains("tile")) {
            addToQueue(tappedItem, tappedQueue)
        } else {
            //better way to handle if click is on board but not tile
            throw new Error("Invalid click on the board")
        }
        
    }
    e.stopPropagation()
    if (isLegalMove(tappedQueue)) {
        let pointA = [tappedQueue[0].dataset.row, tappedQueue[0].dataset.col]
        let pointB = [tappedQueue[1].dataset.row, tappedQueue[1].dataset.col]
        this.swap(pointA, pointB)
        updateBoardTemplate(this)
        prepareBoard(this)
        tappedQueue = []
    }
}

let addToQueue = function (item, queue) {
  if (queue.length === 2) queue.pop()
  if (queue[0] !== item) {
    queue.push(item)
  }
}

let isLegalMove = function (queue) {
    if (queue.length < 2) return false
    let rowA = queue[0].dataset.row
    let colA = queue[0].dataset.col
    let rowB = queue[1].dataset.row
    let colB = queue[1].dataset.col
    if (Math.abs(rowA - rowB) + Math.abs(colA - colB) == 1) {
        return true
    } else {
        return false
    }
}

export { swapEventHandler }<commit_msg>Empty tap queue filled by illegal pair<commit_after>"use strict"
/*
Handle the event attatched to .board DOM element
Add clicked item to a size 2 queue and swap if move is legal
*/

import { updateBoardTemplate } from "script/view/boardView"
import { prepareBoard } from "script/controller/boardController"

let tappedQueue = []
let swapEventHandler = function (e) {
    if (e.target !== e.currentTarget) {
        let tappedItem = e.target
        if (tappedItem.classList.contains("tile")) {
            addToQueue(tappedItem, tappedQueue)
        } else {
            //better way to handle if click is on board but not tile
            throw new Error("Invalid click on the board")
        }
        
    }
    e.stopPropagation()
    if (isLegalMove(tappedQueue)) {
        let pointA = [tappedQueue[0].dataset.row, tappedQueue[0].dataset.col]
        let pointB = [tappedQueue[1].dataset.row, tappedQueue[1].dataset.col]
        this.swap(pointA, pointB)
        updateBoardTemplate(this)
        prepareBoard(this)
        tappedQueue = []
    }
    if (tappedQueue.length === 2) tappedQueue = []
}

let addToQueue = function (item, queue) {
  if (queue.length === 2) queue.pop()
  if (queue[0] !== item) {
    queue.push(item)
  }
}

let isLegalMove = function (queue) {
    if (queue.length < 2) return false
    let rowA = queue[0].dataset.row
    let colA = queue[0].dataset.col
    let rowB = queue[1].dataset.row
    let colB = queue[1].dataset.col
    if (Math.abs(rowA - rowB) + Math.abs(colA - colB) == 1) {
        return true
    } else {
        return false
    }
}

export { swapEventHandler }
{'fraction_non_alphanumeric': 0.11770304568527919, 'fraction_numerical': 0.008565989847715736, 'mean_word_length': 2.6034285714285716, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4471626', 'n_tokens_mistral': 1001, 'n_tokens_neox': 982, 'n_words': 272}