static int32_t initScreen() { cbreak(); keypad(stdscr, TRUE); noecho(); win = newwin(width - 2, height - 1, starty + 1 , startx + 2); winBox = newwin(width, height, starty, startx); updateWin(); updateWin(); return 0; }
void update(int cells[], int size) { int numticks = ticks; while(ticks > 0) { int c; // wclear(game); // wclear(win); render(cells,size); int oldcells[size]; for(c = 0; c < size; c++) { oldcells[c] = cells[c]; } for(c = 0; c < size; c++) { if(oldcells[c] != 0) //Check if we're alive... { /*RULE 1*/ if(checkNeighbors(c % ys, c / ys, oldcells) < 2) cells[c] = 0; /*RULE 3*/ if(checkNeighbors(c % ys, c / ys, oldcells) > 3) cells[c] = 0; /*We don't need to check for rule 2, it's implicit*/ } else //We're dead { if(checkNeighbors(c % ys, c / ys, oldcells) == 3) cells[c] = 1; } } updateWin(cells,size); wrefresh(game); usleep(SLEEP_TIME); ticks--; totalTicks++; } ticks = numticks; }
int main(int argc, char *argv[]) { if(argc >= 2) { if(strcmp(argv[1],"-h") == 0 || strcmp(argv[1],"--help") == 0) { printf("\nConway\n"); printf("Copyright 2015 Harley Wiltzer\n\n"); printf("conway:\t\tOpen conway with a random color\n"); printf("conway 1:\tOpen conway blue theme\n"); printf("conway 2:\tOpen conway red theme\n"); printf("conway 3:\tOpen conway green theme\n"); printf("conway 4:\tOpen conway yellow theme\n"); printf("conway 5:\tOpen conway light blue theme\n"); printf("conway 6:\tOpen conway orange theme\n"); printf("conway 7:\tOpen conway white theme\n"); printf("\n"); printf("While running conway...\n"); printf("=======================\n"); printf("q:\t\tClose conway\n"); printf("space:\t\tActivate/Kill cell\n"); printf("enter:\t\tStart ticking\n"); printf("h:\t\tMove cursor left\n"); printf("j:\t\tMove cursor down\n"); printf("k:\t\tMove cursor up\n"); printf("l:\t\tMove cursor right\n"); printf("d:\t\tClear all cells\n"); printf("=:\t\tIncrease the amount of ticks by 10\n"); printf("-:\t\tDecrease the amount of ticks by 10\n"); printf("+:\t\tIncrease the amount of ticks by 1\n"); printf("_:\t\tDecrease the amount of ticks by 1\n"); return 0; } } int i, c; initCurses(); showSplash(); totalTicks = 0; refresh(); getch(); win = createWindow(WIN_WIDTH,rows,0,cols - WIN_WIDTH - 1); cols = cols - WIN_WIDTH - 1; xs = rows; ys = cols / 2; area = rows * cols; game = createWindow(cols - 1,rows,0,0); ticks = DEFAULT_TICKS; int cells[area / 2]; for(i = 0; i < area/2; i++) { cells[i] = 0; } x = y = row = col = 0; if(has_colors() == 0) wprintw(game,"NO COLORS\n"); if(argc < 2) { srand(time(NULL)); color = rand()%7 + 1; } else { if(atoi(argv[1]) > 7 || atoi(argv[1]) < 1) { srand(time(NULL)); color = rand()%7 + 1; } else { color = atoi(argv[1]); } } x = ys/2; y = xs/2; wmove(game,y, x * 2); x = 2; y = 2; wrefresh(game); updateWin(cells,area/2); wmove(game,y,x * 2); x = ys/2; y = xs/2; wmove(game,y,x * 2); while((ch = getch()) != 'q') { wclear(game); wclear(win); int r, c; render(cells,area/2); wmove(game,y, x * 2); switch(ch) { case 'h': if (x % ys == 0) break; x -= 1; wmove(game,y, x * 2); wrefresh(game); break; case 'j': if ((y + 1) % xs == 0) break; y += 1; wmove(game,y, x * 2); wrefresh(game); break; case 'k': if (y % xs == 0) break; y -= 1; wmove(game,y, x * 2); wrefresh(game); break; case 'l': if((x+1)%ys == 0) break; x += 1; wmove(game,y, x * 2); wrefresh(game); break; case ' ': cells[y * ys + x] = !cells[y * ys + x]; if(cells[y * ys + x]) drawCell(x,y); else eraseCell(x,y); updateWin(cells,area/2); break; case '\n': update(cells, area/2); break; case 'd': memset(cells,0,area/2); int q; for(q = 0; q < area/2; q++) { cells[q] = 0; } totalTicks = 0; render(cells, area/2); updateWin(cells,area/2); break; case '=': ticks += 10; updateWin(cells,area/2); break; case '-': if(ticks >= 20) ticks -= 10; updateWin(cells,area/2); break; case '+': ticks++; updateWin(cells,area/2); break; case '_': if(ticks > 2) ticks--; updateWin(cells, area/2); break; default: break; } updateWin(cells,area/2); wrefresh(game); } destroyWindow(game); destroyWindow(win); endwin(); return 0; }
int main(int argc, char **argv) { int ch; #ifndef SYM struct autocopt *copt; int32_t ret; copt = autocopt_init(AUTOCOPT_DEFAULT_DEV); if (copt == NULL) { return EXIT_FAILURE; } ret = autocopt_control(copt, &ctl); if (ret < 0) { mvwprintw(win, 0, 0, "can't send to copter"); } ret = autocopt_select(copt, AUTOCOPT_MODE_LINUX); if (ret < 0) { fprintf(stderr, "Can't select mode\n"); return EXIT_FAILURE; } #endif initscr(); height = 30; width = 30; starty = (LINES - height) / 2; startx = (COLS - width) / 2; initScreen(); timeout(100); while((ch = getch()) != 'z') { switch(ch) { case 'w': ctl.pitch += PITCH_STEPSIZE; if (ctl.pitch > 1.) { ctl.pitch = 1; } break; case 's': ctl.pitch -= PITCH_STEPSIZE; if (ctl.pitch < -1.) { ctl.pitch = -1; } break; case 'd': ctl.roll += ROLL_SETPSIZE; if (ctl.roll > 1.) { ctl.roll = 1; } break; case 'a': ctl.roll -= ROLL_SETPSIZE; if (ctl.roll < -1.) { ctl.roll = -1; } break; case 'q': ctl.yaw -= YAW_SETPSIZE; if (ctl.yaw < -1.) { ctl.yaw = -1; } break; case 'e': ctl.yaw += YAW_SETPSIZE; if (ctl.yaw > 1.) { ctl.yaw = 1; } break; case 'f': ctl.thrust -= THURST_STEPSIZE; if (ctl.thrust < -1) { ctl.thrust = -1; } break; case 'r': ctl.thrust += THURST_STEPSIZE; if (ctl.thrust > 1.) { ctl.thrust = 1; } break; case 't': ctl.roll = ctlSave.roll; ctl.pitch = ctlSave.pitch; ctl.yaw = ctlSave.yaw; break; case 'g': ctlSave.roll = ctl.roll; ctlSave.pitch = ctl.pitch; ctlSave.yaw = ctl.yaw; break; case 'z': break; case ERR: ctl.roll = 0; ctl.pitch = 0; ctl.yaw = 0; break; case 'c': ctl.thrust = -0.2; break; case 'x': ctl.thrust = -1; break; case '1': #ifndef SYM ret = autocopt_select(copt, AUTOCOPT_MODE_SPECTRUM); if (ret < 0) { mvwprintw(win, 0, 0, "Can't select mode\n"); } #endif break; case '2': #ifndef SYM ret = autocopt_select(copt, AUTOCOPT_MODE_LINUX); if (ret < 0) { mvwprintw(win, 0, 0, "Can't select mode\n"); } #endif break; case '3': #ifndef SYM ret = autocopt_select(copt, AUTOCOPT_MODE_LINUX_THRUST_ONLY); if (ret < 0) { mvwprintw(win, 0, 0, "Can't select mode\n"); } #endif break; case '4': #ifndef SYM ret = autocopt_select(copt, AUTOCOPT_MODE_LINUX_RPY_ONLY); if (ret < 0) { mvwprintw(win, 0, 0, "Can't select mode\n"); } #endif break; case 'y': #ifndef SYM ret = autocopt_emergencyLanding(copt); if (ret < 0) { mvwprintw(win, 0, 0, "Can't send emergency Landing\n"); } #endif break; default: mvwprintw(win, 0, 0, "err: %c", ch); break; } #ifndef SYM ret = autocopt_control(copt, &ctl); if (ret < 0) { mvwprintw(win, 0, 0, "can't send to copter"); } #endif updateWin(); } #ifndef SYM ret = autocopt_select(copt, AUTOCOPT_MODE_SPECTRUM); if (ret < 0) { fprintf(stderr, "Can't select mode\n"); return EXIT_FAILURE; } #endif endwin(); return EXIT_SUCCESS; }