void new_game(void) { char c = 0; cancel_software_timer(foodTimerNum); cancel_software_timer(ratsTimerNum); empty_display(); //wait for space while(c != ' ' && c != 'l' && c != 'L'){ if(input_available()) c = fgetc(stdin); if(c == 'M' || c == 'm'){ toggle_sound(); display_sound_status(); c = 0; } } init_display(); if(c == 'l' || c == 'L'){ if(load_state()) render_board(); else { /* Initialise internal representations. */ init_board(); init_score(); } } else { /* Initialise internal representations. */ init_board(); init_score(); } clear_terminal(); //Place scores update_score(); //place sound status display_sound_status(); show_instruction(PLAYING); /* Make food blink 5 times a second. We should call blink_food ** 10 times a second which is 100ms */ foodTimerNum = execute_function_periodically(BLINKRATE, blink_food); ratsTimerNum = execute_function_periodically(RATSPEED, move_rats); /* Debug * move_cursor(0, TITLEY-1); printf_P(PSTR("new_game %u"), get_score()); wait_for(1000); //*/ }
int receive_score(int sd, int score_packages, score **scores) { if(score_packages < 0) { return -1; } else { score *s = init_score(); if(s == NULL || scores == NULL) return -1; int i; for(i = 0; i < score_packages; i++) { memset(s, 0, sizeof(struct score)); byte buf[12]; if(read(sd, buf, 12) <= 0) { perror("Error reading from client.\n"); free(s); return -1; } int size = bytes_to_int(buf); s->game_type = buf[3]; byte b[2]; b[0] = buf[4]; b[1] = buf[5]; s->score = bytes_to_int(b); s->partners = (byte) buf[6]; s->opponents = (byte) buf[7]; byte result = buf[8]; result = result >> 4; s->result = result; byte t = buf[8]; t = t << 4; byte tmp = buf[9]; tmp = tmp >> 4; t = t | tmp; s->takeouts = t; t = buf[9]; t = t << 4; tmp = buf[10]; tmp = tmp >> 4; t = t | tmp; s->remaining = t; size = size - 12; char name[size]; if(read(sd, name, size) == -1) { perror("Error reading from client.\n"); free(s); return -1; } strncpy(s->player_name, name, size); memcpy(scores[i], s, sizeof(score)); } free(s); return 0; } return 0; }
int main(int argc, char** argv) { (void) argc; (void) argv; env_game game; GdkColor color; gtk_init(&argc, &argv); if(init_game(&game)) return 0; init_score(&game); gdk_color_parse ("brown", &color); gtk_window_set_position(GTK_WINDOW(game.window),GTK_WIN_POS_CENTER); generate_all_button(game.button, &color); g_signal_connect(game.window, "destroy", G_CALLBACK(destroy),&game); while (game.i != 64) { g_signal_connect(game.button[game.i], "clicked", G_CALLBACK(move), &game); assign_button_coord(game.button[game.i], game.terrain, game.i); game.i = game.i + 1; } generated_platform(game.hbox,game.vbox,game.button); gtk_container_add(GTK_CONTAINER(game.window), GTK_WIDGET(game.vbox)); gtk_widget_show_all(game.window); gtk_main(); return (0); }
Partie *init_partie(SDL_Surface *ecran) { //Initialisation de la partie Partie *partie = malloc(sizeof(*partie)); partie->manche[0] = 0; partie->manche[1] = 0; partie->nb_tours = 0; init_paquet(partie); init_phase(partie); init_joueur(ecran, partie); SDL_FillRect(ecran, NULL, COULEUR_FOND); SDL_Flip(ecran); graphique_init_score(ecran); init_score(partie, ecran); return partie; }
void jeu(SDL_Surface * ecran) { SDL_Rect carte_graphique[120]; graphique_init_paquet(carte_graphique); Partie *partie = init_partie(ecran); int gagnant, hordago; while (!est_finie_partie(partie)) { init_score(partie, ecran); enlever_toutes_les_mains(ecran, partie); while (!est_finie_manche(partie)) { init_phase(partie); melange_paquet_debut(partie); distribution_debut(partie, ecran); while (est_mus(ecran, carte_graphique, partie)) { distribution_mus(ecran, partie, carte_graphique); } init_score_phase(ecran); hordago = phase_de_jeu(ecran, partie, carte_graphique); afficher_toutes_les_mains(ecran, partie, carte_graphique); enlever_paquet_4_endroit(ecran); affiche_paquet_esku(ecran, 0); if (hordago) { graphique_mise_a_jour_score(ecran, partie); partie->nb_tours ++; break; } compte_point(ecran, carte_graphique, partie); graphique_mise_a_jour_score(ecran, partie); message_fin_tour(ecran); partie->nb_tours ++; enlever_toutes_les_mains(ecran, partie); } gagnant = (partie->score[0] == NB_POINTS) ? 1 : 2; affiche_gagnant_manche(ecran, gagnant); } gagnant = (partie->manche[0] == NB_MANCHES) ? 1 : 2; affiche_gagnant_partie(ecran, gagnant); free(partie); }
void splash_screen(void) { /* Clear the terminal screen */ clear_terminal(); init_score(); update_score(); /* ** Display some suitable message to the user that includes your name(s) ** and student number(s). ** You may need to use terminalio functions to position the cursor ** appropriately - see terminalio.h for details. */ display_sound_status(); move_cursor(0,TITLEY); printf_P(PSTR("Snake\n\nBy: Justin Mancinelli\n\nID: 42094353")); //move_cursor(NAMEX,NAMEY); //printf_P(PSTR("")); //move_cursor(IDX,IDY); //printf_P(PSTR("")); }
int main( int argc, char* args[] ) { CSnake * snake; int snake_x, snake_y; unsigned char snake_hit; int k = 1, sb_toggle = 0; Uint32 delay = 10; bool bQuit = false; score = 0; srand((unsigned)time( NULL )); init(); init_items(); init_snake(); init_score(); theMap = new CSnakeMap(mapWidth, mapHeight, map_surface); theMap->AddTiles(map_tex, types_end); init_map(theMap); snake = new CSnake(mapWidth / 2, mapHeight / 2, 0, map_surface, snake_tex); snake->Grow(-1, 0); snake->Grow(-1, 0); snake->Draw(); do_move(snake, RIGHT); place_food(theMap, 1); theMap->Draw(); print_score(); draw_screen(); while (!quitcheck()) { if (bQuit) break; k = getInput(); if (k == QUIT) break; if (k >= LEFT && k <= DOWN) do_move(snake, k); if (collision(snake)) break; if (!checkMap(snake)) break; //theMap->Draw(); draw_screen(); } SDL_Rect r; r.x = 128; r.y = 256, r.w = 512, r.h = 256; SDL_BlitSurface( message, NULL, map_surface, &r ); draw_screen(); SDL_Delay(2000); SDL_FreeSurface(message); delete snake; cleanup(); return 0; }
/* Computes the maximum normalized mutual information scores and * returns a mine_score structure. Returns NULL if an error occurs. * Algorithm 5, page 14, SOM */ mine_score *mine_compute_score(mine_problem *prob, mine_parameter *param) { int i, j, k, p, q, ret; double *xx, *yy, *xy, *yx, *M_temp; int *ix, *iy; int *Q_map_temp, *Q_map, *P_map; mine_score *score; score = init_score(prob, param); if (score == NULL) goto error_score; xx = (double *) malloc (prob->n * sizeof(double)); if (xx == NULL) goto error_xx; yy = (double *) malloc (prob->n * sizeof(double)); if (yy == NULL) goto error_yy; xy = (double *) malloc (prob->n * sizeof(double)); if (xy == NULL) goto error_xy; yx = (double *) malloc (prob->n * sizeof(double)); if (yx == NULL) goto error_yx; Q_map_temp = (int *) malloc (prob->n * sizeof(int)); if (Q_map_temp == NULL) goto error_Q_temp; Q_map = (int *) malloc (prob->n * sizeof(int)); if (Q_map == NULL) goto error_Q; P_map = (int *) malloc (prob->n * sizeof(int)); if (P_map == NULL) goto error_P; ix = argsort(prob->x, prob->n); if (ix == NULL) goto error_ix; iy = argsort(prob->y, prob->n); if (iy == NULL) goto error_iy; M_temp = (double *)malloc ((score->m[0]) * sizeof(double)); if (M_temp == NULL) goto error_M_temp; /* build xx, yy, xy, yx */ for (i=0; i<prob->n; i++) { xx[i] = prob->x[ix[i]]; yy[i] = prob->y[iy[i]]; xy[i] = prob->x[iy[i]]; yx[i] = prob->y[ix[i]]; } /* x vs. y */ for (i=0; i<score->n; i++) { k = MAX((int) (param->c * (score->m[i]+1)), 1); ret = EquipartitionYAxis(yy, prob->n, i+2, Q_map, &q); /* sort Q by x */ for (j=0; j<prob->n; j++) Q_map_temp[iy[j]] = Q_map[j]; for (j=0; j<prob->n; j++) Q_map[j] = Q_map_temp[ix[j]]; ret = GetSuperclumpsPartition(xx, prob->n, k, Q_map, P_map, &p); if (ret) goto error_0; ret = OptimizeXAxis(xx, yx, prob->n, Q_map, q, P_map, p, score->m[i]+1, score->M[i]); if (ret) goto error_0; } /* y vs. x */ for (i=0; i<score->n; i++) { k = MAX((int) (param->c * (score->m[i]+1)), 1); ret = EquipartitionYAxis(xx, prob->n, i+2, Q_map, &q); /* sort Q by y */ for (j=0; j<prob->n; j++) Q_map_temp[ix[j]] = Q_map[j]; for (j=0; j<prob->n; j++) Q_map[j] = Q_map_temp[iy[j]]; ret = GetSuperclumpsPartition(yy, prob->n, k, Q_map, P_map, &p); if (ret) goto error_0; ret = OptimizeXAxis(yy, xy, prob->n, Q_map, q, P_map, p, score->m[i]+1, M_temp); if (ret) goto error_0; for (j=0; j<score->m[i]; j++) score->M[j][i] = MAX(M_temp[j], score->M[j][i]); } free(M_temp); free(iy); free(ix); free(P_map); free(Q_map); free(Q_map_temp); free(yx); free(xy); free(yy); free(xx); return score; error_0: free(M_temp); error_M_temp: free(iy); error_iy: free(ix); error_ix: free(P_map); error_P: free(Q_map); error_Q: free(Q_map_temp); error_Q_temp: free(yx); error_yx: free(xy); error_xy: free(yy); error_yy: free(xx); error_xx: for (i=0; i<score->n; i++) free(score->M[i]); free(score->M); free(score->m); free(score); error_score: return NULL; }
//Main program int main() { /* Initialize SDL’s video system and check for errors */ if (SDL_Init(SDL_INIT_VIDEO) != 0) { printf("Unable to initialize SDL: %s\n", SDL_GetError()); return 1; } /* Make sure SDL_Quit gets called when the program exits! */ atexit(SDL_Quit); /*set window title*/ SDL_WM_SetCaption("Essi Invaders", "P"); /* Attempt to set a 800x600 8 bit color video mode */ screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 8, SDL_DOUBLEBUF ); if (screen == NULL) { printf("Unable to set video mode: %s\n", SDL_GetError()); return 1; } //load images load_image("titlescreen.bmp", &title_screen, magenta); load_image("cmap.bmp", &cmap, magenta); load_image("target1.bmp", &invaders_img[0], magenta); load_image("target2.bmp", &invaders_img[1], magenta); load_image("target3.bmp", &invaders_img[2], magenta); load_image("target4.bmp", &invaders_img[3], magenta); load_image("target5.bmp", &invaders_img[4], magenta); load_image("target6.bmp", &invaders_img[5], magenta); load_image("target7.bmp", &invaders_img[6], magenta); load_image("target8.bmp", &invaders_img[7], magenta); load_image("target9.bmp", &invaders_img[8], magenta); load_image("target10.bmp", &invaders_img[9], magenta); load_image("target11.bmp", &invaders_img[10], magenta); load_image("target12.bmp", &invaders_img[11], magenta); load_image("spaceship.bmp", &player_img, magenta); load_image("essi.bmp", &logo_img, magenta); load_image("saucer.bmp", &saucer_img, magenta); load_image("gameover.bmp", &game_over_img, magenta); load_image("damage.bmp", &damage_img, lime); load_image("damagetop.bmp", &damage_top_img, lime); Uint32 next_game_tick = SDL_GetTicks(); int sleep = 0; Uint8 *keystate = 0; int quit = 0; SDL_Event event; init_score(); init_bases(); init_misil(); init_invaders(); init_player(); init_saucer(); init_bullets(bullets, P_BULLETS); init_bullets(enemy_bullets, E_BULLETS); state = menu; title_time = SDL_GetTicks(); /* Animation */ while (quit == 0) { /* Grab a snapshot of the keyboard. */ keystate = SDL_GetKeyState(NULL); while (SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYDOWN: switch( event.key.keysym.sym ) { //exit out of game loop if escape is pressed case SDLK_ESCAPE: quit = 1; break; case SDLK_SPACE: if (state == menu) { state = game; } else if (state == game){ player_shoot(); saucer_ai(); } else if (state == game_over) { init_invaders(); init_bases(); init_score(); init_player(); state = game; match = -1; } break; default: break; } break; case SDL_KEYUP: switch( event.key.keysym.sym ) { case SDLK_UP: move_misil(up); break; case SDLK_DOWN: move_misil(down); break; default: break; } } } draw_background(); if (state == menu) { char s[] = "Press SPACEBAR to start"; SDL_Rect src[60]; int i; if (title_time + 2000 < SDL_GetTicks()) { src[0].x = (SCREEN_WIDTH/100)*30; //Yellow Background of Space Invaders src[0].y = 40; src[0].w = 440; src[0].h = 230; SDL_FillRect(screen, &src[0], 248); } else { int y = 0; for (i = 0; i < 60; i++) { src[i].x = 0; src[i].y = y; src[i].w = SCREEN_WIDTH; src[i].h = 10; SDL_FillRect(screen, &src[i], 227); y += 10; } for (i = 0; i < 60; i++) { SDL_FillRect(screen, &src[i], rand() % 255); } } draw_title_screen(); draw_string(s, (SCREEN_WIDTH / 2) - (strlen(s) * 10), 500); } else if (state == game) { //move player if (keystate[SDLK_LEFT]) { move_player(left); } if (keystate[SDLK_RIGHT]) { move_player(right); } draw_hud(); draw_player(); draw_bases(); draw_invaders(); draw_saucer(); draw_serviceProjectMatch(match); draw_bullets(bullets, P_BULLETS); draw_bullets(enemy_bullets, E_BULLETS); enemy_hit_collision(); player_hit_collision(); enemy_base_collision(); saucer_hit_collision(); bullet_base_collision(enemy_bullets, E_BULLETS, 1); bullet_base_collision(bullets, P_BULLETS, 0); enemy_player_collision(); move_invaders(invaders.speed); move_saucer(); movenemy_bullets(bullets, P_BULLETS, -30); movenemy_bullets(enemy_bullets, E_BULLETS, 15); calculate_level(); enemy_ai(); game_over_ai(); pause_game(); } else if (state == game_over) { draw_hud(); draw_player(); draw_bases(); draw_invaders(); draw_saucer(); draw_bullets(bullets, P_BULLETS); draw_bullets(enemy_bullets, E_BULLETS); draw_game_over(); } else if (state == pause) { draw_hud(); draw_player(); draw_bases(); draw_invaders(); draw_saucer(); draw_bullets(bullets, P_BULLETS); draw_bullets(enemy_bullets, E_BULLETS); pause_game(); } /* Ask SDL to update the entire screen. */ SDL_Flip(screen); next_game_tick += 1000 / 30; sleep = next_game_tick - SDL_GetTicks(); if( sleep >= 0 ) { SDL_Delay(sleep); } } return 0; }
int listen_for_connections(void) { struct sockaddr_in serveraddr, clientaddr; int request_sd; int numsocks = 0; int maxsocks = 10; int sock[maxsocks]; int receive; int clientaddrlen = sizeof(struct sockaddr); fd_set readfds, fds; struct timeval time; time.tv_sec = 20; time.tv_usec = 0; //request-socket, lytter på innkommende forbindelser request_sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //adresse struct memset((void*) &serveraddr, 0, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = INADDR_ANY; if(listening_port != 0) { serveraddr.sin_port = htons(listening_port); printf("Listening for connections on port '%d'\n", listening_port); } else { serveraddr.sin_port = htons(9090); //default if no port is provided printf("Listening for connections on default port '9090'\n"); } //bind adressen til socketen bind(request_sd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)); //start lytting listen(request_sd, SOMAXCONN); FD_ZERO(&readfds); FD_ZERO(&fds); FD_SET(request_sd, &fds); FD_SET(0, &fds); numsocks++; for(;;) { readfds = fds; receive = select(maxsocks + 1, &readfds, NULL, NULL, &time); if(receive < 0) { perror("select encountered an error.\n"); return -1; } else if(receive == 0) { //timeout, do nothing } else { int i; for(i = 0; i < FD_SETSIZE; i++) { if(FD_ISSET(i, &readfds)) { if(i == 0) { //stdin look for ctrl-d if(read(i, NULL, 10) == 0) { for(i = 0; i < numsocks; i++) { close(sock[i]); FD_CLR(sock[i], &fds); } exit_server(); } } if(i == request_sd) { if(numsocks < maxsocks) { sock[numsocks] = accept(request_sd, (struct sockaddr *) &clientaddr, (socklen_t *) &clientaddrlen); FD_SET(sock[numsocks], &fds); numsocks++; } else { //No more space for sockets perror("Ran out of socket space.\n"); for(i = 0; i < numsocks; i++) { close(sock[i]); FD_CLR(sock[i], &fds); } exit_server(); } } else { //read first 4 bytes, and determine what kind of package is coming char buf[4]; memset(buf, 0, 4); if(read(i, buf, 4) == -1) { perror("Error reading from client.\n"); return -1; } int size = bytes_to_int(buf); byte msg_type = buf[2]; if(msg_type == SCORE_LIST) { int score_packages = btotci(buf[3]); if(score_packages > 0) { score *scores[score_packages]; int count; for(count = 0; count < score_packages; count++) { scores[count] = init_score(); } int result = receive_score(i, score_packages, scores); if(result != -1) { for(count = 0; count < score_packages; count++) { add_score(scores[count]); } } for(count = 0; count < score_packages; count++) { free(scores[count]); } } else if(score_packages < 0) { //reply with abs(score_packages); int num = abs(score_packages); score *s[num]; int count; count = get_scores(num, s); if(send_score_list(i, s, count) < 0) { perror("Closing connection.\n"); close(i); FD_CLR(i, &fds); } } } else if(msg_type == SHUTDOWN_NOTIFICATION) { //receive shutdown msg char buf[size - 4]; //First 4 bytes allready read. int result = receive_shutdown_notification(i, buf, size - 4); if(result == 0) { printf("Received shutdown notification: %s\n", buf); close(i); FD_CLR(i, &fds); } } else { //error, unknown type, reply with shutdown notification perror("Sending shutdown_notification.\n"); send_shutdown_notification(i, "Unknown msg_type, got: " + msg_type); //close and remove socket criptor close(i); FD_CLR(i, &fds); } } } } } } int i; for(i = 0; i < maxsocks; i++) { close(sock[i]); } close(request_sd); return 0; }
int listen_for_connections(void) { int request_sd; int sds[2]; player_t *player[2]; player[0] = get_player(0); player[1] = get_player(1); byte game_type; int game_level; int clientaddrlen = sizeof(struct sockaddr); struct sockaddr_in serveraddr, clientaddr; //request-socket, lytter på innkommende forbindelser request_sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //adresse struct memset((void*) &serveraddr, 0, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = INADDR_ANY; if(listening_port != 0) { serveraddr.sin_port = htons(listening_port); printf("Listening for connections on port '%d'\n", listening_port); } else { serveraddr.sin_port = htons(9090); //default if no port is provided printf("Listening for connections on default port '9090'\n"); } //bind adressen til socketen bind(request_sd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)); //start lytting listen(request_sd, SOMAXCONN); //Receive connection from players int i; for(i = 0; i < 2; i++) { sds[i] = accept(request_sd, (struct sockaddr *) &clientaddr, (socklen_t *) &clientaddrlen); } //Receive init from players for(i = 0; i < 2; i++) { int x, y, ox, oy; byte tmp; if(receive_game_init(sds[i], &tmp, &player[i]->opponents, &x, &y, &ox, &oy, player[i]->user_name) == -1) { perror("receive_game_init in listen_for_connection() encountered a error.\n"); return -1; } if(i == 0) { game_type = tmp; game_level = game_type - 0x10; } } //Send init to both players for(i = 0; i < 2; i++) { int opponent = (i == 0) ? 1 : 0; if(send_game_init(sds[i], game_type, player[i]->opponents, player[i]->body[0].x, player[i]->body[0].y, player[opponent]->body[0].x, player[opponent]->body[0].y, player[i]->user_name) == -1) { perror("send_game_init() encountered a problem.\n"); return -1; } } //Send game start to both players for(i = 0; i < 2; i++) { if(send_game_start(sds[i]) == -1) { perror("send_game_start() encountered a problem.\n"); return -1; } } //receive updates from players, check for collisons and respond with a update or //game_end if it was a collision. while(1) { for(i = 0; i < 2; i++) { int x, y; if(receive_game_update(sds[i], &x, &y) == -1) { perror("receive_game_update() encountered a problem.\n"); return -1; } player_increase_size(player[i], 1); update_player_coordinates(player[i], x, y); player_increase_score(player[i], game_level); if(player[i]->score % 50 == 0 && game_level < 9) game_level++; } //check for collisions for(i = 0; i < 2; i++) { int opponent = (i == 0) ? 1 : 0; int result = 0; if(player_hit_borders(player[i])) { result = 0x1; //Player collided with wall player[i]->result = result; player[opponent]->result = 0x7;//No remaining opponents } else if(player_hit_self(player[i])) { result = 0x2; // Player collided with himself player[i]->result = result; player[opponent]->result = 0x7; } else if(player_hit_opponent(player[i], player[opponent])) { result = 0x4; //Player hit opponent player[i]->result = result; player[opponent]->result = 0x7; player[opponent]->takeouts++; } if(result != 0) { //send stop for(i = 0; i < 2; i++) { if(send_game_end(sds[i]) == -1) { perror("send_game_end encountered a error.\n"); return -1; } } //send highscores if(client_host_addr != NULL) { int highscore_sd = get_client_socket_descriptor(); score *sl[2]; for(i = 0; i < 2; i++) { sl[i] = init_score(); sl[i]->game_type = game_type; get_score(player[i], sl[i]); } if(send_score_list(highscore_sd, sl, 2) == -1) { perror("Something went wrong sending score list.\n"); return -1; } } close(sds[0]); close(sds[0]); close(request_sd); return 0; } } //send updates for(i = 0; i < 2; i++) { int which = (i == 0) ? 1 : 0; if(send_game_update(sds[i], player[which]->body[0].x, player[which]->body[0].y) == -1) { perror("send_game_update encountered a error.\n"); return -1; } } } }