Einzelnen Beitrag anzeigen
Ungelesen 24.05.10, 22:33   #6
urga
Mitglied
 
Benutzerbild von urga
 
Registriert seit: Aug 2009
Ort: void* (*wtf[])(void **);
Beiträge: 453
Bedankt: 137
urga ist noch neu hier! | 0 Respekt Punkte
Standard

hier mal was konstruktives. ich finde die funktion etwas "unelegant" aber sie tut was sie soll. und das nach 6 bier
wem eine elegantere (und/oder kürzere) version gelingt, bitte posten!
Code:
    
  private Point[] getNeigbours (int x, int y) {
      int count = 8; // 8 potentielle nachbarn
      // this.size ist das ausmass des spielfeldes
      // also im fall von 15x15 == 15
      if (x == 0 || x == this.size - 1) count -= 3;
      if (y == 0 || y == this.size - 1) count -= 3;
      if (count == 2) count = 3; // falls einer der die 4 eckpunkte geklickt
      Point[] ps = new Point[count];

      Point[] tmp = new Point[8];
      int cnt = 0;
      tmp[cnt++] = new Point (x + 1, y + 1);
      tmp[cnt++] = new Point (x - 1, y - 1);
      tmp[cnt++] = new Point (x - 1, y + 1);
      tmp[cnt++] = new Point (x + 1, y - 1);

      tmp[cnt++] = new Point (x + 1, y);
      tmp[cnt++] = new Point (x - 1, y);
      tmp[cnt++] = new Point (x, y + 1);
      tmp[cnt++] = new Point (x, y - 1);

      cnt = 0;
      foreach (Point p in tmp) {
        if (p.X >= 0 && p.X < this.size && p.Y >= 0 && p.Y < this.size) {
          ps[cnt++] = p;
        }
      }
      return ps;
    } // getNeigbours()
ich verzichte hier bewusst auf generische typen wie List<T>, da es ja um C als zielsprache geht.
__________________
entropie erfordert keine wartung
urga ist offline   Mit Zitat antworten