Lebo inicializácia premennej (priradenie počiatočnej hodnoty) sa môže vyskytovať len spolu s jej definíciou (určenie typu, názvu, vyhradenie miesta v pamäti).
safiya ty sa neriadis podmienkou if(den==piatok.vecer) { zabava;oddych od skoly; } ? ..
A dig prvý krát vidím, že sa k členu štruktúry pristupuje cez bodku. U nás to bolo furt -> dynamicky cez smerník.
typedef enum BOOLEAN{ true=1,false=0 }Boolean;
http://stargate.cnl.tuke.sk/~mirek/pvjc/scenarios/09/index.html"Úloha: Vytvorte enumeračný typ Boolean, ktorý bude mať len dve hodnoty: TRUE (pravda, hodnota 1) a FALSE (nepravda, hodnota 0)."Hodnoty TRUE a FALSE sa však už nachádzajú (ako makrá) vo windef.h, ktorý je vložený kvôli windows.h a funkcii sleep().Takže buď sa nebude dať program na Windows skompilovať, alebo použijeme nejaké iné názvy (napr. YES a NO). Alebo windows.h vložíme až po našich definíciach typov, aj keď to nie je až také bežné.
enum BOOLEAN{ false, true };...BOOLEAN active = false;
#define MAX_HEIGHT 20#define MAX_WIDTH 20#define WALL -1#define EMPTY 0int width,height;int world[MAX_HEIGHT][MAX_WIDTH];int karel_x;int karel_y;int karel_direction;int karel_beepers = 20;void turnOn(){ int i,j; karel_x = 4; karel_y = 3; width=6; height=5; int newWorld[5][6]={ { 0, -1, -1, 0, 0, 0 }, { 0, -1, -1, 0, 0, 0 }, { 0, -1, -1, 0, 0, 0 }, { 0, -1, -1, 0, 0, 0 }, { 0, -1, -1, -1, -1, 0 } };karel_direction = 90;for(i=0;i<height;i++){for(j=0;j<width;j++)world[i][j]=newWorld[i][j];}}void draw(){ system("cls"); printf("pozicia: %d %d \n",karel_x, karel_y); printf("orientacia: karel je gay: "); if (karel_direction == 0) printf("juh\n"); if (karel_direction == 180) printf("sever\n"); if (karel_direction == 270) printf("zapad\n"); if (karel_direction == 90) printf("vychod\n"); printf("pocet beeprov v batohu: %d\n", karel_beepers); printf("tu je %d beeprov", world[karel_y][karel_x]); printf(" \norientacia %d \nclear? %c\nblocked? %c \n", karel_direction, frontIsClear(), frontIsBlocked()); printf("\n"); int i,j; for(i=0;i<height;i++){ for(j=0;j<width;j++){ if(karel_y==i && karel_x==j) { if (karel_direction==0) printf("v"); else if(karel_direction==90) printf(">"); else if(karel_direction==180) printf("^"); else if(karel_direction==270) printf("<"); } else { if (world[i][j] == WALL) printf("#"); if (world[i][j] == EMPTY) printf("."); if (world[i][j] > 0) printf("*"); } } printf("\n"); }sleep(1000);}void turnLeft(){ karel_direction += 90; karel_direction = karel_direction % 360; draw();} frontIsBlocked(){ int x = karel_x; int y = karel_y; if(karel_direction==0) x++ ; //v <-y-> if(karel_direction==180) x--; //^ x if(karel_direction==90) y++; //> | if(karel_direction==270) y--; //< v if (world[x][y] != EMPTY) return 'a'; else return 'n'; }frontIsClear(){ int x = karel_x; int y = karel_y; if(karel_direction==0) x++; // v else if(karel_direction==180) y++; // ^ else if(karel_direction==90) y++; // > else if(karel_direction==270) x++; // < if (world[x][y] != WALL) return 'a'; else return 'n'; } void movek(){ int x,y; if (frontIsClear() == 'a') { if(karel_direction==0) karel_x++; //v <-y-> if(karel_direction==180) karel_y++; //^ x if(karel_direction==90) karel_x++; //> | if(karel_direction==270) karel_x--; //< v draw(); } else printf("\nchyba, je tu stena\n\n");}beepersInBag(){ if (karel_beepers >= 1) return 'a'; else return 'n';}noBeepersInBag(){ return !beepersInBag();}beepersPresent(){ if (world[karel_y][karel_x] > 0) return 'a'; else return 'n';}putBeeper(){ if (world[karel_y][karel_x] >= 0) { world [karel_y][karel_x] += 1; karel_beepers--; } else printf("nie su tu beepre\n"); draw();}pickBeeper(){ if (world[karel_y][karel_x] >0) { world [karel_y][karel_x] -= 1; karel_beepers++; } else printf("tu nie ju beper"); draw();} void testSensors(){ // printf("suradnice: x:%d y:%d \norientacia %d \nclear? %c\nblocked? %c \n", karel_x, karel_y, karel_direction, frontIsClear(), frontIsBlocked()); printf("\nma beepre? %c\npocet beeprov v batohu: %d \nje tu beeper? %c\n", beepersInBag(), karel_beepers, beepersPresent()); printf("tu je %d beeprov\n", world[karel_y][karel_x]);}
vedeli by ste mi niekto povedať, prečo mi nefunguje funkcia frontIsClear / Blocked? Ja som už bezradný Code: [Select]frontIsClear(){ int x = karel_x; int y = karel_y; if(karel_direction==0) x++; // v else if(karel_direction==180) y++; // ^ else if(karel_direction==90) y++; // > else if(karel_direction==270) x++; // < if (world[x][y] != WALL) return 'a'; else return 'n'; }
frontIsClear(){ int x = karel_x; int y = karel_y; if(karel_direction==0) x++; // v else if(karel_direction==180) y++; // ^ else if(karel_direction==90) y++; // > else if(karel_direction==270) x++; // < if (world[x][y] != WALL) return 'a'; else return 'n'; }
vedeli by ste mi niekto povedať, prečo mi nefunguje funkcia frontIsClear / Blocked? Ja som už bezradný Code: [Select]void draw(){ system("cls"); printf("pozicia: %d %d \n",karel_x, karel_y); printf("orientacia: karel je gay: ");
void draw(){ system("cls"); printf("pozicia: %d %d \n",karel_x, karel_y); printf("orientacia: karel je gay: ");
Code: [Select]frontIsClear(){ int x = karel_x; int y = karel_y; if(karel_direction==0) x++; // v else if(karel_direction==180) y++; // ^ else if(karel_direction==90) y++; // > else if(karel_direction==270) x++; // < if (world[x][y] != WALL) return 'a'; else return 'n'; }
#define MAX_HEIGHT 20#define MAX_WIDTH 20#define WALL -1#define EMPTY 0int width,height;int world[MAX_HEIGHT][MAX_WIDTH];int karel_x;int karel_y;int karel_direction;int karel_beepers = 20;void turnOn(){ int i,j; karel_x = 4; karel_y = 3; width=6; height=5; int newWorld[5][6]={ { 0, -1, -1, 0, 0, 0 }, { 0, -1, -1, 0, 0, 0 }, { 0, -1, -1, 0, 0, 0 }, { 0, -1, -1, 0, 0, 0 }, { 0, -1, -1, -1, -1, 0 } };karel_direction = 90;for(i=0;i<height;i++){for(j=0;j<width;j++)world[i][j]=newWorld[i][j];}}void draw(){ system("cls"); printf("pozicia: %d %d \n",karel_x, karel_y); printf("orientacia: karel je gay: "); if (karel_direction == 0) printf("juh\n"); if (karel_direction == 180) printf("sever\n"); if (karel_direction == 270) printf("zapad\n"); if (karel_direction == 90) printf("vychod\n"); printf("pocet beeprov v batohu: %d\n", karel_beepers); printf("tu je %d beeprov", world[karel_y][karel_x]); printf(" \norientacia %d \nclear? %c\nblocked? %c \n", karel_direction, frontIsClear(), frontIsBlocked()); printf("\n"); int i,j; for(i=0;i<height;i++){ for(j=0;j<width;j++){ if(karel_y==i && karel_x==j) { if (karel_direction==0) printf("v"); else if(karel_direction==90) printf(">"); else if(karel_direction==180) printf("^"); else if(karel_direction==270) printf("<"); } else { if (world[i][j] == WALL) printf("#"); if (world[i][j] == EMPTY) printf("."); if (world[i][j] > 0) printf("*"); } } printf("\n"); }sleep(1000);}void turnLeft(){ karel_direction += 90; karel_direction = karel_direction % 360; draw();} frontIsBlocked(){ int x = karel_x; int y = karel_y; if(karel_direction==0) x++ ; //v <-y-> if(karel_direction==180) x--; //^ x if(karel_direction==90) y++; //> | if(karel_direction==270) y--; //< v if (world[x][y] != EMPTY) return 'a'; else return 'n'; }frontIsClear(){ int x = karel_x; int y = karel_y; if(karel_direction==0) x++; // v else if(karel_direction==180) x--; // ^ else if(karel_direction==90) y++; // > else if(karel_direction==270) y--; // < if (world[x][y] != WALL) return 'a'; else return 'n'; } void movek(){ int x,y; if (frontIsClear() == 'a') { if(karel_direction==0) karel_x++; //v <-y-> if(karel_direction==180) karel_x--; //^ x if(karel_direction==90) karel_y++; //> | if(karel_direction==270) karel_y--; //< v draw(); } else printf("\nchyba, je tu stena\n\n");}beepersInBag(){ if (karel_beepers >= 1) return 'a'; else return 'n';}noBeepersInBag(){ return !beepersInBag();}beepersPresent(){ if (world[karel_y][karel_x] > 0) return 'a'; else return 'n';}putBeeper(){ if (world[karel_y][karel_x] >= 0) { world [karel_y][karel_x] += 1; karel_beepers--; } else printf("nie su tu beepre\n"); draw();}pickBeeper(){ if (world[karel_y][karel_x] >0) { world [karel_y][karel_x] -= 1; karel_beepers++; } else printf("tu nie ju beper"); draw();} void testSensors(){ // printf("suradnice: x:%d y:%d \norientacia %d \nclear? %c\nblocked? %c \n", karel_x, karel_y, karel_direction, frontIsClear(), frontIsBlocked()); printf("\nma beepre? %c\npocet beeprov v batohu: %d \nje tu beeper? %c\n", beepersInBag(), karel_beepers, beepersPresent()); printf("tu je %d beeprov\n", world[karel_y][karel_x]);}
hups... ja som to asi 2x menil tak som si to nevšimol. Ale keď to dám takto tak mi to aj tak nejdebtw y - je horizontálneCode: [Select]frontIsClear(){ ... if (world[x][y] != WALL) return 'a'; else return 'n'; }
frontIsClear(){ ... if (world[x][y] != WALL) return 'a'; else return 'n'; }
shwollo ten senzor urob radsej cez switch a nezabudaj kontrolovat aj hranice sveta, to jest height a width, kusok sa nad tym zamysli, nakresli si aj to pole ak ty to pomoze a skusaj hlavne skusaj
wtf: return 'a' return 'n'
#define MAX_HEIGHT 20#define MAX_WIDTH 20#define WALL -1#define EMPTY 0int width,height;int world[MAX_HEIGHT][MAX_WIDTH];int karel_x;int karel_y;int karel_direction;int karel_beepers = 20;void turnOn(){ int i,j; karel_x = 4; karel_y = 3; width=6; height=5; int newWorld[5][6]={ { 0, -1, -1, 0, 0, 0 }, { 0, -1, -1, 0, 0, 0 }, { 0, -1, -1, 0, 0, 0 }, { 0, -1, -1, 0, 0, 0 }, { 0, -1, -1, -1, -1, 0 } };karel_direction = 90;for(i=0;i<height;i++){for(j=0;j<width;j++)world[i][j]=newWorld[i][j];}}void draw(){ system("cls"); printf("pozicia: %d %d \n",karel_x, karel_y); printf("orientacia: karel je gay: "); if (karel_direction == 0) printf("juh\n"); if (karel_direction == 180) printf("sever\n"); if (karel_direction == 270) printf("zapad\n"); if (karel_direction == 90) printf("vychod\n"); printf("pocet beeprov v batohu: %d\n", karel_beepers); printf("tu je %d beeprov", world[karel_y][karel_x]); printf(" \norientacia %d \nclear? %d\nblocked? %d \n", karel_direction, frontIsClear(), frontIsBlocked()); printf("\n"); int i,j; for(i=0;i<height;i++){ for(j=0;j<width;j++){ if(karel_y==i && karel_x==j) { if (karel_direction==0) printf("v"); else if(karel_direction==90) printf(">"); else if(karel_direction==180) printf("^"); else if(karel_direction==270) printf("<"); } else { if (world[i][j] == WALL) printf("#"); if (world[i][j] == EMPTY) printf("."); if (world[i][j] > 0) printf("*"); } } printf("\n"); }sleep(1000);}void turnLeft(){ karel_direction += 90; karel_direction = karel_direction % 360; draw();} frontIsBlocked(){ int x = karel_x; int y = karel_y; if(karel_direction==0) x++ ; //v <-y-> if(karel_direction==180) x--; //^ x if(karel_direction==90) y++; //> | if(karel_direction==270) y--; //< v if (world[x][y] != EMPTY) return 1; else return 0; }frontIsClear(){ int x = karel_x; int y = karel_y; if(karel_direction==0) x++; // v else if(karel_direction==180) x--; // ^ else if(karel_direction==90) y++; // > else if(karel_direction==270) y--; // < if (world[x][y] != WALL) return 1; else return 0; } void movek(){ int x,y; if (frontIsClear() == 'a') { if(karel_direction==0) karel_x++; //v <-y-> if(karel_direction==180) karel_x--; //^ x if(karel_direction==90) karel_y++; //> | if(karel_direction==270) karel_y--; //< v draw(); } else printf("\nchyba, je tu stena\n\n");}beepersInBag(){ if (karel_beepers >= 1) return 1; else return 0;}noBeepersInBag(){ return !beepersInBag();}beepersPresent(){ if (world[karel_y][karel_x] > 0) return 1; else return 0;}putBeeper(){ if (world[karel_y][karel_x] >= 0) { world [karel_y][karel_x] += 1; karel_beepers--; } else printf("nie su tu beepre\n"); draw();}pickBeeper(){ if (world[karel_y][karel_x] >0) { world [karel_y][karel_x] -= 1; karel_beepers++; } else printf("tu nie ju beper"); draw();} void testSensors(){ // printf("suradnice: x:%d y:%d \norientacia %d \nclear? %c\nblocked? %c \n", karel_x, karel_y, karel_direction, frontIsClear(), frontIsBlocked()); printf("\nma beepre? %c\npocet beeprov v batohu: %d \nje tu beeper? %c\n", beepersInBag(), karel_beepers, beepersPresent()); printf("tu je %d beeprov\n", world[karel_y][karel_x]);}
if (world[x][y] != WALL) return 1;
skus sa este pozriet na to, ako pracujes s y-ovou a x-ovou osou v poli - ako ich pouzivas pri nacitavani pola v turnOn() a ako ich pouzivas v senzore frontIsClear()
if (world[x][y] != EMPTY) return 1;
frontIsClear(){ int x = karel_x; int y = karel_y; if(karel_direction==0) x++; // v else if(karel_direction==180) x--; // ^ else if(karel_direction==90) y++; // > else if(karel_direction==270) y--; // < if (world[x][y] == EMPTY || x <= height || y <= width || x > 0 || y > 0 ) return 1; else return 0; } frontIsBlocked(){ return !frontIsClear();