tags:

views:

65

answers:

1

This is NOT a homework question, actually I am doing this for fun.

Here is the problem:

I have one classroom with a specified "width" (in chairs), and I must place all student so there is no conversation in the classroom.

There is a conversation if one student is next to the other. (Diagonals count)

For this example, the classroom width is 3. Let's say that we have some students: Matheus, Gabriel and Ravi, they all talk to each other, now we have the other students, A, B, C, D, E, F.

A solution for the above problem would be:

|---------|--------|---------|
| Gabriel | A      | Matheus |
|---------|--------|---------|
| B       | C      | D       |
|---------|--------|---------|
| Ravi    | E      | F       |
|---------|--------|---------|

My actual code is this, but for some reason it crashes the browser (infinite loop):

/*
    Class Classroom
*/
function Classroom($width){
    $width=$width||6;
    var self=this;
    var alunos=[];
    self.addClassmate=function($classmate){
        alunos.push($classmate);
    }

    self.createClassroom=function(){
        var mapa=[];
        var width=$width;
        var height=Math.ceil(alunos.length/width);
        for(var i=0;i<width;++i){
            mapa[i]=[];
        }

        // Shuffle the array
        alunos.sort(function(a,b){
            return 0.5 - Math.random();
        });


        var i=0;
        var px=width;
        var py=height;


        // Fill the array
        while(px--){
            while(py--){
                if(i<alunos.length){
                    mapa[px][py]=alunos[i];
                }else{
                    mapa[px][py]=new Student('---');
                }

                ++i;
            }
            py=height;
        }

        function changePosition(px,py){
            var dx=Math.floor(Math.random()*width);
            var dy=Math.floor(Math.random()*height);
            var me=mapa[px][py];
            var other=mapa[dx][dy];
            mapa[dx][dy]=me;
            mapa[px][py]=other;
            alert('lol');
            checkChairs();
        }

        // DO IT

        function checkChairs(){
            for(var px=0;px<width;++px){
                for(var py=0;py<height;++py){
                    var me=mapa[px][py];
                    var leftCorner   = px==0;
                    var rightCorner  = px==width-1;
                    var topCorner    = py==0;
                    var bottomCorner = py==height-1;

                    if(!leftCorner){
                        if(mapa[px-1][py].hasRelationWith(me)){
                            changePosition(px,py);
                            return;
                        }
                        if(!topCorner){
                            if(mapa[px-1][py-1].hasRelationWith(me)){
                                changePosition(px,py);
                                return;
                            }
                        }
                        if(!bottomCorner){
                            if(mapa[px-1][py+1].hasRelationWith(me)){
                                return;
                            }
                        }
                    }

                    if(!rightCorner){
                        if(mapa[px+1][py].hasRelationWith(me)){
                            changePosition(px,py);
                            return;
                        }
                        if(!topCorner){
                            if(mapa[px+1][py-1].hasRelationWith(me)){
                                changePosition(px,py);
                                return;
                            }
                        }
                        if(!bottomCorner){
                            if(mapa[px+1][py+1].hasRelationWith(me)){
                                changePosition(px,py);
                                return;
                            }
                        }
                    }

                    if(!topCorner){
                        if(mapa[px][py-1].hasRelationWith(me)){
                            changePosition(px,py);
                            return;
                        }
                    }

                    if(!bottomCorner){
                        if(mapa[px][py+1].hasRelationWith(me)){
                            changePosition(px,py);
                            return;
                        }
                    }
                }
            }
        }

        checkChairs();

        return mapa;
    }
}


/*
    Class Student
*/
function Student($name){
    var self=this;
    var name=$name;
    var relation=[];

    self.addRelationWith=function($classmate,$mutual){
        $mutual=$mutual||true;
        if(self.hasRelationWith($classmate)) return;
        relation.push($classmate);
        if($mutual){
            $classmate.addRelationWith(self, false);
        }
    }

    self.hasRelationWith=function($classmate){
        var i=relation.length;
        while(i--){
            if(relation[i]==$classmate){
                return true;
            }
        }
        return false;
    }

    self.getName=function(){
        return name;
    }

    self.toString=function(){
        return '[Student '+self.getName()+']';
    }
}



var s=new Classroom(3);

var Matheus=new Student('Matheus');
var Gabriel=new Student('Gabriel');
var Ravi=new Student('Ravi');

Matheus.addRelationWith(Gabriel);
Matheus.addRelationWith(Ravi);
Gabriel.addRelationWith(Ravi);

s.addClassmate(Matheus);
s.addClassmate(Gabriel);
s.addClassmate(Ravi);
s.addClassmate('A');
s.addClassmate('B');
s.addClassmate('C');
s.addClassmate('D');
s.addClassmate('E');
s.addClassmate('F');

alert(s.createClassroom());

(The example I gave in this post is in the end of the code)

A: 

LOL! I can't believe this, I was passing strings instead of Students to the function.

Here is the correct code:

/*
    Class Classroom
*/
function Classroom($width){
    $width=$width||6;
    var self=this;
    var alunos=[];
    self.addClassmate=function($classmate){
        alunos.push($classmate);
    }

    self.createClassroom=function(){
        var mapa=[];
        var width=$width;
        var height=Math.ceil(alunos.length/width);
        for(var i=0;i<width;++i){
            mapa[i]=[];
        }

        // Shuffle the array
        alunos.sort(function(a,b){
            return 0.5 - Math.random();
        });



        // Fill the array
        var i=0;
        for(var px=0;px<width;++px){
            for(var py=0;py<height;++py){
                if(i<alunos.length){
                    mapa[px][py]=alunos[i];
                    ++i;
                }else{
                    mapa[px][py]=new Student('---');
                }
            }
            py=height;
        }
        alert(mapa);
        function changePosition(px,py){
            var dx=Math.floor(Math.random()*width);
            var dy=Math.floor(Math.random()*height);
            var me=mapa[px][py];
            var other=mapa[dx][dy];
            mapa[dx][dy]=me;
            mapa[px][py]=other;
            checkChairs();
        }

        // DO IT

        function checkChairs(){
            for(var px=0;px<width;++px){
                for(var py=0;py<height;++py){
                    var me=mapa[px][py];
                    var leftCorner   = px==0;
                    var rightCorner  = px==width-1;
                    var topCorner    = py==0;
                    var bottomCorner = py==height-1;

                    if(!leftCorner){
                        if(mapa[px-1][py].hasRelationWith(me)){
                            changePosition(px,py);
                            return;
                        }
                        if(!topCorner){
                            if(mapa[px-1][py-1].hasRelationWith(me)){
                                changePosition(px,py);
                                return;
                            }
                        }
                        if(!bottomCorner){
                            if(mapa[px-1][py+1].hasRelationWith(me)){
                                return;
                            }
                        }
                    }

                    if(!rightCorner){
                        if(mapa[px+1][py].hasRelationWith(me)){
                            changePosition(px,py);
                            return;
                        }
                        if(!topCorner){
                            if(mapa[px+1][py-1].hasRelationWith(me)){
                                changePosition(px,py);
                                return;
                            }
                        }
                        if(!bottomCorner){
                            if(mapa[px+1][py+1].hasRelationWith(me)){
                                changePosition(px,py);
                                return;
                            }
                        }
                    }

                    if(!topCorner){
                        if(mapa[px][py-1].hasRelationWith(me)){
                            changePosition(px,py);
                            return;
                        }
                    }

                    if(!bottomCorner){
                        if(mapa[px][py+1].hasRelationWith(me)){
                            changePosition(px,py);
                            return;
                        }
                    }
                }
            }
        }

        checkChairs();

        return mapa;
    }
}


/*
    Class Student
*/
function Student($name){
    var self=this;
    var name=$name;
    var relation=[];

    self.addRelationWith=function($classmate,$mutual){
        $mutual=$mutual||true;
        if(self.hasRelationWith($classmate)) return;
        relation.push($classmate);
        if($mutual){
            $classmate.addRelationWith(self, false);
        }
    }

    self.hasRelationWith=function($classmate){
        var i=relation.length;
        while(i--){
            if(relation[i]==$classmate){
                return true;
            }
        }
        return false;
    }

    self.getName=function(){
        return name;
    }

    self.toString=function(){
        return self.getName();
    }
}



var s=new Classroom(3);

var Matheus=new Student('Matheus');
var Gabriel=new Student('Gabriel');
var Ravi=new Student('Ravi');

Matheus.addRelationWith(Gabriel);
Matheus.addRelationWith(Ravi);
Gabriel.addRelationWith(Ravi);
s.addClassmate(Matheus);
s.addClassmate(Gabriel);
s.addClassmate(Ravi);
s.addClassmate(new Student('A'));
s.addClassmate(new Student('B'));
s.addClassmate(new Student('C'));
s.addClassmate(new Student('D'));
s.addClassmate(new Student('E'));
s.addClassmate(new Student('F'));

alert(s.createClassroom());

I need moar coffee...

M28