Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. package aasasass;
  2.  
  3. public class Robot {
  4.  
  5.     public static void main(String[] args)
  6.     {
  7.         int robot_x= 0;
  8.         int robot_y= 0;
  9.         String[] lines = SimpleIO.readTextFile("src/level.txt");
  10.         int[][] level =createLevel(lines);
  11.        
  12.         Draw.init(level[0].length*20, level.length*20 );
  13.         Draw.enableDoubleBuffering( true );
  14.         Draw.setFps(10);
  15.         Draw.setBackgroundColor( 128, 128, 128 );
  16.         int direction_x = 1;
  17.         int direction_y = 1;
  18.        
  19.         while( true )
  20.         {
  21.             direction_x = 1;
  22.             direction_y = 1;
  23.             Draw.clearScreen();
  24.             drawLevel(level, robot_x, robot_y);
  25.             Draw.syncToFrameRate();
  26.            
  27.             if (isPositionValid(level,robot_x+direction_x,robot_y+direction_y))
  28.             {
  29.                 level[robot_y][robot_x]++;
  30.                 robot_y+=direction_y;
  31.                 robot_x+=direction_x;
  32.             }
  33.             else
  34.             {
  35.                 direction_x = randomVelocity();
  36.                 direction_y = randomVelocity();
  37.                
  38.                 if (isDirectionValid(direction_x, direction_y))
  39.                  {
  40.                     if (isPositionValid(level,robot_x+direction_x,robot_y+direction_y))
  41.                     {
  42.                         level[robot_y][robot_x]++;
  43.                         robot_y+=direction_y;
  44.                         robot_x+=direction_x;
  45.                     }
  46.                     //if the position is invalid the direction is changed randomly
  47.                      
  48.                  }
  49.             }
  50.                 //Animationloop
  51.        
  52.             direction_x = randomVelocity();
  53.             direction_y = randomVelocity();
  54.         }
  55.     }
  56.     public static int[][] createLevel(String[] lines)
  57.     {
  58.         String stringlength = lines[0];
  59.         int[][] seperate = new int [lines.length][stringlength.length()];
  60.         for( int i = 0; i < lines.length; i++)
  61.         {
  62.             String tmp = lines[i];
  63.            
  64.             for( int j = 0; j < tmp.length(); j++)
  65.             {
  66.                 char tmp2 = tmp.charAt(j);
  67.                
  68.                 if (tmp2=='.')
  69.                 {
  70.                     seperate[i][j]= 0;
  71.                 }
  72.                 else
  73.                 {
  74.                     seperate[i][j]= -1;
  75.                 }
  76.             }
  77.         }
  78.         return seperate;
  79.         //replaces "." with 0 and "X" with -1.
  80.     }
  81.     public static void drawLevel (int[][] level, int robot_x, int robot_y)
  82.     {
  83.         for(int i = 0; i<level[0].length; i++)
  84.         {
  85.             for(int j = 0; j<level.length; j++)
  86.             {
  87.                 if(level[j][i]<0)
  88.                 {
  89.                     Draw.setColor(255, 255, 255);
  90.                     Draw.filledRect(20*i, 20*j, 20, 20);
  91.                 }
  92.                 if(level[j][i]==1)
  93.                 {
  94.                     Draw.setColor(100, 0, 0);
  95.                     Draw.filledRect(i*20, 20*j, 20, 20);
  96.                 }
  97.                 if(level[j][i]==2)
  98.                 {
  99.                     Draw.setColor(150, 0, 0);
  100.                     Draw.filledRect(i*20, 20*j, 20, 20);
  101.                 }
  102.                 if(level[j][i]>=3)
  103.                 {
  104.                     Draw.setColor(225, 0, 0);
  105.                     Draw.filledRect(i*20, 20*j, 20, 20);
  106.                 }
  107.             }
  108.         }
  109.         // White symbolizes the wall, red symbolizes a path, where the robot has been
  110.         Draw.setColor(0, 204, 0);
  111.         Draw.filledRect(robot_x*20,robot_y*20, 20, 20);
  112.         // creating the robot
  113.     }
  114.     public static boolean isPositionValid (int[][] level, int x, int y)
  115.     {
  116.         if(x >= 0 && x <= level[0].length-1)
  117.         {
  118.             if(y >= 0 && y <= level.length-1)
  119.             {
  120.                 if(level[y][x] >= 0)
  121.                 {
  122.                 return true;
  123.                 }
  124.             }
  125.         }
  126.         return false;
  127.         //Checks if the is not a wall or the end of the level
  128.     }
  129.     public static int randomVelocity()
  130.     {
  131.         int rdm = (int) (Math.random()*2)-1;
  132.         return rdm;
  133.         //random directon for the robot
  134.     }
  135.     public static boolean isDirectionValid( int direction_x, int direction_y )
  136.     {
  137.         if (direction_x !=0 || direction_y != 0)
  138.         {
  139.             return true;
  140.         }
  141.        
  142.         return false;
  143.         //if both numbers were zero, it would not move
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement