我称之为Cell的hava类有两个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个匹配规则,因此获胜。
答案 0 :(得分:0)
我的建议是保持游戏状态并在玩家标记单元格后更新。 我不认为性能是这种2D游戏的关注点。所以不需要采用优化的复杂算法。
class Cell
{
// other codes
public boolean isAdjacent(Cell cell)
{
int xDiff = cell.getX() - x;
int yDiff = cell.getY() -y;
if(xDiff>=-1 && xDiff<=1 && yDiff>=-1 && yDiff<=1)
{
return true;
}
return false;
}
}
引入一个新类来跟踪相邻的单元格块。
public class Block
{
private List<Cell> cells = new ArrayList<Cell>();
public boolean canMerge(Cell cell)
{
if (cells.isEmpty())
{
return true;
}
else
{
for (Cell existingCell : cells)
{
if (existingCell.isAdjacent(cell))
{
return true;
}
}
}
return false;
}
public void merge(Cell cell)
{
cells.add(cell);
}
public void merge(Block block)
{
cells.addAll(block.getCells());
}
public List<Cell> getCells()
{
return cells;
}
}
然后新课程维持游戏状态
一旦用户点击一个单元格,你必须调用markCell方法
public class GameStatus
{
HashMap<Integer,List<Block>> players = new HashMap<Integer, List<Block>>();
public boolean markCell(Cell cell,int player)
{
int count = 0;
List<Block> blocks = players.get(player);
if(blocks==null)
{
// this is the first cell mark by this player
blocks = new ArrayList<Block>();
Block block = new Block();
block.merge(cell);
blocks.add(block);
players.put(player,blocks);
}
else
{
Block alreadyMergedToThisBlock = null;
ArrayList<Block> shouldBeRemovedList = new ArrayList<Block>();
for (Block block : blocks)
{
if(block.canMerge(cell)) // cell is adjacent to a block
{
if (alreadyMergedToThisBlock==null) // this is the first block we found which is adjacent to the given cell
{
block.merge(cell);
count = block.getCells().size();
alreadyMergedToThisBlock = block;
}
else
{
// we have already merge given cell to block,but this block also adjacent to the given cell.
// that means two blocks can be merged together and this block can be removed once it is merged to the previous one
alreadyMergedToThisBlock.merge(block);
count = alreadyMergedToThisBlock.getCells().size();
shouldBeRemovedList.add(block);
}
}
}
blocks.removeAll(shouldBeRemovedList);
}
return count>=9;
}
}