int main(int argc, char *argv[]) { srand(time(NULL)); int die1=0, die2=0; int numberPlayers =argc-1; std::vector<Monopoly::Player> players; players=init_players(players, numberPlayers, argv); std::cout<<std::endl; for(int i=0; i<numberPlayers; i++) { std::cout<<players[i].get_name()<<" at position "<<players[i].get_position()<<" and money £"<< players[i].get_money()<<std::endl; } std::cout<<std::endl; for(int j=0; j<30; j++) { for(int i=0; i<numberPlayers; i++) { std::cout<<players[i].get_name()<<" at position "<<players[i].get_position()<<" shake die..."; die1=roll_die(); die2=roll_die(); std::cout<<die1 <<" "<<die2; players[i].move(die1+die2); std::cout<<" new position "<<players[i].get_position()<<std::endl; } } players.clear(); return 0; }
void Entity::SearchWay(double& next_x, double& next_y ){ for(int j=0; j<=3; ++j){ roll_die(); } int tmp=roll_die(); if(tmp==1){ GoRight(); } else if(tmp==2){ GoUp(); } else if(tmp==3){ GoLeft(); } else if(tmp==4){ GoDown(); } }
void roll_player(int rolls[], const int ndice) { int i; for (i = 0; i < ndice; i++) { rolls[i] = roll_die(6); } // std::cout << "Original: " << rolls[0] << '\n'; sort_int_dsc(rolls, ndice); // std::cout << "Sorted: " << rolls[0] << '\n'; }
/* read dice on standard input, write results on standard output */ void * roll_dice(void *v) { struct sockets * s = (struct sockets *)v; char inbuf[512]; /* think globally, program defensively */ if (!s || !s->out || !s->in) return NULL; fprintf(s->out, "enter die rolls, or q to quit\n"); while (fgets(inbuf, sizeof(inbuf), s->in) != 0) { int dice; int size; if (inbuf[0] == 'q') { fprintf(s->out, "buh-bye!\n"); if (s->local == 0) { shutdown(fileno(s->out), SHUT_RDWR); } fclose(s->out); fclose(s->in); if (s->local == 0) { free(s); } return 0; } if (sscanf(inbuf, "%dd%d", &dice, &size) != 2) { fprintf(s->out, "Sorry, but I couldn't understand that.\n"); } else { int i; int total = 0; for (i = 0; i < dice; ++i) { int x = roll_die(size); total += x; fprintf(s->out, "%d ", x); fflush(s->out); if (do_sleep) { timespec ts; ts.tv_sec = 0; ts.tv_nsec = 1000 * 10; nanosleep(ts); } } fprintf(s->out, "= %d\n", total); } } return 0; }
int main() { for(int i = 0; i < 10; ++i) { std::cout << roll_die() << std::endl; } }
int main(void) { int die1 = 0, die2 = 0, sum_dice = 0, player_point = 0, choice = 0, point_value = 0; double bank_bal = 0.0, wager = 0.0; srand(time(NULL)); while(choice != 3) { do { system("cls"); printf("1.) Print rules\n2.) Play Game\n3.) Exit game\n"); scanf("%d", &choice); }while(choice < 1 || choice > 3); if(choice == 1) { print_game_rules(); choice = 0; } else if(choice == 2) { system("cls"); bank_bal = get_bank_balance(); while( bank_bal > 0) { printf("Current bank balance: $%.2lf\n", bank_bal); wager = get_wager_amount(); check_wager_amount(wager, bank_bal); die1 = roll_die(); die2 = roll_die(); printf("Die number 1: %d\n", die1); printf("Die number 2: %d\n", die2); sum_dice = calculate_sum_dice(die1, die2); if(is_win_loss_or_point(sum_dice) == 1) { bank_bal = adjust_bank_balance(bank_bal, wager, 1); printf("WINNER!\n"); pause_clr(); } else if(is_win_loss_or_point(sum_dice) == 0) { bank_bal = adjust_bank_balance(bank_bal, wager, 0); printf("LOSER!\n"); pause_clr(); } else { point_value = sum_dice; sum_dice = 0; while(is_point_loss_or_neither(sum_dice, point_value) == -1) { system("pause"); die1 = roll_die(); die2 = roll_die(); printf("Die number 1: %d\n", die1); printf("Die number 2: %d\n", die2); sum_dice = calculate_sum_dice(die1, die2); if(is_point_loss_or_neither(sum_dice, point_value) == 1) { bank_bal = adjust_bank_balance(bank_bal, wager, 1); printf("WINNER!\n"); pause_clr(); } else if(is_point_loss_or_neither(sum_dice, point_value) == 0) { bank_bal = adjust_bank_balance(bank_bal, wager, 0); printf("LOSER!\n"); pause_clr(); } } } } choice = 0; } else if(choice == 3) { system("cls"); printf("Thank You for playing!\n"); } } return 0; }
int main(void) { int i; int rd_bad1; for (i = 0; i < 32768; i++) { rd_bad1 = roll_die(32768); assert(rd_bad1 == -1); } int rd_bad2; for (i = 0; i < 32768; i++) { rd_bad2 = roll_die(0); assert(rd_bad2 == -1); } int rd_good1; for (i = 0; i < 32768; i++) { rd_good1 = roll_die(1); assert(rd_good1 == 1); } int rd_good2; for (i = 0; i < 32768; i++) { rd_good2 = roll_die(2); assert(rd_good2 == 1 || rd_good2 == 2); } int rd_good3; for (i = 0; i < 32768; i++) { rd_good3 = roll_die(6); assert(1 <= rd_good3 && rd_good3 <= 6); } int rd_good4; for (i = 0; i < 32768; i++) { rd_good4 = roll_die(8); assert(1 <= rd_good4 && rd_good4 <= 8); } int rd_good5; for (i = 0; i < 32768; i++) { rd_good5 = roll_die(10); assert(1 <= rd_good5 && rd_good5 <= 10); } int sd0 = sumdice(0, 0); assert(sd0 == 0); int sd1 = sumdice(0, 1); assert(sd1 == 0); int sd2 = sumdice(1, 0); assert(sd2 == -1); int sd3 = sumdice(1, 1); assert(sd3 == 1); int sd4 = sumdice(2, 1); assert(sd4 == 2); for (i = 0; i < 32768; i++) { int sd5 = sumdice(1, 2); assert(sd5 == 1 || sd5 == 2); } for (i = 0; i < 32768; i++) { int sd6 = sumdice(2, 1); assert(sd6 == 2); } for (i = 0; i < 32768; i++) { int sd7 = sumdice(2, 2); assert(2 <= sd7 && sd7 <= 4); } for (i = 0; i < 32768; i++) { int sd8 = sumdice(0, 2); assert(sd8 == 0); } for (i = 0; i < 32768; i++) { int sd9 = sumdice(2, 0); assert(sd9 == -1); } for (i = 0; i < 32768; i++) { int sd10 = sumdice(100, 100); assert(100 <= sd10 && sd10 <= 10000); } for (i = 0; i < 32768; i++) { int sd11 = sumdice(1, 100); assert(1 <= sd11 && sd11 <= 100); } for (i = 0; i < 32768; i++) { int sd12 = sumdice(100, 1); assert(sd12 = 100); } for (i = 0; i < 32768; i++) { int sd13 = sumdice(100, 2); assert(100 <= sd13 && sd13 <= 200); } for (i = 0; i < 32768; i++) { int sd14 = sumdice(10, 10); assert(10 <= sd14 && sd14 <= 1000); } return 0; }
/** * void process_dgram(char *dgram, struct sockaddr_in *addr) * * Processes accepted datagram. Checks if sequential ID is correct, if it's lower, * we resend the ACK packet and dont bother with that datagram anymore, because * it was already processed before. */ void process_dgram(char *dgram, struct sockaddr_in *addr) { /* Protocol token */ char *token; /* Token length */ int token_len; /* Command */ char *type; /* Generic char buffer */ char *generic_chbuff; /* Sequential ID of received packet */ int packet_seq_id; /* Client which we receive from */ client_t *client; /* Generic unsigned int var */ unsigned int generic_uint; /* String representation of address */ char addr_str[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &addr->sin_addr, addr_str, INET_ADDRSTRLEN); /* Log */ sprintf(log_buffer, "DATA_IN: %s <--- %s:%d", dgram, addr_str, htons(addr->sin_port) ); log_line(log_buffer, LOG_DEBUG); token = strtok(dgram, ";"); token_len = strlen(token); generic_chbuff = strtok(NULL, ";"); packet_seq_id = (int) strtol(generic_chbuff, NULL, 0); /* Check if datagram belongs to us */ if( (token_len == strlen(STRINGIFY(APP_TOKEN))) && strncmp(token, STRINGIFY(APP_TOKEN), strlen(STRINGIFY(APP_TOKEN))) == 0 && packet_seq_id > 0) { type = strtok(NULL, ";"); /* New client connection */ if(strncmp(type, "CONNECT", 7) == 0) { add_client(addr); client = get_client_by_addr(addr); if(client) { send_ack(client, 1, 0); send_reconnect_code(client); /* Release client */ release_client(client); } } /* Reconnect */ else if(strncmp(type, "RECONNECT", 9) == 0) { client = get_client_by_index(get_client_index_by_rcode(strtok(NULL, ";"))); if(client) { /* Sends ACK aswell after resetting clients SEQ_ID */ reconnect_client(client, addr); /* Release client */ release_client(client); } } /* Client should already exist */ else { client = get_client_by_addr(addr); if(client != NULL) { /* Check if expected seq ID matches */ if(packet_seq_id == client->pkt_recv_seq_id) { /* Get command */ if(strncmp(type, "CREATE_GAME", 11) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); create_game(client); } /* Receive ACK packet */ else if(strncmp(type, "ACK", 3) == 0) { recv_ack(client, (int) strtoul(strtok(NULL, ";"), NULL, 10)); update_client_timestamp(client); } /* Close client connection */ else if(strncmp(type, "CLOSE", 5) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); leave_game(client); remove_client(&client); } /* Keepalive loop */ else if(strncmp(type, "KEEPALIVE", 9) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); } /* Join existing game */ else if(strncmp(type, "JOIN_GAME", 9) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); join_game(client, strtok(NULL, ";")); } /* Leave existing game */ else if(strncmp(type, "LEAVE_GAME", 10) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); leave_game(client); } /* Start game */ else if(strncmp(type, "START_GAME", 10) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); start_game(client); } /* Rolling die */ else if(strncmp(type, "DIE_ROLL", 8) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); roll_die(client); } /* Moving figure */ else if(strncmp(type, "FIGURE_MOVE", 11) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); /* Parse figure id */ generic_chbuff = strtok(NULL, ";"); generic_uint = (unsigned int) strtoul(generic_chbuff, NULL, 10); move_figure(client, generic_uint); } else if(strncmp(type, "MESSAGE", 7) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); broadcast_message(client, strtok(NULL, ";")); } } /* Packet was already processed */ else if(packet_seq_id < client->pkt_recv_seq_id && strncmp(type, "ACK", 3) != 0) { send_ack(client, packet_seq_id, 1); } /* If client didnt close conection */ if(client != NULL) { /* Release client */ release_client(client); } } } } }