fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define INVALID -1
  6.  
  7. #define NROWS 11
  8. #define NCOLS 11
  9. #define NCELLS (NROWS * NCOLS)
  10.  
  11. enum ShipID { INVALID_ID=-1, WATER=0, NAYARXIDA=1, ANTITORPILIKO=2, NSHIPS };
  12. enum ShipDir { INVALID_DIR=-1, HORZ=0, VERT=1, NDIRS };
  13.  
  14. #define VALID_ID( id ) ( (id) > INVALID_ID && (id) < NSHIPS )
  15. #define VALID_DIR( dir ) ( (dir) > INVALID_DIR && (dir) < NDIRS )
  16. #define VALID_POS( pos ) ( (pos) > -1 && (pos) < NCELLS )
  17.  
  18. typedef enum { FALSE=0, TRUE } Bool;
  19.  
  20. typedef struct Cell {
  21. int shipId; /* ti periexei to keli (tipota, nero h kapoio ploio) */
  22. Bool isbombed; /* einai h oxi xtyphmeno to keli ? */
  23. } Cell;
  24.  
  25. typedef struct Ship {
  26. enum ShipID id; /* typos ploioy */
  27. int len; /* mhkos se kelia plegmatos */
  28. int pos; /* thesh sto plegma */
  29. enum ShipDir dir; /* prosanatolismos (HORZ h VERT) */
  30. } Ship;
  31.  
  32. /* -------------------------------------------
  33.  *
  34.  * -------------------------------------------
  35.  */
  36. Bool grid_init( Cell grid[ NCELLS ] )
  37. {
  38. int n = 0;
  39.  
  40. if ( !grid )
  41. return FALSE;
  42.  
  43. for (n=0; n < NCELLS; n++) {
  44. grid[ n ].shipId = WATER;
  45. grid[ n ].isbombed = FALSE;
  46. }
  47.  
  48. return TRUE;
  49. }
  50.  
  51. /* -------------------------------------------
  52.  *
  53.  * -------------------------------------------
  54.  */
  55. Bool grid_print( const Cell grid[ NCELLS ] )
  56. {
  57. int n = 0;
  58.  
  59. if ( !grid )
  60. return FALSE;
  61.  
  62. for (n=0; n < NCELLS; n++)
  63. {
  64. if ( grid[ n ].shipId == WATER )
  65. putchar('-');
  66. else if ( grid[ n ].shipId == NAYARXIDA )
  67. putchar('N');
  68. else if ( grid[ n ].shipId == ANTITORPILIKO )
  69. putchar('A');
  70. else
  71. putchar('!'); /* ERROR */
  72.  
  73. if ( n % NCOLS == NCOLS-1 )
  74. putchar('\n');
  75. }
  76.  
  77. return TRUE;
  78. }
  79.  
  80. /* -------------------------------------------
  81.  *
  82.  * -------------------------------------------
  83.  */
  84. Bool ship_fits_ongrid( Ship *ship, Cell grid[ NCELLS ] )
  85. {
  86. int n, endpos = INVALID;
  87.  
  88. if ( !ship || !grid || !VALID_ID(ship->id) || !VALID_DIR(ship->dir) || !VALID_POS(ship->pos) )
  89. return FALSE;
  90.  
  91. if ( ship->dir == HORZ )
  92. {
  93. endpos = ship->pos + ship->len - 1;
  94. if ( ship->pos / NCOLS != endpos / NCOLS ) /* START & END not in same row*/
  95. return FALSE;
  96.  
  97. for (n = ship->pos; n <= endpos; n++ )
  98. if ( grid[ n ].shipId != WATER )
  99. return FALSE;
  100. }
  101.  
  102. else if ( ship->dir == VERT )
  103. {
  104. endpos = ship->pos + (ship->len-1) * NCOLS;
  105. if ( endpos / NCOLS > NCOLS-1 ) /* END goes beyond last row */
  106. return FALSE;
  107.  
  108. for (n=ship->pos; n <= endpos; n += NCOLS)
  109. if ( grid[ n ].shipId != WATER )
  110. return FALSE;
  111. }
  112.  
  113. return TRUE;
  114. }
  115.  
  116. /* -------------------------------------------
  117.  *
  118.  * -------------------------------------------
  119.  */
  120. Bool ship_put_ongrid( Ship *ship, Cell grid[ NCELLS ] )
  121. {
  122. int n, endpos = INVALID;
  123.  
  124. if ( !ship || !grid || !VALID_ID(ship->id) || !VALID_DIR(ship->dir) || !VALID_POS(ship->pos) )
  125. return FALSE;
  126.  
  127. if ( ship->dir == HORZ )
  128. {
  129. endpos = ship->pos + ship->len - 1;
  130. for (n = ship->pos; n <= endpos; n++ )
  131. grid[ n ].shipId = ship->id;
  132. }
  133. else if ( ship->dir == VERT )
  134. {
  135. endpos = ship->pos + (ship->len-1) * NCOLS;
  136. for (n=ship->pos; n <= endpos; n += NCOLS)
  137. grid[ n ].shipId = ship->id;
  138. }
  139. return TRUE;
  140. }
  141.  
  142. /* -------------------------------------------
  143.  *
  144.  * -------------------------------------------
  145.  */
  146. Bool ship_put_ongrid_randomly( Ship *ship, Cell grid[ NCELLS ] )
  147. {
  148. if ( !ship || !VALID_ID(ship->id) || !grid )
  149. return FALSE;
  150.  
  151. do {
  152. ship->pos = rand() % NCELLS;
  153. ship->dir = rand() % NDIRS;
  154. } while ( !ship_fits_ongrid( ship, grid ) );
  155.  
  156. ship_put_ongrid( ship, grid );
  157. printf( "Ship %d was put at pos %d (%d,%d) with direction %d\n",
  158. ship->id, ship->pos, (ship->pos / NCOLS), (ship->pos % NCOLS), ship->dir );
  159.  
  160. return TRUE;
  161. }
  162.  
  163. /* -------------------------------------------
  164.  *
  165.  * -------------------------------------------
  166.  */
  167. int main( void )
  168. {
  169. Cell grid[ NCELLS ]; /* to plegma mas (tha to arxikopoihsoyme argotera: game_init) */
  170. Ship navarxida = { .id=NAYARXIDA, .len=5, .pos=INVALID, .dir=INVALID };
  171. Ship antitorpiliko = { .id=ANTITORPILIKO, .len=4, .pos=INVALID, .dir=INVALID };
  172.  
  173. srand( time(NULL) );
  174. grid_init( grid );
  175. ship_put_ongrid_randomly( &navarxida, grid );
  176. ship_put_ongrid_randomly( &antitorpiliko, grid );
  177. grid_print( grid );
  178.  
  179. return 0;
  180. }
  181.  
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
Ship 1 was put at pos 113 (10,3) with direction 0
Ship 2 was put at pos 72 (6,6) with direction 1
-----------
-----------
-----------
-----------
-----------
-----------
------A----
------A----
------A----
------A----
---NNNNN---