SHARE
TWEET

BATTLESHIP! (slight update)

a guest May 26th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Structured Programming
  2. // Quinn H.
  3. // BATTLESHIP!
  4. // Started May 13, 2019
  5.  
  6. #include <iostream>
  7. #include <cstdlib>
  8. #include <thread>
  9. #include <chrono>
  10.  
  11. using namespace std;
  12.  
  13. void printBoard( char grid[6][6] );
  14.  
  15. int main()
  16. {
  17.     // intro
  18.     cout << "Welcome to BATTLESHIP!\n\n";
  19.     cout << "The coordinate grid is 6 by 6, and your boat is 3 spaces long,\n";
  20.     cout << "so please keep that in mind when entering the coordinates\n\n";
  21.    
  22.     // initialize variables
  23.     int xBow;
  24.     int yBow;
  25.     int xStern;
  26.     int yStern;
  27.    
  28.     // create the boards
  29.     char gridOne[6][6] =
  30.     {
  31.         { '/', '/', '/', '/', '/', '/' },
  32.         { '/', '/', '/', '/', '/', '/' },
  33.         { '/', '/', '/', '/', '/', '/' },
  34.         { '/', '/', '/', '/', '/', '/' },
  35.         { '/', '/', '/', '/', '/', '/' },
  36.         { '/', '/', '/', '/', '/', '/' }
  37.     };
  38.    
  39.     char gridTwo[6][6] =
  40.     {
  41.         { '/', '/', '/', '/', '/', '/' },
  42.         { '/', '/', '/', '/', '/', '/' },
  43.         { '/', '/', '/', '/', '/', '/' },
  44.         { '/', '/', '/', '/', '/', '/' },
  45.         { '/', '/', '/', '/', '/', '/' },
  46.         { '/', '/', '/', '/', '/', '/' }
  47.     };
  48.    
  49.     char gridOneRadar[6][6] =
  50.     {
  51.         { '-', '-', '-', '-', '-', '-' },
  52.         { '-', '-', '-', '-', '-', '-' },
  53.         { '-', '-', '-', '-', '-', '-' },
  54.         { '-', '-', '-', '-', '-', '-' },
  55.         { '-', '-', '-', '-', '-', '-' },
  56.         { '-', '-', '-', '-', '-', '-' }
  57.     };
  58.    
  59.     char gridTwoRadar[6][6] =
  60.     {
  61.         { '-', '-', '-', '-', '-', '-' },
  62.         { '-', '-', '-', '-', '-', '-' },
  63.         { '-', '-', '-', '-', '-', '-' },
  64.         { '-', '-', '-', '-', '-', '-' },
  65.         { '-', '-', '-', '-', '-', '-' },
  66.         { '-', '-', '-', '-', '-', '-' }
  67.     };
  68.    
  69.     // prompt player one for their ship coords
  70.     cout << " - PLAYER 1 -\nPlease enter the coordinates of your bow (front).\n";
  71.     cout << "x > ";
  72.     cin >> xBow;
  73.     cout << "y > ";
  74.     cin >> yBow;
  75.     cout << endl;
  76.    
  77.     cout << "Now the stern (back) of your boat.\n";
  78.     cout << "x > ";
  79.     cin >> xStern;
  80.     cout << "y > ";
  81.     cin >> yStern;
  82.    
  83.     // "place" player one's ship on the grid
  84.     gridOne[yBow-1][xBow-1] = 'O';
  85.     gridOne[yStern-1][xStern-1] = 'O';
  86.    
  87.    
  88.     // experimental tests to try to fill in 3rd space
  89.     // talk to Quinn before making changes to this
  90.     if( xBow == xStern )
  91.     {
  92.         // ensure that the coords are 2 spaces apart
  93.         if( abs(yBow-yStern) != 2 )
  94.         {
  95.             cout << "\n!! Something has gone wrong with your coordinates.\n\n";
  96.             cout << "Please ensure you bow and stern are 2 spaces apart.\n\n";
  97.             cout << "The program will now terminate.\n";
  98.             exit(2);
  99.         }
  100.        
  101.         if( yBow > yStern )
  102.         {
  103.             gridOne[yStern][xStern-1] = 'O';
  104.         }
  105.         else // yStern > yBow
  106.         {
  107.             gridOne[yBow][xBow-1] = 'O';
  108.         }
  109.     }
  110.     else if( yBow == yStern )
  111.     {
  112.         // ensure that the coords are 2 spaces apart
  113.         if( abs(xBow-xStern) != 2 )
  114.         {
  115.             cout << "\n!! Something has gone wrong with your coordinates.\n\n";
  116.             cout << "Please ensure you bow and stern are 2 spaces apart.\n\n";
  117.             cout << "The program will now terminate.\n";
  118.             exit(2);
  119.         }
  120.  
  121.         if( xBow > xStern )
  122.         {
  123.             gridOne[yStern-1][xStern] = 'O';
  124.         }
  125.         else // xStern > xBow
  126.         {
  127.             gridOne[yBow-1][xBow] = 'O';
  128.         }
  129.     }
  130.     else
  131.     {
  132.         cout << "\n!! Something has gone wrong with your coordinates.\n\n";
  133.         cout << "Please ensure that the bow and the stern are\non the same horizontal or vertical axis.\n\n";
  134.         cout << "The program will now terminate.\n";
  135.         exit(1);
  136.     }
  137.    
  138.     // TEST print w/formatting
  139.     cout << endl;
  140.     printBoard(gridOne);
  141.     cout << endl;
  142.    
  143.     cout << "Does this look right to you? (y/n) > ";
  144.     char userChoice = 'n';
  145.     cin >> userChoice;
  146.     switch(userChoice)
  147.     {
  148.         case 'y':
  149.             cout << "Got it!\n\n";
  150.             break;
  151.         case 'Y':
  152.             cout << "Got it!\n\n";
  153.             break;
  154.         default:
  155.             cout << "Understood. Please rerun the program to reenter coordinates.\n";
  156.             exit(0);
  157.     }
  158.    
  159.    
  160.     cout << "\n\n\n\n\n\n\n\n"; // just to clean up the screen
  161.    
  162.    
  163.     // prompt player 2 for their stuff
  164.     cout << " - PLAYER 2 -\nPlease enter the coordinates of your bow (front).\n";
  165.     cout << "x > ";
  166.     cin >> xBow;
  167.     cout << "y > ";
  168.     cin >> yBow;
  169.     cout << endl;
  170.    
  171.     cout << "Now the stern (back) of your boat.\n";
  172.     cout << "x > ";
  173.     cin >> xStern;
  174.     cout << "y > ";
  175.     cin >> yStern;
  176.    
  177.     // "place" player 2's ship on the grid
  178.     gridTwo[yBow-1][xBow-1] = 'O';
  179.     gridTwo[yStern-1][xStern-1] = 'O';
  180.    
  181.    
  182.     // experimental tests to try to fill in 3rd space
  183.     // talk to Quinn before making changes to this
  184.     if( xBow == xStern )
  185.     {
  186.         // ensure that the coords are 2 spaces apart
  187.         if( abs(yBow-yStern) != 2 )
  188.         {
  189.             cout << "\n!! Something has gone wrong with your coordinates.\n\n";
  190.             cout << "Please ensure you bow and stern are 2 spaces apart.\n\n";
  191.             cout << "The program will now terminate.\n";
  192.             exit(2);
  193.         }
  194.        
  195.         if( yBow > yStern )
  196.         {
  197.             gridTwo[yStern][xStern-1] = 'O';
  198.         }
  199.         else // yStern > yBow
  200.         {
  201.             gridTwo[yBow][xBow-1] = 'O';
  202.         }
  203.     }
  204.     else if( yBow == yStern )
  205.     {
  206.         // ensure that the coords are 2 spaces apart
  207.         if( abs(xBow-xStern) != 2 )
  208.         {
  209.             cout << "\n!! Something has gone wrong with your coordinates.\n\n";
  210.             cout << "Please ensure you bow and stern are 2 spaces apart.\n\n";
  211.             cout << "The program will now terminate.\n";
  212.             exit(2);
  213.         }
  214.        
  215.         if( xBow > xStern )
  216.         {
  217.             gridTwo[yStern-1][xStern] = 'O';
  218.         }
  219.         else // xStern > xBow
  220.         {
  221.             gridTwo[yBow-1][xBow] = 'O';
  222.         }
  223.     }
  224.     else
  225.     {
  226.         cout << "\n!! Something has gone wrong with your coordinates.\n\n";
  227.         cout << "Please ensure that the bow and the stern are\non the same horizontal or vertical axis.\n\n";
  228.         cout << "The program will now terminate.\n";
  229.         exit(1);
  230.     }
  231.    
  232.     // TEST print w/formatting
  233.     cout << endl;
  234.     printBoard(gridTwo);
  235.     cout << endl;
  236.    
  237.     cout << "Does this look right to you? (y/n) > ";
  238.     cin >> userChoice;
  239.     switch(userChoice)
  240.     {
  241.         case 'y':
  242.             cout << "Got it!\n\n";
  243.             break;
  244.         case 'Y':
  245.             cout << "Got it!\n\n";
  246.             break;
  247.         default:
  248.             cout << "Understood. Please rerun the program to reenter coordinates.\n";
  249.             exit(0);
  250.     }
  251.    
  252.    
  253.    
  254.    
  255.     // BEGINNING OF PLAY LOOP //////////////////////////////////////////////////
  256.    
  257.    
  258.    
  259.    
  260.    
  261.     // counter that will be used for the win condition
  262.     // if either of these hits 3, the loop should stop and a win screen should appear
  263.     int p1Score = 0;
  264.     int p2Score = 0;
  265.    
  266.     // This counter will be used to determine who's turn it is.
  267.     // ODD = P1, EVEN = P2
  268.     int turn = 0;
  269.    
  270.     // reusable target coordinates
  271.     int xTarget;
  272.     int yTarget;
  273.    
  274.    
  275.     cout << "\n\n\n\n\n\n\n\n"; // just to clean up the screen
  276.    
  277.    
  278.     while( p1Score != 3 && p2Score != 3 )
  279.     {
  280.         turn++;
  281.         cout << "     TURN " << turn << endl;
  282.         if( turn % 2 != 0 ) // P1's turn
  283.         {
  284.             cout << "    PLAYER 1\n";
  285.         }
  286.         else
  287.         {
  288.             cout << "    PLAYER 2\n";
  289.         }
  290.         cout << "ENEMY RADAR:\n";
  291.        
  292.         // if..else function based on turn that tests for whose turn it is
  293.         if( turn % 2 != 0 ) // P1's turn
  294.         {
  295.             // display opponent's ghost board
  296.             printBoard(gridTwoRadar);
  297.             cout << "\nYOUR BOARD:\n";
  298.             printBoard(gridOne);
  299.            
  300.             // prompt current player for their desired target
  301.             // make it so that they can't attack the same place twice
  302.             cout << "Where would you like to attack?\n";
  303.             cout << "x > ";
  304.             cin >> xTarget;
  305.             cout << "y > ";
  306.             cin >> yTarget;
  307.            
  308.             // check for if they hit an enemy boat
  309.             if( gridTwo[yTarget-1][xTarget-1 ] == 'O' )
  310.             {
  311.                 cout << "\n     Nice shot!\nYou hit the enemy's ship at (" << xTarget << "," << yTarget << ").\n";
  312.                 this_thread::sleep_for (chrono::seconds(3));
  313.                 gridTwo[yTarget-1][xTarget-1]      = 'X';
  314.                 gridTwoRadar[yTarget-1][xTarget-1] = 'X';
  315.                
  316.                 p1Score++;
  317.             }
  318.             else if( gridTwoRadar[yTarget-1][xTarget-1 ] == 'X' || gridTwoRadar[yTarget-1][xTarget-1 ] == '#' )
  319.             {
  320.                 cout << "\n     Sorry, you've already attacked that space.\n";
  321.                 this_thread::sleep_for (chrono::seconds(3));
  322.             }
  323.             else
  324.             {
  325.                 cout << "\nYou missed... better luck next turn.\n";
  326.                 gridTwoRadar[yTarget-1][xTarget-1] = '#';
  327.                 gridTwo[yTarget-1][xTarget-1] = '#';
  328.                 this_thread::sleep_for (chrono::seconds(3));
  329.             }
  330.            
  331.         }
  332.         else // P2's turn
  333.         {
  334.             // display opponent's ghost board
  335.             printBoard(gridOneRadar);
  336.             cout << "\nYOUR BOARD:\n";
  337.             printBoard(gridTwo);
  338.            
  339.             // prompt current player for their desired target
  340.             // make it so that they can't attack the same place twice
  341.             cout << "Where would you like to attack?\n";
  342.             cout << "x > ";
  343.             cin >> xTarget;
  344.             cout << "y > ";
  345.             cin >> yTarget;
  346.            
  347.             // check for if they hit an enemy boat
  348.             if( gridOne[yTarget-1][xTarget-1 ] == 'O' )
  349.             {
  350.                 cout << "\n     Nice shot!\nYou hit the enemy's ship at (" << xTarget << "," << yTarget << ").\n";
  351.                 this_thread::sleep_for (chrono::seconds(3));
  352.                 gridOne[yTarget-1][xTarget-1]      = 'X';
  353.                 gridOneRadar[yTarget-1][xTarget-1] = 'X';
  354.                
  355.                 p2Score++;
  356.             }
  357.             else if( gridOneRadar[yTarget-1][xTarget-1 ] == 'X' || gridOneRadar[yTarget-1][xTarget-1 ] == '#' )
  358.             {
  359.                 cout << "\n     Sorry, you've already attacked that space.\n";
  360.                 this_thread::sleep_for (chrono::seconds(3));
  361.             }
  362.             else
  363.             {
  364.                 cout << "\nYou missed... better luck next turn.\n";
  365.                 gridOneRadar[yTarget-1][xTarget-1] = '#';
  366.                 gridOne[yTarget-1][xTarget-1] = '#';
  367.                 this_thread::sleep_for (chrono::seconds(3));
  368.             }
  369.            
  370.         }
  371.         cout << "\n\n\n\n\n\n\n\n"; // just to clean up the screen
  372.     }
  373.    
  374.    
  375.     // display win message
  376.     cout << "Congratulations to Player ";
  377.     if( turn % 2 != 0 )
  378.     {
  379.         cout << 1;
  380.     }
  381.     else
  382.     {
  383.         cout << 2;
  384.     }
  385.    
  386.     cout << " for winning the game!\n\n";
  387.    
  388.     // end of program
  389.     return 0;
  390. }
  391.  
  392. void printBoard( char grid[6][6] )
  393. {
  394.     cout << "   ";
  395.     for( int i = 0; i < 6; i++ )
  396.     {
  397.         cout << i+1 << "  ";
  398.     }
  399.     cout << endl;
  400.     for( int i = 0; i < 6; i++ )
  401.     {
  402.         cout << i+1 << "  ";
  403.         for( int j = 0; j < 6; j++ )
  404.         {
  405.             cout << grid[i][j] << "  ";
  406.         }
  407.         cout << endl;
  408.     }
  409. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top