Advertisement
Guest User

battleship Constantin

a guest
Nov 12th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6. #include <time.h>
  7. #define SIZE 10
  8.  
  9. typedef struct Node{
  10. char currentState;
  11. char ship_type[20];
  12. char charInput;
  13. int intInput;
  14. struct Node* next;
  15. }Node;
  16.  
  17. char** initialization(){
  18.     int i, j, k, row, col;
  19.     char **board = (char**)malloc(sizeof(char*)*SIZE);
  20.     for (i = 0; i < SIZE; i++){
  21.         board[i] = (char*)malloc(sizeof(char)*SIZE);
  22.     }
  23.     for(i = 0; i < SIZE; i++){
  24.         for(j = 0; j < SIZE; j++){
  25.             board[i][j] = '-';
  26.         }
  27.     }
  28.  
  29.     //place the first ship
  30.     //the direction of placement is hard coded
  31.     //you can change it if you want
  32.     bool placed1 = false;
  33.     while(placed1 == false){
  34.         row = rand()%10;
  35.         col = rand()%10;
  36.         if(row+1 < 10 && row-1 >= 0){
  37.             if((board[row][col] = '-') &&
  38.                 (board[row+1][col] = '-')){
  39.                     board[row][col] = 'D';
  40.                     board[row+1][col] = 'D';
  41.                     placed1 = true;
  42.             }
  43.             else if((board[row][col] == '-') &&
  44.                 (board[row-1][col] == '-')){
  45.                     board[row][col] = 'D';
  46.                     board[row-1][col] = 'D';
  47.                     placed1 = true;
  48.             }
  49.         }
  50.     }
  51.  
  52.     //place the second ship
  53.     bool placed2 = false;
  54.     while (placed2 == false){
  55.         row = rand()%10;
  56.         col = rand()%10;
  57.         if((row+1 < 10 && row+2 < 10) &&
  58.             (row-1 >= 0 && row-2 >= 0) &&
  59.             (col+1 < 10 && col+2 < 10) &&
  60.             (col-1 >= 0 && col-2 >= 0)){
  61.             if((board[row][col] == '-') &&
  62.                 (board[row][col+1] == '-') &&
  63.                 (board[row][col+2] == '-')){
  64.                     board[row][col] = 'C';
  65.                     board[row][col+1] = 'C';
  66.                     board[row][col+2] = 'C';
  67.                     placed2 = true;
  68.             }
  69.             else if((board[row][col] == '-') &&
  70.                 (board[row][col-1] == '-') &&
  71.                 (board[row][col-2] == '-')){
  72.                     board[row][col] = 'C';
  73.                     board[row][col-1] = 'C';
  74.                     board[row][col-2] = 'C';
  75.                     placed2 = true;
  76.             }
  77.             else if((board[row][col] == '-') &&
  78.                 (board[row+1][col] == '-') &&
  79.                 (board[row+2][col] == '-')){
  80.                     board[row][col] = 'C';
  81.                     board[row+1][col] = 'C';
  82.                     board[row+2][col] = 'C';
  83.                     placed2 = true;
  84.             }
  85.             else if((board[row][col] == '-') &&
  86.                 (board[row-1][col] == '-') &&
  87.                 (board[row-2][col] == '-')){
  88.                     board[row][col] = 'C';
  89.                     board[row-1][col] = 'C';
  90.                     board[row-2][col] = 'C';
  91.                     placed2 = true;
  92.             }
  93.         }
  94.     }
  95.  
  96.     //place the third ship
  97.     bool placed3 = false;
  98.     while (placed3 == false){
  99.         row = rand()%10;
  100.         col = rand()%10;
  101.         if((row+1 < 10 && row+2 < 10) &&
  102.             (row-1 >= 0 && row-2 >= 0) &&
  103.             (col+1 < 10 && col+2 < 10) &&
  104.             (col-1 >= 0 && col-2 >= 0)){
  105.             if((board[row][col] == '-') &&
  106.                 (board[row][col+1] == '-') &&
  107.                 (board[row][col+2] == '-')){
  108.                     board[row][col] = 'S';
  109.                     board[row][col+1] = 'S';
  110.                     board[row][col+2] = 'S';
  111.                     placed3 = true;
  112.             }
  113.             else if((board[row][col] == '-') &&
  114.                 (board[row][col-1] == '-') &&
  115.                 (board[row][col-2] == '-')){
  116.                     board[row][col] = 'S';
  117.                     board[row][col-1] = 'S';
  118.                     board[row][col-2] = 'S';
  119.                     placed3 = true;
  120.             }
  121.             else if((board[row][col] =='-') &&
  122.                 (board[row+1][col] == '-') &&
  123.                 (board[row+2][col] == '-')){
  124.                     board[row][col] = 'S';
  125.                     board[row+1][col] = 'S';
  126.                     board[row+2][col] = 'S';
  127.                     placed3 = true;
  128.             }
  129.             else if((board[row][col] == '-') &&
  130.                 (board[row-1][col] == '-') &&
  131.                 (board[row-2][col] == '-')){
  132.                     board[row][col] = 'S';
  133.                     board[row-1][col] = 'S';
  134.                     board[row-2][col] = 'S';
  135.                     placed3 = true;
  136.             }
  137.         }
  138.     }
  139.  
  140.     bool placed4 = false;
  141.     while (placed4 == false){
  142.         row = rand()%10;
  143.         col = rand()%10;
  144.         if((row+1 < 10 && row+2 < 10 && row+3 < 10) &&
  145.             (row-1 >= 0 && row-2 >= 0 && row-3 >= 0) &&
  146.             (col+1 < 10 && col+2 < 10 && col+3 < 10) &&
  147.             (col-1 >= 0 && col-2 >= 0 && col-3 >= 0)){
  148.             if((board[row][col] == '-') &&
  149.                  (board[row+1][col] == '-') &&
  150.                  (board[row+2][col] == '-') &&
  151.                  (board[row+3][col] == '-')){
  152.                     board[row][col] = 'B';
  153.                     board[row+1][col] = 'B';
  154.                     board[row+2][col] = 'B';
  155.                     board[row+3][col] = 'B';
  156.                     placed4 = true;
  157.             }
  158.             else if((board[row][col] == '-') &&
  159.                  (board[row-1][col] == '-') &&
  160.                  (board[row-2][col] == '-') &&
  161.                  (board[row-3][col] == '-')){
  162.                     board[row][col] = 'B';
  163.                     board[row-1][col] = 'B';
  164.                     board[row-2][col] = 'B';
  165.                     board[row-3][col] = 'B';
  166.                     placed4 = true;
  167.             }
  168.             else if((board[row][col] == '-') &&
  169.                  (board[row][col+1] == '-') &&
  170.                  (board[row][col+2] == '-') &&
  171.                  (board[row][col+3] == '-')){
  172.                     board[row][col] = 'B';
  173.                     board[row][col+1] = 'B';
  174.                     board[row][col+2] = 'B';
  175.                     board[row][col+3] = 'B';
  176.                     placed4 = true;
  177.             }
  178.             else if((board[row][col] == '-') &&
  179.                  (board[row][col-1] == '-') &&
  180.                  (board[row][col-2] == '-') &&
  181.                  (board[row][col-3] == '-')){
  182.                     board[row][col] = 'B';
  183.                     board[row][col-1] = 'B';
  184.                     board[row][col-2] = 'B';
  185.                     board[row][col-3] = 'B';
  186.                     placed4 = true;
  187.             }
  188.         }
  189.     }
  190.  
  191.     bool placed5 = false;
  192.     while (placed5 == false){
  193.         row = rand()%10;
  194.         col = rand()%10;
  195.         if((row+1 < 10 && row+2 < 10 && row+3 < 10 && row+4 < 10) &&
  196.             (row-1 >= 0 && row-2 >= 0 && row-3 >= 0 && row-4 >= 0) &&
  197.             (col+1 < 10 && col+2 < 10 && col+3 < 10 && col+4 < 10) &&
  198.             (col-1 >= 0 && col-2 >= 0 && col-3 >= 0 && col-4 >= 0)){
  199.             if((board[row][col] == '-') &&
  200.                (board[row+1][col] == '-') &&
  201.                (board[row+2][col] == '-') &&
  202.                (board[row+3][col] == '-') &&
  203.                (board[row+4][col] == '-')){
  204.                     board[row][col] = 'R';
  205.                     board[row+1][col] = 'R';
  206.                     board[row+2][col] = 'R';
  207.                     board[row+3][col] = 'R';
  208.                     board[row+4][col] = 'R';
  209.                     placed5 = true;
  210.             }
  211.             else if((board[row][col] == '-') &&
  212.                  (board[row-1][col] == '-') &&
  213.                  (board[row-2][col] == '-') &&
  214.                  (board[row-3][col] == '-') &&
  215.                  (board[row-4][col] == '-')){
  216.                     board[row][col] = 'R';
  217.                     board[row-1][col] = 'R';
  218.                     board[row-2][col] = 'R';
  219.                     board[row-3][col] = 'R';
  220.                     board[row-4][col] = 'R';
  221.                     placed5 = true;
  222.             }
  223.             else if((board[row][col] == '-') &&
  224.                 (board[row][col+1] == '-') &&
  225.                 (board[row][col+2] == '-') &&
  226.                 (board[row][col+3] == '-') &&
  227.                 (board[row][col+4] == '-')){
  228.                     board[row][col] = 'R';
  229.                     board[row][col+1] = 'R';
  230.                     board[row][col+2] = 'R';
  231.                     board[row][col+3] = 'R';
  232.                     board[row][col+4] = 'R';
  233.                     placed5 = true;
  234.             }
  235.             else if((board[row][col] == '-') &&
  236.                 (board[row][col-1] == '-') &&
  237.                 (board[row][col-2] == '-') &&
  238.                 (board[row][col-3] == '-') &&
  239.                 (board[row][col-4] == '-')){
  240.                     board[row][col] = 'R';
  241.                     board[row][col-1] = 'R';
  242.                     board[row][col-2] = 'R';
  243.                     board[row][col-3] = 'R';
  244.                     board[row][col-4] = 'R';
  245.                     placed5 = true;
  246.             }
  247.         }
  248.     }
  249.     return board;
  250. }
  251.  
  252. void update_state(char* state, char ** board, char character, int col){
  253.     int row;
  254.        char shipType[20];
  255.         row = character % 65;
  256.         if(board[row][col] == '-'){
  257.             strcpy(state, "MISS");
  258.         }
  259.         else{
  260.             strcpy(state, "HIT!");
  261.             /* add code to change the board to indicate
  262.             * that shot hit , for example you could change
  263.             * the corresponding letter back to '-'.
  264.             * but before this, you need to record the letter
  265.             * and corresponding ship type.
  266.             */ //COMPLETED
  267.             char shipChar = board[row][col];
  268.             if(shipChar == 'R'){
  269.                 strcpy(shipType,"Carrier");
  270.                 printf("%s", shipType);
  271.             }
  272.             else if(shipChar == 'B'){
  273.                 strcpy(shipType,"Battleship");
  274.                 printf("%s", shipType);
  275.             }
  276.             else if(shipChar == 'S'){
  277.                 strcpy(shipType,"Submarine");
  278.                 printf("%s", shipType);
  279.             }
  280.             else if(shipChar == 'C'){
  281.                 strcpy(shipType,"Cruiser");
  282.                 printf("%s", shipType);
  283.             }
  284.             else if(shipChar == 'D'){
  285.                 strcpy(shipType,"Destroyer");
  286.                 printf("%s", shipType);
  287.             }
  288.             printf("%c", shipChar);
  289.             board[row][col] = '-'; //set target coordinates to '-'
  290.         }
  291.  
  292.         /* add code to update temp node's attributes (i.e
  293.         * hit or miss, ship type, then insert the temp node
  294.         * the node into the linked list.
  295.         * You may need to write the insert node function
  296.         * (you can refer to the insert node function in lab5
  297.         * handout )
  298.         */
  299.         struct Node *head, *tail;
  300.         head = tail = NULL;
  301.         insert_node(&head, &tail, col, row, *state, shipType);
  302.         print_node(head);
  303.  
  304.     //check if game is over //completed
  305.     int m = 0;
  306.     int k, l;
  307.     for(k = 0; k < SIZE; k++){
  308.         for(l = 0; l < SIZE; l++){
  309.             if(board[k][l] == '-'){
  310.                 m++;
  311.                 if(m >= 100){
  312.                     strcpy(state, "GAME OVER!");
  313.                 }
  314.             }
  315.         }
  316.     }
  317. }
  318.  
  319. int accept_input(char * c, int * i){
  320.     bool flag = true;
  321.     do{
  322.         printf("Enter a letter A-J and number 0-9 ex. B4 - enter Z0 to end\n");
  323.         int size = scanf(" %c%d", c, i);
  324.         if(size != 2){
  325.             printf("INVALID INPUT\n");
  326.             continue;
  327.         }
  328.         *c = toupper(*c);
  329.         if(*c == 'Z' && *i == 0)
  330.             break;
  331.         if (*c < 65 || *c > 74)
  332.             printf("INVALID INPUT\n");
  333.         else if (*i <0 || *i >9)
  334.             printf("INVALID INPUT\n");
  335.         else
  336.             flag = false;
  337.     }while(flag);
  338. }
  339. /*
  340. char currentState;
  341. char ship_type;
  342. */
  343.  
  344. void insert_node(struct Node **h, struct Node **t, int x, char y, char* state, char shipTyp){
  345.     //create new node with value given by int x, y
  346.     struct Node *temp;
  347.     if ((temp = (struct Node *)malloc(sizeof(struct Node))) == NULL){
  348.         printf("Node Allocation Failed \n");
  349.         exit(1);
  350.     }
  351.     //space for node obtained, copy values into node
  352.     temp->charInput = y; //store user character input
  353.     temp->intInput = x; //store user number input
  354.     temp->currentState = state; //store state
  355.     temp->ship_type[20] = shipTyp; //store shipType
  356.     temp->next = NULL;
  357.     if (*h == NULL){
  358.         //list is empty if so
  359.         *h = *t = temp;
  360.     }
  361.     else{ //list isnt empty, use *t to add node at the end
  362.         (*t)->next = temp;
  363.         *t = (*t)->next;
  364.     }
  365.  
  366. }
  367. //will be converted to a write to file function
  368. void print_node(struct Node *h){
  369.     if(h == NULL){
  370.         printf("The list is empty.\n");
  371.     }
  372.     else{
  373.         printf("Values in the list are: \n");
  374.         while(h != NULL) {
  375.             printf("%c\n", h->charInput);
  376.             printf("%s\n", h->ship_type);
  377.             h = h->next;
  378.         }
  379.     }
  380. }
  381.  
  382. void display_state(char* state, char** board){
  383.     int i, j;
  384.     printf("\n**** %s ****\n", state);
  385.     printf("  0 1 2 3 4 5 6 7 8 9\n");
  386.     for (i = 0; i < SIZE; i++){
  387.         printf("%c ", 65+i);
  388.         for (j = 0; j < SIZE; j++){
  389.             printf("%c ", board[i][j]);
  390.         }
  391.         printf("\n");
  392.     }
  393.  
  394. }
  395.  
  396. int teardown(char ** board){
  397.     int i;
  398.     for(i = 0; i < SIZE; i++)
  399.         free(board[i]);
  400.     free(board);
  401.  
  402.     /* add code below to traverse the linkded list
  403.     * you should create a log file name "log.txt"
  404.     * traverse each node in the linked list, and
  405.     * write the information in each node into "log.txt"
  406.     * Each line should follow this format:
  407.     *   Fired at A1. Hit - Carrier.
  408.     *   Fired at C2. Miss.
  409.     * You may refer to the print_list function in lab5
  410.     * handout.
  411.     * In addition, remember to free the nodes of the
  412.     * linked list.
  413.     */
  414.  
  415.     return 0;
  416. }
  417.  
  418.  
  419.  
  420. int main(void){
  421.     //
  422.     void print_node(struct Node*);
  423.     void insert_node(struct Node**, struct Node**, int, char, char*, char);
  424.     srand(time(NULL));
  425.     char** board;
  426.     char state[] = "GAME START";
  427.     char flag[] = "GAME OVER!";
  428.     char character;
  429.     int integer;
  430.     /* declare a linked list below*/ //COMPLETED
  431.     //struct Node* head = NULL;
  432.     //struct Node* second = NULL;
  433.     //head = (struct Node*)malloc(sizeof(struct Node));
  434.     //second = (struct Node*)malloc(sizeof(struct Node));
  435.  
  436.  
  437.     board = initialization();
  438.     do{
  439.         display_state(state, board);
  440.         if(display_state)
  441.         /*modify the accept_input function
  442.         * accept input function should return
  443.         * a temp node, which stores the current valid
  444.         * input (i.e. character and letter)
  445.         */
  446.         accept_input(&character, &integer);
  447.         if(character == 'Z' && integer == 0)
  448.             break;
  449.         /*modify the update_state function
  450.         * update_state function should accept
  451.         * the head node of linked list and the
  452.         * temp node
  453.         */
  454.         update_state(state, board, character, integer);
  455.     } while((character != 'Z' || integer != 0) && strcmp(state, flag) );
  456.  
  457.     /*modify the teardown function
  458.     * tear_down function should accept
  459.     * a head node of linked list
  460.     */
  461.     teardown(board);
  462.     return 0;
  463. }
  464.  
  465. //modularize insertNode, etc. inito own functions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement