Advertisement
tadejpetric

Untitled

May 2nd, 2016
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.55 KB | None | 0 0
  1. //10x10 polje; ladje: 5x1, 4x1, 3x1, 2x2, 1x2
  2. // g++ raspbian linux
  3. //* --> ladja; # --> neodkrito; O --> prazno
  4. //skupaj 18 ladjic!
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <ctime>
  8. #include <cstdlib>
  9. #define size 10
  10. using namespace std;
  11.  
  12. void vnos_ai(char tab[][size]);
  13. void vnos_usr(char tab[][size]);
  14.  
  15. void vpis_ai(char tab[][size], char fake[][size], int &cnt, bool &prev, int &x, int &y, bool &left, bool &right, bool &top, bool &bot);
  16. void vpis_usr(char tab[][size], char fake[][size], int &cnt);
  17. //void izpis(char ai[][size]);
  18. //void potopljeno(char ai[][size], char polje[][size], int ai_cnt, int usr_cnt);
  19. //jbool zmaga (int ai_cnt, int usr_cnt);
  20.  
  21. int main()
  22. {
  23.     srand(time(NULL));
  24.     bool prev = false, bot, top, left, right;
  25.     int x, y;
  26.     int ai_cnt = 0, usr_cnt = 0;
  27.     char fake[size][size];
  28.     char polje[size][size];
  29.     char fake_a[size][size];
  30.  
  31.     for (int i = 0; i < size; i++) // zapolne polja z #
  32.         for (int j = 0; j < size; j++)
  33.         {
  34.             polje[i][j] = '#';
  35.             fake_a[i][j] = '#';
  36.         }
  37.     char ai[size][size];
  38.     for (int i = 0; i < size; i++)
  39.         for (int j = 0; j < size; j++)
  40.         {
  41.             ai[i][j] = '#';
  42.             fake[i][j] = '#';
  43.         }
  44.  
  45.     vnos_ai(ai);
  46.     vnos_usr(polje);
  47.     bool tst = true;
  48.  
  49.     do {
  50.         if (tst)
  51.         {
  52.             vpis_ai(polje, fake_a, ai_cnt, prev, x, y, left, right, top, bot);
  53.             tst = false;
  54.         }
  55.         else
  56.         {
  57.             vpis_usr(ai, fake, usr_cnt);
  58.             tst = true;
  59.         }
  60.     } while (ai_cnt < 18 && usr_cnt < 18);
  61.     if (ai_cnt == 18)
  62.         cout << endl << "zmaga ai";
  63.     else
  64.         cout << endl << "zmaga uporabnik";
  65.     cout << endl << endl;
  66.     return 0;
  67. }
  68. void vpis_usr(char tab[][size], char fake[][size], int &cnt)
  69. {
  70.     for (int i = -1; i < size; i++)
  71.     {
  72.         if (i > -1) cout << setw(2) << i;
  73.         for (int j = -1; j < size; j++)
  74.         {
  75.             if (i > -1 && j > -1) cout << setw(2) << fake[i][j];
  76.             else if (i == -1 && j == -1) cout << setw(2) << " ";
  77.             else if (i == -1) cout << setw(2) << j;
  78.         }
  79.         cout << endl;
  80.     }
  81.     unsigned int x, y;
  82.     cout << "Vpisi x koordinato(0-9): ";
  83.     do {
  84.         cin >> x;
  85.     } while (x > 9);
  86.     cout << "Vpisi y koordinato(0-9): ";
  87.     do {
  88.         cin >> y;
  89.     } while (y > 9);
  90.  
  91.     if (tab[y][x] == '*')
  92.     {
  93.         cout << "zadel si ladjo!\n";
  94.         fake[y][x] = '*';
  95.         cnt++;
  96.     }
  97.     else
  98.     {
  99.         cout << "zgresil si!\n";
  100.         fake[y][x] = 'O';
  101.     }
  102. }
  103.  
  104.  
  105. void vpis_ai(char tab[][size], char fake[][size], int &cnt, bool &prev, int &x, int &y, bool &left, bool &right, bool &top, bool &bot)
  106. {
  107.     int i = 0, j = 0;
  108.     if (prev)
  109.     {   //fake[y][x] prejsni zadetek
  110.         if (!top)
  111.         {
  112.             for (i = 0; y + i >= 0; i--)
  113.             {
  114.                 if (fake[y + i][x] == '#')
  115.                 {
  116.                     fake[y + i][x] = '*';
  117.                     if (tab[y + i][x] == '#')
  118.                         top = true;
  119.                     else
  120.                         top = false;
  121.                     break;
  122.                 }
  123.             }
  124.         }
  125.  
  126.         else if (!bot)
  127.         {
  128.             for (i = 0; y + i < 10; i++)
  129.             {
  130.                 if (fake[y + i][x] == '#')
  131.                 {
  132.                     fake[y + i][x] = '*';
  133.                     if (tab[y + i][x] == '#')
  134.                         bot = true;
  135.                     else bot = false;
  136.                     break;
  137.                 }
  138.             }
  139.         }
  140.         else if (!right)
  141.         {
  142.             for (j = 0; x + j < 10; j++)
  143.             {
  144.                 if (fake[y][x + j] == '#')
  145.                 {
  146.                     fake[y][x + j] = '*';
  147.                     if (tab[y][x + j] == '#')
  148.                         right = true;
  149.                     else right = false;
  150.                     break;
  151.                 }
  152.             }
  153.         }
  154.         else if (!left)
  155.         {
  156.             for (j = 0; x + j >= 0; j--)
  157.             {
  158.                 if (fake[y][x + j] == '#')
  159.                 {
  160.                     fake[y][x + j] = '*';
  161.                     if (tab[y][x + j] == '#')
  162.                         left = true;
  163.                     else left = false;
  164.                     break;
  165.                 }
  166.             }
  167.         }
  168.         else prev = false;
  169.     }
  170.     if (prev == false)
  171.     {
  172.         do{
  173.             x = rand() % 10;
  174.             y = rand() % 10;
  175.             left = false; top = false; right = false; bot = false;
  176.         } while (fake[y][x] == '*');
  177.     }
  178.     cout << "ai je ciljal na (" << x + j << ", " << y + i << ")" << endl;
  179.     fake[y][x] = '*';
  180.  
  181.     if (tab[y + i][x + j] == '*')
  182.     {
  183.         cnt++;
  184.         prev = true;
  185.         cout << "zadel je" << endl;
  186.     }
  187.     else {
  188.         cout << "zgresil je" << endl;
  189.     }
  190.     cout << "pogled AI:" << endl;
  191.     for (int h = 0; h < size; h++)
  192.     {
  193.         for (int f = 0; f < size; f++)
  194.             cout << setw(2) << fake[h][f];
  195.         cout << endl;
  196.     }
  197.     cout << endl << endl;
  198.  
  199. }
  200.  
  201.  
  202.  
  203.  
  204. void vnos_ai(char tab[][size])
  205. {
  206.     int x, y;
  207.     bool rot = rand() % 2;
  208.     if (rot) // horizontalno 5
  209.     {
  210.         x = rand() % 6;
  211.         y = rand() % 10;
  212.         for (int i = 0; i < 5; i++)
  213.             tab[y][i + x] = '*';
  214.     }
  215.     else // vertikalno
  216.     {
  217.         x = rand() % 10;
  218.         y = rand() % 6;
  219.         for (int i = 0; i < 5; i++)
  220.             tab[i + y][x] = '*';
  221.     }
  222.     bool check = true;
  223.     rot = rand() % 2;
  224.  
  225.     if (rot) // horizontalno 4
  226.     {
  227.         do {
  228.             check = true;
  229.             x = rand() % 7;
  230.             y = rand() % 10;
  231.             for (int i = 0; i < 4; i++)
  232.                 if (tab[y][x + i] == '*')
  233.                     check = false;
  234.         } while (!check);
  235.         for (int i = 0; i < 4; i++)
  236.             tab[y][x + i] = '*';
  237.     }
  238.     else // vertikalno
  239.     {
  240.         do{
  241.             check = true;
  242.             x = rand() % 10;
  243.             y = rand() % 7;
  244.             for (int i = 0; i < 4; i++)
  245.                 if (tab[y + i][x] == '*')
  246.                     check = false;
  247.         } while (!check);
  248.         for (int i = 0; i < 4; i++)
  249.             tab[y + i][x] = '*';
  250.     }
  251.  
  252.     rot = rand() % 2;
  253.     if (rot) // horizontalno 3
  254.     {
  255.         do {
  256.             check = true;
  257.             x = rand() % 8;
  258.             y = rand() % 10;
  259.             for (int i = 0; i < 3; i++)
  260.                 if (tab[y][x + i] == '*')
  261.                     check = false;
  262.         } while (!check);
  263.         for (int i = 0; i < 3; i++)
  264.             tab[y][x + i] = '*';
  265.     }
  266.     else //vertikalno
  267.     {
  268.         do {
  269.             check = true;
  270.             x = rand() % 10;
  271.             y = rand() % 8;
  272.             for (int i = 0; i < 3; i++)
  273.                 if (tab[y + i][x] == '*')
  274.                     check = false;
  275.         } while (!check);
  276.         for (int i = 0; i < 3; i++)
  277.             tab[y + i][x] = '*';
  278.     }
  279.  
  280.     for (int g = 0; g < 2; g++)
  281.     {
  282.         rot = rand() % 2;
  283.         if (rot) // horizontalno 2
  284.         {
  285.             do {
  286.                 check = true;
  287.                 x = rand() % 9;
  288.                 y = rand() % 10;
  289.                 if (tab[y][x] == '*' || tab[y][x + 1] == '*')
  290.                     check = false;
  291.             } while (!check);
  292.             tab[y][x] = '*'; tab[y][x + 1] = '*';
  293.         }
  294.         else //vertikalno
  295.         {
  296.             do {
  297.                 check = true;
  298.                 x = rand() % 10;
  299.                 y = rand() % 9;
  300.                 if (tab[y][x] == '*' || tab[y + 1][x] == '*')
  301.                     check = false;
  302.             } while (!check);
  303.             tab[y][x] = '*'; tab[y + 1][x] = '*';
  304.         }
  305.     }
  306.  
  307.     for (int i = 0; i < 2; i++)
  308.     {
  309.         do {
  310.             check = true;
  311.             x = rand() % 10;
  312.             y = rand() % 10;
  313.             if (tab[x][y] == '*')
  314.                 check = false;
  315.         } while (!check);
  316.         tab[x][y] = '*';
  317.     }
  318.  
  319.     for (int i = 0; i < size; i++){
  320.         for (int j = 0; j < size; j++)
  321.             cout << setw(2) << tab[i][j];
  322.         cout << endl;
  323.     }
  324. }
  325.  
  326. void vnos_usr(char tab[][size])
  327. {
  328.     bool rot, check;
  329.     unsigned int x, y;
  330.     cout << "Vnesi orientacijo ladje 5 (0 vertikalno, 1 horizontalno): ";
  331.     cin >> rot;
  332.     if (rot)// horizontalno
  333.     {
  334.         cout << "Vnesi vrstico (0-9): ";
  335.         do {
  336.             cin >> y;
  337.         } while (y > 9);
  338.         cout << "Vnesi stolpec(0-5): ";
  339.         do {
  340.             cin >> x;
  341.         } while (x > 5);
  342.         for (int i = 0; i < 5; i++)
  343.             tab[y][x + i] = '*';
  344.     }
  345.     else //vertikalno
  346.     {
  347.         cout << "Vnesi vrstico (0-5): ";
  348.         do {
  349.             cin >> y;
  350.         } while (y > 5);
  351.         cout << "Vnesi stolpec (0-9): ";
  352.         do {
  353.             cin >> x;
  354.         } while (x > 9);
  355.         for (int i = 0; i < 5; i++)
  356.             tab[y + i][x] = '*';
  357.     }
  358.  
  359.     for (int i = 0; i < size; i++)
  360.     {
  361.         for (int j = 0; j < size; j++)
  362.             cout << setw(2) << tab[i][j];
  363.         cout << endl;
  364.     }
  365.  
  366.     do {
  367.         check = true;
  368.         cout << "Vnesi orientacijo ladje 4 (0 vertikalno, 1 horizontalno): ";
  369.         cin >> rot;
  370.  
  371.         if (rot) // hor
  372.         {
  373.             cout << "Vnesi vrstico (0-9): ";
  374.             do{
  375.                 cin >> y;
  376.             } while (y > 9);
  377.             cout << "Vnesi stolpec (0-6): ";
  378.             do {
  379.                 cin >> x;
  380.             } while (x > 6);
  381.         }
  382.  
  383.         else //ver
  384.         {
  385.             cout << "Vnesi vrstico (0-6): ";
  386.             do {
  387.                 cin >> y;
  388.             } while (y > 6);
  389.             cout << "Vnesi stolpec (0-9): ";
  390.             do{
  391.                 cin >> x;
  392.             } while (x > 9);
  393.         }
  394.  
  395.         for (int i = 0; i < 4; i++)
  396.         {
  397.             if (rot)
  398.             {
  399.                 if (tab[y][x + i] == '*')
  400.                     check = false;
  401.             }
  402.             else
  403.             {
  404.                 if (tab[y + i][x] == '*')
  405.                     check = false;
  406.             }
  407.         }
  408.  
  409.         if (!check)
  410.             cout << "Ladja se prekriva, vpisi ponovno" << endl;
  411.     } while (!check);
  412.  
  413.     for (int i = 0; i < 4; i++)
  414.     {
  415.         if (rot)
  416.             tab[y][x + i] = '*';
  417.         else
  418.             tab[y + i][x] = '*';
  419.     }
  420.  
  421.     for (int i = 0; i < size; i++)
  422.     {
  423.         for (int j = 0; j < size; j++)
  424.             cout << setw(2) << tab[i][j];
  425.         cout << endl;
  426.     }
  427.  
  428.     do{
  429.         check = true;
  430.         cout << "Vnesi orientacijo ladje 3 (0 vertikalno, 1 horizontalno): ";
  431.         cin >> rot;
  432.         if (rot) // horizontalno
  433.         {
  434.             cout << "Vnesi vrstico (0-9): ";
  435.             do{
  436.                 cin >> y;
  437.             } while (y > 9);
  438.             cout << "Vnesi stoplec (0-7): ";
  439.             do {
  440.                 cin >> x;
  441.             } while (x > 7);
  442.         }
  443.         else {
  444.             cout << "Vnesi vrstico (0-7): ";
  445.             do {
  446.                 cin >> y;
  447.             } while (y > 7);
  448.             cout << "Vnesi stolpec (0-9): ";
  449.             do {
  450.                 cin >> x;
  451.             } while (x > 9);
  452.         }
  453.         for (int i = 0; i < 3; i++)
  454.         {
  455.             if (rot)
  456.             {
  457.                 if (tab[y][x + i] == '*') check = false;
  458.             }
  459.             else
  460.                 if (tab[y + i][x] == '*') check = false;
  461.         }
  462.         if (!check)
  463.             cout << "Ladja se prekriva, vpisi ponovno." << endl;
  464.     } while (!check);
  465.  
  466.     for (int i = 0; i < 3; i++)
  467.     {
  468.         if (rot)
  469.             tab[y][x + i] = '*';
  470.         else
  471.             tab[y + i][x] = '*';
  472.     }
  473.  
  474.     for (int i = 0; i < size; i++)
  475.     {
  476.         for (int j = 0; j < size; j++)
  477.             cout << setw(2) << tab[i][j];
  478.         cout << endl;
  479.     }
  480.  
  481.     for (int huehue = 0; huehue < 2; huehue++)
  482.     {
  483.         do {
  484.             check = true;
  485.             cout << "Vnesi orientacijo ladje 2 (0 vertikalno, 1 hotizontalno): ";
  486.             cin >> rot;
  487.             if (rot) // hor
  488.             {
  489.                 cout << "Vnesi vrstico (0-9): ";
  490.                 do {
  491.                     cin >> y;
  492.                 } while (y > 9);
  493.                 cout << "Vnesi stolpec (0-8): ";
  494.                 do {
  495.                     cin >> x;
  496.                 } while (x > 8);
  497.             }
  498.             else
  499.             {
  500.                 cout << "Vnesi vrstico (0-8): ";
  501.                 do {
  502.                     cin >> y;
  503.                 } while (y > 8);
  504.                 cout << "Vnesi stolpec (0-9): ";
  505.                 do {
  506.                     cin >> x;
  507.                 } while (x > 9);
  508.             }
  509.             if (rot)
  510.             {
  511.                 if (tab[y][x] == '*' || tab[y][x + 1] == '*')
  512.                     check = false;
  513.             }
  514.             else
  515.                 if (tab[y][x] == '*' || tab[y + 1][x] == '*')
  516.                     check = false;
  517.             if (!check)
  518.                 cout << "Ladja se prekriva, vpisi ponovno." << endl;
  519.         } while (!check);
  520.  
  521.         if (rot)
  522.         {
  523.             tab[y][x] = '*'; tab[y][x + 1] = '*';
  524.         }
  525.         else
  526.         {
  527.             tab[y][x] = '*'; tab[y + 1][x] = '*';
  528.         }
  529.  
  530.         for (int i = 0; i < size; i++)
  531.         {
  532.             for (int j = 0; j < size; j++)
  533.                 cout << setw(2) << tab[i][j];
  534.             cout << endl;
  535.         }
  536.     }
  537.  
  538.     for (int i = 0; i < 2; i++)
  539.     {
  540.         do{
  541.             check = true;
  542.             cout << "Vnesi vrstico (0-9): ";
  543.             do {
  544.                 cin >> y;
  545.             } while (y > 9);
  546.             cout << "Vnesi stolpec (0-9): ";
  547.             do {
  548.                 cin >> x;
  549.             } while (x > 9);
  550.             if (tab[y][x] == '*')
  551.             {
  552.                 cout << "Ladja se prekriva, vpisi ponovno." << endl;
  553.                 check = false;
  554.             }
  555.         } while (!check);
  556.         tab[y][x] = '*';
  557.  
  558.         for (int i = 0; i < size; i++)
  559.         {
  560.             for (int j = 0; j < size; j++)
  561.                 cout << setw(2) << tab[i][j];
  562.             cout << endl;
  563.         }
  564.     }
  565. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement