Click here to Skip to main content
15,885,546 members
Home / Discussions / C#
   

C#

 
GeneralRe: reading memory stream in byte chunks Pin
harold aptroot10-Sep-22 9:49
harold aptroot10-Sep-22 9:49 
GeneralRe: reading memory stream in byte chunks Pin
mjeeves10-Sep-22 10:04
mjeeves10-Sep-22 10:04 
GeneralRe: reading memory stream in byte chunks Pin
harold aptroot10-Sep-22 10:15
harold aptroot10-Sep-22 10:15 
QuestionC# Service: Error 1053: The service did not respond to the start or control request in a timely fashion Pin
temuco8-Sep-22 20:41
professionaltemuco8-Sep-22 20:41 
AnswerRe: C# Service: Error 1053: The service did not respond to the start or control request in a timely fashion Pin
Richard Deeming8-Sep-22 21:49
mveRichard Deeming8-Sep-22 21:49 
GeneralRe: C# Service: Error 1053: The service did not respond to the start or control request in a timely fashion Pin
temuco8-Sep-22 22:34
professionaltemuco8-Sep-22 22:34 
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 1:38
professionaltemuco9-Sep-22 1:38 
QuestionC# Battleship gameboard & random assignment Pin
Otto_W5-Sep-22 17:50
Otto_W5-Sep-22 17:50 
Repo: https://github.com/Charles-CarM/Battleship


I am currently building my battleship console application in C#. I have prior experience
building projects with Vanilla JS and in React. I went through a tutorial on C# and could
grasp most of the concepts pretty well. I have jumped into project building because I know
this is where the maximum amount of growth will be as a developer. With that being said I
will lay out the issues arising in my application.

requirements
    *  the ship has a length of 5 
    * ship will be randomly assigned on a 10X10 grid
    * when the ship is hit five times it is sunk and game is over


I am having trouble choosing the best way to implement the 10x10 gameboard grid. With
some searching on the web I have came across the DataTable class, which allows for rows
and columns to be set in a table(I believe this to be the better approach), and I have attempted
to get them displayed in the Console below. Also I know that I can create a 10x10 grid with
a 2D array using int[,] grid = [10,10]; however I have realized that I can't assign the random values for the ship with an already populated array. I would like to get some feedback on how
I can move forward with creation of the gameboard, and random assignment of the ship on the board.

I know this post and my code are a little messy so please excuse me on that part. Any solutions or suggestions you recommend for me moving forward are welcomed and greatly appreciated. Hope you are very well wherever you may be.


using System.Data;


namespace Battleship
{
    class Program
    {
        static void Main(string[] args)
        {
            
            GamePlay ship = new GamePlay("ten", "fast ship", 350);
            Console.ReadLine();

            GreetUser();

            //generate grid

            DataTable gameBoard  = new DataTable("Battleship");
                DataColumn columnA = new DataColumn("A");
                DataColumn columnB = new DataColumn("B");
                DataColumn columnC = new DataColumn("C");
                DataColumn columnD = new DataColumn("D");
                DataColumn columnE = new DataColumn("E");
                DataColumn columnF = new DataColumn("F");
                DataColumn columnG = new DataColumn("G");
                DataColumn columnH = new DataColumn("H");
                DataColumn columnI = new DataColumn("I");
                DataColumn columnJ = new DataColumn("J");
                    gameBoard.Columns.Add(columnA);
                    gameBoard.Columns.Add(columnB); 
                    gameBoard.Columns.Add(columnC);
                    gameBoard.Columns.Add(columnD);
                    gameBoard.Columns.Add(columnE);
                    gameBoard.Columns.Add(columnF);
                    gameBoard.Columns.Add(columnG);
                    gameBoard.Columns.Add(columnH);
                    gameBoard.Columns.Add(columnI);
                    gameBoard.Columns.Add(columnJ);

                DataRow row1 = gameBoard.NewRow();
                DataRow row2 = gameBoard.NewRow();
                DataRow row3 = gameBoard.NewRow();
                DataRow row4 = gameBoard.NewRow();
                DataRow row5 = gameBoard.NewRow();
                DataRow row6 = gameBoard.NewRow();
                DataRow row7 = gameBoard.NewRow();
                DataRow row8 = gameBoard.NewRow();
                DataRow row9 = gameBoard.NewRow();
                DataRow row10 = gameBoard.NewRow();
            row1["A"] = 0;  
            row1["B"] = 0;
            row1["C"] = 0;
            row1["D"] = 0;
            row1["E"] = 0;
            row1["F"] = 0;
            row1["G"] = 0;
            row1["H"] = 0;
            row1["I"] = 0;
            row1["J"] = 0;

            gameBoard.Rows.Add(row1);
                    gameBoard.Rows.Add(row2);
                    gameBoard.Rows.Add(row3);
                    gameBoard.Rows.Add(row4);
                    gameBoard.Rows.Add(row5);
                    gameBoard.Rows.Add(row6);
                    gameBoard.Rows.Add(row7);
                    gameBoard.Rows.Add(row8);
                    gameBoard.Rows.Add(row9);
                    gameBoard.Rows.Add(row10);

           for(int j = 0; j < gameBoard.Rows.Count; j++)
            {
                for (int i = 0; i < gameBoard.Columns.Count; i++)
                {
                    Console.WriteLine(gameBoard.Columns[i].ColumnName + " ");
                    Console.WriteLine(gameBoard.Rows[j].ItemArray[i]);
                }
            }

            Console.ReadLine();

            string coords;

            int[,] numberGrid =
               {
                { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
                { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 },
                { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 },
                { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 },
                { 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 },
                { 51, 52, 53, 54, 55, 56, 57, 58, 59, 60 },
                { 61, 62, 63, 64, 65, 66, 67, 68, 69, 70 },
                { 71, 72, 73, 74, 75, 76, 77, 78, 79, 80 },
                { 81, 82, 83, 84, 85, 86, 87, 88, 89, 90 },
                { 91, 92, 93, 94, 95, 96, 97, 98, 99, 100 }
            };

            int[,] freshGrid = new int[10, 10];
            for(int j = 0; j < freshGrid.Length; j++)
            {    
                coords = Convert.ToString(j);
                Console.Write(coords);

            }
            Console.ReadLine();

            Console.WriteLine(freshGrid[3,4]);
            Console.ReadLine();

            //generate ship
            Random random = new Random();
            int newShip = random.Next();
            
            Console.ReadLine();

            //place ship on grid


            //User input and gameplay logic
            Console.WriteLine("\nEnter guess: ");
            int guess = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine($"\nYou guessed: {guess}");
            //Console.WriteLine(isHit.HitOrMiss());
            Console.ReadLine();        
        }

        static void GreetUser()
        {
            Console.WriteLine("Welcome to Battle! press Enter ");
            Console.ReadLine();

            Console.WriteLine("Enter username: ");
            string username = Console.ReadLine();
            Console.WriteLine($"\nLet's begin {username}!");
        }

        //bool isHit = false;
        //public static string HitOrMiss()
        //{
        //    if (isHit)
        //    {
        //        Console.WriteLine("That's a hit!");
        //    }
        //    else
        //    {
        //        Console.WriteLine("You missed");
        //    }
        //}
    }
}

AnswerRe: C# Battleship gameboard & random assignment Pin
OriginalGriff5-Sep-22 19:11
mveOriginalGriff5-Sep-22 19:11 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W6-Sep-22 15:51
Otto_W6-Sep-22 15:51 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff6-Sep-22 19:19
mveOriginalGriff6-Sep-22 19:19 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W16-Sep-22 21:54
Otto_W16-Sep-22 21:54 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff16-Sep-22 22:13
mveOriginalGriff16-Sep-22 22:13 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W24-Sep-22 1:09
Otto_W24-Sep-22 1:09 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff24-Sep-22 1:34
mveOriginalGriff24-Sep-22 1:34 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W24-Sep-22 2:45
Otto_W24-Sep-22 2:45 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff24-Sep-22 3:16
mveOriginalGriff24-Sep-22 3:16 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W25-Sep-22 21:20
Otto_W25-Sep-22 21:20 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff25-Sep-22 22:01
mveOriginalGriff25-Sep-22 22:01 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W29-Sep-22 16:23
Otto_W29-Sep-22 16:23 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff29-Sep-22 19:10
mveOriginalGriff29-Sep-22 19:10 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W4-Oct-22 0:29
Otto_W4-Oct-22 0:29 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff4-Oct-22 1:09
mveOriginalGriff4-Oct-22 1:09 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W13-Oct-22 17:45
Otto_W13-Oct-22 17:45 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W13-Oct-22 17:46
Otto_W13-Oct-22 17:46 

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.