Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <Windows.h>
  5.  
  6. using namespace std;
  7.  
  8. void Direction(char BoardMe[10][10], char HMSMe[10][10], int ship, bool& Error, int cor1, int cor2, bool Computer); //0 is right, 1 is left, 2 is up, 3 is down
  9. void BoardCreate(char Initialize[10][10]);
  10. void OutputTables(char Display[10][10]);
  11. bool AlreadyFilled(char BoardMe[10][10], int ship, int x, int y, int direction);
  12. int RandomShip();
  13. int RandomCor();
  14. int RandomDirection();
  15.  
  16. int main()
  17. {
  18.     char BoardComputer[10][10];
  19.     char BoardMe[10][10];
  20.     char HMSComputer[10][10];
  21.     char HMSMe[10][10];
  22.     int ship_select, direction;
  23.     int ship;
  24.     int x = 0;
  25.     int y = 0;
  26.     int cor1 = 1;
  27.     int cor2 = 1;
  28.  
  29.     string Carrier = " 1 for your carrier (5 length),";
  30.     string Battleship = " 2 for your battleship (4 length)";
  31.     string Cruiser = "\n3 for your cruiser (3 length),";
  32.     string Sub = " 4 for your sub (3 length),";
  33.     string Destroyer = " or 5 for your destroyer (2 length)";
  34.  
  35.     bool carrier = false; //This block of code is made to decipher what ship the user wants to manipulate and what direction
  36.     bool battle = false;
  37.     bool cruiser = false;
  38.     bool destroyer = false;
  39.     bool sub = false;
  40.  
  41.     bool Finished = false; //Stops setup
  42.     bool check = false;
  43.     bool Error = false;
  44.     bool Computer = true;
  45.     bool ComputerError = false;
  46.     int CFin = 0; //Checks if computer setup is complete
  47.  
  48.     BoardCreate(HMSComputer);
  49.     BoardCreate(HMSMe);
  50.     BoardCreate(BoardMe);
  51.     BoardCreate(BoardComputer);
  52.     /*
  53.     OutputTables(HMSComputer);
  54.     OutputTables(HMSMe);
  55.     OutputTables(BoardComputer);
  56.     OutputTables(BoardMe);
  57.     */
  58.     srand((unsigned)time(0));
  59.  
  60.     while (Finished == false)
  61.     {
  62.         if (Computer == false)
  63.         {
  64.             cout << "Welcome to battleship, which ship would you like to set up?" << endl << endl;
  65.             cout << "Enter" << Carrier << Battleship << Cruiser << Sub << Destroyer << endl;
  66.             cin >> ship_select;
  67.         }
  68.         else
  69.             ship_select = RandomShip();
  70.  
  71.         switch (ship_select) //Selects ship you use.
  72.         {
  73.         case 1:
  74.             //Carrier ship setup
  75.             if (carrier == false)
  76.             {
  77.                 ship = 5;
  78.                 if (Computer == false)
  79.                     Direction(BoardMe, HMSMe, ship, Error, cor1, cor2, Computer);
  80.                 else
  81.                     Direction(BoardComputer, HMSComputer, ship, Error, x, y, Computer);
  82.             }
  83.             if (Error == false)
  84.             {
  85.                 Carrier = "";
  86.                 carrier = true;
  87.                 CFin++;
  88.             }
  89.             Error = false;
  90.             break;
  91.         case 2:
  92.             //battle ship setup
  93.             if (battle == false)
  94.             {
  95.                 ship = 4;
  96.                 if (Computer == false)
  97.                     Direction(BoardMe, HMSMe, ship, Error, cor1, cor2, Computer);
  98.                 else
  99.                     Direction(BoardComputer, HMSComputer, ship, Error, x, y, Computer);
  100.             }
  101.             if (Error == false)
  102.             {
  103.                 Battleship = "";
  104.                 battle = true;
  105.                 CFin++;
  106.             }
  107.                
  108.             Error = false;
  109.             break;
  110.         case 3:
  111.             //cruiser ship setup
  112.             if (cruiser == false)
  113.             {
  114.                 ship = 3;
  115.                 if (Computer == false)
  116.                     Direction(BoardMe, HMSMe, ship, Error, cor1, cor2, Computer);
  117.                 else
  118.                     Direction(BoardComputer, HMSComputer, ship, Error, x, y, Computer);
  119.             }
  120.             if (Error == false)
  121.             {
  122.                 Cruiser = "";
  123.                 cruiser = true;
  124.             }
  125.             if (Computer == true && Error == false)
  126.                 CFin++;
  127.             Error = false;
  128.             break;
  129.         case 4:
  130.             //submarine ship setup
  131.             if (sub == false)
  132.             {
  133.                 ship = 3;
  134.                 if (Computer == false)
  135.                     Direction(BoardMe, HMSMe, ship, Error, cor1, cor2, Computer);
  136.                 else
  137.                     Direction(BoardComputer, HMSComputer, ship, Error, x, y, Computer);
  138.             }
  139.             if (Error == false)
  140.             {
  141.                 Sub = "";
  142.                 sub = true;
  143.             }
  144.             if (Computer == true && Error == false)
  145.                 CFin++;
  146.             Error = false;
  147.             break;
  148.         case 5:
  149.             //destroyer ship setup
  150.             if (destroyer == false)
  151.             {
  152.                 ship = 3;
  153.                 if (Computer == false)
  154.                     Direction(BoardMe, HMSMe, ship, Error, cor1, cor2, Computer);
  155.                 else
  156.                     Direction(BoardComputer, HMSComputer, ship, Error, x, y, Computer);
  157.             }
  158.             if (Error == false)
  159.             {
  160.                 Destroyer = "";
  161.                 destroyer = true;
  162.             }
  163.             if (Computer == true && Error == false)
  164.                 CFin++;
  165.             Error = false;
  166.             break;
  167.         default: cout << "You have entered in an incorrect number, Try again" << endl;
  168.             break;
  169.  
  170.         }
  171. if (CFin == 5 && Computer == true)
  172. {
  173.     if (CFin==5)
  174.     cout << "Got here" << endl << flush;
  175.     Carrier = " 1 for your carrier (5 length),";
  176.     Battleship = " 2 for your battleship (4 length)";
  177.     Cruiser = "\n3 for your cruiser (3 length),";
  178.     Sub = " 4 for your sub (3 length),";
  179.     Destroyer = " or 5 for your destroyer (2 length)";
  180.     carrier = false; //This block of code resets for human setup
  181.     battle = false;
  182.     cruiser = false;
  183.     destroyer = false;
  184.     sub = false;
  185.     Computer == false;
  186. }
  187. cout << CFin << endl;
  188.         if (carrier == true && battle == true && cruiser == true && destroyer == true && sub == true && CFin == 5)
  189.             Finished = true;
  190.     }
  191.  
  192.     return 0;
  193. }
  194. void Direction(char BoardMe[10][10], char HMSMe[10][10], int ship, bool& Error, int cor1, int cor2, bool Computer)
  195. {
  196.     int i = 1;
  197.     int direction;
  198.  
  199.     string up = " 1) up,";
  200.     string down = " 0) down,";
  201.     string left = " 3) left";
  202.     string right = " 2) right,";
  203.     if (Computer == false)
  204.     {
  205.         cout << "Please enter your starting coordinates, they will be numbers between 1-10. For example, 1 3 is first row, third column." << endl;
  206.         cin >> cor1 >> cor2;
  207.     }
  208.     else
  209.     {
  210.         cor1 = RandomCor();
  211.         cor2 = RandomCor();
  212.     }
  213.     if ((cor2 - 1 - ship) < 1)
  214.         left = "";
  215.     if ((cor2 - 1 + ship) > 10)
  216.         right = "";
  217.     if ((cor1 - 1 + ship) > 10)
  218.         down = "";
  219.     if ((cor1 - 1 - ship) < 1)
  220.         up = "";
  221.  
  222.     if (Computer == false)
  223.     {
  224.         cout << "Would you like to go" << down << up << right << left << " from your origin point, ";
  225.         cout << cor1 << " " << cor2 << "?" << endl;
  226.         cin >> direction;
  227.     }
  228.     else
  229.         direction = RandomDirection();
  230.     cout << direction << " " << cor1 << " " << cor2 << endl;
  231.     switch (direction)  //Sets placement of ships
  232.     {
  233.     case 0:
  234.         if (down != "" && AlreadyFilled(BoardMe, ship, cor1, cor2, direction) == true)
  235.         {
  236.             for (int count = 0; count < ship; count++)
  237.             {
  238.                 BoardMe[cor1 - i][cor2 - 1] = '#';
  239.                 HMSMe[cor1 - i][cor2 - 1] = '#';
  240.                 i--;
  241.             }
  242.             i = 1;
  243.         }
  244.         else
  245.         {
  246.             Error = true;
  247.             if (Computer == false)
  248.             {
  249.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
  250.                 cout << "You are not allowed to do that. Try again." << endl;
  251.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
  252.             }
  253.         }
  254.         break;
  255.     case 1:
  256.         if (up != "" && AlreadyFilled(BoardMe, ship, cor1, cor2, direction) == true)
  257.         {
  258.             for (int count = 0; count < ship; count++)
  259.             {
  260.                 BoardMe[cor1 - i][cor2 - 1] = '#';
  261.                 HMSMe[cor1 - i][cor2 - 1] = '#';
  262.                 i++;
  263.             }
  264.             i = 1;
  265.         }
  266.         else
  267.         {
  268.             Error = true;
  269.             if (!Computer)
  270.             {
  271.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
  272.                 cout << "You are not allowed to do that. Try again." << endl;
  273.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
  274.             }
  275.         }
  276.         break;
  277.     case 2:
  278.         if (right != "" && AlreadyFilled(BoardMe, ship, cor1, cor2, direction) == true)
  279.         {
  280.             for (int count = 0; count < ship; count++)
  281.             {
  282.                 BoardMe[cor1 - 1][cor2 - i] = '#';
  283.                 HMSMe[cor1 - 1][cor2 - i] = '#';
  284.                 i--;
  285.             }
  286.             i = 1;
  287.         }
  288.         else
  289.         {
  290.             Error = true;
  291.             if (!Computer)
  292.             {
  293.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
  294.                 cout << "You are not allowed to do that. Try again." << endl;
  295.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
  296.             }
  297.         }
  298.         break;
  299.     case 3:
  300.         if (left != "" && AlreadyFilled(BoardMe, ship, cor1, cor2, direction) == true)
  301.         {
  302.             for (int count = 0; count < ship; count++)
  303.             {
  304.                 BoardMe[cor1 - 1][cor2 - i] = '#';
  305.                 HMSMe[cor1 - 1][cor2 - i] = '#';
  306.                 i++;
  307.             }
  308.             i = 1;
  309.         }
  310.         else
  311.         {
  312.             Error = true;
  313.             if (!Computer)
  314.             {
  315.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
  316.                 cout << "You are not allowed to do that. Try again." << endl;
  317.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
  318.             }
  319.         }
  320.         break;
  321.     default:
  322.         cout << "You have entered in a bad number. Try again." << endl;
  323.         //worry about this later
  324.         break;
  325.     }
  326.     OutputTables(BoardMe);
  327. }
  328. void BoardCreate(char Initialize[10][10])
  329. {
  330.     for (int i = 0; i < 10; i++)
  331.     for (int j = 0; j < 10; j++)
  332.         Initialize[i][j] = '~';
  333. }
  334. void OutputTables(char Display[10][10])
  335. {
  336.     cout << "   1 2 3 4 5 6 7 8 9 10" << endl;
  337.     for (int i = 0; i < 10; i++)
  338.     {
  339.         if ((i + 1) < 10)
  340.             cout << i + 1 << "  ";
  341.         else
  342.             cout << i + 1 << " ";
  343.         for (int j = 0; j < 10; j++)
  344.         {
  345.             if (Display[i][j] == '~')
  346.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
  347.             else
  348.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 128);
  349.             cout << Display[i][j] << " ";
  350.             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
  351.             if (j == 9)
  352.                 cout << endl;
  353.         }
  354.     }
  355.  
  356.     cout << endl << endl;
  357.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
  358. }
  359. bool AlreadyFilled(char BoardMe[10][10], int ship, int x, int y, int direction)
  360. {
  361.     int i = 0;
  362.     if (direction == 0) //down
  363.     {
  364.         while (i<ship)
  365.         {
  366.             if (BoardMe[x - 1 - i][y - 1] == '#')
  367.                 return false;
  368.             i++;
  369.         }
  370.  
  371.         return true;
  372.     }
  373.     if (direction == 1) //up
  374.     {
  375.         while (i<ship)
  376.         {
  377.             if (BoardMe[x - 1 + i][y - 1] == '#')
  378.                 return false;
  379.             i++;
  380.         }
  381.  
  382.         return true;
  383.     }
  384.     if (direction == 2) //right
  385.     {
  386.         while (i<ship)
  387.         {
  388.             if (BoardMe[x - 1][y - 1 + i] == '#')
  389.                 return false;
  390.             i++;
  391.         }
  392.  
  393.         return true;
  394.     }
  395.     if (direction == 3) //left
  396.     {
  397.         while (i<ship)
  398.         {
  399.             if (BoardMe[x - 1][y - 1 - i] == '#')
  400.                 return false;
  401.             i++;
  402.         }
  403.  
  404.         return true;
  405.     }
  406.  
  407. }
  408. int RandomCor()
  409. {
  410.     return((rand() % 10) + 1);
  411. }
  412. int RandomDirection()
  413. {
  414.     return((rand() % 3));
  415. }
  416. int RandomShip()
  417. {
  418.     return((rand() % 5) + 1);
  419. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement