Autor Beitrag
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 12.04.05 15:01 
mir war mal wieder langweilig und ich habe mir eine ki für mein snake gebastelt, sie ist noch net perfekt (soll sie aber werden) und abfragen der wände funktionieren garnet, hier mal der ganze code (timer interval = 80)
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515:
516:
517:
518:
519:
520:
521:
522:
523:
524:
525:
526:
527:
528:
529:
unit UMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Menus, inifiles;

type
  TGame = class(TForm)
    Field: TImage;
    SnakeTimer: TTimer;
    PointDisplay: TLabel;
    HSDisplay: TLabel;
    MainMenu1: TMainMenu;
    Game1: TMenuItem;
    LoadMap1: TMenuItem;
    N1: TMenuItem;
    Exit1: TMenuItem;
    Pause1: TMenuItem;
    LoadLevel: TOpenDialog;
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure SnakeTimerTimer(Sender: TObject);
    procedure LoadMap1Click(Sender: TObject);
    procedure Pause1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure Game1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

const
  //const to check walls, directions, food etc.
  sup = 1; //Up
  sdown = 2; //Down
  sleft = 3; //Left
  sright = 4; //Right
  Wall = 5; //Wall
  Food = 6; //Food
  Bonus = 7; //Bonus

var
  //Game  Variables
  Game: TGame; //Form
  map: array[1..40] of array[1..40] of integer; //Map
  snake: array of TPoint; //Snake
  FoodPos, BonusPos: Tpoint; //Food and Bonus Food Positions
  snakelength, KeyDirection, Direction, Points, HighScore, BonusStay: integer; //Everything changing in Game (Timer)
  HSFile: TIniFile; //IniFile to Save Score
  BonusExists, Pause: boolean; //Bonus Check and Pause
  ai_activated: boolean = false; //AI

implementation

{$R *.dfm}

//Draw Procedures

//Procedure to fill in a Field with any color
procedure FillField(x,y: integer; color: Tcolor);
begin
  with game.field.canvas do
    begin
      pen.color:= color;
      brush.color:= color;
      rectangle(x*5-4, y*5-4, x*5, y*5);
   end;   
end;

//Map(SnakeStyle) (Disabled since 2.0)
procedure DrawWalls;
var
  x, y, i: integer;
begin
  for i:= 1 to 40 do
    begin
      x:= 1;
      y:= i;
      FillField(x, y, clblack);
      Map[x, y]:=  wall;
      x:= 40;
      FillField(x, y, clblack);
      Map[x, y]:=  wall;
      x:= i;
      y:= 1;
      FillField(x, y, clblack);
      Map[x, y]:=  wall;
      y:= 40;
      FillField(x, y, clblack);
      Map[x, y]:=  wall;
    end;
end;

//Draws the MapGrid
procedure DrawMap;
var
  x, y, i: integer;
begin
  with game.Field.Canvas do
    begin
      brush.color:= clwhite;
      rectangle(0, 0, 201, 201);
    end;
  x:= 0;
  y:= 0;
  for i:= 0 to 40 do
    begin
      with game.field.canvas do
        begin
          pen.Color:= clgray;
          moveto(x, 0);
          lineto(x, 201);
          moveto(0, y);
          lineto(201, y);
        end;
      x:= x + 5;
      y:= y + 5;
    end;
end;

//Loads Walls (when custom map is loaded)
procedure LoadWalls;
var
  i, j: integer;
begin
  for i:= 1 to 40 do
    for j:= 1 to 40 do
      if Map[i, j] = wall then
        FillField(i, j, clblack);
end;

//Draws the Snake
procedure DrawSnake;
var
  i: integer;
begin
  FillField(Snake[0].X, Snake[0].Y, clred);
  for i:= 1 to SnakeLength do
    FillField(Snake[i].X, Snake[i].Y, clgreen);
end;

//Draws the Food
procedure DrawFood;
begin
  FillField(foodpos.x, foodpos.y, clblue);
end;

//Draws the Bonus Food
procedure DrawBonus;
begin
  FillField(bonuspos.x, bonuspos.y, clfuchsia);
end;

//Sets random Position for the Food
procedure MakeFood;
var  
  i,rx,ry: integer;  
begin
  if (Map[FoodPos.X, FoodPos.Y] <> food) then begin
    rX:= random(37) + 2;
    rY:= random(37) + 2;
    for i:= 0 to snakelength do begin
      while ((Snake[i].X = rX) and (Snake[i].Y = rY)) or (Map[rx, rY] = Wall) do
        begin
          rX:= random(37) + 2;
          rY:= random(37) + 2;
        end;
    end;
    FoodPos.X:=rx;
    FoodPos.Y:=ry;
    map[FoodPos.x, FoodPos.y]:= food;
  end;  
end; 

//Sets random Position for the Bonus Food
procedure MakeBonus;
var
  i: integer;
begin
  if (Map[FoodPos.X, FoodPos.Y] <> food) then begin
    BonusPos.X:= random(37) + 2;
    BonusPos.Y:= random(37) + 2;
    for i:= 0 to snakelength do
      while (Snake[i].X = BonusPos.X) and (Snake[i].Y = BonusPos.Y) or (Map[BonusPos.x, BonusPos.Y] = Wall) or (Map[BonusPos.x, BonusPos.Y] = Food) do
        begin
          BonusPos.X:= random(37) + 2;
          BonusPos.Y:= random(27) + 2;
        end;
    map[BonusPos.x, BonusPos.y]:= bonus;
    BonusExists:= true;
  end;  
end;

//Starts the Game
procedure StartGame;
var
  i, x, y: integer;
begin
  Pause:= false;
  BonusStay:= 0;
  Map[BonusPos.X, BonusPos.Y]:= 0;
  BonusPos.X:= 0;
  BonusPos.Y:= 0;
  BonusExists:= false;
  try
    HSFile:=TIniFile.Create('Highscore.ini');
    HighScore:= HSFile.Readinteger('Score', 'HighScore', 0);
  finally
    HSFile.Free;
  end;
  Game.HSDisplay.caption:= 'HighScore: ' + inttostr(HighScore);
  Game.SnakeTimer.Enabled:= true;
  Points:= 0;
  Game.PointDisplay.Caption:= 'Points: ' + inttostr(points);
  direction:= random(4)+1;
  snakelength:= 8;
  setlength(snake, snakelength + 1);
  snake[0].x:= random(20) + 10;
  snake[0].y:= random(20) + 10;
  while (snake[0].X = wall) and (snake[0].Y = wall) do
    begin
      snake[0].x:= random(20) + 10;
      snake[0].y:= random(20) + 10;
    end;
  for i:= 1 to snakelength do
    begin
      snake[i].Y:= snake[0].Y;
      snake[i].X:= snake[0].X;
    end;  
  MakeFood;  
  DrawMap;
  DrawFood;
  DrawSnake;
  LoadWalls;  
end;

//Ends the Game
procedure GameOver;
begin
  Game.SnakeTimer.Enabled:= false;
  try
    HSFile:=TIniFile.Create('Highscore.ini');
    if HSFile.Readinteger('Score', 'HighScore', 0) < Points
      then HSFile.Writeinteger('Score', 'Highscore', Points);
  finally
    HSFile.Free;
  end;
  showmessage('Game Over!');
  StartGame;
end;    

//Initialise and Start Game
procedure TGame.FormCreate(Sender: TObject);
begin
  randomize;
  Game.DoubleBuffered:= true;
  StartGame;
end;

//Checks pressed Keys
procedure TGame.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  //General Control
  if (key = vk_up) and (Direction <> sDown) then
    KeyDirection:= sUp;
  if (key = vk_down) and (Direction <> sUp) then
    KeyDirection:= sDown;
  if (key = vk_left) and (Direction <> sRight) then
    KeyDirection:= sleft;
  if (key = vk_right) and (Direction <> sLeft) then
    KeyDirection:= sright;
  //AI (de)activator
  if (key = vk_end) then
    ai_activated:= not ai_activated;
end;

//AI
procedure AI;
var
  //Variables
  i, DX, DY: integer;
  lc, rc, uc, dc: boolean;

begin

  //Reset
  lc:= false;
  rc:= false;
  uc:= false;
  dc:= false;

  //Snake Collisions
  for i:= 1 to snakelength do
    begin
      if map[snake[i].x, snake[i].y] <> map[snake[0].x - 1, snake[0].Y] then
        lc:= true;
      if map[snake[i].x, snake[i].y] <> map[snake[0].x + 1, snake[0].Y] then
        rc:= true;
      if map[snake[i].x, snake[i].y] <> map[snake[0].x, snake[0].Y - 1] then
        uc:= true;
      if map[snake[i].x, snake[i].y] <> map[snake[0].x, snake[0].Y + 1] then
        dc:= true;
    end;

  //Walls
  if map[snake[0].x - 1, snake[0].Y] = wall then
    lc:= true;
  if map[snake[0].x + 1, snake[0].Y] = wall then
    rc:= true;
  if map[snake[0].x, snake[0].Y - 1] = wall then
    uc:= true;
  if map[snake[0].x, snake[0].Y + 1] = wall then
    dc:= true;

  //Foodsearch
  DX:= (snake[0].x - foodpos.x);
  DY:= (snake[0].y - foodpos.y);

  if DX < 0 then DX:= - DX;
  if DY < 0 then DY:= - DY;

  //Foodsearch X < Y
  if DX < DY then
    begin
      if lc = false then
        if (foodpos.x < snake[0].x) and (direction <> sright) then keydirection:= sleft;
      if rc = false then
        if (foodpos.x > snake[0].x) and (direction <> sleft) then keydirection:= sright;
      if foodpos.x = snake[0].x then
        begin
          if uc = false then
            if (foodpos.y < snake[0].y) and (direction <> sdown) then keydirection:= sup;
          if dc = false then
            if (foodpos.y > snake[0].y) and (direction <> sup) then keydirection:= sdown;
        end;
    end;

  //Foodsearch X > Y
  if DX > DY then
    begin
      if uc = false then
        if (foodpos.y < snake[0].y) and (direction <> sdown) then keydirection:= sup;
      if dc = false then
        if (foodpos.y > snake[0].y) and (direction <> sup) then keydirection:= sdown;
      if foodpos.y = snake[0].y then
        begin
          if lc = false then
            if (foodpos.x < snake[0].x) and (direction <> sright) then keydirection:= sleft;
          if rc = false then
            if (foodpos.x > snake[0].x) and (direction <> sleft) then keydirection:= sright;
        end;
    end;

  //Left
  if (lc = true) and (uc = false) and (dc = true) and (direction <> sdown) then
    keydirection:= sup;
  if (lc = true) and (uc = true) and (dc = false) and (direction <> sup) then
    keydirection:= sdown;

  //Right
  if (rc = true) and (uc = false) and (dc = true) and (direction <> sdown) then
    keydirection:= sup;
  if (rc = true) and (uc = true) and (dc = false) and (direction <> sup) then
    keydirection:= sdown;

  //Up
  if (uc = true) and (lc = false) and (rc = true) and (direction <> sright) then
    keydirection:= sleft;
  if (uc = true) and (lc = true) and (rc = false) and (direction <> sleft) then
    keydirection:= sright;

  //Down
  if (dc = true) and (lc = false) and (rc = true) and (direction <> sright) then
    keydirection:= sleft;
  if (dc = true) and (lc = true) and (rc = false) and (direction <> sleft) then
    keydirection:= sright;
    
end;

//The Game Timer, everything happens here
procedure TGame.SnakeTimerTimer(Sender: TObject);
var
  i, c: integer;
begin
  if ai_activated = true then
    AI;
  if BonusStay >= 5 then
      inc(BonusStay);
  if BonusStay > 34 then
    begin
      BonusStay:= 0;
      Map[BonusPos.X, BonusPos.Y]:= 0;
      BonusPos.X:= 0;
      BonusPos.Y:= 0;
      BonusExists:= false;
    end;
  if snake[0].X >= 41 then snake[0].X:= 1;
  if snake[0].Y >= 41 then snake[0].Y:= 1;
  if snake[0].X <= 0 then snake[0].X:= 40;
  if snake[0].Y <= 0 then snake[0].Y:= 40;
  if KeyDirection <> 0 then
    Direction:= KeyDirection;
  for i:= snakelength downto 1 do
    begin
      snake[i].y:= snake[i-1].Y;
      snake[i].X:= snake[i-1].X;
    end;
  if Direction = sUp then
    snake[0].Y:= snake[0].Y - 1;
  if Direction = sDown then
    snake[0].Y:= snake[0].Y + 1;
  if Direction = sLeft then
    snake[0].X:= snake[0].X - 1;
  if Direction = sRight then
    snake[0].X:= snake[0].X + 1;
  if Map[snake[0].X, snake[0].y] = wall then
    begin
      GameOver;
      exit;
    end;
  if Map[snake[0].X, snake[0].Y] = food then
    begin
      Points:= points + 50;
      PointDisplay.Caption:= 'Points: ' + inttostr(points);
      snakelength:= snakelength + 1;
      setlength(snake, snakelength + 1);
      map[FoodPos.x, FoodPos.y]:= 0;
      if BonusStay < 5 then
        inc(BonusStay);
      if (BonusStay = 5) and (BonusExists = false) then
        MakeBonus;
      MakeFood;
    end;
  if Map[snake[0].X, snake[0].Y] = bonus then
    begin
      Points:= points + 150;
      PointDisplay.Caption:= 'Points: ' + inttostr(points);
      snakelength:= snakelength + 3;
      setlength(snake, snakelength + 3);
      BonusStay:= 0;
      Map[BonusPos.X, BonusPos.Y]:= 0;
      BonusPos.X:= 0;
      BonusPos.Y:= 0;
      BonusExists:= false;
    end;
  for c:= 1 to snakelength do
    if (snake[0].X = snake[c].X) and (snake[0].Y = snake[c].Y) then
      begin
        GameOver;
        exit;
      end;
  DrawMap;
  DrawFood;
  DrawBonus;
  DrawSnake;
  LoadWalls;
end;

//Loads the Map from a File
procedure TGame.LoadMap1Click(Sender: TObject);
var
  i, j: integer;
  buffer: string;
  F: TStringlist;
begin
  buffer:= '';
  try
    F:= TStringList.Create;
    if LoadLevel.execute then
      begin
        F.LoadFromFile(LoadLevel.FileName);
        if (F.Count < 40) or (F.Count > 40) then
          begin
            Showmessage('This is not a Snake2 map !');
            Exit;
          end;
        for i:= 0 to 39 do
          begin
            if (length(F[i]) < 40) or (length(F[i]) > 40) then
              begin
                Showmessage('This is not a Snake2 map !');
                Exit;
              end;
            for j:= 1 to 40 do
              begin
                buffer:= F[i];
                if (strtoint(buffer[j]) > 1) or (strtoint(buffer[j]) < 0) then
                  begin
                    Showmessage('This is not a Snake2 map !');
                    Exit;
                  end;
                if strtoint(buffer[j]) = 1 then
                  Map[i + 1, j]:= wall else
                    Map[i + 1, j]:= 0;
              end;
          end;
      end;
  finally
    F.Free;
  end;
  StartGame;
end;

//Pause
procedure TGame.Pause1Click(Sender: TObject);
begin
  Pause:= not Pause;
  SnakeTimer.Enabled:= not Pause;
end;

//Exit
procedure TGame.Exit1Click(Sender: TObject);
begin
  application.terminate;
end;

//Menu (Pause)
procedure TGame.Game1Click(Sender: TObject);
begin
  Pause:= true;
  SnakeTimer.Enabled:= not Pause;
end;

end.

die abfrage fürs bonus essen muss da noch rein, das ist kein problem, aber warum frisst sie sich manchmal immernoch, obwohl sie die möglichkeit hat, auszuweichen und warum funzt die wandabfrage net ? wenn jemand noch binaries haben will ich lade sie dann hier mit nem demo level hoch.

danke schonmal 8)
Larus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 431



BeitragVerfasst: Di 12.04.05 18:22 
wie soll ich bitte die KI aktivieren? Lad ma source hoch...

//Edit schon gefunden... mss man aber auch dabeigesagt bekommen! Ich teste sie gerade habe aber das gefühl deine Schlange ist leicht Kurzsichtig? Mit anderen worten... sie sollte einen Block von 3x3 Pixel scannen und sich dann entscheiden... Ich werds mal überarbeiten
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Mi 13.04.05 13:57 
sie sucht sich immer den kürzesten weg :lol:
Larus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 431



BeitragVerfasst: Do 14.04.05 13:02 
ich würd sagen da muss man nochwas machen z.B. mit prozentanteil.... Schlange vor mir... 75% Schlange links von mir und davon 33% unter mir also gehe ich nach rechts

verstanden wodrauf ich hinaus will?
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Do 14.04.05 14:18 
ist ja schon drin :lol: geht nach mehrheit
Larus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 431



BeitragVerfasst: Fr 15.04.05 09:27 
user profile iconF34r0fTh3D4rk hat folgendes geschrieben:
ist ja schon drin :lol: geht nach mehrheit


dann gib ma die Src! küsst deine Schlange immer noch die Wand?
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Fr 15.04.05 14:46 
die schlange küsst sich und die wand ganz gerne mal, das ausweichmanöver findet einfach net statt, hier mal etwas überarbeitet:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
//AI
procedure AI;
var
  //Variables
  DX, DY: integer;
  lc, rc, uc, dc: boolean;

  procedure reset;
  begin
    lc:= false;
    rc:= false;
    uc:= false;
    dc:= false;
  end;

  procedure snakeC;
  var
    i: integer;
  begin
    for i:= 1 to snakelength do
      begin
        if (snake[0].X - 1 = snake[i].X) and (snake[0].Y = snake[i].Y) then
          lc:= true;
        if (snake[0].X + 1 = snake[i].X) and (snake[0].Y = snake[i].Y) then
          rc:= true;
        if (snake[0].X = snake[i].X) and (snake[0].Y - 1 = snake[i].Y) then
          uc:= true;
        if (snake[0].X = snake[i].X) and (snake[0].Y + 1 = snake[i].Y) then
          dc:= true;
      end;
  end;

  procedure WallC;
  begin
    if map[snake[0].x - 1, snake[0].Y] = wall then
      lc:= true;
    if map[snake[0].x + 1, snake[0].Y] = wall then
      rc:= true;
    if map[snake[0].x, snake[0].Y - 1] = wall then
      uc:= true;
    if map[snake[0].x, snake[0].Y + 1] = wall then
      dc:= true;
  end;

  procedure FoodSearch;
  begin
    DX:= (snake[0].x - foodpos.x);
    DY:= (snake[0].y - foodpos.y);

    if DX < 0 then DX:= - DX;
    if DY < 0 then DY:= - DY;

    //Foodsearch X < Y
    if DX < DY then
      begin
        if lc = false then
          if (foodpos.x < snake[0].x) and (direction <> sright) then keydirection:= sleft;
        if rc = false then
          if (foodpos.x > snake[0].x) and (direction <> sleft) then keydirection:= sright;
        if foodpos.x = snake[0].x then
          begin
            if uc = false then
              if (foodpos.y < snake[0].y) and (direction <> sdown) then keydirection:= sup;
            if dc = false then
              if (foodpos.y > snake[0].y) and (direction <> sup) then keydirection:= sdown;
          end;
      end;

    //Foodsearch X > Y
    if DX > DY then
      begin
        if uc = false then
          if (foodpos.y < snake[0].y) and (direction <> sdown) then keydirection:= sup;
        if dc = false then
          if (foodpos.y > snake[0].y) and (direction <> sup) then keydirection:= sdown;
        if foodpos.y = snake[0].y then
          begin
            if lc = false then
              if (foodpos.x < snake[0].x) and (direction <> sright) then keydirection:= sleft;
            if rc = false then
              if (foodpos.x > snake[0].x) and (direction <> sleft) then keydirection:= sright;
          end;
      end;
  end;

  procedure ChangeDirection;
  begin
    //Left
    if (lc = true) and (uc = false) and (dc = true) and (direction = sleft) then
      keydirection:= sup;
    if (lc = true) and (uc = true) and (dc = false) and (direction = sleft) then
      keydirection:= sdown;

    //Right
    if (rc = true) and (uc = false) and (dc = true) and (direction = sright) then
      keydirection:= sup;
    if (rc = true) and (uc = true) and (dc = false) and (direction = sright) then
      keydirection:= sdown;

    //Up
    if (uc = true) and (lc = false) and (rc = true) and (direction = sup) then
      keydirection:= sleft;
    if (uc = true) and (lc = true) and (rc = false) and (direction = sup) then
      keydirection:= sright;

    //Down
    if (dc = true) and (lc = false) and (rc = true) and (direction = sdown) then
      keydirection:= sleft;
    if (dc = true) and (lc = true) and (rc = false) and (direction = sdown) then
      keydirection:= sright;
  end;
begin
  //Reset
  Reset;

  //Snake Collisions
  SnakeC;

  //Walls
  WallC;

  //Foodsearch
  FoodSearch;

  //ChangeDirection
  ChangeDirection;
end;

ist noch alles von der billigen sorte, aber wenn das erstmal funzt kommt das richtige zeuch :lol:

die abfrage funzt, der fehler muss bei der ChangeDirection Prozedur in der Abfrage liegen, aber ich komm net dahinter :?
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 16.04.05 14:30 
hab den fehler gefunden, war wohl mal wieder zu viel des guten:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
  procedure ChangeDirection;
  begin
    //Left
    if (lc = true) and (uc = false) {and (dc = true)} and (direction = sleft) then
      keydirection:= sup;
    if (lc = true) {and (uc = true)} and (dc = false) and (direction = sleft) then
      keydirection:= sdown;

    //Right
    if (rc = true) and (uc = false) {and (dc = true)} and (direction = sright) then
      keydirection:= sup;
    if (rc = true) {and (uc = true)} and (dc = false) and (direction = sright) then
      keydirection:= sdown;

    //Up
    if (uc = true) and (lc = false) {and (rc = true)} and (direction = sup) then
      keydirection:= sleft;
    if (uc = true) {and (lc = true)} and (rc = false) and (direction = sup) then
      keydirection:= sright;

    //Down
    if (dc = true) and (lc = false) {and (rc = true)} and (direction = sdown) then
      keydirection:= sleft;
    if (dc = true) {and (lc = true)} and (rc = false) and (direction = sdown) then
      keydirection:= sright;
  end;

bissl was ausgeklammert und jetzt funzt es, jedoch kann es passieren, das die schlange dann ewig lang ihrem schwanz hinterherjagt, also muss noch einiges getan werden ^^
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 16.04.05 17:03 
ok neues problem:
die schlange scheint das futter auch zu finden, wenn sie es nicht sieht (wenn sie neben einer Wand ist und die Priorität auszuweichen größer ist und sie das essen nicht erreicht), aber sie bewegt sich oft "im kreis", das heißt sie wiederholt bewegungen ohne das essen zu erreichen, hier mal der code: (wie änder ich das ?)
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
procedure AI;
var
  //Variables
  DX, DY: integer;
  lc, rc, uc, dc: boolean;
  fl, fr, fu, fd: integer;

  procedure reset;
  begin
    lc:= false;
    rc:= false;
    uc:= false;
    dc:= false;
  end;

  procedure snakeC;
  var
    i: integer;
  begin
    for i:= 1 to snakelength do
      begin
        if (snake[0].X - 1 = snake[i].X) and (snake[0].Y = snake[i].Y) then
          lc:= true;
        if (snake[0].X + 1 = snake[i].X) and (snake[0].Y = snake[i].Y) then
          rc:= true;
        if (snake[0].X = snake[i].X) and (snake[0].Y - 1 = snake[i].Y) then
          uc:= true;
        if (snake[0].X = snake[i].X) and (snake[0].Y + 1 = snake[i].Y) then
          dc:= true;
      end;
  end;

  procedure WallC;
  begin
    if map[snake[0].x - 1, snake[0].Y] = wall then
      lc:= true;
    if map[snake[0].x + 1, snake[0].Y] = wall then
      rc:= true;
    if map[snake[0].x, snake[0].Y - 1] = wall then
      uc:= true;
    if map[snake[0].x, snake[0].Y + 1] = wall then
      dc:= true;
  end;

  procedure Check;
  begin
    if Map[snake[0].X, snake[0].Y] = food then
      begin
        fl:= 0;
        fr:= 0;
        fu:= 0;
        fd:= 0;
      end else
        begin
          if direction = sleft then
            inc(fl);
          if direction = sright then
            inc(fr);
          if direction = sup then
            inc(fu);
          if direction = sdown then
            inc(fd);
        end;
  end;

  procedure FoodSearch(D: integer);
  begin
    if D = 1 then
      begin
        if lc = false then
          if (foodpos.x < snake[0].x) and (direction <> sright) then keydirection:= sleft;
        if rc = false then
          if (foodpos.x > snake[0].x) and (direction <> sleft) then keydirection:= sright;
        if (lc = true) and (rc = true) then
          FoodSearch(2);
        if foodpos.x = snake[0].x then
          begin
            if uc = false then
              if (foodpos.y < snake[0].y) and (direction <> sdown) then keydirection:= sup;
            if dc = false then
              if (foodpos.y > snake[0].y) and (direction <> sup) then keydirection:= sdown;
          end;
      end;
      
    if D = 2 then
      begin
        if uc = false then
          if (foodpos.y < snake[0].y) and (direction <> sdown) then keydirection:= sup;
        if dc = false then
          if (foodpos.y > snake[0].y) and (direction <> sup) then keydirection:= sdown;
        if (uc = true) and (dc = true) then
          FoodSearch(1);
        if foodpos.y = snake[0].y then
          begin
            if lc = false then
              if (foodpos.x < snake[0].x) and (direction <> sright) then keydirection:= sleft;
            if rc = false then
              if (foodpos.x > snake[0].x) and (direction <> sleft) then keydirection:= sright;
          end;
      end;
  end;

  procedure ChangeDirection;
  var
    XDir, YDir: integer;
  begin
    XDir:= (snake[0].x - foodpos.x);
    YDir:= (snake[0].y - foodpos.y);

    if fl = 40 then
      keydirection:= random(1) + 1;
    if fr = 40 then
      keydirection:= random(1) + 1;
    if fu = 40 then
      keydirection:= random(1) + 3;
    if fd = 40 then
      keydirection:= random(1) + 3;

    //Left
    if (lc = true) and (uc = false) {and (dc = true)} and (direction = sleft) then
      keydirection:= sup;
    if (lc = true) {and (uc = true)} and (dc = false) and (direction = sleft) then
      keydirection:= sdown;
    if (lc = true) and (uc = false) and (dc = false) and (direction = sleft) then
      begin
        if YDir > 0 then
          keydirection:= sup;
        if YDir < 0 then
          keydirection:= sdown;
      end;  

    //Right
    if (rc = true) and (uc = false) {and (dc = true)} and (direction = sright) then
      keydirection:= sup;
    if (rc = true) {and (uc = true)} and (dc = false) and (direction = sright) then
      keydirection:= sdown;
    if (rc = true) and (uc = false) and (dc = false) and (direction = sright) then
      begin
        if YDir > 0 then
          keydirection:= sup;
        if YDir < 0 then
          keydirection:= sdown;
      end;

    //Up
    if (uc = true) and (lc = false) {and (rc = true)} and (direction = sup) then
      keydirection:= sleft;
    if (uc = true) {and (lc = true)} and (rc = false) and (direction = sup) then
      keydirection:= sright;
    if (uc = true) and (lc = false) and (rc = false) and (direction = sup) then
      begin
        if XDir > 0 then
          keydirection:= sleft;
        if XDir < 0 then
          keydirection:= sright;
      end;

    //Down
    if (dc = true) and (lc = false) {and (rc = true)} and (direction = sdown) then
      keydirection:= sleft;
    if (dc = true) {and (lc = true)} and (rc = false) and (direction = sdown) then
      keydirection:= sright;
    if (dc = true) and (lc = false) and (rc = false) and (direction = sdown) then
      begin
        if XDir > 0 then
          keydirection:= sleft;
        if XDir < 0 then
          keydirection:= sright;
      end;
  end;
begin
  //Reset
  Reset;

  //Check
  Check;

  //Snake Collisions
  SnakeC;

  //Walls
  WallC;

  DX:= (snake[0].x - foodpos.x);
  DY:= (snake[0].y - foodpos.y);

  if DX < 0 then DX:= - DX;
  if DY < 0 then DY:= - DY;

  //Foodsearch
  if DX < DY then
    FoodSearch(1);
  if DX > DY then
    FoodSearch(2);  

  //ChangeDirection
  ChangeDirection;
end;
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: So 17.04.05 14:04 
so: die schlange nimmt jetzt wirklich immer den kürzesten weg (in 90% aller fälle, wenn keine wände im spiel sind), aber ich bekomme einen stack overflow, wenn die schlange sich selbst frisst: (die stelle ist markiert, ich hab jetzt noch die variable is_gameover eingefügt, weiß aber net, ob das die lösung ist [bis jetzt hat sich der fehler eingestellt :? ])
ausblenden Delphi-Quelltext
1:
is_GameOver: boolean = false; //GameOver ?					

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
procedure StartGame;
var
  i, x, y: integer;
begin
  is_GameOver:= false;
...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure GameOver;
begin
  is_GameOver:= true;
...

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
//AI
procedure AI;
var
  //Variables
  DX, DY: integer;
  lc, rc, uc, dc: boolean;
  fl, fr, fu, fd: integer;

  procedure reset;
  begin
    lc:= false;
    rc:= false;
    uc:= false;
    dc:= false;
  end;

  procedure snakeC;
  var
    i: integer;
  begin
    for i:= 1 to snakelength do
      begin
        if (snake[0].X - 1 = snake[i].X) and (snake[0].Y = snake[i].Y) then
          lc:= true;
        if (snake[0].X + 1 = snake[i].X) and (snake[0].Y = snake[i].Y) then
          rc:= true;
        if (snake[0].X = snake[i].X) and (snake[0].Y - 1 = snake[i].Y) then
          uc:= true;
        if (snake[0].X = snake[i].X) and (snake[0].Y + 1 = snake[i].Y) then
          dc:= true;
      end;
  end;

  procedure WallC;
  begin
    if map[snake[0].x - 1, snake[0].Y] = wall then
      lc:= true;
    if map[snake[0].x + 1, snake[0].Y] = wall then
      rc:= true;
    if map[snake[0].x, snake[0].Y - 1] = wall then
      uc:= true;
    if map[snake[0].x, snake[0].Y + 1] = wall then
      dc:= true;
  end;

  procedure Check;
  begin
    if Map[snake[0].X, snake[0].Y] = food then
      begin
        fl:= 0;
        fr:= 0;
        fu:= 0;
        fd:= 0;
      end else
        begin
          if direction = sleft then
            inc(fl);
          if direction = sright then
            inc(fr);
          if direction = sup then
            inc(fu);
          if direction = sdown then
            inc(fd);
        end;
  end;

  procedure FoodSearch(D: integer);
  begin
    if D = 1 then
      begin
        if lc = false then
          if (foodpos.x < snake[0].x) and (direction <> sright) then keydirection:= sleft;
        if rc = false then
          if (foodpos.x > snake[0].x) and (direction <> sleft) then keydirection:= sright;
        if (lc = true) and (rc = true) then
          FoodSearch(2);
        if foodpos.x = snake[0].x then
          begin
            if uc = false then
              if (foodpos.y < snake[0].y) and (direction <> sdown) then keydirection:= sup;
            if dc = false then
              if (foodpos.y > snake[0].y) and (direction <> sup) then keydirection:= sdown;
          end;
      end;
      
    if D = 2 then
      begin
        if uc = false then
          if (foodpos.y < snake[0].y) and (direction <> sdown) then keydirection:= sup;
        if dc = false then
          if (foodpos.y > snake[0].y) and (direction <> sup) then keydirection:= sdown;
        if (uc = true) and (dc = true) then
          FoodSearch(1);
        if foodpos.y = snake[0].y then
          begin
            if lc = false then
              if (foodpos.x < snake[0].x) and (direction <> sright) then keydirection:= sleft;
            if rc = false then
              if (foodpos.x > snake[0].x) and (direction <> sleft) then keydirection:= sright;
          end;
      end;
  end;

  procedure ChangeDirection;
  var
    XDir, YDir: integer;
  begin
    XDir:= (snake[0].x - foodpos.x);
    YDir:= (snake[0].y - foodpos.y);

    if fl = 40 then
      keydirection:= random(1) + 1;
    if fr = 40 then
      keydirection:= random(1) + 1;
    if fu = 40 then
      keydirection:= random(1) + 3;
    if fd = 40 then
      keydirection:= random(1) + 3;

    //Left
    if (lc = true) and (uc = false) {and (dc = true)} and (direction = sleft) then
      keydirection:= sup;
    if (lc = true) {and (uc = true)} and (dc = false) and (direction = sleft) then
      keydirection:= sdown;
    if (lc = true) and (uc = false) and (dc = false) and (direction = sleft) then
      begin
        if YDir > 0 then
          keydirection:= sup;
        if YDir < 0 then
          keydirection:= sdown;
      end;  

    //Right
    if (rc = true) and (uc = false) {and (dc = true)} and (direction = sright) then
      keydirection:= sup;
    if (rc = true) {and (uc = true)} and (dc = false) and (direction = sright) then
      keydirection:= sdown;
    if (rc = true) and (uc = false) and (dc = false) and (direction = sright) then
      begin
        if YDir > 0 then
          keydirection:= sup;
        if YDir < 0 then
          keydirection:= sdown;
      end;

    //Up
    if (uc = true) and (lc = false) {and (rc = true)} and (direction = sup) then
      keydirection:= sleft;
    if (uc = true) {and (lc = true)} and (rc = false) and (direction = sup) then
      keydirection:= sright;
    if (uc = true) and (lc = false) and (rc = false) and (direction = sup) then
      begin
        if XDir > 0 then
          keydirection:= sleft;
        if XDir < 0 then
          keydirection:= sright;
      end;

    //Down
    if (dc = true) and (lc = false) {and (rc = true)} and (direction = sdown) then
      keydirection:= sleft;
    if (dc = true) {and (lc = true)} and (rc = false) and (direction = sdown) then
      keydirection:= sright;
    if (dc = true) and (lc = false) and (rc = false) and (direction = sdown) then
      begin
        if XDir > 0 then
          keydirection:= sleft;
        if XDir < 0 then
          keydirection:= sright;
      end;
  end;
begin
  if is_GameOver = false then
    begin
      //Reset
      Reset;

      //Check
      Check;

      //Snake Collisions
      SnakeC;

      //Walls
      WallC;

      DX:= (snake[0].x - foodpos.x);
      DY:= (snake[0].y - foodpos.y);

    //  if DX < 0 then DX:= - DX;
    //  if DY < 0 then DY:= - DY;

      //Foodsearch
      if DX < DY then
        FoodSearch(1);
      if DX > DY then
        FoodSearch(2);

      //ChangeDirection
      ChangeDirection;
    end;
end;

scheint mit der neuen variable zu funktionieren

sie bewegt sich immer noch im kreis und frisst sich in sich rein also kringelt so zusammen. anhang ist meine kitest map, könnt ihr euch zum testen ziehen.

im spiel selber hab ich noch den fehler gefunden, wenn die schlange über das feld hinauswill ist plötzlich gamover, finde ich irgendwie komisch, ähnlicher fehler ist auch im editor, aber egal :lol:
Einloggen, um Attachments anzusehen!