gg_dialog_t *dialog_saveload_create(gg_dialog_t *parent, int saving) { gg_widget_t *dialog; gg_widget_t *rootvbox = gg_vbox_create(0); gg_widget_t *vbox = gg_vbox_create(0); gg_widget_t *hbox = gg_vbox_create(0); gg_widget_t *hboxtemp; gg_widget_t *widget; char temp[80]; char whiteis[80], blackis[80]; int i=0,j=0; int padding=0; change_saving=saving; if ( !changing_slot ) saveload_selected=0; /*DBG_LOG( "dialog opened with saveselected of %i", saveload_selected );*/ /* Right side.. */ if (!changing_slot) { #ifdef _arch_dreamcast dc_restore_savegames(); #endif for ( i=0; i<SAVEGAME_SLOTS; i++ ) load_save_xml( i ); } if ( get_slots() & (1 << saveload_selected) ) { gg_widget_t *board_box = gg_vbox_create(0); sprintf( temp, "Saved: %s", get_time_save(saveload_selected) ); widget = gg_label_create(temp); gg_container_append(GG_CONTAINER(vbox), widget); switch ( get_config_save(saveload_selected)->player[WHITE] ) { case PLAYER_ENGINE: sprintf( whiteis, "CPU" ); break; case PLAYER_UI: sprintf( whiteis, "Human" ); break; default: /* Whoops */ sprintf( whiteis, "Oh no.." ); break; } switch ( get_config_save(saveload_selected)->player[BLACK] ) { case PLAYER_ENGINE: sprintf( blackis, "CPU" ); break; case PLAYER_UI: sprintf( blackis, "Human" ); break; default: /* Whoops */ sprintf( blackis, "Oh no.." ); break; } sprintf( temp, "%s vs %s", whiteis, blackis ); widget = gg_label_create(temp); gg_container_append(GG_CONTAINER(vbox), widget); sprintf( temp, "Difficulty: %s", get_config_save(saveload_selected)->difficulty ? "Normal" : "Easy" ); widget = gg_label_create(temp); gg_container_append(GG_CONTAINER(vbox), widget); sprintf( temp, "Level: %i", get_config_save(saveload_selected)->cpu_level ); widget = gg_label_create(temp); gg_container_append(GG_CONTAINER(vbox), widget); widget = gg_label_create(" "); gg_container_append(GG_CONTAINER(vbox), widget); /* create board.. */ for ( i=7; i>=0; i-- ) { gg_widget_t *hboxtemp2; gg_colour_t col_white = { 1.0f, 1.0f, 1.0f, 1.0f }; /*gg_colour_t col_grey = { 0.3f, 0.3f, 0.3f, 1.0f };*/ hboxtemp = gg_hbox_create(0); hboxtemp2 = gg_hbox_create(0); gg_set_requested_size(hboxtemp2, 20, 20); gg_container_append(GG_CONTAINER(hboxtemp), hboxtemp2); for ( j=0; j<8; j++ ) { gg_colour_t col_green = {0.5, 0.6, 0.5, 1.0}; gg_colour_t col_yellow = {0.8, 0.7, 0.4, 1.0}; gg_colour_t front, *back; int square = get_saved_board(saveload_selected)->square[i * 8 + j]; sprintf(temp, "%c", xmlsquaretofont(square)); widget = gg_label_create( temp ); gg_set_requested_size(widget, 20, 20); gg_align_set_alignment(GG_ALIGN(widget), 0.5, 0.5); if (COLOUR(square) == WHITE) front = col_white; else front = *get_col(COL_BLACK); if ((i + j) % 2 == 0) back = &col_green; else back = &col_yellow; /* FIXME Hack to turn off shadow */ front.a = 2.0f; gg_label_set_colour(GG_LABEL(widget), &front, back); gg_container_append(GG_CONTAINER(hboxtemp), widget); } gg_container_append(GG_CONTAINER(board_box), hboxtemp); } gg_container_append(GG_CONTAINER(vbox), board_box); } else { sprintf( temp, "Empty slot" ); widget = gg_label_create(temp); gg_container_append(GG_CONTAINER(vbox), widget); for ( i=0; i<12; i++ ) { widget = gg_label_create(" "); gg_container_append(GG_CONTAINER(vbox), widget); } } gg_set_requested_size(vbox, 201, 0); gg_container_append(GG_CONTAINER(hbox), gg_frame_create(vbox)); /* left side */ vbox = gg_vbox_create(0); /* padding.. */ for ( i=0; i<padding; i++ ) { widget = gg_label_create(" "); gg_container_append(GG_CONTAINER(vbox), widget); } widget = gg_option_create(); for ( i=0; i<SAVEGAME_SLOTS; i++ ) { sprintf( temp, "Save slot: %i", i+1 ); gg_option_append_label(GG_OPTION(widget), temp, 0.5f, 0.0f); } gg_widget_subscribe_signal_name(widget, widget->id, "option_changed", dialog_saveload_change, widget); gg_container_append(GG_CONTAINER(vbox), widget); if ( changing_slot ) gg_option_set_selected(GG_OPTION(widget), saveload_selected); if ( saving ) { widget = gg_action_create_with_label("Save Game", 0.5f, 0.0f); gg_widget_subscribe_signal_name(widget, widget->id, "action_pressed", dialog_savegame_save, vbox); } else { widget = gg_action_create_with_label("Load Game", 0.5f, 0.0f); gg_widget_subscribe_signal_name(widget, widget->id, "action_pressed", dialog_loadgame_load, vbox); } gg_container_append(GG_CONTAINER(vbox), widget); widget = gg_action_create_with_label("Cancel", 0.5f, 0.0f); gg_widget_subscribe_signal_name(widget, widget->id, "action_pressed", dialog_close_cb, NULL); gg_container_append(GG_CONTAINER(vbox), widget); /*for ( i=0; i<SAVEGAME_SLOTS; i++ ) { sprintf( temp, "%i: ", i ); widget = gg_action_create_with_label(temp, 0.0f, 0.0f); gg_action_set_callback(GG_ACTION(widget), dialog_saveload_change, vbox); gg_container_append(GG_CONTAINER(vbox), widget); }*/ gg_container_append(GG_CONTAINER(hbox), vbox ); if ( changing_slot ) gg_vbox_set_selected(vbox, padding ); /* Dialog stuff */ gg_container_append(GG_CONTAINER(rootvbox), hbox); dialog = gg_dialog_create(rootvbox, NULL, parent, GG_DIALOG_AUTOHIDE_PARENT); if ( saving ) gg_dialog_set_style(GG_DIALOG(dialog), get_ingame_style()); else gg_dialog_set_style(GG_DIALOG(dialog), get_menu_style()); return GG_DIALOG(dialog); }
int main(){ SDL_Init(SDL_INIT_EVERYTHING); struct sockaddr_in addr; // The necessary structure to store all pieces of information about the server (IP address, port, etc) int sock; // The socket to connect to the server socklen_t addr_size = sizeof(struct sockaddr_in); char buffer[BUFFER_SIZE]; // The buffer where we store what we get from the server char move; char IP_string[64]; printf("Please enter the IP address you want to connect to.\n"); do{ fgets(IP_string,16,stdin); }while(inet_addr(IP_string) <= 0); // Creating the necessary structure to store the address you're trying to connect to memset(&addr, 0, sizeof addr); //creates a memory space in which to store the constant for address addr.sin_family = AF_INET; addr.sin_port = htons(7778); // the port dedicated to observers addr.sin_addr.s_addr = inet_addr(IP_string); inet_pton(AF_INET, inet_ntoa(addr.sin_addr), &addr.sin_addr); // Creating the client socket sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); // AF_INET is the protocol domain, SOCK_STREAM indicates we want a two way connection and IPROTO8TCP is the protocol if(sock == -1){ printf("Error : couldn't create the socket !"); return 0; } // Connexion to the server if(connect(sock,(struct sockaddr *)&addr,addr_size) == -1){ printf("Error : couldn't connect to the server socket !\n"); //error return 0; } printf("Successful connection to the server.\n"); printf("You are player 2 !\n\n\n"); recv(sock,buffer,BUFFER_SIZE,0); //get grid get_saved_board(buffer); //save the initial grid init_window(); //open window while(nextturn()){ update_board(); //display bord printf("Player 1 : %d\n",score(PLAYER1)); printf("Player 2 : %d\n",score(PLAYER2)); printf("\n"); recv(sock,buffer,BUFFER_SIZE,0); //receive informatition, (player, color) for the turn play(PLAYER1,TEMP,buffer[0]); // The move is done here the game is reconstructed. update_board(); if(nextturn()){ move = your_turn(); play(PLAYER2,TEMP,move); buffer[0] = move; buffer[1] = '\0'; send(sock,buffer,BUFFER_SIZE,0); } } //end of game update and score update_board(); printf("Player 1 : %d\n",score(PLAYER1)); printf("Player 2 : %d\n",score(PLAYER2)); printf("\n"); printf("Press a key, then Enter to quit"); getchar(); //leaving the game shutdown(sock, SHUT_RDWR); close(sock); SDL_Quit(); return 0; }
/** Program entry point */ int main() { printf("\n\n Welcome to the 7 wonders of the world of the 7 colors\n" " *****************************************************\n\n" "Current board state:\n"); srand(time(NULL)); print_board(); int i = 0; int j = 0; int victories[5][5]; // victories[i][j] contains the number of victories of i over j for(i = 0 ; i < 5 ; i++){ for(j = 0 ; j < 5 ; j++){ victories[i][j] = 0; } } long int start = clock(); void (*strats[5])(char) = {improved_random_play, spider, greedy, double_greedy, mix}; char* names[5] = {"Improved Random","Spider","Greedy","Double Greedy","Mix"}; int k = 0; float score1 = 0; float score2 = 0; for(i = 0 ; i < 4 ; i++){ for(j = i+1 ; j < 5 ; j++){ for(k = 0 ; k < NB_SIMULATIONS ; k++){ printf("%s VS %s :\n", names[i], names[j]); random_filling(); copy_board(); run_game(PLAYER1,strats[i],strats[j]); score1 = 100*score(PLAYER1)/((float) BOARD_SIZE*BOARD_SIZE); score2 = 100*score(PLAYER2)/((float) BOARD_SIZE*BOARD_SIZE); if(score1 > score2){(victories[i][j])++;} else{(victories[j][i])++;} get_saved_board(); set_cell(0,BOARD_SIZE-1,PLAYER2); set_cell(BOARD_SIZE-1,0,PLAYER1); run_game(PLAYER2,strats[i],strats[j]); score1 = 100*score(PLAYER1)/((float) BOARD_SIZE*BOARD_SIZE); score2 = 100*score(PLAYER2)/((float) BOARD_SIZE*BOARD_SIZE); if(score1 > score2){(victories[i][j])++;} else{(victories[j][i])++;} } } } printf("Total execution time : %ld", (clock()-start)/CLOCKS_PER_SEC); printf("\n\n"); for(i = 0 ; i < 4 ; i++){ for(j = i+1 ; j < 5 ; j++){ printf("%s VS %s :\n", names[i], names[j]); printf("%s : %d victories\n", names[i], victories[i][j]); printf("%s : %d victories\n", names[j], victories[j][i]); printf("\n\n"); } } return 0; // Everything went well }