【发布时间】:2015-10-30 02:04:15
【问题描述】:
我有一个名为 Cell 的类,它有两个属性 x 和 y,它们扩展了 JButton。这是代码。
private static final long serialVersionUID = 1L;
private int x;
private int y;
public Cell(int x, int y) {
this.x = x;
this.y = y;
String cellCoord = x + "," + y;
JLabel cellLbl = new JLabel(cellCoord);
this.add(cellLbl);
this.setBackground(Color.ORANGE);
}
public int getx() {
return x;
}
public int gety() {
return y;
}
public void setx(int x) {
this.x = x;
}
public void sety(int y) {
this.y = y;
}
还有一个名为 Grid 的类,用于创建扩展 JPanel 的 20x20 Grid。
public class Grid extends JPanel {
private static final long serialVersionUID = 1L;
private ArrayList<Cell> cells;
private int width = 20;
private int height = 20;
public Grid() {
cells = new ArrayList<Cell>();
}
public void drawGrid() {
this.setLayout(new GridLayout(width, height, 5, 5));
this.setBackground(Color.RED);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Cell cell = new Cell(i, j);
cells.add(cell);
}
}
for (Cell c : cells) {
this.add(c);
}
}
public void refreshGrid() {
this.removeAll();
this.repaint();
for (Cell c : cells) {
this.add(c);
}
}
public ArrayList<Cell> getCells() {
return cells;
}
public void setCells(ArrayList<Cell> cells) {
this.cells = cells;
}
public void changeCell(Cell c) {
for (Cell cell : cells) {
if (cell.getx() == c.getx() && cell.gety() == c.gety()
&& cell.getBackground() != Color.black ) {
cell.setBackground(c.getBackground());
refreshGrid();
}
}
}
public Cell validateCell(int x , int y){
Cell tmp = new Cell(x,y);
for(Cell cell : this.cells){
if(cell.getx() == x && cell.gety() == y){
tmp = cell;
}
}
return tmp;
}
我怎样才能找到一种方法来创建检查获胜者的方法。基本上其他功能已经完成,比如改变玩家的回合,并根据玩家的动作改变选择的按钮的颜色(每个玩家可以选择一个单元格并将其更改为一种颜色,它真的类似于收集 4 但彩色按钮的图案有点不同)。
如果有 9 个按钮,这些按钮的模式检查每个彩色单元格的 North、North East、East、SouthEast、South、SouthWest、West、NorthWest,则用户可以获胜。如果 9 个彩色单元格以相同的颜色连接,则用户获胜。
这是一个有效模式的示例,其中以黄色圆圈突出显示的模式相互连接以形成模式。带有红色圆圈的那个是在这种情况下被丢弃的最后一个单元格,程序注意到有 11 个相同颜色的单元格,这意味着它超过了 9 个匹配规则,因此有一个赢家。
【问题讨论】:
-
第一个玩家不是每次都赢吗?有9个方向可以将一个块链接到另一个块,没有办法通过每回合放置一个块来阻止第一个玩家
-
玩家也可以使用一种特殊的力量来禁用细胞,他在每场比赛中有两次机会这样做,因此玩家二可以阻止玩家一获胜。它看起来像这样Image
-
好了,除此之外,你需要的是使用图遍历算法,统计图中的节点数。
-
我会检查一下,谢谢你的帮助。
标签: java arrays swing arraylist jbutton