Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #define BOLA 1
- #define PAREDE 2
- #define OBJETIVO 3
- void usleep(int);
- typedef struct{
- int tam;
- char bola;
- char parede;
- char objetivo;
- } config_;
- int readIntInRange(int a,int b){
- int x;
- while(a<=b){
- scanf("%d",&x);
- while(getchar()!='\n');
- if(x>=a && x<=b)return x;
- printf("Digite um valor valido (%d>=x>=%d)\n",a,b);
- }
- return 0;
- }
- void preencheFase(int* fase,char* str){
- for(int i=0;str[i];i++){
- if(str[i]==' ')fase[i]=0;
- if(str[i]=='o')fase[i]=BOLA;
- if(str[i]=='#')fase[i]=PAREDE;
- if(str[i]=='0')fase[i]=OBJETIVO;
- }
- }
- void printFase(int* fase,int dir,config_ config){
- if(dir==0){//w
- for(int y=0;y<(7+5*(config.tam));y++){
- for(int x=0;x<(7+5*(config.tam));x++){
- if(!(x+y))printf("▓▓");
- else if(fase[x+((7+5*(config.tam))*y)]==BOLA) printf("%c ",config.bola);
- else if(fase[x+((7+5*(config.tam))*y)]==PAREDE) printf("%c%c",config.parede,config.parede);
- else if(fase[x+((7+5*(config.tam))*y)]==OBJETIVO)printf("%c ",config.objetivo);
- else printf(" ");
- // printf("%d", fase[x+((7+5*(config.tam))*y)]);
- }
- printf("\n");
- }
- }
- if(dir==1){//a
- for(int x=(7+5*(config.tam))-1;x>=0;x--){
- for(int y=0;y<(7+5*(config.tam));y++){
- if(!(x+y))printf("▓▓");
- else if(fase[x+((7+5*(config.tam))*y)]==BOLA) printf("%c ",config.bola);
- else if(fase[x+((7+5*(config.tam))*y)]==PAREDE) printf("%c%c",config.parede,config.parede);
- else if(fase[x+((7+5*(config.tam))*y)]==OBJETIVO)printf("%c ",config.objetivo);
- else printf(" ");
- }
- printf("\n");
- }
- }
- if(dir==2){//s
- for(int y=(7+5*(config.tam))-1;y>=0;y--){
- for(int x=(7+5*(config.tam))-1;x>=0;x--){
- if(!(x+y))printf("▓▓");
- else if(fase[x+((7+5*(config.tam))*y)]==BOLA) printf("%c ",config.bola);
- else if(fase[x+((7+5*(config.tam))*y)]==PAREDE) printf("%c%c",config.parede,config.parede);
- else if(fase[x+((7+5*(config.tam))*y)]==OBJETIVO)printf("%c ",config.objetivo);
- else printf(" ");
- }
- printf("\n");
- }
- }
- if(dir==3){//d
- for(int x=0;x<(7+5*(config.tam));x++){
- for(int y=(7+5*(config.tam))-1;y>=0;y--){
- if(!(x+y))printf("▓▓");
- else if(fase[x+((7+5*(config.tam))*y)]==BOLA) printf("%c ",config.bola);
- else if(fase[x+((7+5*(config.tam))*y)]==PAREDE) printf("%c%c",config.parede,config.parede);
- else if(fase[x+((7+5*(config.tam))*y)]==OBJETIVO)printf("%c ",config.objetivo);
- else printf(" ");
- }
- printf("\n");
- }
- }
- }
- void moveBola(int* fase,int dir,config_ config,int* x,int* y){
- //printf("x%d,y%d\n",*x,*y);
- //printf("fase x,y+1 = %d\n",fase[(*x)+((7+5*config.tam)*((*y)+1))]);
- system("clear");
- printFase(fase,dir,config);
- printf("Digite 'a' para virar para a esquerda ou 'd' para virar para a direita ou 'q' para sair.\n");
- usleep(500000);
- if(dir==0 && fase[(*x)+((7+5*config.tam)*((*y)+1))]!=PAREDE){
- fase[(*x)+((7+5*config.tam)*(*y))] = 0;
- (*y)++;
- }
- else if(dir==1 && fase[((*x)-1)+((7+5*config.tam)*(*y))]!=PAREDE){
- fase[(*x)+((7+5*config.tam)*(*y))] = 0;
- (*x)--;
- }
- else if(dir==2 && fase[(*x)+((7+5*config.tam)*((*y)-1))]!=PAREDE){
- fase[(*x)+((7+5*config.tam)*(*y))] = 0;
- (*y)--;
- }
- else if(dir==3 && fase[((*x)+1)+((7+5*config.tam)*(*y))]!=PAREDE){
- fase[(*x)+((7+5*config.tam)*(*y))] = 0;
- (*x)++;
- }
- else return;
- if(fase[(*x)+((7+5*config.tam)*(*y))] == OBJETIVO)return;
- fase[(*x)+((7+5*config.tam)*(*y))] = BOLA;
- moveBola(fase,dir,config,x,y);
- }
- void jogar(config_ config){
- int fase[(7+5*(config.tam))*(7+5*(config.tam))],x,y,dir=0;
- char c;
- if(config.tam==1){
- preencheFase(fase,"#############o# # ## # # ## # ### ## ## 0 ## # # ## ## # ## #############");
- x=1;
- y=1;
- //############
- //#o# # #
- //# # # #
- //# # #
- //## #
- //# #
- //# 0 #
- //# # # #
- //# #
- //# # #
- //# #
- //############
- }
- if(config.tam==2){
- preencheFase(fase,"################## # # ## # # ### # ### # ## # # # ##o ## # # # ## ## # # ## # # ## # # ## # ## # ## # ## #0 ##################");
- x=1;
- y=6;
- //#################
- //# # # #
- //# # # ##
- //# # #
- //## # #
- //# # # # #
- //#o #
- //# # # # #
- //# #
- //# # # #
- //# # # #
- //# # # #
- //# # #
- //# # #
- //# # #
- //# #0 #
- //#################
- }
- if(config.tam==3){
- preencheFase(fase,"####################### # # ## # # ### # ## # ## # ## # # ### # ## # # # ##o ## # # ## # # # ## ## # # ## # # ## # ## # # ## # ## # ## # ## #0 #######################");
- x=1;
- y=9;
- //######################
- //# # # #
- //# # # ##
- //# # #
- //# # #
- //# # #
- //# # # #
- //## # #
- //# # # # #
- //#o #
- //# # # #
- //# # # # #
- //# #
- //# # # #
- //# # # #
- //# # #
- //# # # #
- //# # #
- //# # #
- //# # #
- //# #0 #
- //######################
- }
- moveBola(fase,dir,config,&x,&y);
- while(fase[x+((7+5*config.tam) * y)]!=OBJETIVO){
- system("clear");
- //printf("x=%d y=%d\n",x,y);
- printFase(fase,dir,config);
- printf("Digite 'a' para virar para a esquerda ou 'd' para virar para a direita ou 'q' para sair.");
- do{
- c = getchar();
- while(getchar()!='\n');
- }while(c!='a' && c!='d' && c!='A' && c!='D' && c!='q' && c!='Q');
- if(c=='a' || c=='A')dir= (dir+1)%4;
- if(c=='d' || c=='D')dir= (dir+3)%4;
- if(c=='q' || c=='Q')return;
- moveBola(fase,dir,config,&x,&y);
- }
- system("clear");
- printFase(fase,dir,config);
- printf("Batata, enter pra voltar.\n");
- while(getchar()!='\n');
- }
- void configura(config_ *config){
- int x;
- do{
- system("clear");
- printf("1 - tamanho = %d(%dx%d)\n",config->tam,5+5*(config->tam),5+5*(config->tam));
- printf("2 - bola = '%c'\n",config->bola);
- printf("3 - parede = '%c'\n",config->parede);
- printf("4 - objetivo = '%c'\n",config->objetivo);
- printf("0 - sair\n");
- x=readIntInRange(0,4);
- if(x==1)config->tam = readIntInRange(1,3);
- if(x==2)do{
- config->bola = getchar();
- while(getchar()!='\n');
- }while(config->bola=='\n' || config->bola==' ' || config->bola==config->parede || config->bola==config->objetivo);
- if(x==3)do{
- config->parede = getchar();
- while(getchar()!='\n');
- }while(config->parede=='\n' || config->parede==' ' || config->parede==config->bola || config->parede==config->objetivo);
- if(x==4)do{
- config->objetivo = getchar();
- while(getchar()!='\n');
- }while(config->objetivo=='\n' || config->objetivo==' ' || config->objetivo==config->parede || config->objetivo==config->bola);
- }while(x);
- }
- int main(){
- int x;
- config_ config;
- config.tam=1;
- config.bola='o';
- config.parede='#';
- config.objetivo='0';
- do{
- system("clear");
- printf("╔═════════════════════════════════╗\n");
- printf("║MAZE RUNNER MAZE RUNNER MAZE RUNN║\n");
- printf("║ER MAZE RUNNER MAZE RUNNER MAZE R║\n");
- printf("║UNNE╔═════════════════════╗ER MAZ║\n");
- printf("║E RU║ 1 - Jogar ║RUNNER║\n");
- printf("║MAZE╚═════════════════════╝E RUNN║\n");
- printf("║ER MAZE RUNNER MAZE RUNNER MAZE R║\n");
- printf("║UNNE╔═════════════════════╗ER MAZ║\n");
- printf("║E RU║ 2 - Configurações ║RUNNER║\n");
- printf("║MAZE╚═════════════════════╝E RUNN║\n");
- printf("║ER MAZE RUNNER MAZE RUNNER MAZE R║\n");
- printf("║UNNE╔═════════════════════╗ER MAZ║\n");
- printf("║E RU║ 0 - Sair ║RUNNER║\n");
- printf("║MAZE╚═════════════════════╝E RUNN║\n");
- printf("║ER MAZE RUNNER MAZE RUNNER MAZE R║\n");
- printf("║UNNER MAZE RUNNER MAZE RUNNER MAZ║\n");
- printf("╚═════════════════════════════════╝\n");
- x=readIntInRange(0,2);
- if(x==1)jogar(config);
- if(x==2)configura(&config);
- }while(x);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement