Javascript 打一个「;“完美课堂”;

Javascript 打一个「;“完美课堂”;,javascript,Javascript,这不是一个家庭作业问题,实际上我这样做是为了好玩 问题是: 我有一间指定“宽度”的教室(在椅子上),我必须安排所有学生,这样教室里就不会有对话 如果一个学生挨着另一个学生,就会有对话。(对角线计数) 对于本例,教室宽度为3。假设我们有一些学生:马修、加布里埃尔和拉维,他们都互相交谈,现在我们有其他学生,A、B、C、D、E、F 上述问题的解决方案是: |---------|--------|---------| | Gabriel | A | Matheus | |---------|-

这不是一个家庭作业问题,实际上我这样做是为了好玩

问题是:

我有一间指定“宽度”的教室(在椅子上),我必须安排所有学生,这样教室里就不会有对话

如果一个学生挨着另一个学生,就会有对话。(对角线计数)

对于本例,教室宽度为3。假设我们有一些学生:马修、加布里埃尔和拉维,他们都互相交谈,现在我们有其他学生,A、B、C、D、E、F

上述问题的解决方案是:

|---------|--------|---------|
| Gabriel | A      | Matheus |
|---------|--------|---------|
| B       | C      | D       |
|---------|--------|---------|
| Ravi    | E      | F       |
|---------|--------|---------|
我的实际代码是这样的,但由于某种原因它会使浏览器崩溃(无限循环):

/*
课堂
*/
多功能教室(宽度){
$width=$width | | 6;
var self=这个;
var alunos=[];
self.addClassmate=函数($classmate){
推(同学);
}
self.create教室=函数(){
var mapa=[];
变量宽度=$width;
变量高度=数学单元(单元长度/宽度);

对于(var i=0;iLOL!我简直不敢相信,我是在向函数传递字符串而不是学生

以下是正确的代码:

/*
    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());
/*
课堂
*/
多功能教室(宽度){
$width=$width | | 6;
var self=这个;
var alunos=[];
self.addClassmate=函数($classmate){
推(同学);
}
self.create教室=函数(){
var mapa=[];
变量宽度=$width;
变量高度=数学单元(单元长度/宽度);

对于(var i=0;我建议使用另一种算法?首先,将一名学生放在一个角落(一个只有三个邻居的地方,按对角线计算)。然后,将学生旁边的所有座位标记为已坐满。现在将一名学生放在另一个“角落”如果没有角落,选择一个邻居最少的地方。重复这个过程直到所有的学生都被安排好。好主意,我会在有时间的时候实现这个算法,谢谢!
/*
    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());