1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
//initialize positions for player A
for (short int ship_index = 0; ship_index < 5; ship_index++) {
short int x_pos = rand(); // x_pos, have to research rand() function
x_pos = std::min(x_pos, BOARD_WIDTH);
x_pos = std::max(x_pos, ZERO);
short int y_pos = rand(); // y_pos
y_pos = std::min(y_pos, BOARD_HEIGHT);
y_pos = std::max(y_pos, ZERO);
//need to take care of overlapping holes for ships
bool break_flag = false;
while (true) {
break_flag = false;
x_pos = rand();
x_pos = std::min(x_pos, BOARD_WIDTH);
x_pos = std::max(x_pos, ZERO);
y_pos = rand();
y_pos = std::min(y_pos, BOARD_HEIGHT);
y_pos = std::max(y_pos, ZERO);
//decide if goes across or down here
short int rand_var = rand();
for (short int hole_index = 0; hole_index < ships_A[ship_index].get_num_holes(); hole_index++) {
ships_A[ship_index].set_position_indice(hole_index, x_pos, y_pos);
if (rand_var < 0) // direction is across
x_pos++;
if (rand_var > 0) //direction is down
y_pos++;
}
// check to see if shares any holes with other ships
// if it does, go on
// if it does not, break
for (short int ship_check_index = 0; ship_check_index <= ship_index; ship_check_index++) {
for (short int hole_index = 0; hole_index < ships_A[ship_index].get_num_holes(); hole_index++) {
for (short int hole_check_index = 0; hole_check_index < ships_A[ship_check_index].get_num_holes(); hole_check_index++) {
if ( ships_A[ship_index].get_x_position_index(hole_index) == ships_A[ship_check_index].get_x_position_index(hole_check_index) ) {
//we have a conflict, don't set break flag
#ifdef DEBUG
std::cout << hole_conflict_counter << " ship_index: " << ship_index << " ship_check_index: " << ship_check_index << " hole_index: " << hole_index << " hole_check_index: " << hole_check_index << " we have a conflict, don't break - X index. Ships A\n";
hole_conflict_counter++;
#endif
} else {
//we do not have a conflict, set break flag
#ifdef DEBUG
std::cout << "set break flag\n";
#endif
break_flag = true;
}
if ( ships_A[ship_index].get_y_position_index(hole_index) == ships_A[ship_check_index].get_y_position_index(hole_check_index) ) {
//we have a conflict, don't set break flag
#ifdef DEBUG
std::cout << hole_conflict_counter << " ship_index: " << ship_index << " ship_check_index: " << ship_check_index << " hole_index: " << hole_index << " hole_check_index: " << hole_check_index << " we have a conflict, don't break - Y index, Ships A\n";
hole_conflict_counter++;
#endif
} else {
//we do not have a conflict, set break flag
#ifdef DEBUG
std::cout << "set break flag\n";
#endif
break_flag = true;
}
system("PAUSE");
if (break_flag == true) {
break;
#ifdef DEBUG
std::cout << "breaking for loop 3\n";
#endif
}
}
if (break_flag == true) {
break;
#ifdef DEBUG
std::cout << "breaking for loop 2\n";
#endif
}
}
if (break_flag == true) {
break;
#ifdef DEBUG
std::cout << "breaking for loop 1\n";
#endif
}
}
} // end of while loop
}
|