Advertisement
Leedwon

Untitled

Jun 12th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.91 KB | None | 0 0
  1. // wersja z dzialajacym zaprzyjaznieniem header
  2.  
  3. #ifndef STATKIH_H_
  4. #define STATKIH_H_
  5.  
  6. class Board;
  7.  
  8. class Ship
  9. {
  10. private:
  11.     int length;
  12.     int hp;
  13.     int * x;
  14.     int * y;
  15. public:
  16.     friend class Board;
  17.     Ship(int l = 0) : length(l), hp(l) { x = new int[length]; y = new int[length]; }
  18.     ~Ship() { delete[] x; delete[] y; }
  19.     void SetLen(int l) { delete[] x; delete[] y; x = new int[length]; y = new int[length]; }
  20.     void SetHp(int h) { hp = h; }
  21.     void GetCoords(Board & b);
  22.     bool IsDead();
  23.     bool IsHit();
  24.  
  25. };
  26.  
  27. class Board
  28. {
  29. private:
  30.     char board[10][20];
  31.     int privateBoard[10][10];
  32.     Ship destroyer;
  33.     Ship cruiser[2];
  34.     Ship frigate[3];
  35.     Ship submarine[4];
  36.     bool CoordsApproval(int x, int y); // sprawdza czy podane koordyanty sa poprawne, sasiadujace statki
  37.     bool CoordsApproval2(); // sprawdza czy podany statek jest budowane poprawnie a nie np w kwadrat dodaj tu jescze parametry do wymyslenia potem
  38.     void PlaceCoords(int x, int y) { privateBoard[x][y] = 1; } // zmienia wartosci privateBoard z 0 na 1 "buduje czesc statku"
  39.     void PrintCoords(); // zamienia wspolrzedne z privateBoard 0, 1 na statki na planszy 'X'
  40. public:
  41.     friend class Ship;
  42.     Board();
  43.     void ShowBoard(); // wyswietla plansze
  44.     void PutShips(); // laczy ze soba wszystkie funkcje zwiazane z koordynatami i rozmieszcza statki
  45.     void Update(); // uptadtuje plansze?
  46. };
  47. #endif
  48.  
  49.  
  50.  
  51. --------------------------------------------------------------------------------------------------------------------------------------
  52.  
  53.  
  54. metody z headera
  55.  
  56. #include "StatkiH.h"
  57. #include <iostream>
  58.  
  59. Board::Board()  // tworzenie planszy
  60. {
  61.     for (int i = 0; i < 10; i++) // rysowanie planszy
  62.     {
  63.         for (int j = 0; j < 20; j++)
  64.         {
  65.             if (j % 2 == 0)
  66.                 board[i][j] = '|';
  67.             else if(j % 2 == 1)
  68.                 board[i][j] = '_';
  69.         }
  70.     }
  71.     for (int i = 0; i < 10; i++) // plansza intow na ktorej beda przeprowadzane operacje
  72.     {
  73.         for (int j = 0; j < 20; j++)
  74.         {
  75.             privateBoard[i][j] = 0;
  76.         }
  77.     }
  78.     destroyer.SetLen(4);
  79.     destroyer.SetHp(4);
  80.     for (int i = 0; i < 3; i++)
  81.     {
  82.         if (i < 2)
  83.         {
  84.             cruiser[i].SetHp(3);
  85.             cruiser[i].SetLen(3);
  86.         }
  87.         if (i < 3)
  88.         {
  89.             frigate[i].SetHp(2);
  90.             frigate[i].SetLen(2);
  91.         }
  92.         submarine[i].SetHp(1);
  93.         submarine[i].SetLen(1);
  94.     }
  95. }
  96. void Board::PutShips()
  97. {
  98.     std::cout << "Podaj koordnaty niszczyciela (4): ";
  99.     destroyer.GetCoords(*this);
  100.     std::cout << "Podaj koordynaty krazownikow (3) x 2: ";
  101.     for (int i = 0; i < 2; i++)
  102.         cruiser[i].GetCoords(*this);
  103.     std::cout << "Podaj koordynaty fregat (2) x 3: ";
  104.     for (int i = 0; i < 3; i++)
  105.         frigate[i].GetCoords(*this);
  106.     std::cout << "Podaj koordynaty lodzi podwodnych (2) x 3: ";
  107.     for (int i = 0; i < 3; i++)
  108.         submarine[i].GetCoords(*this);
  109.  
  110. }
  111. void Board::ShowBoard()
  112. {
  113.     std::cout << "  ";
  114.     for (char i = 'A'; i < 'K'; i++)
  115.         std::cout << " " << i;
  116.     std::cout << std::endl;
  117.     std::cout << "   ";
  118.     for (int i = 0; i < 10; i++)
  119.         std::cout << "_ ";
  120.     for (int i = 0; i < 10; i++)
  121.     {
  122.         if (i == 9)
  123.             std::cout << "\n" << i + 1;
  124.         else
  125.             std::cout << "\n " << i + 1;
  126.         for(int j = 0; j < 20; j++)
  127.         {
  128.             std::cout << board[i][j];
  129.         }
  130.         std::cout << "|";
  131.     }
  132. }
  133. bool Board::CoordsApproval(int x, int y)
  134. {
  135.     bool checks[9];
  136.     if (privateBoard[x][y] == 0) // x, y
  137.         checks[0] = true;
  138.     if (x - 1 < 0 || x + 1 > 9) // x + 1, y poza mapa
  139.         checks[1] = true;
  140.     else if ((x - 1 < 0 || x + 1 > 9) == false) // x + 1, y w mapie
  141.     {
  142.         if (privateBoard[x + 1][y] == 0)
  143.             checks[1] = true;
  144.     }
  145.     if (x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) // x+1, y+1 poza mapa
  146.         checks[2] = true;
  147.     else if ((x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) == false) // x+1, y+1 w mapie
  148.     {
  149.         if (privateBoard[x + 1][y + 1] == 0)
  150.             checks[2] = true;
  151.     }
  152.     if (y - 1 < 0 || y + 1 > 9) // x, y+1 poza mapa
  153.         checks[3] = true;
  154.     else if ((y - 1 < 0 || y + 1 > 9) == false) // x, y+1 w mapie
  155.     {
  156.         if (privateBoard[x][y + 1] == 0)
  157.             checks[3] = true;
  158.     }
  159.     if (x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) // x-1, y+1 poza mapa
  160.         checks[4] = true;
  161.     else if ((x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) == false) // x-1, y+1 w mapie
  162.     {
  163.         if (privateBoard[x - 1][y + 1] == 0)
  164.             checks[4] = true;
  165.     }
  166.     if (x - 1 < 0 || x + 1 > 9) // x-1, y poza mapa
  167.         checks[5] = true;
  168.     else if ((x - 1 < 0 || x + 1 > 9) == false)
  169.     {
  170.         if (privateBoard[x - 1][y] == 0)
  171.             checks[5] = true;
  172.     }
  173.     if (x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) // x-1, y-1 poza mapa
  174.         checks[6] = true;
  175.     else if ((x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) == false)
  176.     {
  177.         if (privateBoard[x - 1][y - 1] == 0)
  178.             checks[6] = true;
  179.     }
  180.     if (y - 1 < 0 || y + 1 > 9) // x, y-1 poza mapa
  181.         checks[7] = true;
  182.     else if ((y - 1 < 0 || y + 1 > 9) == false)
  183.     {
  184.         if (privateBoard[x][y - 1] == 0)
  185.             checks[7] = true;
  186.     }
  187.     if (x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) // x+1, y-1 poza mapa
  188.         checks[8] = true;
  189.     else if ((x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) == false)
  190.     {
  191.         if (privateBoard[x + 1][y + 1] == 0)
  192.             checks[8] = true;
  193.     }
  194.    
  195.     int checkCount = 0;
  196.     for (int i = 0; i < 9; i++) // sprawdzenie wszystkich "check markow"
  197.     {
  198.         if (checks[i] == true)
  199.             checkCount++;
  200.     }
  201.     if (checkCount == 8)
  202.         return true;
  203.     else
  204.         return false;
  205. }
  206. void Ship::GetCoords(Board & b)
  207. {
  208.     char c, d;
  209.     std::cout << "Podaj koordynaty statku (np. A5): ";
  210.     for (int i = 0; i < length; i++)
  211.     {
  212.         while (std::cin.get() != '\n')
  213.             continue;
  214.         std::cin >> c;
  215.         x[i] = c - 65;
  216.         while (std::cin.get() != '\n')
  217.             continue;
  218.         std::cin >> d;
  219.         y[i] = d - 49;
  220.         if (b.CoordsApproval(x[i], y[i]) == false)
  221.         {
  222.             std::cout << "Bleedne koordynaty podaj ponownie.\n";
  223.             i--;
  224.         }
  225.     }
  226.     for (int i = 0; i < length; i++)
  227.         b.PlaceCoords(x[i], y[i]);
  228. }
  229.  
  230. ---------------------------------------------------------------------------------------------------------------------------------------
  231.  
  232. main
  233.  
  234. #include "StatkiH.h"
  235.  
  236. int main()
  237. {
  238.     Board PlayersBoard;
  239.     PlayersBoard.ShowBoard();
  240.     return 0;
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement