fork download
  1. /*********************************************************//**
  2.  * Author: migf1
  3.  * from Book: C Programming, A Modern Approach (2nd edition)
  4.  * Chapter: 8 (Arrays)
  5.  * Programming project: 9 (page 179) - Better implementation
  6.  ************************************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12.  
  13. #define NROWS 10 /* table height in rows */
  14. #define NCOLS 10 /* table width in cols */
  15. #define NDIRS 4 /* # next-mv directions */
  16. //enum Dir { UP=0, DN, LF, RT, NDIRS }; /* allowed mv directions*/
  17.  
  18. #define EMPTY '.' /* empty cells' content */
  19. #define USED "ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* used cells' contents */
  20. #define ISEMPTY(pos, tab) ( EMPTY == (tab)[ (pos) ] ) /* is tab[pos] empty? */
  21.  
  22. #define ROW(pos) ( (pos) / NCOLS ) /* calc row-index of pos*/
  23. #define COL(pos) ( (pos) % NCOLS ) /* calc col-index of pos*/
  24.  
  25. #define POSUP(pos) ( (pos) - NCOLS ) /* calc pos minus 1 row */
  26. #define POSDN(pos) ( (pos) + NCOLS ) /* calc pos plus 1 row */
  27. #define POSLF(pos) ( (pos) - 1 ) /* calc pos minus 1 col */
  28. #define POSRT(pos) ( (pos) + 1 ) /* calc pos plus 1 col */
  29.  
  30. #define myMIN(x,y) ( (x) < (y) ? (x) : (y) )
  31.  
  32. /*********************************************************//**
  33.  * Store into avail[] all those positions adjacent to pos which are available to
  34.  * be selected for the next move.
  35.  * Return the count of the available posistions found.
  36.  ************************************************************/
  37. int avail_count( const int pos, const char tab[], int avail[ NDIRS ] )
  38. {
  39. int temp = 0;
  40. int navail = 0;
  41.  
  42. /* is row-up'ed pos available? */
  43. if ( ROW(pos) > 0 && ISEMPTY( (temp=POSUP(pos)), tab ) )
  44. avail[ navail++ ] = temp;
  45.  
  46. /* is row-dn'ed pos available? */
  47. if ( ROW(pos) < NROWS-1 && ISEMPTY( (temp=POSDN(pos)), tab ) )
  48. avail[ navail++ ] = temp;
  49.  
  50. /* is col-lf'ed pos available? */
  51. if ( COL(pos) > 0 && ISEMPTY( (temp=POSLF(pos)), tab ) )
  52. avail[ navail++ ] = temp;
  53.  
  54. /* is col-rt'ed pos available? */
  55. if ( COL(pos) < NCOLS-1 && ISEMPTY( (temp=POSRT(pos)), tab ) )
  56. avail[ navail++ ] = temp;
  57.  
  58. return navail;
  59. }
  60.  
  61. /*********************************************************//**
  62.  * Print the contents of the board.
  63.  ************************************************************/
  64. void tab_print( const char tab[] )
  65. {
  66. for (int i=0; i < NROWS * NCOLS; i++)
  67. {
  68. if ( i != 0 && i % NCOLS == 0 )
  69. putchar('\n');
  70. printf( "%c ", tab[i] );
  71. }
  72.  
  73. putchar('\n');
  74. }
  75.  
  76. /*********************************************************//**
  77.  *
  78.  ************************************************************/
  79. int main( void )
  80. {
  81. /* total # of moves allowed */
  82. const int NMOVES = myMIN( NROWS*NCOLS, (int)(strlen(USED) - 1) );
  83. int pos = 0, imove = 0; /* current pos & moves counter */
  84. char tab[ NROWS * NCOLS ]; /* our un-initialized board */
  85. int avail[ NDIRS ] = {-1,-1,-1,-1}; /* available neighbor positions */
  86.  
  87. srand( (unsigned)time(NULL) ); /* init pseudo-random seed */
  88. memset( tab, EMPTY, sizeof(tab) ); /* init table with dots */
  89.  
  90. tab[0] = USED[0]; /* start with filled 1st pos */
  91. while ( imove < NMOVES )
  92. {
  93. int navail = avail_count(pos, tab, avail);
  94. if ( 0 == navail )
  95. break;
  96. pos = avail[ rand() % navail ];
  97. tab[ pos ] = USED[ ++imove ];
  98. }
  99. tab_print( tab );
  100.  
  101. exit( EXIT_SUCCESS );
  102. }
  103.  
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
A B C . . . . . . . 
F E D . . . . . . . 
G . K L . . . . . . 
H I J M N . . . . . 
. . . P O . . X Y . 
. . . Q R U V W Z . 
. . . . S T . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . .