Cover
 This is the archive Discourse for the Processing (ALPHA) software. Please visit the new Processing forum for current information.
 Processing 1.0 _ALPHA_    Programming Questions & Help    Programs (Moderators: fry, REAS)    A 4-state 2-dimensional Turing machine « Previous topic | Next topic »

 Pages: 1
 Author Topic: A 4-state 2-dimensional Turing machine  (Read 476 times)
tlz
Guest

 A 4-state 2-dimensional Turing machine « on: Jul 16th, 2004, 4:22pm »

Hello Im a beginner in programming and tried to do the ant simulation: http://mathworld.wolfram.com/LangtonsAnt.html

not that hard, right? ....but for me...phew

here are my questions:
1. wrong output. dont look like it should be. logical program error?
2. the ant starting always at 0,0 not at random position.
3. i tried to put the colordefinition outside the loop.  wont work. why?
4. code simplyfinging?
5. when everything is done, I like to do a OOP version of that.

Quote:
 //  A 4-state 2-dimensional Turing machine invented in the 1980s. The ant starts out on a grid containing black and white cells, and then follows the following set of rules. // //   1. If the ant is on a black square, it turns right 90° and moves forward one unit. //   2. If the ant is on a white square, it turns left 90° and moves forward one unit. //   3. When the ant leaves a square, it inverts the color. // //   http://mathworld.wolfram.com/LangtonsAnt.html     int xpos, ypos, xOld, yOld;    // Starting position of shape   int x=int(random(width)); int y=int(random(height)); int direction=0;   void setup() {   color rot = color(255, 0, 0);   color blau = color(0, 0, 255);   color weiss = color(255, 255, 255);   color schwarz = color(0, 0, 0);   size(100, 100);   background(255);   /*for (int i=0; i

cheers
thomas
flight404

 Re: A 4-state 2-dimensional Turing machine « Reply #1 on: Jul 16th, 2004, 8:28pm »

1. I think your order of operations was slightly inverted and that is why the behaviour seemed weird.
2. you assigned the random placement based on the width and height specified in size() before size was actually defined so it always got 0,0.
3. I think it might be a good coding habit to create the color definition first, then assign it within the setup method (see code below).  I think what you were doing was scoping the color to the setup method and therefore, nobody else could see it.
4. There are certainly places to simplify.  But that should probably correspond with your #5.

Code:
 int x, y, xOld, yOld;    // Starting position of shape int direction = 0; color rot, blau, weiss, schwarz;   void setup(){   size(100, 100);     rot = color(255, 0, 0);   blau = color(0, 0, 255);   weiss = color(255, 255, 255);   schwarz = color(0, 0, 0);     // now that size has been defined, you can // go a head and reference the width and height.   x = int(random(width));   y = int(random(height));     background(255); }   void loop(){   xOld = x;   yOld = y;   // direction = direction - 90 can be written as // direction -= 90.   if (get(x,y) == weiss){     direction -= 90;   } else {     direction += 90;   }       if (direction==360) {direction=0;}   if (direction==-90) {direction=270;}   // you can use the increment or decrement // to add or subtract 1 from a variable (just ints i think)   if (direction==0) { y--;}   if (direction==90) { x++;}   if (direction==180) { y++;}   if (direction==270) { x--;}     if (get(xOld,yOld) == weiss) {     set(xOld,yOld,schwarz);   } else {     set(xOld,yOld,weiss);   } }
 Pages: 1

 Forum Jump: ----------------------------- Discussion -----------------------------  - Community, Collaboration, Status   - Events, Publications, Propaganda   - General Processing Discussion ----------------------------- Programming Questions & Help -----------------------------  - Syntax => Programs   - Integration ----------------------------- Topics & Contributions -----------------------------  - Tools   - Responsive Form, Games   - Information Visualization   - Simulation, Artificial Life   - Tangible Computing   - Automated Systems   - Sound   - Video, Camera   - Beyond Categories ----------------------------- Suggestions -----------------------------  - Software Suggestions   - Website, Reference, Example Suggestions ----------------------------- Bugs -----------------------------  - Software Bugs   - Website, Reference, Example Bugs   - Bug Fixes, Implemented Suggestions ----------------------------- Teaching -----------------------------  - Course Blueprints   - Theory and Practice « Previous topic | Next topic »