1 package org.woehlke.computer.kurzweil.commons.model.turing;
2
3 import org.woehlke.computer.kurzweil.tabs.wator.WatorModel;
4 import org.woehlke.computer.kurzweil.commons.model.fractal.GaussianNumberPlane;
5
6
7
8
9
10
11
12
13
14
15
16 public class MandelbrotTuringMachine {
17
18 private volatile GaussianNumberPlane gaussianNumberPlane;
19 private volatile MandelbrotTuringPositions turingPositions;
20 private volatile MandelbrotTuringPhaseState turingPhaseState;
21
22 public MandelbrotTuringMachine(WatorModel model) {
23 this.gaussianNumberPlane = model.getGaussianNumberPlane();
24 this.turingPhaseState = new MandelbrotTuringPhaseState();
25 this.turingPositions = new MandelbrotTuringPositions(model.getWorldDimensions());
26 start();
27 }
28
29 public void start() {
30 this.turingPhaseState.start();
31 this.gaussianNumberPlane.start();
32 this.turingPositions.start();
33 }
34
35 public synchronized boolean step() {
36 boolean repaint=true;
37 switch(turingPhaseState.getTuringTuringPhase()){
38 case SEARCH_THE_SET:
39 stepGoToSet();
40 repaint=false;
41 break;
42 case WALK_AROUND_THE_SET:
43 stepWalkAround();
44 break;
45 case FILL_THE_OUTSIDE_WITH_COLOR:
46 fillTheOutsideWithColors();
47 break;
48 case FINISHED:
49 default:
50 repaint=false;
51 break;
52 }
53 return repaint;
54 }
55
56 private void stepGoToSet(){
57 if(this.gaussianNumberPlane.isInMandelbrotSet(this.turingPositions.getTuringPosition())){
58 this.turingPositions.markFirstSetPosition();
59 this.turingPhaseState.finishSearchTheSet();
60 } else {
61 this.turingPositions.goForward();
62 }
63 }
64
65 private void stepWalkAround(){
66 if(gaussianNumberPlane.isInMandelbrotSet(this.turingPositions.getTuringPosition())){
67 this.turingPositions.turnRight();
68 } else {
69 this.turingPositions.turnLeft();
70 }
71 this.turingPositions.goForward();
72 if(this.turingPositions.isFinishedWalkAround()){
73 this.turingPhaseState.finishWalkAround();
74 }
75 }
76
77 private void fillTheOutsideWithColors(){
78 this.gaussianNumberPlane.fillTheOutsideWithColors();
79 this.turingPhaseState.finishFillTheOutsideWithColors();
80 }
81 }