Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aasasass;
- public class Robot {
- public static void main(String[] args)
- {
- int robot_x= 0;
- int robot_y= 0;
- String[] lines = SimpleIO.readTextFile("src/level.txt");
- int[][] level =createLevel(lines);
- Draw.init(level[0].length*20, level.length*20 );
- Draw.enableDoubleBuffering( true );
- Draw.setFps(10);
- Draw.setBackgroundColor( 128, 128, 128 );
- int direction_x = 1;
- int direction_y = 1;
- while( true )
- {
- direction_x = 1;
- direction_y = 1;
- Draw.clearScreen();
- drawLevel(level, robot_x, robot_y);
- Draw.syncToFrameRate();
- if (isPositionValid(level,robot_x+direction_x,robot_y+direction_y))
- {
- level[robot_y][robot_x]++;
- robot_y+=direction_y;
- robot_x+=direction_x;
- }
- else
- {
- direction_x = randomVelocity();
- direction_y = randomVelocity();
- if (isDirectionValid(direction_x, direction_y))
- {
- if (isPositionValid(level,robot_x+direction_x,robot_y+direction_y))
- {
- level[robot_y][robot_x]++;
- robot_y+=direction_y;
- robot_x+=direction_x;
- }
- //if the position is invalid the direction is changed randomly
- }
- }
- //Animationloop
- direction_x = randomVelocity();
- direction_y = randomVelocity();
- }
- }
- public static int[][] createLevel(String[] lines)
- {
- String stringlength = lines[0];
- int[][] seperate = new int [lines.length][stringlength.length()];
- for( int i = 0; i < lines.length; i++)
- {
- String tmp = lines[i];
- for( int j = 0; j < tmp.length(); j++)
- {
- char tmp2 = tmp.charAt(j);
- if (tmp2=='.')
- {
- seperate[i][j]= 0;
- }
- else
- {
- seperate[i][j]= -1;
- }
- }
- }
- return seperate;
- //replaces "." with 0 and "X" with -1.
- }
- public static void drawLevel (int[][] level, int robot_x, int robot_y)
- {
- for(int i = 0; i<level[0].length; i++)
- {
- for(int j = 0; j<level.length; j++)
- {
- if(level[j][i]<0)
- {
- Draw.setColor(255, 255, 255);
- Draw.filledRect(20*i, 20*j, 20, 20);
- }
- if(level[j][i]==1)
- {
- Draw.setColor(100, 0, 0);
- Draw.filledRect(i*20, 20*j, 20, 20);
- }
- if(level[j][i]==2)
- {
- Draw.setColor(150, 0, 0);
- Draw.filledRect(i*20, 20*j, 20, 20);
- }
- if(level[j][i]>=3)
- {
- Draw.setColor(225, 0, 0);
- Draw.filledRect(i*20, 20*j, 20, 20);
- }
- }
- }
- // White symbolizes the wall, red symbolizes a path, where the robot has been
- Draw.setColor(0, 204, 0);
- Draw.filledRect(robot_x*20,robot_y*20, 20, 20);
- // creating the robot
- }
- public static boolean isPositionValid (int[][] level, int x, int y)
- {
- if(x >= 0 && x <= level[0].length-1)
- {
- if(y >= 0 && y <= level.length-1)
- {
- if(level[y][x] >= 0)
- {
- return true;
- }
- }
- }
- return false;
- //Checks if the is not a wall or the end of the level
- }
- public static int randomVelocity()
- {
- int rdm = (int) (Math.random()*2)-1;
- return rdm;
- //random directon for the robot
- }
- public static boolean isDirectionValid( int direction_x, int direction_y )
- {
- if (direction_x !=0 || direction_y != 0)
- {
- return true;
- }
- return false;
- //if both numbers were zero, it would not move
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement