На поле отображается только последний добавленный в список корабль. Такое ощущение, что поле сбрасывает координаты всех других кораблей, кроме последнего.
Вот коды:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Sea_Battle
{
class Program
{
static void Main(string[] args)
{
BattleField field = new BattleField();
field.PlaceShips();
field.PrintArea();
Console.ReadLine();
}
}
}
Класс корабля:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sea_Battle
{
class Ship
{
public bool Horizontal { get; private set; }
public int SizeX { get; private set; }
public int SizeY { get; private set; }
public List<int[]> Coords { get; private set; }
public List<int[]> ShotCoords { get; set; }
public Ship(int size)
{
Random fiftyfifty = new Random();
Coords = new List<int[]>();
ShotCoords = new List<int[]>();
if (fiftyfifty.Next(0, 2) == 0)
{
Horizontal = true;
SizeX = size;
SizeY = 1;
}
else
{
Horizontal = false;
SizeX = 1;
SizeY = size;
}
}
}
}
Класс игрового поля:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sea_Battle
{
class BattleField
{
string[] letters = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "K" };
char[,] visibleArea = new char[10, 10];
bool[,] hiddenArea = new bool[10, 10];
List<Ship> shipList = new List<Ship>();
public void PlaceShips()
{
Ship odnushka1 = new Ship(1);
Ship odnushka2 = new Ship(1);
Ship odnushka3 = new Ship(1);
Ship odnushka4 = new Ship(1);
Ship dvushka1 = new Ship(2);
Ship dvushka2 = new Ship(2);
Ship dvushka3 = new Ship(2);
Ship treshka1 = new Ship(3);
Ship treshka2 = new Ship(3);
Ship chetire = new Ship(4);
shipList.Add(odnushka1);
shipList.Add(odnushka2);
shipList.Add(odnushka3);
shipList.Add(odnushka4);
shipList.Add(dvushka1);
shipList.Add(dvushka2);
shipList.Add(dvushka3);
shipList.Add(treshka1);
shipList.Add(treshka2);
shipList.Add(chetire);
foreach (Ship ship in shipList)
{
PlaceShip(ship);
Console.WriteLine("jvlisdbgv");
}
}
void PlaceShip(Ship ship)
{
Random r = new Random();
int row, col;
if (ship.Horizontal)
{
row = r.Next(0, 10);
col = r.Next(0, 11 - ship.SizeX);
}
else
{
row = r.Next(0, 11 - ship.SizeY);
col = r.Next(0, 10);
}
for (int i = 0; i < ship.SizeX; i++)
{
for (int j = 0; j < ship.SizeY; j++)
{
hiddenArea[col + i, row + j] = true;
ship.Coords.Add(new int[] { col + i, row + j });
}
}
}
public void PrintArea()
{
string areaString = " 0 1 2 3 4 5 6 7 8 9\n";
for (int i = 0; i < 10; i++)
{
areaString += $"{letters[i]} ";
for (int j = 0; j < 10; j++)
{
if (visibleArea[i, j] == '\0')
{
visibleArea[i, j] = '~';
}
if (hiddenArea[i, j]) visibleArea[i, j] = 'O';
areaString += $"{visibleArea[i,j]} ";
}
areaString += "\n";
}
Console.WriteLine(areaString);
}
}
}
Консоль выводит:
0 1 2 3 4 5 6 7 8 9
A ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
B ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
C ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
D ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
E O ~ ~ ~ ~ ~ ~ ~ ~ ~
F O ~ ~ ~ ~ ~ ~ ~ ~ ~
G O ~ ~ ~ ~ ~ ~ ~ ~ ~
H O ~ ~ ~ ~ ~ ~ ~ ~ ~
J ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
K ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Как видно, на экран выводится только четырёхплубный корабль. Я пробовал его убрать, но выводился последний созданный корабль. Видимо что-то не так с методом PrintArea()