#include iostream
#include ctime
#include windows.h
#include string
using namespace std;
class Player
{
private:
char **field = new char*[10];
string separation;
int dir, positionX, positionY, numOneDecks = 1, numTwoDecks = 2, numThreeDecks = 3, numFourDecks = 4;
bool SettingIsPossibleScope(int direction, int x, int y, int numDecks);
bool SettingIsPossibleObstacles(char** arr, int x, int y, int numDecks);
void foo(int numDecks);
void randXY(char** arr, int& x, int& y, int &direction);
public:
Player();
void PrintField();
void SetShips(int numDeck);
~Player();
};
Player:layer()
{
for (int i = 0; i < 10; i++)
{
field = new char[10];
}
for (int i = 0; i < 10; i++) // заполнение поля пустыми пробелами
{
for (int j = 0; j < 10; j++)
{
field[j] = ' ';
}
}
separation = " ----------------------------------------";
randXY(field, positionX, positionY, dir);
}
Player::~Player()
{
for (int i = 0; i < 10; i++)
{
delete[] field;
}
delete[] field;
}
void Player:rintField()
{
cout << " ";
for (char i = 'а'; i <= 'й'; i++)
{
cout << i << " ";
}
cout << endl << separation << endl;
for (int i = 0; i < 10; i++)
{
if (i == 9)
{
cout << " " << i + 1 << "| ";
}
else
{
cout << " " << i + 1 << " | ";
}
for (int j = 0; j < 10; j++)
{
cout << field[j] << " | ";
}
cout << endl << separation << endl;
}
}
bool Player::SettingIsPossibleScope(int direction, int x, int y, int numDecks)
{
switch (direction)
{
case 0:
if (x + (numDecks-1) > 9)
{
return false;
}
return true;
case 1:
if (x - (numDecks - 1) < 0)
{
return false;
}
return true;
case 2:
if (y + (numDecks - 1) > 9)
{
return false;
}
return true;
case 3:
if (y - (numDecks - 1) < 0)
{
return false;
}
return true;
}
}
bool Player::SettingIsPossibleObstacles(char** arr, int x, int y, int numDecks) // проверяет нет ли кораблей вокруг
{
for (int i = 0; i < numDecks; i++)
{
if ((x < 9 && y < 9 && x > 0 && y > 0) && (arr[x + 1][y] == 'X' || arr[x - 1][y] == 'X'
|| arr[x][y + 1] == 'X' || arr[x][y - 1] == 'X'
|| arr[x - 1][y - 1] == 'X' || arr[x - 1][y + 1] == 'X'
|| arr[x + 1][y - 1] == 'X' || arr[x + 1][y + 1] == 'X'))
{
return false;
}
x++;
y++;
}
return true;
}
void Player::randXY(char **arr, int &x, int &y, int &direction) // рандомин начальные координаты
{
do
{
direction = rand() % 4;
y = rand() % 10;
x = rand() % 10;
} while (arr[x][y] != ' ');
}
void Player::foo(int numDecks)
{
for (int i = 0; i < numDecks; i++) //расстановка 4-x палубных
{
field[positionX][positionY] = 'X';
switch (dir)
{
case 0:
if ((!SettingIsPossibleScope(dir, positionX, positionY, numDecks) || !SettingIsPossibleObstacles(field, positionX, positionY, numDecks)) && i == 0)
{
field[positionX][positionY] = ' ';
randXY(field, positionX, positionY, dir);
i--;
break;
}
positionX++;
break;
case 1:
if ((!SettingIsPossibleScope(dir, positionX, positionY, numDecks) || !SettingIsPossibleObstacles(field, positionX, positionY, numDecks)) && i == 0)
{
field[positionX][positionY] = ' ';
randXY(field, positionX, positionY, dir);
i--;
break;
}
positionX--;
break;
case 2:
if ((!SettingIsPossibleScope(dir, positionX, positionY, numDecks) || !SettingIsPossibleObstacles(field, positionX, positionY, numDecks)) && i == 0)
{
field[positionX][positionY] = ' ';
randXY(field, positionX, positionY, dir);
i--;
break;
}
positionY++;
break;
#include ctime
#include windows.h
#include string
using namespace std;
class Player
{
private:
char **field = new char*[10];
string separation;
int dir, positionX, positionY, numOneDecks = 1, numTwoDecks = 2, numThreeDecks = 3, numFourDecks = 4;
bool SettingIsPossibleScope(int direction, int x, int y, int numDecks);
bool SettingIsPossibleObstacles(char** arr, int x, int y, int numDecks);
void foo(int numDecks);
void randXY(char** arr, int& x, int& y, int &direction);
public:
Player();
void PrintField();
void SetShips(int numDeck);
~Player();
};
Player:layer()
{
for (int i = 0; i < 10; i++)
{
field = new char[10];
}
for (int i = 0; i < 10; i++) // заполнение поля пустыми пробелами
{
for (int j = 0; j < 10; j++)
{
field[j] = ' ';
}
}
separation = " ----------------------------------------";
randXY(field, positionX, positionY, dir);
}
Player::~Player()
{
for (int i = 0; i < 10; i++)
{
delete[] field;
}
delete[] field;
}
void Player:rintField()
{
cout << " ";
for (char i = 'а'; i <= 'й'; i++)
{
cout << i << " ";
}
cout << endl << separation << endl;
for (int i = 0; i < 10; i++)
{
if (i == 9)
{
cout << " " << i + 1 << "| ";
}
else
{
cout << " " << i + 1 << " | ";
}
for (int j = 0; j < 10; j++)
{
cout << field[j] << " | ";
}
cout << endl << separation << endl;
}
}
bool Player::SettingIsPossibleScope(int direction, int x, int y, int numDecks)
{
switch (direction)
{
case 0:
if (x + (numDecks-1) > 9)
{
return false;
}
return true;
case 1:
if (x - (numDecks - 1) < 0)
{
return false;
}
return true;
case 2:
if (y + (numDecks - 1) > 9)
{
return false;
}
return true;
case 3:
if (y - (numDecks - 1) < 0)
{
return false;
}
return true;
}
}
bool Player::SettingIsPossibleObstacles(char** arr, int x, int y, int numDecks) // проверяет нет ли кораблей вокруг
{
for (int i = 0; i < numDecks; i++)
{
if ((x < 9 && y < 9 && x > 0 && y > 0) && (arr[x + 1][y] == 'X' || arr[x - 1][y] == 'X'
|| arr[x][y + 1] == 'X' || arr[x][y - 1] == 'X'
|| arr[x - 1][y - 1] == 'X' || arr[x - 1][y + 1] == 'X'
|| arr[x + 1][y - 1] == 'X' || arr[x + 1][y + 1] == 'X'))
{
return false;
}
x++;
y++;
}
return true;
}
void Player::randXY(char **arr, int &x, int &y, int &direction) // рандомин начальные координаты
{
do
{
direction = rand() % 4;
y = rand() % 10;
x = rand() % 10;
} while (arr[x][y] != ' ');
}
void Player::foo(int numDecks)
{
for (int i = 0; i < numDecks; i++) //расстановка 4-x палубных
{
field[positionX][positionY] = 'X';
switch (dir)
{
case 0:
if ((!SettingIsPossibleScope(dir, positionX, positionY, numDecks) || !SettingIsPossibleObstacles(field, positionX, positionY, numDecks)) && i == 0)
{
field[positionX][positionY] = ' ';
randXY(field, positionX, positionY, dir);
i--;
break;
}
positionX++;
break;
case 1:
if ((!SettingIsPossibleScope(dir, positionX, positionY, numDecks) || !SettingIsPossibleObstacles(field, positionX, positionY, numDecks)) && i == 0)
{
field[positionX][positionY] = ' ';
randXY(field, positionX, positionY, dir);
i--;
break;
}
positionX--;
break;
case 2:
if ((!SettingIsPossibleScope(dir, positionX, positionY, numDecks) || !SettingIsPossibleObstacles(field, positionX, positionY, numDecks)) && i == 0)
{
field[positionX][positionY] = ' ';
randXY(field, positionX, positionY, dir);
i--;
break;
}
positionY++;
break;