Ejemplo n.º 1
0
  /*	public void randomizeBoard(Board board) {
  		// TODO: Figure out a clever way to randomly initialize boards.
  		for (int i = 0; i < board.getHeight(); i++) {
  			for (int j = 0; j < board.getWidth(); j++) {
  			   if(Math.random()>0.5)
  			   {
  				   board.setAlive(i, j, true);
  			   }
  			}
  		}
  	}
  */
  public void randomizeBoard(Integer seed_num, Integer cell_num, Board board) {
    // TODO: Figure out a clever way to randomly initialize boards.
    if (seed_num > cell_num) {
      System.out.print("Seed# must be less than the Cell#!");
    } else {

      for (int i = 0; i < board.getHeight(); i++) { // Initialization the board
        for (int j = 0; j < board.getWidth(); j++) {
          board.setAlive(i, j, false);
        }
      }

      ArrayList<Integer> rand_index =
          new ArrayList<
              Integer>(); // List of Saving the 1 to Height*Width number which used to be chosed as
      // random number
      ArrayList<Integer> seed_index = new ArrayList<Integer>(); // List of Saving the Seed index
      for (int i = 0;
          i <= board.getHeight() * board.getWidth() - 1;
          i++) { // input the num from 1 to Height*Width
        rand_index.add(i);
      }
      int cur_num = 0;
      while (cur_num < seed_num) { // random choose #cur_num of seed_index
        int value = (int) (Math.random() * rand_index.size());
        seed_index.add(rand_index.get(value) + 1);
        ++cur_num;
      }

      for (int i = 0; i < seed_index.size(); i++) { // transfer the seed_index to coordinate
        int temp_v = ((List<Integer>) seed_index).get(i);

        if (temp_v % board.getWidth() != 0) {
          int x_axis = (int) (temp_v / board.getWidth());
          int y_axis = temp_v % board.getWidth() - 1;
          board.setAlive(x_axis, y_axis, true);
        } else {
          int x_axis = (int) (temp_v / board.getWidth()) - 1;
          int y_axis = board.getWidth() - 1;
          board.setAlive(x_axis, y_axis, true);
        }
      }

      int cur_cellnum = seed_num;

      while (cur_cellnum < cell_num) //
      {
        // System.out.println(cur_cellnum);
        ArrayList<Coordinate> cur_liveCollection = (ArrayList<Coordinate>) board.getLiveCells();
        int value = (int) (Math.random() * cur_liveCollection.size());
        Coordinate selected_cell = cur_liveCollection.get(value);
        // System.out.println("wow "+board.countLiveNeighbors(selected_cell.getX(),
        // selected_cell.getY(), 1));
        if (board.countLiveNeighbors(selected_cell.getX(), selected_cell.getY(), 1) != 8) {
          List<Coordinate> toCheck = new ArrayList<Coordinate>();
          List<Coordinate> deadneigbours = new ArrayList<Coordinate>();

          for (int i = (selected_cell.getX() - 1); i <= (selected_cell.getX() + 1); i++) {
            for (int j = (selected_cell.getY() - 1); j <= (selected_cell.getY() + 1); j++) {
              if (i <= board.getHeight() && i >= 0 && j <= board.getWidth() && j >= 0) {
                toCheck.add(new Coordinate(i, j));
              } else {
                continue;
              }
            }
          }

          for (Coordinate c : toCheck) {
            if (board.getCellsMap().get(c) != null) {
              if (board.getCellsMap().get(c).isAlive() == false) {
                deadneigbours.add(c);
              }
            }
          }

          if (deadneigbours.size() != 0) {
            int neigbour_index = (int) (Math.random() * deadneigbours.size());
            Coordinate selectedlive_cell = deadneigbours.get(neigbour_index);
            board.setAlive(selectedlive_cell.getX(), selectedlive_cell.getY(), true);

            ++cur_cellnum;
          }
        }
      }
    }
  }