#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX 10
#define SHIPS 5

typedef struct {
   char ch;
   int ship;
   int hit;
} sea_t;

typedef struct {
   char type[20];
   char name[30];
   int size;
   int x;
   int y;
   int dir;
   int hits;
} ship_t;

void init();
void display();
void deployAll();
int deploy(int i);
int deployable(int s, int x, int y, int dir);
int randInt(int i);

sea_t sea[MAX][MAX];
ship_t ship[MAX];

// -----------------  Main Program ------------------------
int main() {
   init();
   display();
   deployAll();
   return 0;
}

// --------------- Initialize the Sea and Ships -----------
void init() {
   int i, j;
   sea_t null_sea = {'+', -1, 0};

   srand(time(NULL));

   for (i=0; i<MAX; i++) {
      for (j=0; j<MAX; j++) {
         sea[i][j] = null_sea;
      }
   }

   FILE *file;
   file = fopen("ships.dat", "rb");
   fread(&ship, sizeof(ship_t), SHIPS, file);
   fclose(file);

   printf("\n\nYour fleet consists of the following!\n");
   printf("  TYPE                       NAME                       SIZE\n");
   for (i=0; i<SHIPS; i++) {
      printf("%-25s  %-30s %d\n", ship[i].type, ship[i].name, ship[i].size);
   }
}
 
// --------------- Display the Sea -------------------------
void display() {
   int i, j;
   printf("\n\n     ");
   for (i=0; i<MAX; i++) printf("  %d  ", i);
   printf("\n\n");
   for (i=0; i<MAX; i++) {
      printf(" %d   ", i);
      for (j=0; j<MAX; j++) {
         printf("  %c  ", sea[i][j]);
      }
      printf("\n\n");
   }
}

// ---------------- Deploy the Ships -----------------------
void deployAll() {
   int i;
   for (i=0;i<SHIPS;i++) {
      printf("Deploying ship %d\n", i);
      while (!deploy(i));
   }
}

// --------------- Try to Deploy a Ship --------------------
int deploy(int i) {
   int x = randInt(MAX);
   int y = randInt(MAX);
   int dir = randInt(2);
   int j;

   if (deployable(ship[i].size, x, y, dir)) {
      ship[i].x = x;
      ship[i].y = y;
      ship[i].dir = dir;
      for (j=0; j<ship[i].size; j++) {
         if (dir)
            sea[x][y++].ship = i;
         else
            sea[x++][y].ship = i;
      }
      printf("Ship %d at (%d,%d) direction %d\n", i, x, y, dir);
      return 1;
   }
   else
      return 0;
}

// ------------------- Is Ship Deployable Here? -----------
int deployable(int s, int x, int y, int dir) {
   int i;
   int ok = 1;
   if ((dir && y + s >= MAX) || (!dir && x + s >= MAX)) {
      printf("Oops, out of bounds\n");
      return 0;
   }
   if (dir)
      for (i=0; i<s; i++)
         if (sea[x][y++].ship != -1) {
            ok = 0;
            printf("Oops, overlay vert\n");
            break;
          }
   else
      for (i=0; i<s; i++) 
         if (sea[x++][y].ship != -1) {
            ok = 0;
            printf("Oops, overlay horiz\n");
            break;
         }
   return ok;
}

// -------------------- Random Integer ---------------------
int randInt(int i) {
   return (int)((double) rand()/RAND_MAX*i);
}
 
