In this exercise, you will turn your buggle into a Langton's ant. These artificial little animals are very interesting because they are given simple rules that depend only on their local environment, and after a period of apparent chaotic behavior, a general pattern emerges.
The rules are absolutely trivial: to compute what the next step should be,
you should check the current color of the ground (using
getGroundColor()
). If it's white, change it to black, turn
right and move forward by one cell. If the ground is currently black, change
it to white, turn left and move forward by one cell.
It's hard to come up with simpler rules isn't it? Well, let's go and code it
now. You have to complete the step()
method, which encodes the
behavior of the ant at each step. You will probably use the
getGroundColor()
method to retrieve the color of the cell on
which the ant is currently. Colors of interest are simply named
Color.black
and Color.white
.
To compare colors, you cannot use the equal signs (==), because these things are not scalar values but objects. Instead, you need to write something like the following:
Color c /* = some initialization */; if (c.equals(Color.black)) { /* that's equal */ } else { /* that was not equal */ }[/!]
mudar a cor do chão não é difícil, mas meio demorado: você tem que
mudar a cor do pincel de seu buggle, baixar o pincel (para marcar a
célula atual -- com brushDown()
), e levantar o pincel de
novo (com levantarPincel()
) para evitar problemas quando
o buggle se mexer. Você é, naturalmente, livre para organizar seu
código da forma que quiser, mas você deve querer escrever um método
setCorDoChão(color)
para separar um pouco as coisas.
As you can see from the execution of this exercise, the interest in this algorithm is that after about 10000 steps of relative chaotic behavior, the ant start building a regular pattern. This emergence of a regular pattern from the chaos is rather fascinating, isn't it? Move on to the next exercise to see more of them.