void randomly_place_ships(char board[10][10], Player *player) { int direction = 0, row_start = 0, col_start = 0; while(check_ship_place (board, row_start, col_start, direction, 5, 'c') == 0) { generate_direction (&direction); generate_random_start_pt (direction, 5, &row_start, &col_start); } place_ship (board, row_start, col_start, direction, 5, 'c'); while(check_ship_place (board, row_start, col_start, direction, 4, 'b') == 0) { generate_direction (&direction); generate_random_start_pt (direction, 4, &row_start, &col_start); } place_ship (board, row_start, col_start, direction, 4, 'b'); while(check_ship_place (board, row_start, col_start, direction, 3, 's') == 0) { generate_direction (&direction); generate_random_start_pt (direction, 3, &row_start, &col_start); } place_ship (board, row_start, col_start, direction, 3, 's'); while(check_ship_place (board, row_start, col_start, direction, 3, 'r') == 0) { generate_direction (&direction); generate_random_start_pt (direction, 3, &row_start, &col_start); } place_ship (board, row_start, col_start, direction, 3, 'r'); while(check_ship_place (board, row_start, col_start, direction, 2, 'd') == 0) { generate_direction (&direction); generate_random_start_pt (direction, 2, &row_start, &col_start); } place_ship (board, row_start, col_start, direction, 2, 'd'); }
void manually_place_ships(char board[10][10], Ship boat) { int direction = 0, row_start = 0, col_start = 0, valid = -1; system("cls"); print_board(board, 10, 10); while(valid != 1) { if(valid == 0) { printf("Please place in a valid spot\n"); valid = -1; } generate_manual_start_pt (&boat, &row_start, &col_start); generate_manual_direction (&direction); if((direction == 1) && ((boat.ship_length + row_start) > 10)) { valid = 0; } else if((direction == 0) && ((boat.ship_length + col_start) > 10)) { valid = 0; } else if(check_ship_place(board, row_start, col_start, direction, boat.ship_length, boat.ship_symbol) == 0) { valid = 0; } else{ valid = 1; } } place_ship (board, row_start, col_start, direction, boat.ship_length, boat.ship_symbol); }
/* Task a: Place <num> ships on the game map. */ void set_ships(unsigned int num) { int n = 0; while (n < num) { if (place_ship(rand() % 10, rand() % 10, rand() % 2)) n++; } }
/* Task a: Place <num> ships on the game map. */ void set_ships(unsigned int num) { for (unsigned int i = 0; i < num; i++) { int xpos; int ypos; int dir; do { xpos = rand() % xsize; ypos = rand() % ysize; dir = rand() % 2; } while (place_ship(xpos, ypos, dir) == 0); } }
int main() { std::srand( std::time(nullptr) ) ; char board[N][N] ; for( auto& row : board ) for( char& c : row ) c = empty ; // empty // place 12 ships randomly for( int i = 0 ; i < 20 ; ++i ) place_ship(board) ; for( auto& row : board ) { for( char c : row ) std::cout << c ; std::cout << '\n' ; } }