Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.43 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <windows.h>
  4.  
  5. using namespace std;
  6.  
  7. #define ROW 10
  8. #define COL 10
  9.  
  10.  
  11.  
  12. // The size of each type of ship
  13. #define S_CARRIER 5
  14. #define S_BATTLESHIP 4
  15. #define S_DESTROYER 3
  16. #define S_SUBMARINE 3
  17. #define S_PATROLBOAT 2
  18.  
  19.  
  20.  
  21.  
  22.  
  23. enum hit {
  24.   BLANK = ' ',
  25.   MISS = '*',
  26.   HIT = 'x',
  27.   CARRIER = 'C',
  28.   BATTLESHIP = 'B',
  29.   DESTROYER = 'D',
  30.   SUBMARINE = 'S',
  31.   PATROLBOAT = 'P'
  32. };
  33.  
  34. // Various prototypes for the functions
  35. void welcome_display();
  36. bool play();
  37.  
  38.  
  39. void player_populate(hit players_board_def[ROW][COL]);
  40. void boat_check(hit players_board_def[ROW][COL], int boat_type);
  41.  
  42. void board_display(hit board[ROW][COL]);
  43. void board_display(hit board[ROW][COL], hit ref[ROW][COL]);
  44.  
  45. bool is_sunk(hit boat, hit board[ROW][COL], hit ref[ROW][COL]);
  46. bool is_game_over(hit board[ROW][COL], hit ref[ROW][COL]);
  47.  
  48. int hit2space(int space);
  49.  
  50. void player_attack(const hit computer_const_board_ref[ROW][COL], hit player_board_off[ROW][COL]);
  51.  
  52.  
  53. int main()
  54. {
  55.     bool valid=true;
  56.  
  57.     // Clears the screen then displays a graphic for the startup of the program
  58.     //welcome_display();
  59.     system("cls");
  60.    
  61.     do{
  62.         valid = play();
  63.     }while (valid);
  64.  
  65.  
  66.            
  67.      
  68.                    
  69.     printf("\n");
  70.     system("pause");
  71. }
  72.  
  73. bool play(){
  74.    
  75. //    bool valid;
  76.        
  77.     // For the players own board
  78.     hit player_board_def[ROW][COL];
  79.     hit computer_board_def[ROW][COL] = {
  80.         { BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK},
  81.         { BLANK, DESTROYER, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK},
  82.         { BLANK, DESTROYER, CARRIER, CARRIER, CARRIER, CARRIER, CARRIER, BLANK, BLANK, BLANK},
  83.         { BLANK, DESTROYER, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK},
  84.         { BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK},
  85.         { BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BATTLESHIP, BLANK, BLANK, BLANK},
  86.         { BLANK, SUBMARINE, SUBMARINE, SUBMARINE, BLANK, BLANK, BATTLESHIP, BLANK, BLANK, PATROLBOAT},
  87.         { BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BATTLESHIP, BLANK, BLANK, PATROLBOAT},
  88.         { BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BATTLESHIP, BLANK, BLANK, BLANK},
  89.         { BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK}
  90.     };
  91.    
  92.     // Invisible reference board keeping track of boat locations... ((Should be const))
  93.     hit player_const_board_ref[ROW][COL];
  94.     hit computer_const_board_ref[ROW][COL];
  95.        
  96.     // For the board the players will be attacking
  97.     hit player_board_off[ROW][COL];
  98.     hit computer_board_off[ROW][COL];
  99.    
  100.     for(int i = 0; i < ROW; i++)
  101.        for(int j = 0; j < COL; j++)
  102.            player_board_def[i][j] = /*computer_board_def[i][j] =*/ player_const_board_ref[i][j] = computer_const_board_ref[i][j] = player_board_off[i][j] = computer_board_off[i][j] = BLANK;
  103.    
  104. //    player_populate(player_board_def);
  105. //    board_display(player_board_def);
  106.    
  107. //    placementLogic(computer_board_def);
  108. //    board_display(computer_board_def);
  109.  
  110.  
  111.     // Assume all boards are set at this point so copy said boards
  112.     for(int i = 0; i < ROW; i++)
  113.         for(int j = 0; j < COL; j++){
  114.         player_const_board_ref[i][j] = player_board_def[i][j];
  115.         computer_const_board_ref[i][j] = computer_board_def[i][j];
  116.         }
  117.  
  118. /*  
  119.     do{
  120.         player_attack
  121.         is_game_over
  122.         ai_move
  123.         is_game_over
  124.     }while (valid);
  125.  
  126.    
  127. */
  128.     for (int y=0; y<15; y++){
  129.         printf("\n Computer player's board...\n");
  130.         board_display(computer_board_def);
  131.         player_attack(computer_const_board_ref, player_board_off);
  132.         printf(" Human player's board...\n");
  133.         board_display(player_board_off);
  134.     }        
  135.    
  136.    
  137.    
  138.     // This is just to ask if you want to make a new game or not. Based on that it will start a new game or quit
  139.     // This can be put in a separate function if needed later...
  140.    
  141.     bool new_game;
  142.     char choice;
  143.     int x=0;
  144.  
  145.     do{
  146.         printf("\n\n Do you want to start a new game? (y/n): ");
  147.         scanf("%c", &choice);
  148.         fflush(stdin);
  149.         switch (toupper(choice)){
  150.             case 'Y':
  151.                 x=1;
  152.                 new_game=true;
  153.                 break;    
  154.             case 'N':
  155.                 x=2;
  156.                 new_game=false;
  157.                 break;
  158.             default:
  159.                 printf(" Incorrect option entered, please choose again\n\n");
  160.                 x=3;
  161.         }
  162.     }while (x==3);
  163.     return (new_game);
  164. }
  165.  
  166.  
  167. void player_attack(const hit computer_const_board_ref[ROW][COL], hit player_board_off[ROW][COL]){
  168.    
  169.     char coord[3]={0};
  170.     char tchar;
  171.     bool valid;
  172.  
  173.  
  174.     do{  
  175.         do{
  176.             valid = true;
  177.             printf("\n Enter the coordinate you want to attack: ");
  178.             fgets(coord, 3, stdin);
  179.             fflush(stdin);
  180.             strupr(coord);
  181.             if(!isalpha(coord[0])){
  182.                 tchar = coord[0];
  183.                 coord[0] = coord[1];
  184.                 coord[1] = tchar;
  185.             }
  186.             if ((coord[0] < 'A' || coord[0] > 'J') || (coord[1] <'0' || coord[1] >'9')){
  187.                 printf("\n Incorrect coordinate entered. Please choose again.\n");
  188.                 valid = false;
  189.             }else
  190.                 valid = true;
  191.         }while (!valid);
  192.  
  193.         coord[0] -= 'A';
  194.         coord[1] -= '0';
  195.        
  196.         if (player_board_off[coord[1]][coord[0]] != BLANK){
  197.             printf("\n That coordinate has already been attacked. Please choose again.\n");
  198.             valid = false;
  199.         }
  200.  
  201.     }while (!valid);
  202.  
  203.     printf(" You are attacking: %d%d\n", coord[0], coord[1]);
  204.    
  205.     if (computer_const_board_ref[coord[1]][coord[0]]!=BLANK){
  206.         player_board_off[coord[1]][coord[0]]=HIT;
  207.     }else if (computer_const_board_ref[coord[1]][coord[0]]==BLANK){
  208.         player_board_off[coord[1]][coord[0]]=MISS;
  209.     }
  210. }
  211.  
  212. /*********************************************************************************************************
  213. Purpose: This is to have the player choose which boat they want to place and then proceed to
  214. another function that based on the boat choosen will ask for coordinates and valid them. This continues
  215. until all boats have been placed.
  216.  
  217. Input: hit board[ROW][COL]
  218.  
  219. Returns: Nothing
  220. *********************************************************************************************************/
  221. void player_populate(hit player_board_def[ROW][COL]){
  222.    
  223.     int boattypes[5] = { CARRIER, BATTLESHIP, DESTROYER, SUBMARINE, PATROLBOAT };
  224.     bool boatplaced[5] = { false } ;
  225.     int boats = 0;
  226.     int choice;
  227.     bool valid;
  228.    
  229.     printf("\n");
  230.    
  231.     do{
  232.         do{
  233.             valid = true;
  234.             board_display(player_board_def);
  235.             printf("\n 1) Aircraft Carrier\n 2) Battleship\n 3) Destroyer\n 4) Submarine\n 5) Patrol Boat\n\n");
  236.             printf(" Choose which ship you want to place: ");
  237.             scanf("%d", &choice);
  238.             fflush(stdin);
  239.             if (choice > 0 && choice < 6){
  240.                 valid = true;
  241.             }else{
  242.                 printf(" Incorrect option entered. Please choose again.\n\n");
  243.                 valid = false;
  244.             }
  245.         }while (!valid);
  246.        
  247.         // Comment needed here...
  248.         choice--;
  249.         if(!boatplaced[choice]){
  250.             boat_check(player_board_def, boattypes[choice]);
  251.             boats++;
  252.             boatplaced[choice] = true;
  253.         }else{
  254.             printf("\n You have already placed that ship. Please choose a different ship.\n\n");
  255.         }
  256.     }while (boats < 5);
  257. }
  258.  
  259. /*********************************************************************************************************
  260. Purpose: This is to have the player choose which boat they want to place and then proceed to
  261. another function that based on the boat choosen will ask for coordinates and valid them. This continues
  262. until all boats have been placed.
  263.  
  264. Input: hit player_board_def[ROW][COL]
  265.  
  266. Returns: Nothing
  267. *********************************************************************************************************/
  268. void boat_check(hit player_board_def[ROW][COL], int boat_type)
  269. {
  270.     char coord1[3]={0};
  271.     char coord2[3]={0};
  272.     char tchar;
  273.     bool valid;
  274.        
  275.     do{
  276.         valid = true;
  277.        
  278.         // Input & validation for the start coordinates from the user
  279.         do{
  280.             printf("\n Enter first coordinate: ");
  281.             fgets(coord1, 3, stdin);
  282.             fflush(stdin);
  283.             strupr(coord1);
  284.             if(!isalpha(coord1[0])){
  285.                 tchar = coord1[0];
  286.                 coord1[0] = coord1[1];
  287.                 coord1[1] = tchar;
  288.             }
  289.         }while ((coord1[0] < 'A' || coord1[0] > 'J') && (coord1[0] <'0' || coord1[0] >'9'));
  290.        
  291.         // Input & validation for the end coordinates from the user      
  292.         do{
  293.             printf(" Enter second coordinate: ");
  294.             fgets(coord2, 3, stdin);
  295.             fflush(stdin);
  296.             strupr(coord2);
  297.             if(!isalpha(coord2[0])){
  298.                 tchar = coord2[0];
  299.                 coord2[0] = coord2[1];
  300.                 coord2[1] = tchar;
  301.             }
  302.         }while ((coord2[1] < 'A' || coord2[1] > 'J') && (coord2[1] <'0' || coord2[1] >'9'));
  303.                
  304.         // This changes the chars in the arrays to an actual number so that we can use them to interface
  305.         // with the location in the array it corrisponds to.
  306.         coord1[0] -= 'A';
  307.         coord1[1] -= '0';
  308.         coord2[0] -= 'A';
  309.         coord2[1] -= '0';
  310.                
  311.         // This checks the inputted numbers to see if they are valid by one of the element pairs being equal
  312.         // to each other. Such as if you have A2 & D2 as the coordinates the second element in both arrays
  313.         // are the same or if you have A2 & A5, the 'A's are the same (This verifies that the start & end points
  314.         // are truly in a horizontal or vertical line)
  315.                
  316.         if ((coord1[0]==coord2[0] && coord1[1]!=coord2[1]) || (coord1[0]!=coord2[0] && coord1[1]==coord2[1]))
  317.         {
  318.             for(int i = 0; i < 2; i++)
  319.             {
  320.                 if(coord1[i] > coord2[i]) // Comment needed here...
  321.                 {
  322.                     for(int j = 0; j < 2; j++)
  323.                     {
  324.                         tchar = coord1[j];
  325.                         coord1[j] = coord2[j];
  326.                         coord2[j] = tchar;
  327.                     }
  328.                 }
  329.             }
  330.                        
  331.             // Comment needed here...
  332.             if((coord2[0] - coord1[0]) + (coord2[1] - coord1[1]) + 1 != hit2space(boat_type))
  333.                 valid = false;
  334.                        
  335.             for(int i = coord1[0]; i <= coord2[0]; i++) // Checking horizontal
  336.                 if(player_board_def[coord1[1]][i] != BLANK)
  337.                     valid = false;
  338.                        
  339.             for(int i = coord1[1]; i <= coord2[1]; i++) // Checking vertical
  340.                 if(player_board_def[i][coord1[0]] != BLANK)
  341.                     valid = false;
  342.                        
  343.         }
  344.         // If none of the previous checks have not been met then the coordinates cannot be
  345.         // correct therefore we must ask again
  346.         else
  347.         {
  348.             valid = false;
  349.         }
  350.     if (!valid)
  351.         printf("\n Incorrect coordinates entered, please choose again\n");
  352.     } while (!valid);
  353.        
  354.     for(int i = coord1[0]; i <= coord2[0]; i++)
  355.         player_board_def[coord1[1]][i] = (hit) boat_type; // Or respective boat type
  356.        
  357.     for(int i = coord1[1]; i <= coord2[1]; i++)
  358.         player_board_def[i][coord1[0]] = (hit) boat_type; // Or respective boat type
  359. }
  360.  
  361.  
  362. /*********************************************************************************************************
  363. Purpose: This function simply prints out any array it is given in the board format that the battleship
  364. game is supposed to be displayed in
  365.  
  366. Input: hit board[ROW][COL]
  367.  
  368. Returns: Nothing
  369. *********************************************************************************************************/
  370. void board_display(hit board[ROW][COL]){
  371.  
  372.     char letters_array[11]={' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
  373.  
  374.     // This is for displaying the first line of dashes
  375.     for (int z=0; z < 4*COL+4; z++){
  376.         printf("-");
  377.     }
  378.     printf("\n");
  379.    
  380.     // This is for displaying A-J
  381.     for (int x=0; x < 11; x++){
  382.         printf(" %c |", letters_array[x]);
  383.     }
  384.     printf("\n");    
  385.    
  386.     // These for loops are for making the board dynamically based on the #define values and displays
  387.     // what is in the array in the board format accordingly
  388.     for (int x=0, y=0; x < ROW; x++, y++){
  389.         for (int z=0; z < 4*COL+4; z++){
  390.            printf("-");
  391.         }
  392.         printf("\n");
  393.        
  394.         // This is for displaying 0-9
  395.         printf(" %d |", y);
  396.        
  397.         // This is for displaying the contents of the element of the array we are currently at
  398.         for (int y=0; y < COL; y++){
  399.             printf(" %c |", board[x][y]);
  400.         }
  401.         printf("\n");
  402.     }
  403.  
  404.     // This is for displaying the last line of dashes
  405.     for (int z=0; z < 4*COL+4; z++){
  406.         printf("-");
  407.     }
  408.     printf("\n");
  409. }    
  410.  
  411. /*********************************************************************************************************
  412. Purpose: This is the overloaded version of the function that prints out any array it is given in the board
  413. format that the battleship game is supposed to be displayed in. This functions takes two boards
  414.  
  415. Input: hit board[ROW][COL] & hit ref[ROW][COL]
  416.  
  417. Returns: Nothing
  418. *********************************************************************************************************/
  419. /*void board_display(hit board[ROW][COL], hit ref[ROW][COL]){
  420.  
  421.     char letters_array[11]={' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
  422.  
  423.     // This is for displaying the first line of dashes
  424.     for (int z=0; z < 4*COL+4; z++){
  425.         printf("-");
  426.     }
  427.     printf("\n");
  428.    
  429.     // This is for displaying A-J
  430.     for (int x=0; x < 11; x++){
  431.         printf(" %c |", letters_array[x]);
  432.     }
  433.     printf("\n");    
  434.    
  435.     // These for loops are for making the board dynamically based on the #define values and displays
  436.     // what is in the array in the board format accordingly
  437.     for (int x=0, y=0; x < ROW; x++, y++){
  438.         for (int z=0; z < 4*COL+4; z++){
  439.            printf("-");
  440.         }
  441.         printf("\n");
  442.        
  443.         // This is for displaying 0-9
  444.         printf(" %d |", y);
  445.        
  446.         // This is for displaying the contents of the element of the array we are currently at
  447.         for (int y=0; y < COL; y++){
  448.             printf(" %c |", is_sunk(ref[x][y], board, ref) ? tolower(board[x][y]) : board[x][y]);
  449.         }
  450.         printf("\n");
  451.     }
  452.  
  453.     // This is for displaying the last line of dashes
  454.     for (int z=0; z < 4*COL+4; z++){
  455.         printf("-");
  456.     }
  457.     printf("\n");
  458. }
  459.  
  460. // Comment needed here...
  461. bool is_game_over(hit board[ROW][COL], hit ref[ROW][COL])
  462. {
  463.         int boats[BOATCOUNT] = { CARRIER, BATTLESHIP, DESTROYER, SUBMARINE, PATROLBOAT };
  464.         int sinks = BOATCOUNT;
  465.        
  466.         for(int i = 0; i < BOATCOUNT; i++)
  467.                 if(is_sunk(boats[i], board, ref))
  468.                         sinks--;
  469.         return !sinks;
  470. }
  471.  
  472. // Comment needed here...
  473. bool is_sunk(hit boat, hit board[ROW][COL], hit ref[ROW][COL])
  474. {
  475.         int size = hit2space(boat);
  476.         if(!size)
  477.                 return false;
  478.        
  479.         for(int i = 0; i < ROW; i++)
  480.                 for(int j = 0; j < COL; j++)
  481.                         if(ref[i][j] == BOAT && board[i][j] == HIT)
  482.                                 size--;
  483.         return !size;
  484. }
  485. */
  486. // Comment needed here...
  487. int hit2space(int space)
  488. {
  489.         switch(space)
  490.         {
  491.                 case CARRIER:           return S_CARRIER;
  492.                 case BATTLESHIP:        return S_BATTLESHIP;
  493.                 case DESTROYER:         return S_DESTROYER;
  494.                 case SUBMARINE:         return S_SUBMARINE;
  495.                 case PATROLBOAT:        return S_PATROLBOAT;
  496.                 default:                        return 0;
  497.         }
  498. }
  499.  
  500.  
  501. /*********************************************************************************************************
  502. Purpose: This just prints out the first screen to display with a cool ascii graphic for the player to see
  503.  
  504. Input: Nothing
  505.  
  506. Returns: Nothing
  507. *********************************************************************************************************/
  508. void welcome_display(){
  509.    
  510.     // This just gives time for the user so they can fullscreen the command prompt to be able to see
  511.     // the game without scrolling up & down
  512.     printf("\n Loading; Please wait");
  513.     for (int x=0; x<3; x++){
  514.         printf(". ");
  515.         Sleep(1150);
  516.     }
  517.     system("cls");
  518.    
  519.     // This is fairly obvious that this is the title and graphic that is displayed when you start the program
  520.     printf("\n");
  521.     printf("\t\t\t\tWelcome to\n");
  522.     printf("\t\t\t\     ================\n");
  523.     printf("   ____              __    __    ___                   __                    \n");
  524.     printf("  /\\  _`\\           /\\ \\__/\\ \\__/\\_ \\                 /\\ \\      __           \n");
  525.     printf("  \\ \\ \\_\\ \\     __  \\ \\ ,_\\ \\ ,_\\//\\ \\      __    ____\\ \\ \\___ /\\_\\  _____   \n");
  526.     printf("   \\ \\  _ <'  /'__`\\ \\ \\ \\/\\ \\ \\/ \\ \\ \\   /'__`\\ /',__\\\\ \\  _ `\\/\\ \\/\\ '__`\\ \n");
  527.     printf("    \\ \\ \\_\\ \\/\\ \\_\\.\\_\\ \\ \\_\\ \\ \\_ \\_\\ \\_/\\  __//\\__, `\\\\ \\ \\ \\ \\ \\ \\ \\ \\_\\ \\\n");
  528.     printf("     \\ \\____/\\ \\__/.\\_\\\\ \\__\\\\ \\__\\/\\____\\ \\____\\/\\____/ \\ \\_\\ \\_\\ \\_\\ \\ ,__/\n");
  529.     printf("      \\/___/  \\/__/\\/_/ \\/__/ \\/__/\\/____/\\/____/\\/___/   \\/_/\\/_/\\/_/\\ \\ \\/ \n");
  530.     printf("                                                                       \\ \\_\\ \n");    
  531.     printf("   Developers are:                                                      \\/_/ \n");
  532.     printf("                                      #    _                                    ");
  533.     printf("   Larry Ing                       _ # #  (.)\n");
  534.     printf("   Luke Kontes               =====|_|#_#___|__                                  ");
  535.     printf("   Erich Healy                _  |____________|  _                              ");
  536.     printf("   Mark de Guzman       =====|.| | .  .  .  . | |.|====                         ");
  537.     printf("                       _ .---------------------------. _                        ");
  538.     printf("                 =====|.||  .  .  .  .  .  .  .  .   ||.|====\n");
  539.     printf("   \\---------------------'                           `--------------/\n");
  540.     printf("    \\ .  .  .  .           .  .  .  .        .  .  .  .     USS-42 /\n");
  541.     printf("     \\          .  .  .  .           .  .  .           .  .  .  . /\n");
  542.     printf("      \\__________________________________________________________/\n");
  543.     printf("  wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\n");
  544.     printf(" wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\n");
  545.     printf("   wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww \n");
  546.     printf("\n");
  547.     printf("\n");    
  548.    
  549.     // Possibly use a sleep instead here...
  550.     system("pause");    
  551. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement