Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // wersja z dzialajacym zaprzyjaznieniem header
- #ifndef STATKIH_H_
- #define STATKIH_H_
- class Board;
- class Ship
- {
- private:
- int length;
- int hp;
- int * x;
- int * y;
- public:
- friend class Board;
- Ship(int l = 0) : length(l), hp(l) { x = new int[length]; y = new int[length]; }
- ~Ship() { delete[] x; delete[] y; }
- void SetLen(int l) { delete[] x; delete[] y; x = new int[length]; y = new int[length]; }
- void SetHp(int h) { hp = h; }
- void GetCoords(Board & b);
- bool IsDead();
- bool IsHit();
- };
- class Board
- {
- private:
- char board[10][20];
- int privateBoard[10][10];
- Ship destroyer;
- Ship cruiser[2];
- Ship frigate[3];
- Ship submarine[4];
- bool CoordsApproval(int x, int y); // sprawdza czy podane koordyanty sa poprawne, sasiadujace statki
- bool CoordsApproval2(); // sprawdza czy podany statek jest budowane poprawnie a nie np w kwadrat dodaj tu jescze parametry do wymyslenia potem
- void PlaceCoords(int x, int y) { privateBoard[x][y] = 1; } // zmienia wartosci privateBoard z 0 na 1 "buduje czesc statku"
- void PrintCoords(); // zamienia wspolrzedne z privateBoard 0, 1 na statki na planszy 'X'
- public:
- friend class Ship;
- Board();
- void ShowBoard(); // wyswietla plansze
- void PutShips(); // laczy ze soba wszystkie funkcje zwiazane z koordynatami i rozmieszcza statki
- void Update(); // uptadtuje plansze?
- };
- #endif
- --------------------------------------------------------------------------------------------------------------------------------------
- metody z headera
- #include "StatkiH.h"
- #include <iostream>
- Board::Board() // tworzenie planszy
- {
- for (int i = 0; i < 10; i++) // rysowanie planszy
- {
- for (int j = 0; j < 20; j++)
- {
- if (j % 2 == 0)
- board[i][j] = '|';
- else if(j % 2 == 1)
- board[i][j] = '_';
- }
- }
- for (int i = 0; i < 10; i++) // plansza intow na ktorej beda przeprowadzane operacje
- {
- for (int j = 0; j < 20; j++)
- {
- privateBoard[i][j] = 0;
- }
- }
- destroyer.SetLen(4);
- destroyer.SetHp(4);
- for (int i = 0; i < 3; i++)
- {
- if (i < 2)
- {
- cruiser[i].SetHp(3);
- cruiser[i].SetLen(3);
- }
- if (i < 3)
- {
- frigate[i].SetHp(2);
- frigate[i].SetLen(2);
- }
- submarine[i].SetHp(1);
- submarine[i].SetLen(1);
- }
- }
- void Board::PutShips()
- {
- std::cout << "Podaj koordnaty niszczyciela (4): ";
- destroyer.GetCoords(*this);
- std::cout << "Podaj koordynaty krazownikow (3) x 2: ";
- for (int i = 0; i < 2; i++)
- cruiser[i].GetCoords(*this);
- std::cout << "Podaj koordynaty fregat (2) x 3: ";
- for (int i = 0; i < 3; i++)
- frigate[i].GetCoords(*this);
- std::cout << "Podaj koordynaty lodzi podwodnych (2) x 3: ";
- for (int i = 0; i < 3; i++)
- submarine[i].GetCoords(*this);
- }
- void Board::ShowBoard()
- {
- std::cout << " ";
- for (char i = 'A'; i < 'K'; i++)
- std::cout << " " << i;
- std::cout << std::endl;
- std::cout << " ";
- for (int i = 0; i < 10; i++)
- std::cout << "_ ";
- for (int i = 0; i < 10; i++)
- {
- if (i == 9)
- std::cout << "\n" << i + 1;
- else
- std::cout << "\n " << i + 1;
- for(int j = 0; j < 20; j++)
- {
- std::cout << board[i][j];
- }
- std::cout << "|";
- }
- }
- bool Board::CoordsApproval(int x, int y)
- {
- bool checks[9];
- if (privateBoard[x][y] == 0) // x, y
- checks[0] = true;
- if (x - 1 < 0 || x + 1 > 9) // x + 1, y poza mapa
- checks[1] = true;
- else if ((x - 1 < 0 || x + 1 > 9) == false) // x + 1, y w mapie
- {
- if (privateBoard[x + 1][y] == 0)
- checks[1] = true;
- }
- if (x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) // x+1, y+1 poza mapa
- checks[2] = true;
- else if ((x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) == false) // x+1, y+1 w mapie
- {
- if (privateBoard[x + 1][y + 1] == 0)
- checks[2] = true;
- }
- if (y - 1 < 0 || y + 1 > 9) // x, y+1 poza mapa
- checks[3] = true;
- else if ((y - 1 < 0 || y + 1 > 9) == false) // x, y+1 w mapie
- {
- if (privateBoard[x][y + 1] == 0)
- checks[3] = true;
- }
- if (x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) // x-1, y+1 poza mapa
- checks[4] = true;
- else if ((x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) == false) // x-1, y+1 w mapie
- {
- if (privateBoard[x - 1][y + 1] == 0)
- checks[4] = true;
- }
- if (x - 1 < 0 || x + 1 > 9) // x-1, y poza mapa
- checks[5] = true;
- else if ((x - 1 < 0 || x + 1 > 9) == false)
- {
- if (privateBoard[x - 1][y] == 0)
- checks[5] = true;
- }
- if (x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) // x-1, y-1 poza mapa
- checks[6] = true;
- else if ((x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) == false)
- {
- if (privateBoard[x - 1][y - 1] == 0)
- checks[6] = true;
- }
- if (y - 1 < 0 || y + 1 > 9) // x, y-1 poza mapa
- checks[7] = true;
- else if ((y - 1 < 0 || y + 1 > 9) == false)
- {
- if (privateBoard[x][y - 1] == 0)
- checks[7] = true;
- }
- if (x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) // x+1, y-1 poza mapa
- checks[8] = true;
- else if ((x - 1 < 0 || x + 1 > 9 || y - 1 < 0 || y + 1 > 9) == false)
- {
- if (privateBoard[x + 1][y + 1] == 0)
- checks[8] = true;
- }
- int checkCount = 0;
- for (int i = 0; i < 9; i++) // sprawdzenie wszystkich "check markow"
- {
- if (checks[i] == true)
- checkCount++;
- }
- if (checkCount == 8)
- return true;
- else
- return false;
- }
- void Ship::GetCoords(Board & b)
- {
- char c, d;
- std::cout << "Podaj koordynaty statku (np. A5): ";
- for (int i = 0; i < length; i++)
- {
- while (std::cin.get() != '\n')
- continue;
- std::cin >> c;
- x[i] = c - 65;
- while (std::cin.get() != '\n')
- continue;
- std::cin >> d;
- y[i] = d - 49;
- if (b.CoordsApproval(x[i], y[i]) == false)
- {
- std::cout << "Bleedne koordynaty podaj ponownie.\n";
- i--;
- }
- }
- for (int i = 0; i < length; i++)
- b.PlaceCoords(x[i], y[i]);
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- main
- #include "StatkiH.h"
- int main()
- {
- Board PlayersBoard;
- PlayersBoard.ShowBoard();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement