Example #1
0
  public RestPoint findBestMoveSequence(Figure f, TetrisGrid tg, JPanel[][] cells) {

    this.cells = cells;
    this.f = f;
    this.tg = tg;

    RestPoint point = new RestPoint();
    List<RestPoint> points = new ArrayList<RestPoint>();
    int loops;

    if (f.getGridVal() == 2 || f.getGridVal() == 4) {
      loops = 4;
    } else if (f.getGridVal() == 3 || f.getGridVal() == 5) {
      loops = 3;
    } else if (f.getGridVal() == 6 || f.getGridVal() == 7 || f.getGridVal() == 1) {
      loops = 2;
    } else {
      loops = 1;
    }

    for (int t = 0; t < loops; t++) {
      for (int x = 9; x >= 0; x--) {
        for (int y = 19; y >= 0; y--) {
          if (tg.isNextMoveValid(f, x, y)) {
            RestPoint rp = new RestPoint();
            rp.x = x;
            rp.y = y;
            rp.transform = t;
            points.add(rp);
            break;
          }
        }
      }
      f.rotationRight();
    }

    int currentRotation = 0;
    int bestTransform = 0, bestX = 0, bestY = 0;
    double bestRating = -1;
    for (RestPoint rp : points) {
      int loop = rp.transform - currentRotation;
      while (loop != 0) {
        f.rotationRight();
        currentRotation++;
        loop = rp.transform - currentRotation;
      }

      if (tg.isNextMoveValid(f, rp.x, rp.y)) {
        /*
         * changeOffsets(rp.x, rp.y); tg.addFiguretoGrid(f);
         * System.out.println(tg.toString());
         * System.out.println("----------------------------------");
         * tg.removeFigureFromGrid(f); bestX = rp.x; bestY = rp.y;
         */
        if (pointRating(rp) > bestRating) {
          bestX = rp.x;
          bestY = rp.y;
          bestTransform = rp.transform;
          bestRating = pointRating(rp);
        }
      }
    }

    // Rotate Back to 0
    f.rotationRight();
    currentRotation = 0;

    // Loop to the best Transform
    int loop = bestTransform - currentRotation;
    while (loop != 0) {
      f.rotationRight();
      currentRotation++;
      loop = bestTransform - currentRotation;
    }

    changeOffsets(bestX, bestY);
    tg.addFiguretoGrid(f);

    gridOffsets[0] = bestX;
    gridOffsets[1] = bestY;

    return point;
  }
Example #2
0
 private boolean isAtBottom(RestPoint rp) {
   if (!tg.isNextMoveValid(f, rp.x, rp.y + 1)) {
     return true;
   }
   return false;
 }