fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h> // Para 'memset( )`.
  5.  
  6. #define SIZE 10
  7. #define BOMBS 10
  8.  
  9. int main( void ) {
  10. int bombs = BOMBS + 1;
  11. char map[SIZE * SIZE];
  12. int pos;
  13.  
  14. srand( time( NULL ) );
  15.  
  16. // Inicializamos el array con puntos '.'.
  17. memset( map, '.', SIZE * SIZE );
  18.  
  19. // MIENTRAS queden bombas sin esablecer.
  20. while( --bombs ) {
  21. // Repetimos HASTA que en el lugar NO HAY bomba.
  22. do {
  23. pos = rand( ) % ( SIZE * SIZE );
  24. } while( map[pos] == 'X' );
  25.  
  26. // NO HAY bomba en esa posiciĆ³n. La ponemos.
  27. map[pos] = 'X';
  28. }
  29.  
  30. // Mostramos el tablero.
  31. for( pos = 0; pos < ( SIZE * SIZE ); pos += 10 ) {
  32. // SABEMOS las filas son de longitud fija.
  33. printf("%.*s\n", SIZE, map + pos );
  34. }
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
..X.......
..........
..X...X...
......X.X.
..........
X.........
..........
X.....X...
.....XX...
..........