This site requires JavaScript, please enable it in your browser!
Greenfoot back
Jemy2
Jemy2 wrote ...

2021/5/4

how to reset my timer

1
2
Jemy2 Jemy2

2021/5/4

#
  public Server()
    {
        if (baseImage == null) baseImage = getImage();
        timer = 150+Greenfoot.getRandomNumber(151);

    }

    /**
     * Act - do whatever the Server wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        move();
        Candidate();
        if(isCandidate){
            Candidate_RequestVote();
        }
        if(IsGrantedVote){
            grantVote();
        }
            if(isCandidate){
                leader();
            } 
    }

    public void Candidate(){
        if (done) return;
        if (lowServer != null) 
        {
            setImage("Follow.png");
            done = true;
            return;
        }
        if (--timer == 0)
        {
            lowServer = this;
            setImage("Candidate.png");
            done = true;
            isCandidate=true;
            voteSelf=1;
            return;
        }
        GreenfootImage numImg = new GreenfootImage(""+timer, 24, Color.BLACK, new Color(0, 0, 0, 0));
        GreenfootImage img = new GreenfootImage(baseImage);
        int x = (img.getWidth()-numImg.getWidth())/2;
        int y = (img.getHeight()-numImg.getHeight())/2;
        img.drawImage(numImg, x, y);
        setImage(img);

    }

    public void move()
    {
        move(3);
        if(isAtEdge()){
            turn(87);
        }
        if(Greenfoot.getRandomNumber(100)<20){
            turn(Greenfoot.getRandomNumber(61)-30);
        }
    }

    /**
     * candidate request vote from others by intersecting other servers
     */
    public void RequestVote()
    {
        if(!IsGrantedVote){
            Request_Rate= REQUEST_TIME;
            setImage("RequestVote.png");
            IsGrantedVote=true;
        }

    }

    public void Candidate_RequestVote(){
        Server request = (Server)getOneIntersectingObject(Server.class);
        if(request!= null){
            request.RequestVote();
        }
    }

    private boolean Received_Request()
    {
        return Request_Rate > 0;
    }

    private void grantVote()
    {
        if(Received_Request())
        {
            Request_Rate--;
            if(Request_Rate == 0)
            {
                totalVoteReceived = voteSelf + grant_voteCount++;
                setImage("GrantedVote.png");
                IsGrantedVote=true;
            }
           
        }
    }

    private void leader()
    {
           
             if(totalVoteReceived >= 21 && lowServer!=null){
             lowServer=this;
             setImage("Leader.png");
            }
        
    }
when i run the code, a server becomes a candidate and thereafter becomes a leader but when i run it again, it continues from the leadership state instead of candidate state. how do i reset the timer in the constructor so that each time i run the code it will start by changing to candidate
Jemy2 Jemy2

2021/5/4

#
I think the issue is that how do we retain line 38 after calling line 109 in leader method in subsequent code runs? Please i need help on this.
Jemy Jemy

2021/5/6

#
Any clue on the above will be appreciated
danpost danpost

2021/5/6

#
Jemy2 wrote...
I think the issue is that how do we retain line 38 after calling line 109 in leader method in subsequent code runs? Please i need help on this.
If I understand correctly, the only difference between runs is which server might be leader. How are you going from one run to the next? Show transition codes. Or, what controls are you using between runs?
Jemy Jemy

2021/5/6

#
danpost wrote...
Jemy2 wrote...
I think the issue is that how do we retain line 38 after calling line 109 in leader method in subsequent code runs? Please i need help on this.
If I understand correctly, the only difference between runs is which server might be leader. How are you going from one run to the next? Show transition codes. Or, what controls are you using between runs?
after running the code, everything works fine but when i try to reset the simulation and run the code again instead of changing to candidate first before becoming the leader, it changes to leader. so invariably, it retains the leader image of the previous run
Jemy Jemy

2021/5/6

#
what i want is that each time i run the code, it moves from follower to candidate and then to leader after meeting up with the conditions that is first run : follower candidate leader second run should also be: follower candidate and leader but it is giving me: follower leader on the second run
Jemy Jemy

2021/5/6

#
danpost wrote...
Jemy2 wrote...
I think the issue is that how do we retain line 38 after calling line 109 in leader method in subsequent code runs? Please i need help on this.
If I understand correctly, the only difference between runs is which server might be leader. How are you going from one run to the next? Show transition codes. Or, what controls are you using between runs?
I am just using the Run and Reset buttons on the simulation platform. I was just thinking that resetting and running the codes again should give me a different result with same ORDER-FOLLOWER, CANDIDATE, and LEADER
danpost danpost

2021/5/6

#
Jemy wrote...
I am just using the Run and Reset buttons on the simulation platform.
Are you not, then, creating all new servers? What all static content are you using?
Jemy Jemy

2021/5/6

#
danpost wrote...
Jemy wrote...
I am just using the Run and Reset buttons on the simulation platform.
Are you not, then, creating all new servers? What all static content are you using?
creating new servers is the idea so that the new servers follows the program logic but i found out that when it gets to changing to candidate image it retains the leader image of the previous run. The following are my initializations in the server class:
public static Server lowServer;
    public static GreenfootImage baseImage;
    private int timer;
    private boolean done;
    private boolean isCandidate;
    private boolean IsGrantedVote;
    private static final int REQUEST_TIME= 200;
    private static int grant_voteCount=0;
    private static int voteSelf=0;
    private static int totalVoteReceived = 0;
    private int Request_Rate = 0;
    private boolean Isleader; 
    MyWorld world;
    int qourum = (world.NUMBER_OF_SERVER)-10;
    
the following are myworld class:
public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
         super(1000, 600, 1); 
         serverPopulation();
         Server.lowServer = null;
        
    }
    
    public void serverPopulation()
    {      
       for(int i=0; i< NUMBER_OF_SERVER; i++)
       {
        int x= Greenfoot.getRandomNumber(getWidth());
        int y= Greenfoot.getRandomNumber(getHeight());
        //int server= Greenfoot.getRandomNumber(151)+150;
        addObject(new Server(), x, y);
       }
       
    }
Super_Hippo Super_Hippo

2021/5/6

#
Static variables are only created once when the project is compiled. They don’t reset when you reset the scenario. You either need to remove the static keyword or make sure to reset every static variable from the constructor of world in which the project starts.
Jemy Jemy

2021/5/6

#
Super_Hippo wrote...
Static variables are only created once when the project is compiled. They don’t reset when you reset the scenario. You either need to remove the static keyword or make sure to reset every static variable from the constructor of world in which the project starts.
Thank you Super_Hippo, This is my constructor of the world class;
 public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
         super(1000, 600, 1); 
         serverPopulation();
         Server.lowServer = null;
        
    }
line 6 is used to reset the Server but i dont know why it does not work when i run it the second time
danpost danpost

2021/5/6

#
Your MyWorld constructor will have to reset some of your static fields -- particularly, lines 8 thru 10 of Server fields given above.
Jemy Jemy

2021/5/6

#
danpost wrote...
Your MyWorld constructor will have to reset some of your static fields -- particularly, lines 8 thru 10 of Server fields given above.
how do we reset lines 1 and 2 of the server fields?
danpost danpost

2021/5/7

#
Jemy wrote...
how do we reset lines 1 and 2 of the server fields?
Line 1 is already being reset (line 6 of constructor). Line 2 should not need reset.
Jemy Jemy

2021/5/7

#
danpost wrote...
Jemy wrote...
how do we reset lines 1 and 2 of the server fields?
Line 1 is already being reset (line 6 of constructor). Line 2 should not need reset.
Then why does it change to leader instead of candidate in subsequent runs? is there a way to changing the candidate to leader without using
lowServer.setImage("Leader.png");
I think the code above retains the image in the subsequent runs; if there is a way to change candidate to leader without using it might help
There are more replies on the next page.
1
2