Click here to Skip to main content
15,511,837 members
Home / Discussions / C#
   

C#

 
General[Solved] Re: C# Service: Error 1053: The service did not respond to the start or control request in a timely fashion Pin
temuco9-Sep-22 2:38
professionaltemuco9-Sep-22 2:38 
QuestionC# Battleship gameboard & random assignment Pin
Otto_W5-Sep-22 18:50
Otto_W5-Sep-22 18:50 
AnswerRe: C# Battleship gameboard & random assignment Pin
OriginalGriff5-Sep-22 20:11
mveOriginalGriff5-Sep-22 20:11 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W6-Sep-22 16:51
Otto_W6-Sep-22 16:51 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff6-Sep-22 20:19
mveOriginalGriff6-Sep-22 20:19 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W16-Sep-22 22:54
Otto_W16-Sep-22 22:54 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff16-Sep-22 23:13
mveOriginalGriff16-Sep-22 23:13 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W24-Sep-22 2:09
Otto_W24-Sep-22 2:09 
Hello there!

Yeah that slipped my mind that there are four representations of the board in the physical version. I definitely realize it's better to have two boards instead of just one. (original assigned spec for this project was a single player version with one ship of five values). I've decided to move forward with the dual ship design like you suggested (it makes more sense).

I had tried to declare the method outside of the Main, but I wasn't able to access TheBoard

So I have created a GameBoard class, and then created TheBoard

C#
namespace ConsoleBattle
{
    public class Program
    {
        public static void Main(string[] args)
        {
            GameBoard playerA = new GameBoard();
            GameBoard playerB = new GameBoard();
            GameBoard playerAGuesses = new GameBoard();
            GameBoard playerBGuesses = new GameBoard();
            bool sunk = false;
            string message;

            Welcome();
            Console.WriteLine("X: 0 - 9");
            Console.WriteLine("Y: 0 - 9");

            while (true)
            {
                Console.WriteLine(playerA.AddShip());
                Console.ReadLine();
                //playerB.AddShip();
                while(!sunk)
                {

                try
                {
                    Console.WriteLine("Enter your guess: X, Y");
                    string guess = Console.ReadLine();
                    int xPos = Convert.ToInt32(guess.Split(',')[0]) - 1;
                    int yPos = Convert.ToInt32(guess.Split(',')[1]) - 1;

                    if (xPos > 9 || yPos > 9)
                    {
                        Console.WriteLine(message = "You are off the board, try again!");
                    }
                    playerA.CheckGuess(xPos, yPos);                   
                }
                catch
                {
                    Console.WriteLine(message = "Unable to process coordinates");
                }
                Console.ReadLine();
                //finished = true;
                }

                Console.WriteLine(message = "You sunk the ship!");
                Console.WriteLine("Play Again? [Y or N]");
                string answer = Console.ReadLine().ToUpper();

                if (answer == "Y")
                {
                    continue;
                }
                else if (answer == "N")
                {
                    return;
                }
                else
                {
                    return;
                }
            }   
        }
        private static void Welcome()
        {
            Console.WriteLine("Welcome to Console Battle!");
            //Console.ReadLine();
            Console.WriteLine("Enter username: ");
            string username = Console.ReadLine();
            Console.WriteLine($"\nLet's begin {username} press Enter!");
            Console.ReadLine();
        }

        //static void Bomb()
        //{
        //
        //}

    }
}


When I call my playerA.CheckGuess(xPos, yPos) it is working as intended
in regards to getting the correct value or message back in the console.

I am able to enter my guess iteratively(which I was struggling with)

I have came up with a quasi solution for the ship assignment, but it only
seems to assign one ship value on the board when I wish to have five. Any
suggestions on how I can get five ships assigned?

Cheers for all the help so far,

C#
namespace ConsoleBattle
{
    public class GameBoard
    {
        public enum Square
        {
            Water,
            Miss,
            Ship,
            Hit,
        }
        private Square[,] TheBoard = new Square[10, 10];
        public Square[,] CheckGuess(int x, int y)
        {
            if (TheBoard[x, y] == Square.Ship)
            {
                Console.WriteLine(TheBoard[x, y] = Square.Hit);
            }
            else if (TheBoard[x, y] == Square.Water)
            {
                Console.WriteLine(TheBoard[x, y] = Square.Miss);
            }
            else if (TheBoard[x, y] == Square.Miss)
            {
                string message1 = "you already missed here";
                Console.WriteLine(message1);
                TheBoard[x, y] = Square.Miss;
            }
            else
            {
                string message2 = "this spot was hit already";
                Console.WriteLine(message2);
                TheBoard[x, y] = Square.Hit;
            }
            return TheBoard;
        }

        public Square[,] AddShip()
        {
            Random random = new();

            string direction;

            int x = random.Next(0, 9);
            int y = random.Next(0, 9);

            if (x < 4)
            {
                direction = "right";
            }
            else if (x > 4)
            {
                direction = "left";
            }
            else
            {
                direction = "left,right";
            }

            if (y < 4)
            {
                direction += "down";
            }
            else if (y > 4)
            {
                direction += "up";
            }
            else
            {
                direction += "up,down";
            }

            direction = direction.Split(',')[random.Next(0, direction.Split(',').Length)];

            if (direction == "up")
            {
                for (int i = y; i < y - 4; i--)
                {
                    TheBoard[i, y] = Square.Ship;
                }
            }
            else if (direction == "down")
            {
                for (int i = y; i < y + 4; i++)
                {
                    TheBoard[i, y] = Square.Ship;
                }
            }
            else if (direction == "left")
            {
                for (int i = x; i < x - 4; i--)
                {
                    TheBoard[x, i] = Square.Ship;
                }
            }
            else
            {
                for (int i = x; i < x + 4; i++)
                {
                    TheBoard[x, i] = Square.Ship;
                }
            }
            return TheBoard;
        }
    }
}

GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff24-Sep-22 2:34
mveOriginalGriff24-Sep-22 2:34 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W24-Sep-22 3:45
Otto_W24-Sep-22 3:45 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff24-Sep-22 4:16
mveOriginalGriff24-Sep-22 4:16 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W25-Sep-22 22:20
Otto_W25-Sep-22 22:20 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff25-Sep-22 23:01
mveOriginalGriff25-Sep-22 23:01 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W29-Sep-22 17:23
Otto_W29-Sep-22 17:23 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff29-Sep-22 20:10
mveOriginalGriff29-Sep-22 20:10 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W4-Oct-22 1:29
Otto_W4-Oct-22 1:29 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff4-Oct-22 2:09
mveOriginalGriff4-Oct-22 2:09 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W13-Oct-22 18:45
Otto_W13-Oct-22 18:45 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W13-Oct-22 18:46
Otto_W13-Oct-22 18:46 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W25-Oct-22 18:16
Otto_W25-Oct-22 18:16 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff25-Oct-22 20:34
mveOriginalGriff25-Oct-22 20:34 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W30-Oct-22 20:30
Otto_W30-Oct-22 20:30 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W15-Nov-22 15:46
Otto_W15-Nov-22 15:46 
AnswerRe: C# Battleship gameboard & random assignment Pin
Gerry Schmitz6-Sep-22 6:06
mveGerry Schmitz6-Sep-22 6:06 
QuestionChanging Windows registry with C# Pin
Ismael_199931-Aug-22 13:50
Ismael_199931-Aug-22 13:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.