online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
/****************************************************************************** Online C Compiler. Code, Compile, Run and Debug C program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ //Dinosaur game by gil nadler for hishovit //////////This part wastaken from snake game in moodle char nonblocking_getch(); void close_screen(), positional_putch(int x, int y, char ch); void millisecond_sleep(int n), init_screen(), update_screen(); #include <time.h> // nanosleep #include <ncurses.h> // getch, mvaddch, etc. char nonblocking_getch() { return getch(); } void positional_putch(int x, int y, char ch) { mvaddch(x, y, ch); } void millisecond_sleep(int n) { struct timespec t = { 0, n * 1000000 }; nanosleep(&t, 0);} void init_screen() { initscr(); noecho(); cbreak(); nodelay(stdscr, TRUE);} void update_screen() { refresh(); } void close_screen() { endwin(); } #include <stdio.h> ////////// //height and width of game #define w 50 #define h 16 int board[h][w]; int dino_pos = 0,quit = 0; // dino_pos-vertical position of dinosaur, changes if jumps bool dino_up = false; // true if dino is on his way up, false if on way down or not jumping bool is_dino_duck = false;// true if dino ducks enum State { SPACE=0, BORDER=1, DINO=2,CLOUD=3,CACTUS=4,BIRD=5 }; int mob = SPACE; int count_dino = 0;// counts number of dinosaur tiles, needed to detect collision int cloud_x = -1,cloud_y = 4; int cactus_x = -1,cactus_type = 1; int bird_x = -1,bird_y = 7; int score = 0; bool spawn_cloud = false; void start() { for(int j = 0; j < w;j++) board[0][j] = board[h-1][j] = BORDER; for(int i = 0; i < h;i++) board[i][0] = board[i][w-1] = BORDER; } void cloud_move() { if (spawn_cloud == true) { if (cloud_x > 3) { board[cloud_y][cloud_x]=board[cloud_y][cloud_x-1] =board[cloud_y][cloud_x-2] =board[cloud_y+1][cloud_x]=board[cloud_y+1][cloud_x-1]=board[cloud_y+1][cloud_x-2]= board[cloud_y][cloud_x-3]=board[cloud_y+1][cloud_x-3]= CLOUD; cloud_x--; } else { cloud_x = -1; spawn_cloud = false; } } } void cactus_move() { if (cactus_x > 3) { if (cactus_type == 0) { board[h-2][cactus_x] = board[h-3][cactus_x]= board[h-4][cactus_x] =board[h-2][cactus_x-1]= board[h-3][cactus_x-1]= board[h-4][cactus_x-1]= CACTUS; } else if(cactus_type == 1) { board[h-2][cactus_x] = board[h-3][cactus_x] =board[h-2][cactus_x-1]= board[h-3][cactus_x-1]= CACTUS; } else if(cactus_type == 2) { board[h-2][cactus_x] = board[h-2][cactus_x-1] = board[h-2][cactus_x-2]=board[h-2][cactus_x-3] = CACTUS; } else { board[h-2][cactus_x-1] = board[h-3][cactus_x-1] =board[h-4][cactus_x] = board[h-3][cactus_x+1]=board[h-2][cactus_x+1] = CACTUS; } cactus_x--; } else { cactus_x = -1; mob = SPACE; } } void bird_move() { if (bird_x > 3) { board[bird_y][bird_x] = board[bird_y][bird_x-1] = board[bird_y][bird_x-2] = board[bird_y][bird_x-3] = board[bird_y+1][bird_x-1] = board[bird_y-1][bird_x-1] = BIRD; bird_x--; } else { bird_x = -1; mob = SPACE; score += 30;//"achievement" } } void dino_jump() { if (dino_pos > 0) { board[h-2-dino_pos][6] = board[h-2-dino_pos][7] = board[h-2-dino_pos][5] = board[h-3-dino_pos][6] = board[h-4-dino_pos][6]=board[h-2-dino_pos][8] =board[h-3-dino_pos][7]=board[h-4-dino_pos][7]=board[h-5-dino_pos][8]=board[h-5-dino_pos][7]= DINO; if (dino_up) if (dino_pos < 8) dino_pos++; else dino_up = false; else dino_pos--; } else if (is_dino_duck == true) { dino_duck(); } else board[h-2][6] = board[h-2][7] = board[h-2][5] = board[h-3][6] = board[h-4][6] =board[h-2][8]=board[h-3][7]=board[h-4][7]=board[h-5][8]=board[h-5][7]= DINO; } void dino_duck() { if (dino_pos == 0 && is_dino_duck) { board[h-2- dino_pos][5] = board[h-2- dino_pos][6] = board[h-2- dino_pos][7] = board[h-2- dino_pos][8] = board[h-3- dino_pos][5] = board[h-3- dino_pos][6] = board[h-3- dino_pos][7] = board[h-3- dino_pos][8] = board[h-3- dino_pos][9] = board[h-3- dino_pos][10] = DINO; } } void spawn_mob(){ if (mob == SPACE) { mob = CACTUS; int temp = rand() % 7; if (temp < 2) mob = BIRD; } if (mob == CLOUD && cloud_x == -1) { cloud_x = w - 4; } else if (mob == CACTUS && cactus_x == -1) { cactus_x = w - 4; cactus_type = rand() % 4; } if (mob == BIRD && bird_x == -1) { bird_x = w - 4; bird_y = (rand() % 7) + 7; /* if ((bird_y >= 8)&& (bird_y <= 10)) bird_y += 11 - bird_y % 11;*/ // no need because now we can duck } } void show() { count_dino = 0; for (int i = 0; i < h;i++) for (int j = 0;j < w;j++) { if (board[i][j] == BORDER) positional_putch(i, j,'.'); else // tzarih leapes kol paam mehadash { if (board[i][j] == SPACE) positional_putch(i, j, ' '); else if (board[i][j] == CLOUD) positional_putch(i, j,'~'); else if (board[i][j] == CACTUS) positional_putch(i, j,'#'); else if (board[i][j] == BIRD) positional_putch(i, j,'+'); else if (board[i][j] == DINO) { positional_putch(i, j,'s'); count_dino++; } board[i][j] = SPACE; } } //colllision detection if (count_dino < 10 && score > 1) { //if (mob == CACTUS && dino_pos < 3) //positional_putch(0, 0,'t');//lose //else if (mob == BIRD && dino_pos >) quit = 1; } //cloud spawn if (rand() % 15 == 0 && spawn_cloud == false) { spawn_cloud = true; cloud_x = w - 3; } show_score(); update_screen(); } void show_score() { char name[] = "GIL NADLER"; char s[] = "score: "; for (int j = 0;j < 7;j++) { positional_putch(1, j+1, s[j]); positional_putch(0, j+1, name[j]); } positional_putch(0, 8, name[7]); positional_putch(0, 9, name[8]); positional_putch(0, 10, name[9]); char sc[10]; sprintf(sc, "%d", score); // converts int to string for (int j = 0;j < 10;j++) { if ((sc[j] != '0') &&(sc[j] != '1')&&(sc[j] != '2')&&(sc[j] != '3')&&(sc[j] != '4')&&(sc[j] != '5') &&(sc[j] != '6')&&(sc[j] != '7')&&(sc[j] != '8')&&(sc[j] != '9')) break; positional_putch(1, j+7, sc[j]); } } int main() { init_screen(); start(); do { show(); switch(nonblocking_getch()) { case 'w': if (dino_pos == 0) { dino_pos++; dino_up = true; } case 's': is_dino_duck = true; break; case 'q': quit = 1; break; } cloud_move(); dino_jump(); is_dino_duck = false; switch(mob) { case CACTUS:cactus_move();break; case BIRD:bird_move();break; } spawn_mob(); score++; srand(time(NULL)); millisecond_sleep(100); } while(!quit); millisecond_sleep(999); close_screen(); printf("Your score is %d",score); return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue