Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <unistd.h>
- #define DIM 16
- #define WALL '#'
- #define UP 1
- #define RIGHT 2
- #define DOWN 3
- #define LEFT 4
- #define VICTORY '@'
- int victory = 0;
- char LEVEL [] [16] = {
- {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', '#', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
- {'#', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', '#', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', '#', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', '#', '#', '#', ' ', '#'},
- {'@', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'@', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'@', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}
- };
- typedef struct{
- int x;
- int y;
- int last_direction;
- }ROBOT;
- ROBOT robot;
- void stampaMatrice(){
- int i,j;
- for(i=0;i<DIM;i++){
- for(j=0;j<DIM;j++){
- printf("%c",LEVEL[i][j]);
- }
- printf("\n");
- }
- }
- //Posiziona il robot
- void setPosition(int x,int y){
- if (LEVEL[y][x] == VICTORY)
- victory = 1;
- LEVEL[robot.y][robot.x] = ' ';
- robot.x = x;
- robot.y = y;
- LEVEL[robot.y][robot.x] = 'R';
- }
- int longestWay(int steps[]){
- int i,max1,max2,ind_max1,ind_max2;
- /*
- Setto max1 e max2 considerando i primi due
- elementi dell'array
- */
- if (steps[UP] >= steps[RIGHT]){
- max1 = steps[UP];
- max2 = steps[RIGHT];
- ind_max1 = UP;
- ind_max2 = RIGHT;
- }
- else{
- max1 = steps[RIGHT];
- max2 = steps[UP];
- ind_max1 = RIGHT;
- ind_max2 = UP;
- }
- for(i=3;i<5;i++){
- /*Se trovo un numero maggiore di max1*/
- if (steps[i] > max1){
- max2 = max1;
- ind_max2 = ind_max1;
- max1 = steps[i];
- ind_max1 = i;
- }
- /*Se trovo un numero maggio di max2 ma minore di max1*/
- else if(steps[i] > max2){
- max2 = steps[i];
- ind_max2 = i;
- }
- }
- if(max1 != max2){
- return ind_max1;
- }
- if(rand()%2 == 0)
- return ind_max1;
- else
- return ind_max2;
- }
- void randomMove(){
- int direction;
- int next_step;
- int flag;
- do{
- flag = 0;
- direction = rand()%4+1;
- printf("%d\n",direction);
- switch(direction){
- case UP:
- if(robot.y == 1 && LEVEL[0][robot.x] != VICTORY)
- break;
- next_step = LEVEL[robot.y-1][robot.x];
- flag = 1;
- if(next_step == WALL || robot.last_direction == DOWN)
- break;
- else{
- setPosition(robot.x,robot.y-1);
- robot.last_direction = direction;
- }
- break;
- case RIGHT:
- if(robot.x == DIM-1 && LEVEL[robot.y][DIM] != VICTORY)
- break;
- next_step = LEVEL[robot.y][robot.x+1];
- flag = 1;
- if(next_step == WALL || robot.last_direction == LEFT)
- break;
- else{
- setPosition(robot.x+1,robot.y);
- robot.last_direction = direction;
- }
- break;
- case DOWN:
- if(robot.y == DIM-1 && LEVEL[DIM][robot.x] != VICTORY)
- break;
- next_step = LEVEL[robot.y+1][robot.x];
- flag = 1;
- if(next_step == WALL || robot.last_direction == UP)
- break;
- else{
- setPosition(robot.x,robot.y+1);
- robot.last_direction = direction;
- }
- break;
- case LEFT:
- if(robot.x == 1 && LEVEL[robot.y][0] != VICTORY)
- break;
- next_step = LEVEL[robot.y][robot.x-1];
- flag = 1;
- if(next_step == WALL || robot.last_direction == RIGHT)
- break;
- else{
- setPosition(robot.x-1,robot.y);
- robot.last_direction = direction;
- }
- break;
- }
- }while(next_step == WALL || flag==0);
- }
- void longMove(){
- int x,y,direction;
- int step[5] = {0};
- //UP
- x = robot.x;
- y = robot.y;
- if(robot.last_direction == DOWN)
- step[UP] = -1;
- else{
- while(LEVEL[y][x] != WALL){
- step[UP]++;
- y--;
- }
- }
- //RIGHT
- x = robot.x;
- y = robot.y;
- if(robot.last_direction == LEFT)
- step[RIGHT] = -1;
- else{
- while(LEVEL[y][x] != WALL){
- step[RIGHT]++;
- x++;
- }
- }
- //DOWN
- x = robot.x;
- y = robot.y;
- if(robot.last_direction == UP)
- step[DOWN] = -1;
- else{
- while(LEVEL[y][x] != WALL){
- step[DOWN]++;
- y++;
- }
- }
- //LEFT
- x = robot.x;
- y = robot.y;
- if(robot.last_direction == RIGHT)
- step[LEFT] = -1;
- else{
- while(LEVEL[y][x] != WALL){
- step[LEFT]++;
- x--;
- }
- }
- direction = longestWay(step);
- switch(direction){
- case UP:
- setPosition(robot.x,robot.y-1);
- break;
- case RIGHT:
- setPosition(robot.x+1,robot.y);
- break;
- case DOWN:
- setPosition(robot.x,robot.y+1);
- break;
- case LEFT:
- setPosition(robot.x-1,robot.y);
- break;
- }
- }
- int main(){
- srand((unsigned int)time(0));
- //Setup
- setPosition(2,1);
- int l;
- int cont = 0;
- stampaMatrice();
- do{
- l = rand()%100+1;
- // Legge 30%
- if(l <= 30){
- randomMove();
- }
- // Legge 70%
- else{
- longMove();
- }
- system("clear");
- stampaMatrice();
- //sleep(1);
- cont ++;
- }while(victory == 0);
- printf("Tot. mosse = %d\n", cont);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement