Dados_comuns cadastra_dados_comuns(){ Dados_comuns dados_comuns; printf("Digite o nome do produto: "); fgets(dados_comuns.nome, TAMANHO_NOME, stdin); //strcpy(dados_comuns.nome, valida_string(dados_comuns.nome, TAMANHO_NOME)); valida_string(dados_comuns.nome, TAMANHO_NOME); printf("Digite o preco do produto: "); scanf(" %f", &dados_comuns.preco); flush_in(); dados_comuns.preco = valida_float(dados_comuns.preco, 0, MAX_PRECO); printf("Digite a quantidade no estoque do produto: "); scanf(" %d", &dados_comuns.qtde_estoque); flush_in(); dados_comuns.qtde_estoque = valida_float(dados_comuns.qtde_estoque, 0, MAX_ESTOQUE); return dados_comuns; }
void handle_incoming_connect(struct queue *p) { struct in_addr *ip; unsigned short *port; char *name; char ans; ip = (struct in_addr *) p->buffer; port = (unsigned short *)(p->buffer + sizeof(struct in_addr)); name = (char *)(p->buffer+ sizeof(struct in_addr) + sizeof(unsigned short)); do{ printf("Il client %s, IP %s, porta %hu, vuole giocare. " "Accetti la richiesta? [Y|N]: ", name, inet_ntoa(*ip), *port); fflush(stdout); scanf("%c", &ans); flush_in(); } while (ans != 'y' && ans != 'Y' && ans != 'n' && ans != 'N'); if(ans == 'N' || ans == 'n') { printf("Richiesta rifiutata\n"); p->flags = CL_REF; } else if(ans == 'y' || ans == 'Y' ) { memset(&opponent_info.addr, 0, sizeof(opponent_info.addr)); opponent_info.addr.sin_family = AF_INET; opponent_info.addr.sin_port = htons(*port); opponent_info.addr.sin_addr = *ip; strcpy(opponent_info.name, name); printf("Richiesta accettata\n"); turn = 0; p->flags = CL_ACC; command_mode = 0; } p->length = strlen(name)+1; p->buffer = (char*)realloc(p->buffer, p->length); strcpy(p->buffer, name); p->sd = sd; p->step = 1; FD_SET(sd, &write_set); }
void read_comb(char * comb) { int ret = 0; int i = 0; memset(comb, 0, 5); ret = read(STDIN_FILENO, (void*) comb, 5); if (ret > 5) { flush_in(); } else if(ret < 0) { cprintf("errore in lettura, riprova"); } for(i=0; i<4 ; i++) if(comb[i]>'9' || comb[i]<'0') return; insertingCombination = 0; if (isYourTurn == 0) /* Giocatore che NON HA richiesto la partita */ { queue_add(&queue_l, cd, CL_INS, 0, 0); FD_SET(cd, &write_set); printf("E' il turno di %s\n", opponent_info.name); } else /* Giocatore che HA richiesto la partita (deve aspettare CL_INS)*/ { if (opponentHasInsertedComb) { printf("%s ha inserito la combinazione, la partita può cominciare\n", opponent_info.name); printf("E' il tuo turno\n"); cprintf(""); opponentHasInsertedComb = 0; } else { printf("Attendi che %s inserisca la sua combinazione...\n", opponent_info.name); } } }
/* * play_level: * Let the player play the current level */ void play_level() { COORD *cp; move(My_pos.y, My_pos.x); addch(PLAYER); refresh(); for (cp = Robots; cp < &Robots[MAXROBOTS]; cp++) { if (cp->y < 0) continue; move(cp->y, cp->x); addch(ROBOT); } refresh(); # ifdef DEBUG standout(); move(Min.y, Min.x); addch(inch()); move(Max.y, Max.x); addch(inch()); standend(); # endif /* DEBUG */ setjmp(End_move); flush_in(); while (!Dead && Num_robots > 0) { move(My_pos.y, My_pos.x); if (!jumping()) refresh(); get_move(); if (Real_time) alarm(0); if (Field[My_pos.y][My_pos.x] != 0) Dead = TRUE; if (!Dead) move_robots(FALSE); if (Was_bonus) { move(Y_PROMPT, X_PROMPT); clrtoeol(); move(Y_PROMPT + 1, X_PROMPT); clrtoeol(); Was_bonus = FALSE; } } /* * if the player didn't die, add on the possible bonuses */ if (!Dead) { Was_bonus = FALSE; if (Level == Start_level && Start_level > 1) { move(Y_PROMPT, X_PROMPT); printw("Advance bonus: %d", S_BONUS); refresh(); add_score(S_BONUS); Was_bonus = TRUE; } if (Wait_bonus != 0) { if (!Was_bonus) move(Y_PROMPT, X_PROMPT); else move(Y_PROMPT + 1, X_PROMPT); printw("Wait bonus: %d", Wait_bonus); refresh(); add_score(Wait_bonus); Was_bonus = TRUE; } } }
int main(int argc, char** argv) { char done = 0; char has_on_cmd; char cmd[BUFFER_SIZE]; char const* ret_code = "stay"; char const* cur_funct = "root"; lua_State* L; L = lua_open(); luaL_openlibs(L); if (argc<2){ printf("usage: %s <script_name>\n",argv[0]); return 0; } if (luaL_dofile(L, argv[1])!=0){ fprintf(stderr,"%s\n",lua_tostring(L,-1)); return 0; } lua_getglobal(L, "on_cmd"); //call the lua function on_cmd at each command (for user script) if (lua_isfunction(L,-1))has_on_cmd = 1; else has_on_cmd = 0; lua_pop(L,1); lua_pushstring(L, cur_funct); lua_setglobal(L, "cur_funct"); lua_getglobal(L, cur_funct); if (!lua_isfunction(L,-1)) { puts("The game script must contains a root function where begin."); return EXIT_FAILURE; } lua_call(L,0,1); cur_funct = lua_tostring(L,-1); do{ printf("> "); fgets(cmd, BUFFER_SIZE, stdin); char* lf = strchr(cmd, '\n'); if (lf != NULL){*lf = '\0';lf=NULL;}else{flush_in();} if(has_on_cmd){ lua_getglobal(L, "on_cmd"); //call the lua function on_cmd at each command (for user script) lua_pushstring(L, cmd); lua_call(L,1,2); ret_code = lua_tostring(L,-2); if(!strcmp(ret_code, "new_location")){ cur_funct = lua_tostring(L,-1); } } lua_getglobal(L, cur_funct); //get the lua function with the name of cur_funct lua_pushstring(L, cur_funct); //the name is pushed to call this function later lua_setglobal(L, "cur_funct"); //the lua variable cur_funct is updated with the current name (for user script) if(!has_on_cmd || strcmp(ret_code, "was_cmd")){ if (lua_isfunction(L,-1)){ lua_pushstring(L,cmd); lua_call(L,1,1); const char* prev_code = ret_code; ret_code = lua_tostring(L,-1); if(!strcmp(ret_code, "unknow")) { if(strcmp(prev_code, "new_location"))printf("'%s' impossible\n", cmd); } else if(strcmp(ret_code, "stay")){ cur_funct = ret_code; } } else{ printf("Error, can't call function %s\n", cur_funct); lua_pop(L,1); done=1; } } if(!strcmp(ret_code, "quit")){ done = 1;break; } }while(done == 0); return EXIT_SUCCESS; }
/* * get_move: * Get and execute a move from the player */ void get_move(void) { int c; #ifdef FANCY int lastmove; #endif if (Waiting) return; #ifdef FANCY if (Pattern_roll) { if (Next_move >= Move_list) lastmove = *Next_move; else lastmove = -1; /* flag for "first time in" */ } else lastmove = 0; /* Shut up gcc */ #endif for (;;) { if (Teleport && must_telep()) goto teleport; if (Running) c = Run_ch; else if (Count != 0) c = Cnt_move; #ifdef FANCY else if (Num_robots > 1 && Stand_still) c = '>'; else if (Num_robots > 1 && Pattern_roll) { if (*++Next_move == '\0') { if (lastmove < 0) goto over; Next_move = Move_list; } c = *Next_move; mvaddch(0, 0, c); if (c == lastmove) goto over; } #endif else { over: c = getchar(); if (isdigit(c)) { Count = (c - '0'); while (isdigit(c = getchar())) Count = Count * 10 + (c - '0'); if (c == ESC) goto over; Cnt_move = c; if (Count) leaveok(stdscr, true); } } switch (c) { case ' ': case '.': if (do_move(0, 0)) goto ret; break; case 'y': if (do_move(-1, -1)) goto ret; break; case 'k': if (do_move(-1, 0)) goto ret; break; case 'u': if (do_move(-1, 1)) goto ret; break; case 'h': if (do_move(0, -1)) goto ret; break; case 'l': if (do_move(0, 1)) goto ret; break; case 'b': if (do_move(1, -1)) goto ret; break; case 'j': if (do_move(1, 0)) goto ret; break; case 'n': if (do_move(1, 1)) goto ret; break; case 'Y': case 'U': case 'H': case 'J': case 'K': case 'L': case 'B': case 'N': case '>': Running = true; if (c == '>') Run_ch = ' '; else Run_ch = tolower(c); leaveok(stdscr, true); break; case 'q': case 'Q': if (query("Really quit?")) quit(); refresh(); break; case 'w': case 'W': Waiting = true; leaveok(stdscr, true); goto ret; case 't': case 'T': teleport: Running = false; mvaddch(My_pos.y, My_pos.x, ' '); My_pos = *rnd_pos(); mvaddch(My_pos.y, My_pos.x, PLAYER); leaveok(stdscr, false); refresh(); flush_in(); goto ret; case CTRL('L'): wrefresh(curscr); break; case EOF: break; default: putchar(CTRL('G')); reset_count(); fflush(stdout); break; } } ret: if (Count > 0) if (--Count == 0) leaveok(stdscr, false); }
int main(int argc, char **argv) { struct sockaddr_in server_addr; int ret, max_fd, i; int invalid, sent; fd_set read_tmp, write_tmp; int msg_len; char * msg; queue_l = 0; invalid = 0; sent = 0; if(argc != 3) { printf("Sinstassi: mastermind_client <host> <porta>\n"); exit(1); } //connessione al server FD_ZERO(&read_set); FD_ZERO(&write_set); memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; //ipv4 server_addr.sin_port = htons(atoi(argv[2])); ret = inet_pton(AF_INET, argv[1], &server_addr.sin_addr); if(ret==0) { printf("Indirizzo non valido\n"); exit(1); } sd = socket(PF_INET, SOCK_STREAM, 0); if(sd < 0) { printf("Errore creazione socket\n"); exit(1); } ret = fcntl(sd, F_GETFL, 0); //ritorna la lista dei flag fcntl(sd, F_SETFL, ret| O_NONBLOCK); connect(sd, (struct sockaddr *)&server_addr, sizeof(server_addr)); max_fd = sd; FD_SET(sd, &write_set); memset(&player_info.addr, 0, sizeof(player_info.addr)); player_info.addr.sin_family = AF_INET; write_tmp = write_set; select(max_fd+1, NULL, &write_tmp, NULL, NULL); if(FD_ISSET(sd, &write_tmp)) { int ok; socklen_t len = sizeof(ok); getsockopt(sd, SOL_SOCKET, SO_ERROR, (void*)&ok, &len); if(ok!=0) { printf("Errore connessione: %s\n",strerror(ok) ); exit(1); } FD_CLR(sd, &write_set); } FD_SET(sd, &read_set); printf("Connessione al server %s (porta %d) effettuata con successo\n", argv[1], atoi(argv[2])); show_help(); for(;;) { printf("Inserisci il tuo nome: "); fflush(stdout); scanf("%s",player_info.name); flush_in(); printf("Inserisci la porta di ascolto UDP (>1024): "); fflush(stdout); scanf("%hu", &player_info.port); flush_in(); player_info.addr.sin_port = htons(player_info.port); player_info.addr.sin_addr.s_addr = htonl(INADDR_ANY); msg_len = sizeof(player_info.port) + strlen(player_info.name)+1 ; msg = (char*)malloc(msg_len); memset(msg, 0, msg_len); memcpy(msg, &player_info.port, sizeof(player_info.port)); memcpy(msg+sizeof(player_info.port), player_info.name, strlen(player_info.name)); printf("%hu | %s\n", *(unsigned short *) msg, &msg[2]); queue_add(&queue_l, sd, CL_INFO, msg_len, msg ); free(msg); FD_SET(sd, &write_set); sent = 0; for(;;) { read_tmp = read_set; write_tmp = write_set; select(max_fd+1, &read_tmp, &write_tmp, NULL, NULL); for(i=0; i<max_fd+1; i++) { if(FD_ISSET(i, &write_tmp)) { struct queue *p = queue_search(queue_l, sd); ret = send_msg(p); if(p->step == 4) { queue_remove(&queue_l, &p); FD_CLR(sd, &write_set); } } if(FD_ISSET(i, &read_tmp)) { struct queue *p = queue_search(queue_l, sd); if(p==0) p=queue_add(&queue_l, sd, CL_INFO, 0, 0); ret = rec_msg(sd, p); if(ret <= 0) { printf("Server disconnesso\n"); close(sd); exit(1); } if(p->step == 4) { switch(p->flags) { case CL_OK: sent = 1; invalid = 0; break; case CL_INFO: sent = 1; invalid = 1; default: break; } queue_remove(&queue_l, &p); } } } if(sent == 1) break; } if(invalid == 0) { printf("Info inviate correttamente\n"); break; } else { printf("Nome già in uso\n"); } } //risveglio il descrittore relativo alla tastiera FD_SET(fileno(stdin), &read_set); cd = socket(PF_INET, SOCK_DGRAM, 0); //connessione UDP bind(cd, (struct sockaddr*)&player_info.addr, sizeof(player_info.addr)); max_fd = (max_fd < cd)?cd : max_fd; FD_SET(cd, &read_set); cprintf(""); while(1) { fflush(stdout); struct timeval to, *pto; pto = 0; to.tv_sec = 60; to.tv_usec = 0; read_tmp = read_set; write_tmp = write_set; if(command_mode == 0){ pto = &to; } ret = select(max_fd+1, &read_tmp, &write_tmp, NULL, pto); if(ret < 0) { printf("Errore select\n"); cprintf(""); continue; } if (ret == 0){ queue_add(&queue_l, cd, CL_DISC, 0, 0); queue_add(&queue_l, sd, CL_DISC, 0, 0); FD_SET(cd, &write_set); FD_SET(sd, &write_set); command_mode = 1; cprintf("ti sei disconnesso per inattività\n"); continue; } for(i = 0; i < max_fd+1; i++) { if(FD_ISSET(i, &read_tmp)) { if(i == fileno(stdin)) { //nuovi dati dall'utente if (insertingCombination) { read_comb(comb); } else { read_cmd(); } } else if(i == sd) { //nuovi dati dal server int ret; struct queue *p; p = queue_search(queue_l, i); if(p==0){ p = queue_add(&queue_l, i, CL_UNDEF, 0, 0); } ret = rec_msg(sd, p); if(ret <= 0) { printf("Server disconnesso\n"); close(sd); close(cd); exit(0); } if(p->step == 4) { switch(p->flags) { case CL_WHO: printf("%s\n", p->buffer); cprintf(""); queue_remove(&queue_l, &p); break; case CL_CONN: handle_incoming_connect(p); break; case CL_REF: printf("%s ha rifiutato la partita\n", p->buffer); cprintf(""); queue_remove(&queue_l, &p); cprintf(""); break; case CL_ACC: handle_incoming_accept(p); printf("Digita la combinazione segreta: \n"); cprintf(""); fflush(stdout); insertingCombination = 1; isYourTurn = 1; //read_comb(comb); break; case CL_NEC: printf("nessun giocatore con quel nome\n"); cprintf(""); queue_remove(&queue_l, &p); break; case CL_BUSY: printf("giocatore occupato\n"); cprintf(""); queue_remove(&queue_l, &p); break; case CL_DISC: command_mode = 1; insertingCombination = 0; printf("Il tuo avversario si è disconnesso\n"); cprintf(""); queue_remove(&queue_l, &p); break; default: break; } } } else //ricevo dati da un altro client tramite porta UDP { struct queue* p; p = queue_search(queue_l, i); if(p == 0){ p = queue_add(&queue_l, i, CL_UNDEF, 0, 0); } ret = rec_from(i, p, &opponent_info); if(ret < 0) { printf("Client disconnesso, termino la partita\n"); cprintf(""); FD_CLR(i, &write_set); } if(p->step == 4) { switch(p->flags) { case CL_DISC: command_mode = 1; insertingCombination = 0; printf("Il tuo avversario si è disconnesso\n"); cprintf(""); break; case CL_INS: opponentHasInsertedComb = 1; if (insertingCombination == 0) { printf("%s ha inserito la combinazione, la partita può cominciare\n", opponent_info.name); printf("E' il tuo turno\n"); cprintf(""); opponentHasInsertedComb = 0; } FD_CLR(cd, &write_set); break; case CL_COMB: FD_CLR(cd, &write_set); isYourTurn = 1; printf("è il tuo turno\n"); cprintf(""); handle_combination(p->buffer); break; case CL_ANS:{ command_mode = 0; printf("%s\n", p->buffer ); printf("è il turno di %s\n",opponent_info.name ); cprintf(""); FD_CLR(cd, &write_set); break; } case CL_WIN: command_mode = 1; printf("hai vinto!\n"); cprintf(""); FD_CLR(cd, &write_set); queue_add(&queue_l, sd, CL_WIN, 0, 0); FD_SET(sd, &write_set); isYourTurn =0; break; default: break; } queue_remove(&queue_l, &p); } } } if(FD_ISSET(i, &write_tmp)) { if(i == sd) { //server pronto a ricevere struct queue * p; p = queue_search(queue_l, i); ret = send_msg(p); if(ret < 0){ printf("Errore ret send_msg\n"); close(sd); exit(1); } if(p->step == 4) { switch(p->flags) { case CL_QUIT: //close(sd); close(cd); printf("Disconnesso con successo\n"); cprintf(""); exit(0); case CL_ACC: printf("Digita la combinazione segreta: \n"); cprintf(""); fflush(stdout); insertingCombination = 1; isYourTurn = 0; //read_comb(comb); //queue_add(&queue_l, cd, CL_INS, 0, 0); /*p->flags = CL_INS; p->buffer = 0; free(p->buffer); p->length = 0; p->sd = cd; p->step = 1;*/ //FD_SET(cd, &write_set); command_mode = 0; //printf("E' il turno di %s\n", opponent_info.name); break; default: break; } queue_remove(&queue_l, &p); FD_CLR(sd, &write_set); } } else { //client pronto a ricevere struct queue *p; p = queue_search(queue_l, i); ret = send_to(i, p, &opponent_info); if(p->step == 4) { switch(p->flags) { case CL_DISC: break; default: break; } queue_remove(&queue_l, &p); FD_CLR(i, &write_set); } } } } } return 0; }
void caminho_curto(){ // ----------------------------------- MATRIZ ADJACENTE FILE *arq; int cont, e; arq = fopen("grafo.txt", "r"); char x = 1, atual = 0, aux; int cont2 = 0, cont3 = 1, i = 0, j = 0; cont = qtd_vertices(arq); // CRIANDO MATRIZ int mat[cont][cont]; for(i = 0; i < cont; i++){ for(j = 0; j < cont; j++){ mat[i][j] = 0; } } // CRIA LISTA E ELEMENTO AUXILIAR Lista *L = cria_lista(); ElementoL *E; // CORE fflush(stdout); fseek(arq, 2, SEEK_SET); // Posiciona cursor na segunda linha x = fgetc(arq); while(x != EOF){ while(x != '\n'){ // varre uma linha do txt // Busca Letra na Lista E = busca_letra(x, L); if (E == NULL){ adiciona_letra(L, x , cont2); cont2++; } i = get_numero(busca_letra(x, L)); // ------------- J fseek(arq, 1, SEEK_CUR); x = fgetc(arq); E = busca_letra(x, L); if(E == NULL){ adiciona_letra(L, x , cont3); cont3++; } j = get_numero(busca_letra(x, L)); // ------------- J fseek(arq, 1, SEEK_CUR); fscanf(arq, "%i", &mat[i][j]); x = fgetc(arq); // Pula uma letra } x = fgetc(arq); if(atual != x){ // mudou de linha atual = x; // recebe letra do vertice i++; } } for(i = 0; i + 1 < cont ; i++){ printf("\n"); for(j = 0; j + 1< cont; j++){ printf(" %d |",mat[i][j]); } } // ----------------------------------- char c1, c2; // LER OS VERTICES flush_in(); printf("Entre com o primeiro vértice:"); scanf("%c", &c1); flush_in(); printf("\nEntre com o segundo vértice:"); scanf("%c", &c2); // --- com fila! // printa c1 for(i = 0;i < cont;i++){ // analisa o i atual for(j = 0;j < cont;j++){ // joga coluna atual na fila } } // --- Analisa linha A /*E = busca_letra(c1, L); printf("\n %c - ", c1); for(j = 0; j < cont; j++){ if(mat[E->numero][j] != 0){ // É solução ? // printa // return // Não é solução } }*/ // ------------------- // FECHAR ARQUIVO DO GRAFO fclose(arq); }