/*_______________________________________________________________________________________ File : hamster8.c Contents: hamster control program Author : Stefan Weidner History : 16.10.1997 file created by Christian Borgelt 18.01.1998 first changes (more steps, first decisions, ...) 27.02.1998 first explorations -> array lab[][] (north,east,south,west) 31.03.1998 improve lab[][] (explored,corn), first case instructions 01.04.1998 decision-making on directions (wall positions) 05.04.1998 block blind alleys (from the end to the entry) 23.04.1998 notice way back 20.05.1998 avoid cross ways / correct way back 28.05.1998 first test: best_way (lab[][].best)/ opt_way (shortest way back) 01.06.1998 improve decision-making (hms_corn_around, hms-_unexplored, ...) 09.06.1998 opt_way (shortest way back), empty_cnt 12.06.1998 last optimizations _______________________________________________________________________________________*/ #include #include #include #include "hamster.h" /*_______________________________________________________________________________________ Initialisierung Struktur Feld, um Eigenschaften eines jeden Labyrinthfeldes abspeichern zu können: explored - Feld erforscht / nicht erforscht north,west,south,east - Informationen über angrenzende Felder corn - aktuelle Kornmenge auf Feld _______________________________________________________________________________________*/ struct Feld { char explored, north, west, south, east, corn; int best; /* Start Korrektur, Christian Borgelt, 25.06.1998 */ /* Da die Initialisierung mit ``={0,0,0,0,0,0,4096};'' sowieso ohne */ /* Wirkung ist, weil es in hms_ctrl eine Schleife gibt, die die */ /* ganze Matrix initialisiert, kann sie auch weggelassen werden. */ /* I.a. wird das ausf"uhrbare Programm dadurch deutlich kleiner. */ #if 0 } lab[130][130] = {0,0,0,0,0,0,4096}; /* Erklärung siehe 4.1 best_way */ #else } lab[130][130]; /* Erklärung siehe 4.1 best_way */ #endif /* Ende Korrektur, Christian Borgelt, 25.06.1998 */ /*_______________________________________________________________________________________ Funktion zum Speichern folgender Informationen noch nicht erforschter Felder in lab[][]: forward, left, right : 0 - leeres Feld 1 - Feld mit Korn 2 - Wand explored : 1 - Feld jetzt erforscht corn : aktuelle Kornmenge _______________________________________________________________________________________*/ void explore (HAMSTER *hms,int x, int y, char *way, int i) { char forward, left, right; /* Blickrichtungen */ forward = hms_look(hms); hms_turn(hms,1); left = hms_look(hms); hms_turn(hms,-1); hms_turn(hms,-1); right = hms_look(hms); hms_turn(hms,1); lab[x][y].explored = 1; /* Feld nun erforscht */ lab[x][y].corn = hms_corn(hms); /* aktuelle Kornmenge */ switch (way[i-1]) /* Belegung von lab[][].{north,east,south,west} */ { case 0 : lab[x][y].west = forward; /* richtet sich nach Blickrichtung des Hamsters */ lab[x][y].south = left; lab[x][y].north = right; break; case 1 : lab[x][y].south = forward; lab[x][y].east = left; lab[x][y].west = right; break; case 2 : lab[x][y].east = forward; lab[x][y].north = left; lab[x][y].south = right; break; case 3 : lab[x][y].north = forward; lab[x][y].west = left; lab[x][y].east = right; break; } } /*_______________________________________________________________________________________ Funktion zum Berechnen des Minimums eines Zahlenquadrupels _______________________________________________________________________________________*/ int min4(int a, int b, int c, int d) { if (b