program not breaking when it should

I'm creating a game of battleship, and I'm having a problem with my program where it is not breaking when it should. In the section with three for loops that looks for concurrent holes between ships, if the holes coincide it sets the break flag so it will break through the three for loops and tries again with a different random starting hole than before (its picks a random hole and lays out the rest of the holes for that ship from there). Unfortunately, it does not appear to be breaking through those for loops at all (it should). Maybe there is something you will see that I did not.

Also, it seems to be doing the same thing over and over. here's a sample of the output:


Battleship 1.0 by Timothy Grindall

0 ship_index: 0 ship_check_index: 0 hole_index: 0 hole_check_index: 0 we have a conflict, don't break - X index. Ships A
1 ship_index: 0 ship_check_index: 0 hole_index: 0 hole_check_index: 0 we have a conflict, don't break - Y index, Ships A
Press any key to continue . . .
2 ship_index: 0 ship_check_index: 0 hole_index: 0 hole_check_index: 1 we have a conflict, don't break - X index. Ships A
set break flag
Press any key to continue . . .
3 ship_index: 0 ship_check_index: 0 hole_index: 0 hole_check_index: 0 we have a conflict, don't break - X index. Ships A
4 ship_index: 0 ship_check_index: 0 hole_index: 0 hole_check_index: 0 we have a conflict, don't break - Y index, Ships A
Press any key to continue . . .
5 ship_index: 0 ship_check_index: 0 hole_index: 0 hole_check_index: 1 we have a conflict, don't break - X index. Ships A
set break flag
Press any key to continue . . .
6 ship_index: 0 ship_check_index: 0 hole_index: 0 hole_check_index: 0 we have a conflict, don't break - X index. Ships A
7 ship_index: 0 ship_check_index: 0 hole_index: 0 hole_check_index: 0 we have a conflict, don't break - Y index, Ships A
Press any key to continue . . .
8 ship_index: 0 ship_check_index: 0 hole_index: 0 hole_check_index: 1 we have a conflict, don't break - X index. Ships A
set break flag
Press any key to continue . . .


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
	}


edit:I've found the problem with the code and fixed it - no worries.
Last edited on
Topic archived. No new replies allowed.