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

2020/10/28

Need help with Mars Rover.

MaxMiller96 MaxMiller96

2020/10/28

#
Hello, My rover is supposed to explore the entire field independently and circle the hills in the process. The area to be explored is with border hills. My problem is that when the rover hits the boundary hill when it comes back from the right side it turns left even though the code says it should turn right. The code of the rover is right below. I hope you can help me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public void act()
    {
      var fahrtrichtung = "links";
        if(!huegelVorhanden("vorne") && !grenzHuegelVorhanden("vorne"))
        {
         fahre();  
        }
        else if(huegelVorhanden("vorne"))
        {
         if(fahrtrichtung == "rechts")
         {
             drehe("links");
             fahre();
             drehe("rechts");
             fahre();
             fahre();
             drehe("rechts");
             fahre();
             drehe("links");
            }
         else if(fahrtrichtung == "links")
         {
             drehe("rechts");
             fahre();
             drehe("links");
             fahre();
             fahre();
             drehe("links");
             fahre();
             drehe("rechts");
            }
        }
        else if(grenzHuegelVorhanden("vorne") && fahrtrichtung == "rechts")
        {
         drehe("links");
         fahre();
         drehe("links");
         fahrtrichtung = "links";
        }
         
        else if(grenzHuegelVorhanden("vorne") && fahrtrichtung == "links")
        {
         drehe("rechts");
         fahre();
         drehe("rechts");
         fahrtrichtung = "rechts";
        }
MaxMiller96 MaxMiller96

2020/10/28

#
I forgot the translation: fahrtrichtung = direction of travel links = left rechts = right fahre = drive drehe = turn huegelVorhanden = Hill present grenzHuegelVorhanden = border hill present vorne = front
danpost danpost

2020/10/28

#
Change all string comparisons from: var == "string" to "string".equals(var)
MaxMiller96 MaxMiller96

2020/10/29

#
Unfortunately, your suggestion didn't bring anything. He still turns left instead of right on the left side. Still, many thanks to you.
danpost danpost

2020/10/29

#
MaxMiller96 wrote...
Unfortunately, your suggestion didn't bring anything. He still turns left instead of right on the left side. Still, many thanks to you.
You need to move line 3 up outside the act method (line 0 or -1). You have it where it is always a new var assigned with "links". It ?may? also be required that it should be assigned "rechts", instead.
You need to login to post a reply.