My problem is arising in the method named 'addNewTile()'. it is only supposed to add a single tile to the world and then stop but it seems that it is adding a number related to the current number of tiles already in the world. Any suggestions are greatly appreciated.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class TileBoard here. * * @author (your name) * @version (a version number or a date) */ public class TileBoard extends World { public static final int LEFT = 0, RIGHT = 1, UP = 2, DOWN = 3, TILE_SIZE = 56; private int direction = -1; private boolean isDown = false; /** * Constructor for objects of class TileBoard. * */ public TileBoard() { super(4, 4, 60); for (int i = 0; i < 2; i++){ int x = Greenfoot.getRandomNumber(4); int y = Greenfoot.getRandomNumber(4); if (!getObjectsAt(x, y, Tile.class).isEmpty()){ i -= 1; }else{ addObject(new Tile(), x, y); } } addObject(new Main(), -1, -1); } public void checkKeys(){ if (!isDown){ setKeyDirection(); } if (isDown && !(Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("down"))){ isDown = false; } } public void setKeyDirection(){ if (Greenfoot.isKeyDown("left")){ isDown = true; direction = LEFT; canCombine(); countNumberOfEmptyCellsInFront(); }else if (Greenfoot.isKeyDown("right")){ isDown = true; direction = RIGHT; canCombine(); countNumberOfEmptyCellsInFront(); }else if (Greenfoot.isKeyDown("up")){ isDown = true; direction = UP; canCombine(); countNumberOfEmptyCellsInFront(); }else if (Greenfoot.isKeyDown("down")){ isDown = true; direction = DOWN; canCombine(); countNumberOfEmptyCellsInFront(); }else{ direction = -1; } } public void canCombine(){ switch (direction){ case 0: for (int x = 1; x < 4; x++){ for (int y = 0; y < 4; y++){ if (!getObjectsAt(x, y, Tile.class).isEmpty()){ Tile tile = (Tile) getObjectsAt(x, y, Tile.class).get(0); for (int i = tile.getX() - 1; i > -1; i--){ if (!getObjectsAt(i, y, Tile.class).isEmpty()){ Tile tile1 = (Tile) getObjectsAt(i, y, Tile.class).get(0); if ((tile.getValueOfTile() == tile1.getValueOfTile()) && !tile1.getDidCombine()){ removeObject(tile); System.out.println(tile1.getDidCombine()); tile1.setDidCombine(true); System.out.println(tile1.getDidCombine()); setValuesAndImages(tile1); } break; } } } } } break; case 1: for (int x = 2; x > -1; x--){ for (int y = 0; y < 4; y++){ if (!getObjectsAt(x, y, Tile.class).isEmpty()){ Tile tile = (Tile) getObjectsAt(x, y, Tile.class).get(0); for (int i = tile.getX() + 1; i < 4; i++){ if (!getObjectsAt(i, y, Tile.class).isEmpty()){ Tile tile1 = (Tile) getObjectsAt(i, y, Tile.class).get(0); if (tile.getValueOfTile() == tile1.getValueOfTile() && !tile1.getDidCombine()){ removeObject(tile); tile1.setDidCombine(true); setValuesAndImages(tile1); } break; } } } } } break; case 2: for (int y = 1; y < 4; y++){ for (int x = 0; x < 4; x++){ if (!getObjectsAt(x, y, Tile.class).isEmpty()){ Tile tile = (Tile) getObjectsAt(x, y, Tile.class).get(0); for (int i = tile.getY() - 1; i > -1; i--){ if (!getObjectsAt(x, i, Tile.class).isEmpty()){ Tile tile1 = (Tile) getObjectsAt(x, i, Tile.class).get(0); if (tile.getValueOfTile() == tile1.getValueOfTile() && !tile1.getDidCombine()){ removeObject(tile); tile1.setDidCombine(true); setValuesAndImages(tile1); } break; } } } } } break; case 3: for (int y = 2; y > -1; y--){ for (int x = 0; x < 4; x++){ if (!getObjectsAt(x, y, Tile.class).isEmpty()){ Tile tile = (Tile) getObjectsAt(x, y, Tile.class).get(0); for (int i = tile.getY() + 1; i < 4; i++){ if (!getObjectsAt(x, i, Tile.class).isEmpty()){ Tile tile1 = (Tile) getObjectsAt(x, i, Tile.class).get(0); if (tile.getValueOfTile() == tile1.getValueOfTile() && !tile1.getDidCombine()){ removeObject(tile); tile1.setDidCombine(true); setValuesAndImages(tile1); } break; } } } } } break; default: break; } } public void setValuesAndImages(Tile tile){ if (tile.getDidCombine()){ tile.setIndividualImage(); } } public void countNumberOfEmptyCellsInFront(){ switch(direction){ case 0: for (int x = 1; x < 4; x++){ for (int y = 0; y < 4; y++){ if (getObjectsAt(x, y, Tile.class).isEmpty()) continue; Tile tile = (Tile) getObjectsAt(x, y, Tile.class).get(0); int count = 0; for (int i = x - 1; i > -1; i--){ if (!getObjectsAt(i, y, Tile.class).isEmpty()) continue; count++; } if (count != 0){ tile.setNumOfEmptyCellsInFront(count); shiftTiles(tile); } } } break; case 1: for (int x = 2; x > -1; x--){ for (int y = 0; y < 4; y++){ if (getObjectsAt(x, y, Tile.class).isEmpty()) continue; Tile tile = (Tile) getObjectsAt(x, y, Tile.class).get(0); int count = 0; for (int i = x + 1; i < 4; i++){ if (!getObjectsAt(i, y, Tile.class).isEmpty()) continue; count++; } if (count != 0){ tile.setNumOfEmptyCellsInFront(count); shiftTiles(tile); } } } break; case 2: for (int y = 1; y < 4; y++){ for (int x = 0; x < 4; x++){ if (getObjectsAt(x, y, Tile.class).isEmpty()) continue; Tile tile = (Tile) getObjectsAt(x, y, Tile.class).get(0); int count = 0; for (int i = y - 1; i > -1; i--){ if (!getObjectsAt(x, i, Tile.class).isEmpty()) continue; count++; } if (count != 0){ tile.setNumOfEmptyCellsInFront(count); shiftTiles(tile); } } } break; case 3: for (int y = 2; y > -1; y--){ for (int x = 0; x < 4; x++){ if (getObjectsAt(x, y, Tile.class).isEmpty()) continue; Tile tile = (Tile) getObjectsAt(x, y, Tile.class).get(0); int count = 0; for (int i = y + 1; i < 4; i++){ if (!getObjectsAt(x, i, Tile.class).isEmpty()) continue; count++; } if (count != 0){ tile.setNumOfEmptyCellsInFront(count); shiftTiles(tile); } } } break; default: break; } } public void shiftTiles(Tile tile){ switch(direction){ case 0: if (tile != null){ tile.setLocation(tile.getX() - tile.getNumOfEmptyCellsInFront(), tile.getY()); } break; case 1: if (tile != null){ tile.setLocation(tile.getX() + tile.getNumOfEmptyCellsInFront(), tile.getY()); } break; case 2: if (tile != null){ tile.setLocation(tile.getX(), tile.getY() - tile.getNumOfEmptyCellsInFront()); } break; case 3: if (tile != null){ tile.setLocation(tile.getX(), tile.getY() + tile.getNumOfEmptyCellsInFront()); } break; default: break; } resetDidCombine(); addNewTile(); } public void resetDidCombine(){ for (int x = 0; x < 4; x++){ for (int y = 0; y < 4; y++){ if (getObjectsAt(x, y, Tile.class).isEmpty()) continue; Tile temp = (Tile) getObjectsAt(x, y, Tile.class).get(0); temp.setDidCombine(false); } } } public void addNewTile(){ int x, y; do { x = Greenfoot.getRandomNumber(4); y = Greenfoot.getRandomNumber(4); if (!getObjectsAt(x, y, Tile.class).isEmpty()) continue; addObject(new Tile(), x, y); break; } while(false); } public void pln(String s){ System.out.println(s); } public void p(String s){ System.out.print(s); } }