需要算法帮助 - 网格中的行/列

时间:2010-12-31 13:54:32

标签: android

我一直在试图开发一种算法来确定网格中单元格所在的行或列的位置。

例如,我有一个8x8网格,我需要确定第3行中单元格的所有位置。

我遇到的问题是,如果没有很多循环,我需要找出一种方法来将点击的位置映射到包含该单元格的行和列。

这是我创建单元格的ArrayList所做的:

public class GameBoard {
public ArrayList<Row> rows = new ArrayList<Row>();
public ArrayList<Column> columns = new ArrayList<Column>();

private int maxPositions;

public GameBoard(int rowCount, int colCount) {
    this.maxPositions = (rowCount * colCount) - 1;

    for(int x = 0;x < rowCount;x++){
        Row row = new Row(x);
        row.cells.addAll(this.getRowCells(x, colCount));
        rows.add(row);
    }

    for(int x = 0;x < colCount;x++){
        Column column = new Column(x);
        column.cells.addAll(getColumnCells(x, colCount));
        columns.add(column);
    }
}

private ArrayList<Cell> getRowCells(int rowId, int colCount) {
    ArrayList<Cell> row_cells = new ArrayList<Cell>();

    int first_position  = ((rowId * colCount) -1);
    Cell cell = new Cell();
    cell.id = first_position;
    row_cells.add(cell);
    int x = 1;
    while(x < colCount) {
        cell = new Cell();
        cell.id = first_position + x;
        row_cells.add(cell);
        x++;
    }
    return row_cells;       
}

private ArrayList<Cell> getColumnCells(int columnId, int rowCount) {
    ArrayList<Cell> col_cells = new ArrayList<Cell>();

    int first_position  = columnId;
    Cell cell = new Cell();
    cell.id = first_position;
    col_cells.add(cell);
    int x = 1;
    while(x < rowCount) {
        cell = new Cell();
        cell.id = rows.get(x).cells.get(columnId).id;
        col_cells.add(cell);
        x++;
    }
    return col_cells;
}

public Cell getColumnNextCell(int position, int columnId) {
    Cell cell = null;
    if ((position > -1) && (position <= maxPositions)) {
        cell = columns.get(columnId).cells.get(position + 1);
    }
    return cell;
}

public Cell getColumnPreviousCell(int position, int columnId) {
    Cell cell = null;
    if ((position > 0) && (position <= maxPositions)) {
        cell = columns.get(columnId).cells.get(position - 1);
    }
    return cell;
}

public Cell getRowNextCell(int position, int rowId) {
    Cell cell = null;
    if ((position > -1) && (position <= maxPositions)) {
        cell = rows.get(rowId).cells.get(position + 1);
    }
    return cell;
}

public Cell getRowPreviousCell(int position, int rowId) {
    Cell cell = null;
    if ((position > 0) && (position <= maxPositions)) {
        cell = rows.get(rowId).cells.get(position - 1);
    }
    return cell;
}

}

正如你可以通过getRowPreviousCell()看到getColumnNextCell()这样的方法,逻辑是一团糟,我仍然坚持如何确定点击单元格的列和行(正如我所解释的)。任何帮助将不胜感激。

为了澄清,Row和Column对象是相同的。它们看起来像这样。

public class Column {
public int id;
public ArrayList<Cell> cells = new ArrayList<Cell>();

public Column(int idx) {
    this.id = idx;
}

}

2 个答案:

答案 0 :(得分:1)

你不能只为细胞使用一个数组

int rows, cols;
Cell[][] cells;

public GameBoard(int rowCount, int colCount) {
    this.maxPositions = (rowCount * colCount) - 1;

    this.rows = rowCount;
    this.cols = colCount;
    this.cells = new Cell[rows];
    for(int i=0; i < rows; i++) {
        cells[i] = new Cell[cols];
        for(int j = 0; j < cols; j++) {
            cells[i][j] = new Cell();
            // whatever else you need to do...
        }
    }
}

通过这种方式,您可以轻松获得所需的单元格

Cell getCell(int position) {
     return cells[position/rows][position%cols];
}

答案 1 :(得分:0)

为什么不使用简单的二维数组?如果在创建后未调整网格大小,则使用ArrayList对象没有任何优势。那么让你的基类像这样:

public class GameBoard {
  private Cell[][] cells;
  private int maxPositions;
  private int rows;
  private int cols;

  public GameBoard(int rowCount, int colCount) {
    this.rows = rowCount;
    this.cols = colCount;
    cells = new Cell[rowCount][colCount];
    for (int i = 0; i < rowCount; i++) {
      for (int j = 0; j < colCount; j++) {
        cells[i][j] = new Cell();
      }
    }
    this.maxPositions = (rowCount * colCount) - 1;
  }
  ...
}

现在你没有行和列对象,只有单元格数组。这些单元格实际上不是您需要的,还是那些行和列数组有另一个目的?所以我认为你试图以太困难的方式做一件简单的事情。