Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 927275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:49:25+00:00 2026-05-15T19:49:25+00:00

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

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:49:26+00:00Added an answer on May 15, 2026 at 7:49 pm

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to solve this problem, its not a homework question, its just code
This is not a homework problem. This questions was asked to one of my
I'm newbie in Python, but this question is not a homework (actually this code
this one is not homework, it just the fact that i've been out of
well I'm doing this homework but I'm still not viewing the fault... When I
This is a question for a homework problem that I cant figure out: Question
This IS NOT a Homework question! While building my current student database project I
Firstly this is not a homework question. I am practicing my knowledge on java.
This isn't actually a homework question per se, just a question that keeps nagging
This is not a homework question, just something at work that's bugging me. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.