Domanda

I'm coding a battleship in console mode with C#. When I drop my boats they cross over each other so I have a class cases (mean square of the playing board) and a Listposition which contain a boolean proprety Used.

Here's a part of the code:

bool isok;
do
{
    isok = true;
    Console.WriteLine("Donner la position du haut de votre " + bat.Name + " en X");
    int y = int.Parse(Console.ReadLine());
    Console.WriteLine("Donner la position du haut du navire en Y");
    int x = int.Parse(Console.ReadLine());
    Console.WriteLine("Positionnement Vertical (1) ou Horizontal (2) ");                  
    int direction = int.Parse(Console.ReadLine());

    var essay = b1.Position.Select(e=>e.Used);

    bool overlap = false;
    for (int i = y; i <= y + bat.Size - 1; i++)
        overlap = overlap || b1.Position.Exists(c => c.Y == y && c.X == x && c.Used);

    switch (direction)
    {
        case 2:
        {
            if ((y + bat.Size - 1 > 9) || overlap)
            {
                isok = false;
            }  

            {
                isok = false;
            }  
            else
            {
                for (int i = y; i <= y + bat.Size-1 ; i++)
                {

                    b1.Position.Add(new cases(x, i, true, false, false, false));
                    b1.changetype(x, i);
                    b1.changeUsed(x, i);
                    b2.changetype(x, i);

                }
            }       
            break;
        }
    }
}

public cases(int x , int y, bool target, bool touch, bool played, bool used)
{
    this.x = x;
    this.y = y;
    this.target = target;
    this.touch = touch;
    this.played = played;
    this.used = used;
}

public void changetype(int x,int y)
{           
        vue[x, y].Target = true;
}

public void changeUsed(int x, int y)
{
    vue[x, y].Used = true;
}

public List<cases> Position
{
    get
    {
        return position;
    }
    set
    {
        position = value;
    }
}

It's in French but it's just asking to enter two coordinates (x and y) to get the place of the starting case of the boat. My question is which extensible method I can use in my if condition? I want in fact that the list being read before the boat is placed there is a hold in the if line help me to fill in.

È stato utile?

Soluzione

I think List.Exists() is what you are looking for.

Position.Exists(c => c.used)

Edit: I think you'll need a for loop before the if statement that records if there are any overlaps.

bool overlap = false;
for (int i = y; i <= y + bat.Size-1; i++)
    overlap = overlap || Position.Exists(c => c.y == y && c.x == x && c.used);

After that, use overlap in the if condition.

Edit: I made a mistake with the loop.

bool overlap = false;
for (int i = y; i <= y + bat.Size-1; i++)
    overlap = overlap || Position.Exists(c => c.y == i && c.x == x && c.used);

What this code is doing is that it checks if any space that your boat needs is already in use and records if it is. If it's easier for you to read, this is the same as the above:

bool overlap = false;
for (int i = y; i <= y + bat.Size-1; i++)
    if (Position.Exists(c => c.y == i && c.x == x && c.used))
        overlap = true;

Also, this code should be inside case 2 and before the if statement.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top