Friday, June 27, 2008

Code post testing in different plugins

   1: public class SpiralWalking{
   2: public int totalPoints(String[] levelMap){
   3:       int[] pos={0,0};
   4:       int dim=0;
   5:       int ymax=levelMap.length;
   6:       int xmax=levelMap[0].length();
   7:       int cells=(ymax)*(xmax);
   8:       int[] size={xmax,ymax};
   9:       boolean isForward=true;
  10:       boolean[][] isDone=new boolean[xmax][ymax]; 
  11:       int score=0;
  12:       isDone[0][0]=true;
  13:       int modCounter=0;
  14:      for(int count=1;count<cells;count++){
  15:           if(pos[dim]==size[dim]-1 || (pos[dim]==0 && count!=1)
  16:                   || ((isForward && dim==0 && isDone[pos[0]+1][pos[1]]) 
  17:                           || (isForward && dim==1 && isDone[pos[0]][pos[1]+1]))
  18:                                   || ((!isForward && dim==0 && isDone[pos[0]-1][pos[1]]) 
  19:                                           || (!isForward && dim==1 && isDone[pos[0]][pos[1]-1])))  {
  20:           if(++modCounter%2==0) isForward=!isForward;
  21:           dim=++dim%2;
  22:           if(isForward)pos[dim]++; else pos[dim]--;
  23:           isDone[pos[0]][pos[1]]=true;
  24:           }else {
  25:               score+=Integer.parseInt(String.valueOf(levelMap[pos[1]].charAt(pos[0])));
  26:               if(isForward)pos[dim]++; else pos[dim]--;
  27:               isDone[pos[0]][pos[1]]=true;
  28:           }
  29:       }
  30:       score+=Integer.parseInt(String.valueOf(levelMap[pos[1]].charAt(pos[0])));
  31:       return score;
  32: }
  33:  
  34: public static void main(String z[]){
  35:     String[] inp={"86850","76439",
  36:             "15863",
  37:             "24568",
  38:             "45679",
  39:             "71452",
  40:             "05483"
  41:             };
  42:     System.out.println(new SpiralWalking().totalPoints(inp));
  43: }
  44: }

 

public class SpiralWalking{
public int totalPoints(String[] levelMap){
      int[] pos={0,0};
      int dim=0;
      int ymax=levelMap.length;
      int xmax=levelMap[0].length();
      int cells=(ymax)*(xmax);
      int[] size={xmax,ymax};
      boolean isForward=true;
      boolean[][] isDone=new boolean[xmax][ymax]; 
      int score=0;
      isDone[0][0]=true;
      int modCounter=0;
     for(int count=1;count<cells;count++){
          if(pos[dim]==size[dim]-1 || (pos[dim]==0 && count!=1)
                  || ((isForward && dim==0 && isDone[pos[0]+1][pos[1]]) 
                          || (isForward && dim==1 && isDone[pos[0]][pos[1]+1]))
                                  || ((!isForward && dim==0 && isDone[pos[0]-1][pos[1]]) 
                                          || (!isForward && dim==1 && isDone[pos[0]][pos[1]-1])))  {
          if(++modCounter%2==0) isForward=!isForward;
          dim=++dim%2;
          if(isForward)pos[dim]++; else pos[dim]--;
          isDone[pos[0]][pos[1]]=true;
          }else {
              score+=Integer.parseInt(String.valueOf(levelMap[pos[1]].charAt(pos[0])));
              if(isForward)pos[dim]++; else pos[dim]--;
              isDone[pos[0]][pos[1]]=true;
          }
      }
      score+=Integer.parseInt(String.valueOf(levelMap[pos[1]].charAt(pos[0])));
      return score;
}
 
public static void main(String z[]){
    String[] inp={"86850","76439",
            "15863",
            "24568",
            "45679",
            "71452",
            "05483"
            };
    System.out.println(new SpiralWalking().totalPoints(inp));
}
}
blog comments powered by Disqus

Labels