Click here to Skip to main content
15,567,221 members
Home / Discussions / C#
   

C#

 
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
mvaOriginalGriff25-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
mvaOriginalGriff29-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
mvaOriginalGriff4-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 
GameBoard.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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;
        }

        //Random rand = new Random();

        //Point pop = new Point(rand.Next(0, 10), rand.Next(0, 10));

        //Orientation orient = Enum.GetValues(typeof(Orientation))
        //                    .OfType<Orientation>()
        //                    .ElementAt(rand.Next(0, ));
        public Ship PlaceShip(Ship ship, Point loc, Orientation wayUp)
        {
            Console.WriteLine(ship.Name);
            Console.WriteLine(wayUp);
            //foreach (int point in loc)
            //{
            //    TheBoard[i, ship[i]] = Square.Ship;

            //}
            Console.WriteLine(loc);

            return ship;
        }

        //Ship s = PlaceShip(new Battleship, loc, orient);
     
        //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;
        //}     
        //Ship s = PlaceShip(new Battleship, loc, orient);

    }  
    public enum Orientation
    {
        Up = 1,
        Down = 2,
        Left = 4,
        Right = 8,
    }
    public struct Point
    {
        public int x;
        public int y;

        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
    public abstract class Ship
    {
        public abstract string Name { get; }
        public abstract int Length { get; }
    }
    public class Destroyer : Ship
    {
        public override string Name { get => "Destroyer"; }
        public override int Length { get => 2; }
    }
    public class Submarine : Ship
    {
        public override string Name { get => "Submarine"; }
        public override int Length { get => 3; }
    }
    public class Cruiser : Ship
    {
        public override string Name { get => "Cruiser"; }
        public override int Length { get => 3; }
    }
    public class Battleship : Ship
    {
        public override string Name { get => "Battleship"; }
        public override int Length { get => 4; }
    }
    public class Carrier : Ship
    {
        public override string Name { get => "Carrier"; }
        public override int Length { get => 5; }
    }
}


It has gotten pretty messy and I am feeling a little frustrated Frown | :(

Any suggestions on what I can do to clean it up?

This must be one of the longest threads ever
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
mvaOriginalGriff25-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 
AnswerRe: Changing Windows registry with C# Pin
Dave Kreskowiak31-Aug-22 15:37
mveDave Kreskowiak31-Aug-22 15:37 
GeneralRe: Changing Windows registry with C# Pin
Ismael_19993-Sep-22 5:18
Ismael_19993-Sep-22 5:18 
GeneralRe: Changing Windows registry with C# Pin
Dave Kreskowiak3-Sep-22 10:51
mveDave Kreskowiak3-Sep-22 10:51 
AnswerRe: Changing Windows registry with C# Pin
OriginalGriff31-Aug-22 19:51
mvaOriginalGriff31-Aug-22 19:51 
GeneralRe: Changing Windows registry with C# Pin
Ismael_19993-Sep-22 5:20
Ismael_19993-Sep-22 5:20 
GeneralRe: Changing Windows registry with C# Pin
OriginalGriff3-Sep-22 5:43
mvaOriginalGriff3-Sep-22 5:43 
GeneralRe: Changing Windows registry with C# Pin
Ismael_19994-Sep-22 12:41
Ismael_19994-Sep-22 12:41 
GeneralRe: Changing Windows registry with C# Pin
Dave Kreskowiak4-Sep-22 14:12
mveDave Kreskowiak4-Sep-22 14:12 
GeneralRe: Changing Windows registry with C# Pin
OriginalGriff4-Sep-22 20:19
mvaOriginalGriff4-Sep-22 20:19 
GeneralRe: Changing Windows registry with C# Pin
Dave Kreskowiak5-Sep-22 4:43
mveDave Kreskowiak5-Sep-22 4:43 
GeneralRe: Changing Windows registry with C# Pin
Ismael_19996-Sep-22 5:10
Ismael_19996-Sep-22 5:10 

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.