Subversion Repositories sokoban

Rev

Blame | Last modification | View Log | RSS feed

  1. package gdi.ws0809.test;
  2.  
  3. import gdi1sokoban.exceptions.InternalFailureException;
  4. import gdi1sokoban.exceptions.ParameterOutOfRangeException;
  5. import gdi1sokoban.exceptions.ParseException;
  6. import gdi1sokoban.main.Sokoban;
  7.  
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.net.URISyntaxException;
  11.  
  12.  
  13. /*============================================================================*/
  14. /**
  15.  * Provide an implementation for SokobanTest in this class. Your game must
  16.  * compile and run without this class. This class is just meant to adapt the
  17.  * test cases to your implementation.
  18.  *
  19.  * @author daniel
  20.  *
  21.  */
  22. public class SokobanTestAdapter implements SokobanTest {
  23.         private String playerName = "";
  24.  
  25.         public static void init() {
  26.                 Sokoban.startEngine(null);
  27.                 try {
  28.                         Sokoban.getEngine().newGame();
  29.                 } catch (final IOException e) {
  30.                         e.printStackTrace();
  31.                 } catch (final ParameterOutOfRangeException e) {
  32.                         e.printStackTrace();
  33.                 } catch (final InternalFailureException e) {
  34.                         e.printStackTrace();
  35.                 } catch (final URISyntaxException e) {
  36.                         e.printStackTrace();
  37.                 } catch (ParseException e) {
  38.                         e.printStackTrace();
  39.                 }
  40.         }
  41.  
  42.  
  43.         public String currentLevelToString()
  44.         {
  45.                 return Sokoban.getEngine().getGameLevel().toString();
  46.         }
  47.  
  48.         public void loadLevel(final File lvl) throws IOException, ParameterOutOfRangeException, InternalFailureException, ParseException, URISyntaxException
  49.         {
  50.                 Sokoban.getEngine().newGame(lvl);
  51.         }
  52.        
  53.         public boolean isSolved()
  54.         {
  55.                 return Sokoban.getEngine().getGameLevel().isSolved();
  56.         }
  57.  
  58.         public void moveWorker(final char direction)
  59.         {
  60.                 try {
  61.                         Sokoban.getEngine().roundEnd(Sokoban.getEngine().movePlayer(direction), playerName);
  62.                 } catch (final ParameterOutOfRangeException e) {
  63.                         System.err.println("Fehler: Ungültige Levelkoordinaten!");
  64.                 }
  65.         }
  66.  
  67.         public void setLevelDir(final File levelDir)
  68.         {
  69.                 try {
  70.                         Sokoban.getEngine().setLevelDir(levelDir, 10);
  71.                 } catch (final InternalFailureException e) {
  72.                         e.printStackTrace();
  73.                 }
  74.         }
  75.  
  76.         public void startNextLevel() throws Exception
  77.         {
  78.                 Sokoban.getEngine().newGame(Sokoban.getEngine().getCurrentLevelNr()+1);
  79.         }
  80.        
  81.  
  82.         public int getStepsInCurrentLevel()
  83.         {
  84.                 return Sokoban.getEngine().getGameLevel().getSteps();
  85.         }
  86.  
  87.         public void writeHighScoreFile()
  88.         {
  89.                 try {
  90.                         Sokoban.getEngine().getHighscores().saveHighscores();
  91.                 } catch (final InternalFailureException e) {
  92.                         e.printStackTrace();
  93.                 }
  94.         }
  95.  
  96.         public void setPlayerName(final String name)
  97.         {
  98.                 playerName = name;
  99.         }
  100.  
  101.         public void redoLastUndoneMove() throws Exception
  102.         {
  103.                 Sokoban.getEngine().redo();
  104.         }
  105.  
  106.         public void undoLastMove() throws Exception
  107.         {
  108.                 Sokoban.getEngine().undo();
  109.         }
  110.  
  111.         public void loadGame(final File saveFile)
  112.         {
  113.                 Sokoban.getEngine().loadGame(saveFile);
  114.         }
  115.  
  116.         public void saveGame(final File f)
  117.         {
  118.                 Sokoban.getEngine().saveGame(f);
  119.         }
  120.  
  121.         public boolean isDeadlock()
  122.         {
  123.                 try {
  124.                         return  Sokoban.getEngine().getGameLevel().hasDeadlock();
  125.                 } catch (final ParameterOutOfRangeException e) {
  126.                         System.err.println("Fehler: Ungültige Levelkoordinaten!");
  127.                         return false;
  128.                 }
  129.         }
  130.  
  131.         public boolean canMoveCrate(final int i, final int j, final char c)
  132.         {
  133.                 int destTop = j, destLeft = i;
  134.                 switch(c){
  135.                 case 'R':
  136.                         destLeft++;
  137.                 case 'L':
  138.                         destLeft--;
  139.                 case 'U':
  140.                         destTop--;
  141.                 case 'D':
  142.                         destTop++;
  143.                 }
  144.                 try {
  145.                         return Sokoban.getEngine().getGameLevel().setPosition(j, i, destTop, destLeft);
  146.                 } catch (final ParameterOutOfRangeException e) {
  147.                         System.err.println("Fehler: Ungültige Levelkoordinaten!");
  148.                 }
  149.                 return false;
  150.         }
  151.  
  152.         public boolean createHighscoreEntry(final String playername, final int i, final int j, final int k)
  153.         {
  154.                 if (Sokoban.getEngine().getHighscores().isHighscore(i, j, k)) {
  155.                         Sokoban.getEngine().getHighscores().addHighscore(i, playername, String.valueOf(j), String.valueOf(k));
  156.                         return true;
  157.                 } else
  158.                         return false;
  159.         }
  160.  
  161.         public String getBestPlayerName()
  162.         {
  163.                 final String highscoreData = Sokoban.getEngine().getHighscores().toString();
  164.                 final String[] hsRows = highscoreData.split("\n");
  165.                 final String[] hsFirstRow = hsRows[0].split("\t");
  166.                 return hsFirstRow[1];
  167.         }
  168.  
  169.         public int getCrateCount()
  170.         {
  171.                 return Sokoban.getEngine().getGameLevel().countCrates();
  172.         }
  173.  
  174.         public int getGoalCount()
  175.         {
  176.                 return Sokoban.getEngine().getGameLevel().countGoals();
  177.         }
  178.  
  179.         public int getHighscoreCount()
  180.         {
  181.                 return Sokoban.getEngine().getHighscores().getAllHighscoresCount();
  182.         }
  183.  
  184.         public int getLevelHeight()
  185.         {
  186.                 return Sokoban.getEngine().getGameLevel().getHeight();
  187.         }
  188.  
  189.         public int getLevelWidth()
  190.         {
  191.                 return Sokoban.getEngine().getGameLevel().getWidth();
  192.         }
  193.  
  194.         public int getWallCount()
  195.         {
  196.                 return Sokoban.getEngine().getGameLevel().countWalls();
  197.         }
  198.  
  199.         public int getWorkerPositionX()
  200.         {
  201.                 try {
  202.                         return Sokoban.getEngine().getGameLevel().getPlayerPos().left;
  203.                 } catch (final ParameterOutOfRangeException e) {
  204.                         System.err.println("Fehler: Ungültige Levelkoordinaten!");
  205.                         return -1;
  206.                 }
  207.         }
  208.  
  209.         public int getWorkerPositionY()
  210.         {
  211.                 try {
  212.                         return Sokoban.getEngine().getGameLevel().getPlayerPos().top;
  213.                 } catch (final ParameterOutOfRangeException e) {
  214.                         System.err.println("Fehler: Ungültige Levelkoordinaten!");
  215.                         return -1;
  216.                 }
  217.         }
  218.  
  219.         public boolean isCrateAt(final int i, final int j)
  220.         {
  221.                 try {
  222.                         return Sokoban.getEngine().getGameLevel().isCrate(j, i);
  223.                 } catch (final ParameterOutOfRangeException e) {
  224.                         System.err.println("Fehler: Ungültige Levelkoordinaten!");
  225.                         return false;
  226.                 }
  227.         }
  228.  
  229.         public boolean isGoalAt(final int i, final int j)
  230.         {
  231.                 try {
  232.                         return Sokoban.getEngine().getGameLevel().isGoal(j, i);
  233.                 } catch (final ParameterOutOfRangeException e) {
  234.                         System.err.println("Fehler: Ungültige Levelkoordinaten!");
  235.                         return false;
  236.                 }
  237.         }
  238.  
  239.         public boolean isWallAt(final int i, final int j)
  240.         {
  241.                 try {
  242.                         return Sokoban.getEngine().getGameLevel().isWall(j, i);
  243.                 } catch (final ParameterOutOfRangeException e) {
  244.                         System.err.println("Fehler: Ungültige Levelkoordinaten!");
  245.                         return false;
  246.                 }
  247.         }
  248.  
  249.         public void clearHighscoreList()
  250.         {
  251.                 Sokoban.getEngine().getHighscores().resetHighscores();
  252.         }
  253.  
  254.         public void moveWorkerSequence(final String moves)
  255.         {
  256.                 for(final char m : moves.toCharArray())
  257.                         try {
  258.                                 Sokoban.getEngine().movePlayer(m);
  259.                         } catch (final ParameterOutOfRangeException e) {
  260.                                 System.err.println("Fehler: Ungültige Levelkoordinaten!");
  261.                         }
  262.         }
  263.  
  264.  
  265.         public boolean checkWallBounding() {
  266.                 try {
  267.                         return Sokoban.getEngine().getGameLevel().checkWallBounding();
  268.                 } catch (final ParameterOutOfRangeException e) {
  269.                         System.err.println("Fehler: Ungültige Levelkoordinaten!");
  270.                         return false;
  271.                 }
  272.         }
  273.  
  274.  
  275.         public String solveLevel(final int maxTime) {
  276.                 try {
  277.                         return Sokoban.getEngine().solve(maxTime);
  278.                 } catch (final ParameterOutOfRangeException e) {
  279.                         System.err.println("Fehler: Ungültige Levelkoordinaten!");
  280.                         return "";
  281.                 }
  282.         }
  283.  
  284. }