index.js
/**
     * Use DFS (Depth First Search) algorithm to detect and find out nodes of cycle
     */
Use DFS (Depth First Search) algorithm to detect and find out nodes of cycle
Use DFS ( Depth First Search ) algorithm to detect and find out nodes of cycle
function findCycle() {
        let visited = new Array(CIRCLE_COUNT * CIRCLE_COUNT).fill(false);
        let cycleCompleted = false;
        let cycle = function(id, parentId) {
            visited[id] = true;
            for (let i = 0; i < circles[id].connected.length; i++) {
                if(!visited[circles[id].connected[i]]) {
                    let cycleArray = cycle(circles[id].connected[i], id);
                    if(cycleArray.length > 0) {
                        if(cycleArray[0] == circles[id].connected[i]){
                            cycleCompleted = true;
                        }
                        if(cycleCompleted) {
                            return cycleArray;
                        } else {
                            return cycleArray.concat(circles[id].connected[i]);
                        }
                    }
                } else if(circles[id].connected[i] != parentId) {
                    return [circles[id].connected[i]];
                }
            }
            return [];
        }
        for(let i = 0; i < visited.length; i++) {
            circles[i].connected.sort();
        }
        let result = [];
        for(let i = 0; i < visited.length; i++) {
            if(visited[i] === false) {
                result = cycle(i, -1);
                if(result.length > 0){
                    break;
                }
            }
        }
        if(result.length > 0) {
            let linesOfCycle = [];
            for(let v = 1; v <= result.length; v++) {
                let a = result[v - 1];
                let b = result[v % result.length];
                eraseLine(a,b);
                if(a < b) {
                    linesOfCycle.push(a + "-" + b);
                } else {
                    linesOfCycle.push(b + "-" + a);
                }
            }
            let value = 0;
            let color = false;
            for (let i = 0; i < CIRCLE_COUNT - 1; i++) {
                for (let j = 0; j < CIRCLE_COUNT - 1; j++) {
                    if(linesOfCycle.indexOf((i * CIRCLE_COUNT + j) + "-" + ((i + 1) * (CIRCLE_COUNT) + j)) != -1) {
                        color = !color;
                    }
                    if(color) { value += squares[i * (CIRCLE_COUNT-1) + j].set(player); }
                }
                color = false;
            }
            score[player-1] += value;
            updateSquaresLinesWeight();
            console.log(score[0] + " : " + score[1]);
        }
    }
function findCycle ( ) { let visited = new Array ( CIRCLE_COUNT * CIRCLE_COUNT ) . fill ( false ) ; let cycleCompleted = false ; let cycle = function ( id , parentId ) { visited [ id ] = true ; for ( let i = 0 ; i < circles [ id ] . connected . length ; i ++ ) { if ( ! visited [ circles [ id ] . connected [ i ] ] ) { let cycleArray = cycle ( circles [ id ] . connected [ i ] , id ) ; if ( cycleArray . length > 0 ) { if ( cycleArray [ 0 ] == circles [ id ] . connected [ i ] ) { cycleCompleted = true ; } if ( cycleCompleted ) { return cycleArray ; } else { return cycleArray . concat ( circles [ id ] . connected [ i ] ) ; } } } else if ( circles [ id ] . connected [ i ] != parentId ) { return [ circles [ id ] . connected [ i ] ] ; } } return [ ] ; } for ( let i = 0 ; i < visited . length ; i ++ ) { circles [ i ] . connected . sort ( ) ; } let result = [ ] ; for ( let i = 0 ; i < visited . length ; i ++ ) { if ( visited [ i ] === false ) { result = cycle ( i , - 1 ) ; if ( result . length > 0 ) { break ; } } } if ( result . length > 0 ) { let linesOfCycle = [ ] ; for ( let v = 1 ; v <= result . length ; v ++ ) { let a = result [ v - 1 ] ; let b = result [ v % result . length ] ; eraseLine ( a , b ) ; if ( a < b ) { linesOfCycle . push ( a + "-" + b ) ; } else { linesOfCycle . push ( b + "-" + a ) ; } } let value = 0 ; let color = false ; for ( let i = 0 ; i < CIRCLE_COUNT - 1 ; i ++ ) { for ( let j = 0 ; j < CIRCLE_COUNT - 1 ; j ++ ) { if ( linesOfCycle . indexOf ( ( i * CIRCLE_COUNT + j ) + "-" + ( ( i + 1 ) * ( CIRCLE_COUNT ) + j ) ) != - 1 ) { color = ! color ; } if ( color ) { value += squares [ i * ( CIRCLE_COUNT - 1 ) + j ] . set ( player ) ; } } color = false ; } score [ player - 1 ] += value ; updateSquaresLinesWeight ( ) ; console . log ( score [ 0 ] + " : " + score [ 1 ] ) ; } }
Use DFS (Depth First Search) algorithm to detect and find out nodes of cycle
Use DFS ( Depth First Search ) algorithm to detect and find out nodes of cycle
// Array of all nodes, initialize all as non visited // Set actual circle as visited // For all connected circles // If connected circle has not been visited yet // Recurrent call of cycle check function // Check if cycle is already in array // If cycle is already in array and remaining circles are just tail of cycle // If actual node and another visited are going to create cycle and that another is not parent // If it is parent, we are talking about cycle with two members // If there are no connection from this circle // Sort all array of connected circles // Check all circles for cycles // If cycle is found // Compute from and to circle // Remove line of cycle // Insert pseudo id of line into cycle lines array // To find out squares of cycle use coloring from one side to other side (Scan Line Algorithm) // 1. If there is line, start coloring of squares // 2. If there is line, stop coloring of squares // Repeat from 1. until end of row //showLinesWeight();