C#
C#

help

Root Question Message

Pav
Pav12/17/2022
Battleships

Hey! I'm making a battleships program. I am currently working on the boat placing and checking that they don't overlap. A computer placing a boat works and there are no problems, however, when a player places a boat there is an infinite loop. This is most likely due to something wrong in the PlacingPlayer, CheckPlacement or CheckOverlap methods:
Pav
Pav12/17/2022
using static System.Console;

WriteLine("Enter 1 if you want to play a new game\nEnter 2 if you would like to resume a game\nEnter 3 if you would like to display instructions\nEnter anything else to quit");
string choice = ReadLine();

if (choice == "1")
{
    NewGame();
}

static void NewGame()
{
    char[,] computerGridNew = {
        {'-','-', '-','-', '-', '-', '-', '-'},
        {'-','-', '-','-', '-', '-', '-', '-'},
        {'-','-', '-','-', '-', '-', '-', '-'},
        {'-','-', '-','-', '-', '-', '-', '-'},
        {'-','-', '-','-', '-', '-', '-', '-'},
        {'-','-', '-','-', '-', '-', '-', '-'},
        {'-','-', '-','-', '-', '-', '-', '-'},
        {'-','-', '-','-', '-', '-', '-', '-'},
    };

    char[,] userGridNew = {
        {'-','-', '-','-', '-', '-', '-', '-'},
        {'-','-', '-','-', '-', '-', '-', '-'},
        {'-','-', '-','-', '-', '-', '-', '-'},
        {'-','-', '-','-', '-', '-', '-', '-'},
        {'-','-', '-','-', '-', '-', '-', '-'},
        {'-','-', '-','-', '-', '-', '-', '-'},
        {'-','-', '-','-', '-', '-', '-', '-'},
        {'-','-', '-','-', '-', '-', '-', '-'},
    };

    Play(computerGridNew, userGridNew);
}

static void Play(char[,] computerGrid, char[,] userGrid)
{
    var currentGame = new Run();
    currentGame.gridComputer = computerGrid;
    currentGame.gridUser = userGrid;
    currentGame.PlacingComputer();
    currentGame.PlacingPlayer();

}

public class Boat
{
    public Boat(int length, string name)
    {
        this.length = length;
        this.name = name;
    }
    public int row { get; set; }
    public int column { get; set; }
    public char status { get; set; }
    public int direction { get; set; }
    public string name { get; set; }
    public int length { get; set; }

    public int[,] eachCoord = new int[4, 2]; //Max boat size
}
Pav
Pav12/17/2022
public class Run
{
    static readonly Boat[] computerBoats =
    {
        new (1, "Computer Destroyer1"),
        new (1, "Computer Destroyer2"),
        new (2, "Computer Submarine1"),
        new (2, "Computer Submarine2"),
        new (4, "Computer Carrier"),
    };
    static readonly Boat[] playerBoats =
    {
        new (1, "Player Destroyer1"),
        new (1, "Player Destroyer2"),
        new (2, "Player Submarine1"),
        new (2, "Player Submarine2"),
        new (4, "Player Carrier"),
    };

    public char[,] gridComputer { get; set; }
    public char[,] gridUser { get; set; }

    public bool win = false;

    public void winCheck()
    {

    }
    public void PlacingComputer()
    {
        Random random = new Random();
        foreach (Boat currentBoat in computerBoats)
        {
            do
            {
                currentBoat.column = random.Next(0, 8);
                currentBoat.row = random.Next(0, 8);
                currentBoat.direction = random.Next(1, 5);
            }
            while (!CheckPlacement(currentBoat, true));
        }
        Display(gridComputer, true);

        foreach (Boat b in computerBoats)
        {
            for (int i = 0; i < b.length; i++)
            {
                WriteLine($"Boat {b.name} coordinate is: {Convert.ToChar(b.eachCoord[i, 0] + 65)} {b.eachCoord[i, 1] + 1}");
            }
        }
    }
Pav
Pav12/17/2022
   public void PlacingPlayer()
    {
        WriteLine("Enter the coordinate at which you'd like to place your boat. (e.g A7)\nYou will then be prompted for the direction you'd like to face your boat. (NESW)");
        foreach (Boat currentBoat in playerBoats)
        {
            do
            {
                bool convertCheck = false;
                int directionAsInt = 0;

                WriteLine($"Your coordinate for {currentBoat.name}:");
                string userCoordinate = ReadLine().ToUpper();

                while (!convertCheck)
                {
                    try
                    {
                        currentBoat.column = Convert.ToInt32(userCoordinate[1] - 1);
                        currentBoat.row = Convert.ToInt32(userCoordinate[0] - 65);
                        convertCheck = true;
                    }
                    catch (Exception ex)
                    {
                        WriteLine("Enter correct coordinates");
                    }
                }

                WriteLine($"Your direction for {currentBoat.name}:");
                string direction = ReadLine().ToUpper();


                directionAsInt = direction switch
                {
                    "N" => 1,
                    "E" => 2,
                    "S" => 3,
                    "W" => 4,
                    _ => 5,
                };

                currentBoat.direction = directionAsInt;


            }
            while (!CheckPlacement(currentBoat, false)!);
        }
        Display(gridComputer, true);
    }
Pav
Pav12/17/2022
   public bool CheckPlacement(Boat currentBoat, bool checkForComputer)
    {
        Boat[] currentBoats;
        char[,] currentGrid;

        if (checkForComputer)
        {
            currentBoats = computerBoats;
            currentGrid = gridComputer;
        }
        else
        {
            currentBoats = playerBoats;
            currentGrid = gridUser;
        }

        try
        {
            if (checkForComputer)
            {
                if (!CheckOverlap(currentBoat, currentGrid))
                {
                    gridComputer = Place(currentBoat, currentGrid);
                    return true;
                }
            }
            else
            {
                if (!CheckOverlap(currentBoat, currentGrid))
                {
                    gridUser = Place(currentBoat, currentGrid);
                    return true;
                }
            }
        }
        catch (Exception ex)
        {
        }

        return false;

    }
Pav
Pav12/17/2022
    public bool CheckOverlap(Boat currentBoat, char[,] currentGrid)
    {
        if (currentBoat.direction == 1)
        {
            for (int i = 0; i < currentBoat.length; i++)
            {
                if (currentGrid[currentBoat.row - i, currentBoat.column] == 'S')
                {
                    return true;
                }
            }
        }
        else if (currentBoat.direction == 2)
        {
            for (int i = 0; i < currentBoat.length; i++)
            {
                if (currentGrid[currentBoat.row, currentBoat.column + i] == 'S')
                {
                    return true;
                }
            }
        }
        else if (currentBoat.direction == 3)
        {
            for (int i = 0; i < currentBoat.length; i++)
            {
                if (currentGrid[currentBoat.row + i, currentBoat.column] == 'S')
                {
                    return true;
                }
            }
        }
        else if (currentBoat.direction == 4)
        {
            for (int i = 0; i < currentBoat.length; i++)
            {
                if (currentGrid[currentBoat.row, currentBoat.column - i] == 'S')
                {
                    return true;
                }
            }
        }
        return false;
    }
Pav
Pav12/17/2022
    public static void Display(char[,] grid, bool showAll)

    {
        char[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
        WriteLine("  1 2 3 4 5 6 7 8");
        WriteLine("  ________________");
        for (int i = 0; i < 8; i++)
        {
            Write($"{letters[i]}|");
            for (int j = 0; j < 8; j++)
            {
                char marker = grid[i, j];
                if (showAll)
                {
                    Write($"{marker} ");
                }
                else
                {
                    Write($"- ");
                }
            }
            Write("|");
            WriteLine();
        }
        WriteLine("  -----------------");
    }
}
Pav
Pav12/17/2022
    public static void Display(char[,] grid, bool showAll)

    {
        char[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
        WriteLine("  1 2 3 4 5 6 7 8");
        WriteLine("  ________________");
        for (int i = 0; i < 8; i++)
        {
            Write($"{letters[i]}|");
            for (int j = 0; j < 8; j++)
            {
                char marker = grid[i, j];
                if (showAll)
                {
                    Write($"{marker} ");
                }
                else
                {
                    Write($"- ");
                }
            }
            Write("|");
            WriteLine();
        }
        WriteLine("  -----------------");
    }
}
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy