<commit_before>// import R from 'ramda';
import { IField, IFieldConfig } from './IField';
import { IPosition } from './IPosition';

// import { LogFile } from 'ptz-log-file';
// const log = LogFile({});

function getBombs(field: IField, fieldConfig: IFieldConfig): IField {
    for (let i = 0; i < fieldConfig.bombs; i++) {
        const width = Math.floor((fieldConfig.width - 1) * Math.random() + 1);
        const heigth = Math.floor((fieldConfig.heigth - 1) * Math.random() + 1);
        if (field[width][heigth] && field[width][heigth].isBomb)
            i--;
        field[width][heigth].isBomb = true;
    }
    return field;
}

function isValidConfig(fieldConfig: IFieldConfig) {
    const totalFields = fieldConfig.width * fieldConfig.heigth;
    return totalFields > fieldConfig.bombs ? true : false;
}

function countNearBombs(field: IField): IField {
    const countedField: IField = field;
    field.map((col, colIndex) => col.map((pos, index) => {
        if (pos.isBomb) {
            for (let i = -1; i < 2; i++) {
                for (let j = -1; j < 2; j++) {
                    if (countedField[pos.x + i] && countedField[pos.x + i][pos.y + j])
                        countedField[pos.x + i][pos.y + j].nearBombs++;
                }
            }
        }
    }));
    return countedField;
}

function getInitialField(fieldConfig: IFieldConfig): IField {

    if (!isValidConfig(fieldConfig)) {
        throw new Error('Invalid field configuration');
    }
    const initialField = [];
    for (let i = 0; i < fieldConfig.width; i++) {
        initialField[i] = [];
        for (let j = 0; j < fieldConfig.heigth; j++) {
            const pos: IPosition = { x: i, y: j, isBomb: false, nearBombs: 0 };
            initialField[i][j] = pos;
        }
    }
    const bombedField: IField = getBombs(initialField, fieldConfig);
    return bombedField;
}

export {
    getInitialField, countNearBombs
};<commit_msg>Get Empty Field<commit_after>// import R from 'ramda';
import { IField, IFieldConfig } from './IField';
import { IPosition } from './IPosition';

// import { LogFile } from 'ptz-log-file';
// const log = LogFile({});

function getBombs(field: IField, fieldConfig: IFieldConfig): IField {
    for (let i = 0; i < fieldConfig.bombs; i++) {
        const width = Math.floor((fieldConfig.width - 1) * Math.random() + 1);
        const heigth = Math.floor((fieldConfig.heigth - 1) * Math.random() + 1);
        if (field[width][heigth] && field[width][heigth].isBomb)
            i--;
        field[width][heigth].isBomb = true;
    }
    return field;
}

function isValidConfig(fieldConfig: IFieldConfig) {
    const totalFields = fieldConfig.width * fieldConfig.heigth;
    return totalFields > fieldConfig.bombs ? true : false;
}

function countNearBombs(field: IField): IField {
    const countedField: IField = field;
    field.map((col, colIndex) => col.map((pos, index) => {
        if (pos.isBomb) {
            for (let i = -1; i < 2; i++) {
                for (let j = -1; j < 2; j++) {
                    if (countedField[pos.x + i] && countedField[pos.x + i][pos.y + j])
                        countedField[pos.x + i][pos.y + j].nearBombs++;
                }
            }
        }
    }));
    return countedField;
}

function getEmptyField(fieldConfig: IFieldConfig) {
    const initialField = [];
    for (let i = 0; i < fieldConfig.width; i++) {
        initialField[i] = [];
        for (let j = 0; j < fieldConfig.heigth; j++) {
            const pos: IPosition = { x: i, y: j, isBomb: false, nearBombs: 0 };
            initialField[i][j] = pos;
        }
    }
    return initialField;
}

function getInitialField(fieldConfig: IFieldConfig): IField {

    if (!isValidConfig(fieldConfig)) {
        throw new Error('Invalid field configuration');
    }
    const emptyField = getEmptyField(fieldConfig);
    const bombedField: IField = getBombs(emptyField, fieldConfig);
    const countedField = countNearBombs(bombedField);
    return countedField;
}

export {
    getInitialField, countNearBombs
};
{'fraction_non_alphanumeric': 0.14602861371484954, 'fraction_numerical': 0.0059200789343857915, 'mean_word_length': 2.492678725236865, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '4172861', 'n_tokens_mistral': 1355, 'n_tokens_neox': 1320, 'n_words': 325}