<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,11 +1,27 @@
-/*imperative program*/enum Search
+/*imperative program*/void showError(String error)
 {
-	SUCCESS, BLOCKED, NOTFOUND
+	schreib(error);
 }
 
-enum Field
+void goBack(int fields)
 {
-	EMPTY, CORN, BLOCKED
+	turnAround();
+	
+	for(;fields &gt; 0; fields--)
+	{
+		try
+		{
+			forward();
+		}
+		catch(Exception exception)
+		{
+			//cannot do more than trying to get back
+			return;
+		}
+	}
+	
+	//make sure we look in the initial direction
+	turnAround();
 }
 
 void turnRight()
@@ -42,7 +58,7 @@ void turnAround()
 
 boolean canPutCorn()
 {
-	return !maulLeer();
+	return !cornPresent() &amp;&amp; !maulLeer();
 }
 
 boolean putCorn()
@@ -80,6 +96,21 @@ int takeCorns()
 	return takeCount;
 }
 
+int takeCorns(int cornCount)
+{
+	if(cornCount &lt;= 0)
+		return 0;
+
+	int takeCount = 0;
+
+	for(; cornCount != 0 &amp;&amp; takeCorn(); takeCount++, cornCount--)
+	{
+		
+	}
+	
+	return takeCount;
+}
+
 int countCorns()
 {
 	int takeCount = takeCorns();
@@ -95,37 +126,49 @@ int countCorns()
 
 void main()
 {
-	Estimation estimation = enterArea();
-	int fieldCount = estimation.calculateSquare(this);
-	
-	getCorns(fieldCount);
+	try
+	{
+		enterArea();
+		int fieldCount = calculateRectangle();
+
+		getCorns(fieldCount);
 	
-	fillArea();
+		fillArea();
+	}
+	catch(Exception exception)
+	{
+		showError(exception.getMessage());
+	}
 }
 
-void putCornOnField()
+int putCornOnFields()
 {
-	while(forward())
-	{
-		if(hasCorn())
-			continue;
-		
+	int fieldCount = 0;
+
+	do
+	{	
 		putCorn();
+		fieldCount++;
 	}
+	while(forward());
+	
+	return fieldCount;
 }
 
-void putCornOnField(int fieldCount)
+void putCornOnFields(int fieldCount)
 {
-	if(fieldCount &lt; 0)
+	if(fieldCount &lt;= 0)
 		return;
-		
+
+	putCorn();
+
 	for(int i = 0; i != fieldCount; i++)
-	{
-		if(hasCorn())
-			continue;
-		
+	{	
 		putCorn();
+		forward();
 	}
+	
+	putCorn();
 }
 
 void fillArea()
@@ -133,7 +176,9 @@ void fillArea()
 	putCorn();
 	turnLeft();
 
+	//go forward to corner
 	int steps = 0;
+	
 	if(forward())
 	{
 		putCorn();
@@ -149,44 +194,52 @@ void fillArea()
 		
 		turnRight();
 	}
-
-	size1 = putCornOnFields();
+	
+	//we are in the corner looking at the opposite direction
+	int size1 = putCornOnFields();
+	int size2 = 0;
 		
 	turnRight();
-	
-	if(!forward())
-		break;
 
-	turnRight();
-	
+	if(forward())
+		size2++;
+
 	while(true)
 	{
-		putCornOnField(size1 - 1);
+		turnRight();
+		putCornOnFields(size1 - 2);
 		
 		turnLeft();
 
-		if(forward)
+		if(forward())
 			size2++;
 		else
 			break;
 
 		turnLeft();
 		
-		putCornOnField(size1 - 1);
+		putCornOnFields(size1 - 2);
 		
 		turnRight();
-		
-		if(forward)
+
+		if(forward())
 			size2++;
 		else
 			break;
-		
-		turnRight();
 	}
 	
 	turnRight();
+	goForward(size1 - 1);
+	turnRight();
 	
-	putCornOnField(size2 - steps);
+	putCornOnFields(size2 - steps);
+}
+
+void goForward(int fieldCount)
+{
+	for(int i = 0; i != fieldCount &amp;&amp; forward(); i++)
+	{
+	}
 }
 
 void getCorns(int cornCount)
@@ -202,194 +255,120 @@ void getCorns(int cornCount)
 	forward();
 }
 
-Field takeCornsForward()
+//goes forward and checks field to be free
+//returns to starting point if field is not
+boolean searchFreeFieldForward()
 {
-	if(!forward())
-		return Field.BLOCKED;
+	if(forward())
+	{		
+		if(cornPresent())
+		{
+			turnAround();
+			forward();
+			forward();
+		}
+
+		//we should now stand in the area
+		return true;
+	}
 
-	if(cornPresent())
-		return Field.CORN; //found corn stash
+	return false;
+}
+
+boolean turnAndSearchFreeFieldForward()
+{	
+	turnLeft();
 	
-	return EMPTY;
+	return searchFreeFieldForward();
 }
 
-class Estimation
+public int calculateSquare()
 {
-	public Estimation()
-	{
-	}
+	int sideSize = measureSize();
+	return sideSize * sideSize;
+}
 
-	//save field data
-	//  F0
-	//F1  F3
-	//  F2
-	
-	Field fields[] = {BLOCKED, BLOCKED, BLOCKED, BLOCKED};
-	
-	int currentFieldIndex = 0;
-	
-	boolean done = false;
+public int calculateRectangle()
+{
+	turnLeft();
 	
-//	int turns = 0;
+	int fieldCount = 1;
 	
-	//given the position of a corn we can estimate
-	//our surrounding
-	void estimateByCorn(int index)
-	{
-		estimateByUnique(index, false);
-	}
-	
-	void estimateByFree(int index)
-	{
-		estimateByUnique(index, true);
-	}
+	int size1 = 0;
+	int size2 = 0;
+
+	int steps = 0;
 	
-	//the current field is unique and surrounding
-	//can be predicted
-	void estimateByUnique(int index, boolean isFree)
+	if(forward())
 	{
-		//calculate index of opposite field
-		int otherIndex = index - 2;
+
+		steps++;
 		
-		if(otherIndex &lt; 0)
-			otherIndex = index + 2;
-				
-		//other fields have to be blocked
-		for(int i = 0; i != 4; i++)
+		//go to wall
+		while(forward())
 		{
-			if(i == index)
-				fields[i] = isFree ? Field.EMPTY : Field.CORN;
-			else if(i == otherIndex)
-				fields[i] = isFree ? Field.CORN : Field.EMPTY;
-			else
-				fields[i] = Field.BLOCKED;
+			steps++;
 		}
 		
-		done = true;
-	}
-	
-	public boolean canFindArea()
-	{
-		return done;
-	}
-	
-	//goes forward and checks field to be free
-	//returns to starting point if field is not
-	public boolean searchFreeFieldForward(solution hamster)
-	{
-		if(done)
-			return true;
-		
-		if(forward())
-		{		
-			Field field = Field.EMPTY;
-		
-			if(hamster.cornPresent())
-			{
-				estimateByCorn(currentFieldIndex);
-				
-				hamster.goBack(1);
-			}
-			else
-				estimateByFree(currentFieldIndex);
-
-			return true;
-		}
-		else
-			return false;
-	}
-	
-	public boolean turnAndSearchFreeFieldForward(solution hamster)
-	{
-		if(done)
-			return true;
-			
-//		turns++;
-//		turnLeft();
+		turnRight();
+		size1 = measureSize();
+		turnRight();
+		size2 = measureSizeAndStay(steps);
 		
-		return searchFreeFieldForward(hamster);
 	}
-	
-	public int calculateSquare(solution hamster)
+	else
 	{
-		return measureSize(hamster) ^ 2;
+		turnRight();
+		size1 = measureSize();
+		turnRight();
+		size2 = measureSize();
 	}
-	
-	public int calculateRectangle(solution hamster)
-	{
-		turnLeft();
-		
-		int fieldCount = 1;
-		
-		int size1 = 0;
-		int size2 = 0;
 
-		if(forward())
-		{
-			int steps = 0;
-			steps++;
-			
-			//go to wall
-			while(forward())
-			{
-				steps++;
-			}
-			
-			turnRight();
-			size1 = measureSize(hamster);
-			turnRight();
-			size2 = measureSizeAndStay(hamster, steps);
-			
-		}
-		else
-		{
-			turnRight();
-			size1 = measureSize(hamster);
-			turnRight();
-			size2 = measureSize(hamster);
-		}
+	turnLeft();
+	//we should have the initial position
+	//and looking direction
+	return size1 * size2;			
+}
 
-		turnLeft();
-		//we should have the initial position
-		//and looking direction
-		return size1 * size2;			
-	}
+//goes to opposite wall, collects the went fields
+//and returns to the starting point
+int measureSize()
+{
+	return measureSizeAndStay(0);
+}
 
-	//goes to opposite wall, collects the went fields
-	//and returns to the starting point
-	int measureSize(solution hamster)
-	{
-		return meastureSizeAndStay(hamster, 0);
-	}
+//measures the size to opposite wall
+//but does not full return - stops
+//some fields
+int measureSizeAndStay(int fieldCount)
+{
+	int steps = 0;
 	
-	//measures the size to opposite wall
-	//but does not full return - stops
-	//some fields
-	int meastureSizeAndStay(solution hamster, int fieldCount)
+	for(;forward(); steps++)
 	{
-		int steps = 0;
-		
-		for(;hamster.forward(); steps++)
-		{
-		}
-		
-		goBack(steps - fieldCount);
-		return steps;
 	}
 	
+	goBack(steps - fieldCount);
+	return steps + 1;
 }
 
 //searches for the area entry and steps forward
 //inside the area
-Estimation enterArea() throws Exception
+void enterArea() throws Exception
 {
-	Estimation estimation = new Estimation();
 	//search in 4 directions
-	estimation.searchFreeFieldForward(this);
-	estimation.turnAndSearchFreeFieldForward(this);
-	estimation.turnAndSearchFreeFieldForward(this);
+	boolean found = searchFreeFieldForward();
+
+	if(!found)
+		found = turnAndSearchFreeFieldForward();
+
+	if(!found)
+		found = turnAndSearchFreeFieldForward();
+
+	if(!found)
+		found = turnAndSearchFreeFieldForward();
 
-	if(!estimation.turnAndSearchFreeFieldForward(this))
+	if(!found)
 		throw new Exception(&quot;Could not find entry of area&quot;);
 		
 	//we should now be positioned in front of the entry</diff>
      <filename>exercise 2/3/a/solution.ham</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>523112239527b92c0c6475175d71dd85e68238a1</id>
    </parent>
  </parents>
  <author>
    <name>LCID Fire</name>
    <email>lcid-fire@gmx.net</email>
  </author>
  <url>http://github.com/LCID-Fire/uni_stuff/commit/dc5aeda4cee93ffdf6f96f7f5f3b0ddcdb22ae7c</url>
  <id>dc5aeda4cee93ffdf6f96f7f5f3b0ddcdb22ae7c</id>
  <committed-date>2009-11-06T03:10:44-08:00</committed-date>
  <authored-date>2009-11-06T03:10:44-08:00</authored-date>
  <message>Simplify solution for 3</message>
  <tree>827d6bf6d5645436858d23da3df65bb10efd406d</tree>
  <committer>
    <name>LCID Fire</name>
    <email>lcid-fire@gmx.net</email>
  </committer>
</commit>
