示例#1
0
  public void crossoverWith(DesignBoard b) {
    if (width != b.width || height != b.height) {
      throw new RuntimeException("Trying to crossover boards that have different dimensions.");
    }

    boolean isVerticalSplit = parameters.random.nextBoolean();

    // This gives a range slightly in from the maxHeight or maxWidth.
    // For example, if the maxWidth is 10, this gives us a range of 1 - 8 instead of 0 - 9.
    int max = isVerticalSplit ? width : height;
    int splitEnd = parameters.random.nextInt(max - 1) + 1;

    int length = isVerticalSplit ? height : width;
    for (int i = 0; i < splitEnd; i++) {
      for (int j = 0; j < length; j++) {
        int x = i;
        int y = j;
        if (!isVerticalSplit) {
          x = j;
          y = i;
        }

        // Swap the values.
        DesignCell temp = get(x, y);
        set(x, y, b.get(x, y));
        b.set(x, y, temp);
      }
    }
  }