Don't like ads? PRO users don't see any ads ;-)
Guest

Craig Battleship

By: a guest on May 8th, 2012  |  syntax: C  |  size: 11.27 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. #define NUMROWS 10
  6. #define NUMCOLS 10
  7.  
  8. int checkBoard(char**);
  9. void printBoard(char**);
  10. void copyBoard(char**, char**);
  11. void playerMove(char**);
  12. int* serverMove(char**);
  13. int* checkForHits(char**, int, int);
  14. char* placeDestroyer();
  15. char* placeSub();
  16. char* placePatrol();
  17. char** makeOcean();
  18. void freeOcean(char **ocean);
  19.  
  20. int checker();
  21. int coord1;
  22. int coord2;
  23. char coord1a;
  24. int direction;
  25. int size;
  26.  
  27.  
  28.  
  29. int main () {
  30.  
  31.         /*char **ocean =
  32.         {
  33.                 {' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', ' '},
  34.                 {'1', '-', '-', '-', '-', '-', '-', '-', '-', '1'},
  35.                 {'2', '-', '-', '-', '-', '-', '-', '-', '-', '2'},
  36.                 {'3', '-', '-', '-', '-', '-', '-', '-', '-', '3'},
  37.                 {'4', '-', '-', '-', '-', '-', '-', '-', '-', '4'},
  38.                 {'5', '-', '-', '-', '-', '-', '-', '-', '-', '5'},
  39.                 {'6', '-', '-', '-', '-', '-', '-', '-', '-', '6'},
  40.                 {'7', '-', '-', '-', '-', '-', '-', '-', '-', '7'},
  41.                 {'8', '-', '-', '-', '-', '-', '-', '-', '-', '8'},
  42.                 {' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', ' '}
  43.         };*/
  44.         char **ocean = makeOcean();
  45.         printBoard(ocean);
  46.         char **ocean2 = makeOcean();
  47.  
  48.  
  49.         int bstatus;
  50.         //bstatus = checkBoard(ocean);
  51.         //printf("\nStatus: %i\n", bstatus);
  52.         int col;
  53.         int row;
  54.         int i;
  55.         int test = 0;
  56.         srand(time(NULL));
  57.        
  58.         //placing Destroyer
  59.         do
  60.         {
  61.                 placeDestroyer();
  62.         test = checker(ocean);
  63.         col = coord1a - 'A' + 1;
  64.                 row = coord2;
  65.                 if (test == 1)
  66.                 {
  67.                 for (i = 0; i < size; i++)
  68.                 {
  69.                         ocean[row][col] = 'D';
  70.                         if (direction == 1)
  71.                                 row--;
  72.                         if (direction == 2)
  73.                                 col++;
  74.                         if (direction == 3)
  75.                                 row++;
  76.                         if (direction == 4)
  77.                                 col--;
  78.                 }
  79.         }
  80.     }while (test == 0);
  81.     test = 0;
  82.    
  83.     //placing Sub
  84.     do
  85.     {
  86.         placeSub();
  87.         test = checker(ocean);
  88.                 col = coord1a - 'A' + 1;
  89.                 row = coord2;
  90.         if (test == 1)
  91.                 {
  92.                 for (i = 0; i < size; i++)
  93.                 {
  94.                         ocean[row][col] = 'S';
  95.                         if (direction == 1)
  96.                                 row--;
  97.                         if (direction == 2)
  98.                                 col++;
  99.                         if (direction == 3)
  100.                                 row++;
  101.                         if (direction == 4)
  102.                                 col--;
  103.                 }
  104.         }
  105.     }while (test == 0);
  106.     test = 0;
  107.    
  108.     //placing Patrol
  109.     do
  110.     {
  111.         placePatrol();
  112.         test = checker(ocean);
  113.                 col = coord1a - 'A' + 1;
  114.                 row = coord2;
  115.         if (test == 1)
  116.                 {
  117.                 for (i = 0; i < size; i++)
  118.                 {
  119.                         ocean[row][col] = 'P';
  120.                         if (direction == 1)
  121.                                 row--;
  122.                         if (direction == 2)
  123.                                 col++;
  124.                         if (direction == 3)
  125.                                 row++;
  126.                         if (direction == 4)
  127.                                 col--;
  128.                 }
  129.         }
  130.     }while (test == 0);
  131.        
  132.         printBoard(ocean);
  133.         copyBoard(ocean2,ocean);
  134.         printBoard(ocean2);
  135.         printf("\n");
  136.        
  137.         //the following is horrible code, really, since this should be done in the function playerMove, but
  138.         //I'm too lazy to error check and make it work at the moment -- logically it's fine for now
  139.        
  140.         //int row, col;
  141.         /*int incorrect;
  142.         do
  143.         {
  144.                 incorrect = 0;
  145.                 int *move = playerMove(ocean);
  146.                 row = move[0];
  147.                 col = move[1];
  148.                 incorrect = move[2];
  149.         }while(incorrect == -1);
  150.        
  151.         if(incorrect == 0)
  152.                 ocean[row][col] = 'X';
  153.         else if(incorrect == 1)
  154.                 ocean[row][col] = 'O';
  155.         */     
  156.        
  157.         for(;;)
  158.         {
  159.                 playerMove(ocean);
  160.                 printBoard(ocean);
  161.         }
  162.        
  163.         bstatus = checkBoard(ocean);
  164.         printf("\nStatus: %i\n", bstatus);
  165.                
  166.         printBoard(ocean);
  167.         freeOcean(ocean);
  168.         freeOcean(ocean2);
  169.         exit(1);
  170. }
  171. void freeOcean(char **ocean)
  172. {
  173.         int i;
  174.         for (i = 0; i < NUMROWS; i++){  
  175.                 free(ocean[i]);  
  176.     }  
  177.     free(ocean);
  178. }
  179. char** makeOcean()
  180. {
  181.         char** anOcean;
  182.         int i, j;
  183.         anOcean = (char**) malloc(NUMROWS*sizeof(char*));  
  184.         for (i = 0; i < NUMROWS; i++)  
  185.            anOcean[i] = (char*) malloc(NUMCOLS*sizeof(char));
  186.        
  187.         //fill with values
  188.         for(i = 0; i < NUMROWS; i++)
  189.         {
  190.                 if(i == 0 || i == NUMROWS -1)
  191.                 {
  192.                         anOcean[i][0] = '-';
  193.                         anOcean[i][1] = 'A';
  194.                         anOcean[i][2] = 'B';
  195.                         anOcean[i][3] = 'C';
  196.                         anOcean[i][4] = 'D';
  197.                         anOcean[i][5] = 'E';
  198.                         anOcean[i][6] = 'F';
  199.                         anOcean[i][7] = 'G';
  200.                         anOcean[i][8] = 'H';
  201.                         anOcean[i][9] = '-';
  202.                 }
  203.                 else
  204.                 {
  205.                         anOcean[i][0] = '1' + i - 1;
  206.                         anOcean[i][1] = '-';
  207.                         anOcean[i][2] = '-';
  208.                         anOcean[i][3] = '-';
  209.                         anOcean[i][4] = '-';
  210.                         anOcean[i][5] = '-';
  211.                         anOcean[i][6] = '-';
  212.                         anOcean[i][7] = '-';
  213.                         anOcean[i][8] = '-';
  214.                         anOcean[i][9] = '1' + i - 1;
  215.                 }
  216.         }
  217.         return anOcean;
  218. }
  219.  
  220. void copyBoard(char **copy, char **original)
  221. {
  222.         int i, j;
  223.         for(i = 0; i < NUMROWS; i++)
  224.         {
  225.                 for(j =0; j < NUMCOLS; j++)
  226.                 {
  227.                         copy[i][j] = original[i][j];
  228.                 }
  229.         }
  230. }
  231.  
  232.  
  233. void printBoard(char **ocean)
  234. {
  235.         int i, j;
  236.         for(i = 0; i < NUMROWS; i++)
  237.         {
  238.                 for(j =0; j < NUMCOLS; j++)
  239.                 {
  240.                         printf("%c ",ocean[i][j]);
  241.                 }
  242.                         printf("\n");
  243.         }
  244.  
  245. }
  246.  
  247. //check for a cleared board
  248. /*
  249.         1 - player wins
  250.         0 - nobody wins, game is on
  251. */
  252. int checkBoard(char **ocean)
  253. {
  254.         //simple idea: scan through the board array and look for ship symbols.
  255.         //if no more ship symbols exist on a board, that board is done and the other player wins
  256.         int i, j;
  257.         //boolean for checking for end of game
  258.         int end;
  259.         end = 1;
  260.        
  261.         for (i = 1; i < NUMROWS-1 && end; i++)
  262.         {
  263.                 for(j = 1; j < NUMCOLS-1 && end; j++)
  264.                 {
  265.                         if(ocean[i][j] == 'D' || ocean[i][j] == 'S' || ocean[i][j] == 'P')
  266.                                 end = 0;
  267.                 }
  268.         }
  269.        
  270.         return end;
  271. }
  272.  
  273. //returns an array of move locations at the moment because...I want it to return
  274. //the updated board but am too lazy to make that work with c, due to not liking pointers at the moment
  275. /*
  276.         -1 - bad move, tried to bomb an X or an O
  277.         0 - correct move but missed, mark the X back in main
  278.         1 - correct move that hit, mark the O back in main
  279. */
  280. void playerMove(char **ocean)
  281. {
  282.         char buffer[3];
  283.         //buffer[2] = ' ';
  284.         printf("Missile Co-ordinate: ");
  285.         scanf("%s",&buffer);
  286.         buffer[0] = toupper(buffer[0]);
  287.         //make sure they didn't use the extra buffer slot
  288.         if(buffer[2] != NULL)
  289.         {
  290.                 printf("You entered in 3 characters rather than 2! Shoot again.\n");
  291.                 //call the function again
  292.                 playerMove(ocean);
  293.         }
  294.         //no need to +1 to get rid of the top due to array indexing
  295.         int row = atoi(&buffer[1]);
  296.         //doesn't rely on array indexing, pure subtraction of ascii values, so need to +1
  297.         int col = buffer[0] - 'A' + 1;
  298.        
  299.         //logic goes here
  300.         char square;
  301.         square = ocean[row][col];
  302.        
  303.         int maxCol = 'H' - 'A' +1;
  304.         int maxRow = 8;
  305.  
  306.         if(row > maxRow || row < 1 || col < 1 || col > maxCol)
  307.         {
  308.                 printf("You tried to shoot your missile off the board! Shoot again.\n");
  309.                 playerMove(ocean);
  310.         }
  311.  
  312.         switch(square)
  313.         {
  314.                 case 'X':
  315.                 case 'O':
  316.                         //not a correct move, try again
  317.                         printf("You have already bombed here; please bomb elsewhere.\n");
  318.                         playerMove(ocean);
  319.                         break;
  320.                 case 'D':
  321.                 case 'P':
  322.                 case 'S':
  323.                         //hit
  324.                         printf("Hit! You hit an enemy ship!\n");
  325.                         ocean[row][col] = 'O';
  326.                         break;
  327.                 case '-':
  328.                         //miss
  329.                         printf("Miss. Your missile hit nothing but water and maybe some aquatic wildlife. Congrats.\n");
  330.                         ocean[row][col] = 'X';
  331.                         break;
  332.                 default:
  333.                         break;
  334.         }
  335. }
  336.  
  337. //serverMove - AI function for firing missiles
  338. int* serverMove(char **ocean)
  339. {
  340.         //simple idea: scan through the board array and look for 'O'.
  341.         //try to hit around the 'O' to sink a ship
  342.         /*
  343.                 1. Found a O
  344.                 2. Check the directions around it
  345.                 2a. Found another O around it
  346.                         3. Try to find another O in the direction
  347.                 2b. Did not find another O around it
  348.                         3. Try a random direction
  349.                         4. Check if the random direction has an 'X'
  350.                         5. If it did, go back to 3, if not return the co-ords
  351.        
  352.         */
  353.         int i, j;
  354.         int k, l;
  355.         //boolean for checking for end of logic
  356.         int end;
  357.         end = 1;
  358.         //up    - 1
  359.         //right - 2
  360.         //down  - 3
  361.         //left  - 4
  362.         int dir;
  363.        
  364.         for (i = 1; i < NUMROWS-1 && end; i++)
  365.         {
  366.                 for(j = 1; j < NUMCOLS-1 && end; j++)
  367.                 {
  368.                         //something was already hit
  369.                         if(ocean[i][j] == 'O')
  370.                         {
  371.                                 //check for more hits around it to get a direction heading
  372.                                 if(j < NUMCOLS-2 && ocean[i][j+1] == 'O')
  373.                                 {
  374.                                         //Right
  375.                                         dir = 1;
  376.                                         k = 1;
  377.                                         while((checkForHits(ocean, i, j+k) == 1) && ocean[i][j+k+1] != 'X')
  378.                                                 k++;
  379.                                                
  380.                                         end = 0;
  381.                                 }
  382.                                 else if(j > 1 && ocean[i][j-1] == 'O')
  383.                                 {
  384.                                         //Left
  385.                                         dir = 4;
  386.                                         end = 0;
  387.                                 }
  388.                                 else if(i < NUMROWS-2 && ocean[i+1][j] == 'O')
  389.                                 {
  390.                                         //Down
  391.                                         dir = 3;
  392.                                         end = 0;
  393.                                 }
  394.                                 else if(i > 1 && ocean[i-1][j] == 'O')
  395.                                 {
  396.                                         //Up
  397.                                         dir = 1;
  398.                                         end = 0;
  399.                                 }
  400.                                 else
  401.                                 {
  402.                                         /*no other hits around it, so go in a random direction */
  403.                                         dir = rand() % 4;
  404.                                 }
  405.                         }
  406.                 }
  407.         }
  408.        
  409.         if(!end)
  410.         {
  411.                
  412.         }
  413.         else
  414.         {
  415.                 /*shoot somewhere random that is not 'X'
  416.                 probably its own function so I can loop the selection based on some error
  417.                 code for hitting 'X' again */
  418.                 // or a do while...
  419.         }
  420.        
  421.         return 0;
  422. }
  423.  
  424. int* checkForHits(char **ocean, int i, int j)
  425. {
  426.         if(ocean[i][j] == 'O')
  427.                 return 1;
  428.         return 0;
  429. }
  430.  
  431. char* placeDestroyer ()
  432. {
  433.         char letters [8] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
  434.         int bound = 8;
  435.         size = 4;
  436.         coord1 = rand() % bound;
  437.         coord2 = 1 + rand() % bound;
  438.         coord1a = letters[coord1];
  439.         direction = 1 + rand() % size;
  440.         char results [4] = {coord1a, coord2, direction, size};
  441.         return results;
  442. }
  443.  
  444. char* placeSub ()
  445. {
  446.         char letters [8] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
  447.         int bound = 8;
  448.                 size = 3;
  449.         coord1 = rand() % bound;
  450.         coord2 = 1 + rand() % bound;
  451.         coord1a = letters[coord1];
  452.         direction = 1 + rand() % size;
  453.         char results [4] = {coord1a, coord2, direction, size};
  454.         return results;
  455. }
  456.  
  457. char* placePatrol ()
  458. {
  459.         char letters [8] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
  460.         int bound = 8;
  461.         size = 2;
  462.         coord1 = rand() % bound;
  463.         coord2 = 1 + rand() % bound;
  464.         coord1a = letters[coord1];
  465.         direction = 1 + rand() % size;
  466.         char results [4] = {coord1a, coord2, direction, size};
  467.         return results;
  468. }
  469.  
  470. int checker (char **ocean)
  471. {
  472.         int sizeCompare = size;
  473.         int testRight = 'I' - coord1a;
  474.         int testLeft = coord1a - 'A' + 1;
  475.         int testDown = 9 - coord2;
  476.         int i;
  477.  
  478.         if (direction == 1)
  479.         {
  480.                 if (coord2 < sizeCompare)
  481.                         return 0;
  482.         }
  483.         if (direction == 2)
  484.         {
  485.                 if (testRight < sizeCompare)
  486.                         return 0;
  487.         }
  488.         if (direction == 3)
  489.         {
  490.                 if (testDown < sizeCompare)
  491.                         return 0;
  492.         }
  493.         if (direction == 4)
  494.         {
  495.                 if (testLeft < sizeCompare)
  496.                         return 0;
  497.         }
  498.  
  499.         int col = coord1a - 'A' + 1;
  500.         int row = coord2;
  501.         for (i = 0; i < size; i++)
  502.     {
  503.         if(ocean[row][col] != '-')
  504.         {
  505.                 return 0;
  506.         }
  507.         else
  508.                 {
  509.                 if (direction == 1)
  510.                         row--;
  511.                 if (direction == 2)
  512.                         col++;
  513.                 if (direction == 3)
  514.                         row++;
  515.                 if (direction == 4)
  516.                         col--;
  517.         }
  518.     }
  519.         return 1;
  520. }
  521.  
  522. /*
  523. //rough outline of gameflow
  524. int gametime()
  525. {
  526.         while()
  527.         {
  528.                 p2ocean = playerOneMove(p2ocean);
  529.                 if(checkBoard(p2ocean))
  530.                 {
  531.                         //send out end of game message, victory, etc.
  532.                         break; //or put a variable up top of loop and set it here and
  533.                         //check it at all points after here
  534.                 }
  535.                 p1ocean = playerTwoMove(p1ocean);
  536.                 if(checKBoard(p1ocean))
  537.                 {
  538.                         //send out end of game message, victory, etc.
  539.                         break; //or put a variable up top of loop and set it here and
  540.                         //check it at all points after here
  541.                 }
  542.         }
  543. }
  544. */