Inspiration

I like battleship

What it does

simulates a potential game of battleship based on user inputs.

How I built it

With coding

Challenges I ran into

coding the whole thing in time

Accomplishments that I'm proud of

that it works

What I learned

coding is fun

What's next for Battleship

Code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO;

namespace Consoleapp1 { class Program1 { static void Main3(string[] args) { //Prints 10 times int i; for (i = 0; i < 10; i++) { Console.WriteLine("Harini"); } Console.ReadKey(); int a = 0; while (a < 10) { Console.WriteLine("Hellow"); a += 2; } //Prints Backward //int i; //Console.WriteLine("Enter a Word: "); //string userinput = Console.ReadLine(); //int a = userinput.Length; //for (i = a - 1; i >= 0; i--) //{ // Console.Write(userinput[i]); //} //Console.ReadLine(); // Go to http://aka.ms/dotnet-get-started-console to continue learning how to build a console app! } } class ArrayProgram { static void Main2(string[] args) { int[,] array1 = new int[3, 2] { { 2, 3 }, { 3, 4 }, { 4, 5 } }; int i; int j; for (i = 0; i < 3; i++) { for (j = 0; j < 2; j++) { Console.Write(array1[i, j]); } Console.WriteLine(); }

    }
}
class FileIOProgram
{
    static void Main4(string[] args)
    {
        FileStream n = new FileStream("C:\\Users\\harin\\Documents\\harini.txt", FileMode.OpenOrCreate);
        //SOLVE DIS
    }
}

class Battleship
{
    static void Main(string[] args)
    { 
        //Make Starting Board
        int r = 0;
        int c = 0;
        int row = 0;
        int col = 0;
        int[,] board = new int[8, 8];
        int i;
        int j;
        printboard();

        placeboat1();
        printboard();
        placeboat2();
        printboard();
        while (gameinplay() == true) 
        {
            strike();
            printboard();
        }
       //clearboard();

        int oldrow;
        int oldcol;
        void strike()
        {
            bool invalidR = true;
            bool invalidC = true;
            bool invalidSpot = true;
            System.ConsoleKeyInfo kp;

            while (invalidSpot == true)
            {

                while (invalidR == true)
                {
                    Console.WriteLine("Choose row to strike ");
                    kp = Console.ReadKey();
                    Console.WriteLine();
                    row = int.Parse(kp.KeyChar.ToString());

                    if (checkrows(row) == true)
                    {
                        invalidR = false;
                    }
                    else
                    {
                        Console.WriteLine("Enter a Number in range");
                        invalidR = true;
                    }
                }
                while (invalidC == true)
                {
                    Console.WriteLine("Choose column to strike ");
                    kp = Console.ReadKey();
                    Console.WriteLine();
                    col = int.Parse(kp.KeyChar.ToString());

                    if (checkcols(col, 8) == true)
                    {
                        invalidC = false;
                    }
                    else
                    {
                        Console.WriteLine("Enter a column in range");
                        invalidC = true;
                    }
                }
                if (board[row, col] == 0)
                {
                    board[row, col] = 1;
                    Console.WriteLine("Miss");
                    invalidSpot = false;
                }
                else if (board[row, col] == 2 || board[row, col] == 3)
                {
                    board[row, col] = 4;
                    Console.WriteLine("Hit");
                    boatsunkandgameover();
                    // if boatsunk, check if gameover, if gameover clearboard() + "game over"
                    //else continue; else continue; else continue
                    invalidSpot = false;
                }
                else
                {
                    Console.WriteLine("You already hit that spot. Try again");
                    invalidSpot = true;
                    break;
                }
            }
        }

        void boatsunkandgameover()
        {
            List<int> lsd = board.Cast<int>().ToList();
            //checking if hit or not
            if (board[row, col] == 4)
            {
                //checking if board conatains 2 and 3
                if (lsd.Contains(2) && lsd.Contains(3))
                {
                }
                else
                {
                    Console.WriteLine("You have sunk 1 boat! keep going!");
                    //check if gameover
                    if (gameinplay() == false)
                    {
                        Console.WriteLine("You have sunk both boats");
                        Console.WriteLine("GAME OVER");
                    }
                }
            }
        }
        bool gameinplay()
        {
            List<int> lsd = board.Cast<int>().ToList();
            if (lsd.Contains(2) || lsd.Contains(3))
            {
                return true;
            }
            else 
            {
                return false;
            }
        }

        void clearboard()
        {
            i = 0;
            j = 0;
            for (i = 0; i < 8; i++)
        {
            for (j = 0; j < 8; j++)
            {
                board[i, j] = 0;
                Console.Write(board[i, j]);
            }
            Console.WriteLine();
        }
        }
        void printboard()
        {
            i = 0;
            j = 0;
            for (i = 0; i < 8; i++)
            {
                for (j = 0; j < 8; j++)
                {
                    Console.Write(board[i, j] + "  ");
                }
                Console.WriteLine();
            }
        }

        bool checkrows( int ro)
        {
            if (ro > -1 && ro < 8)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        bool checkcols(int co, int x)
        {
            if (co > -1 && co < x)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        bool nooverlap(int rows, int colu)
        {
            //check if rows overlap
            if (oldrow == r)
            {
                //rows overlap, now checking columns
                if (c > oldcol + 2 || c < oldcol - 3)
                {
                    //no overlap
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                //not in same row (np)
                return true;
            }

        }

        void placeboat1()
        {
            bool invalidRow = true;
            bool invalidCol = true;
            System.ConsoleKeyInfo kp;

            while (invalidRow == true)
            {
                    Console.WriteLine("Choose row for Boat 1: ");
                    kp = Console.ReadKey();
                    Console.WriteLine();
                    r = int.Parse(kp.KeyChar.ToString());

                    if (checkrows(r) == true)
                    {
                        invalidRow = false;
                    }
                    else
                    {
                        Console.WriteLine("Enter a Number in range");
                        invalidRow = true;
                    }



            }
            while (invalidCol == true)
            {
                Console.WriteLine("Choose column for Boat 1: ");
                kp = Console.ReadKey();
                Console.WriteLine();
                c = int.Parse(kp.KeyChar.ToString());

                if (checkcols(c, 7) == true)
                {
                    invalidCol = false;
                }
                else
                {
                    Console.WriteLine("Enter a column in range");
                    invalidCol = true;
                }
            }

            //everything makes sense, placing the boat
            board[r, c] = 2;
            board[r, c + 1] = 2;
            Console.WriteLine("Done placing boat 1");
            //saving copy of row and column for overlap checking (bt.2)
            oldrow = r;
            oldcol = c;
        }
        void placeboat2()
        {

            bool redoBoat = true;
            System.ConsoleKeyInfo kp;

            while (redoBoat == true)
            {
                bool invalidRow = true;
                bool invalidCol = true;

                while (invalidRow == true)
                {
                    Console.WriteLine("Choose row for Boat 2: ");
                    kp = Console.ReadKey();
                    Console.WriteLine();
                    r = int.Parse(kp.KeyChar.ToString());

                    if (checkrows(r) == true)
                    {
                        invalidRow = false;
                    }
                    else
                    {
                        Console.WriteLine("Enter a Number in range");
                        invalidRow = true;
                    }
                }
                while (invalidCol == true)
                {
                    Console.WriteLine("Choose column for Boat 2: ");
                    kp = Console.ReadKey();
                    Console.WriteLine();
                    c = int.Parse(kp.KeyChar.ToString());

                    if (checkcols(c,6) == true)
                    {
                        invalidCol = false;
                    }
                    else
                    {
                        Console.WriteLine("Enter a column in range");
                        invalidCol = true;
                    }
                }
                if (nooverlap(r, c) == false)
                {
                    redoBoat = true;
                    Console.WriteLine("redoing boat");
                }
                else
                {
                    redoBoat = false;
                }
            }

            //everything makes sense, placing the boat
            board[r, c] = 3;
            board[r, c + 1] = 3;
            board[r, c + 2] = 3;
            Console.WriteLine("Done placing boat 2");


            }


        }
    }

class TestArrayExist
{
    static void Main1(string[] args)
    {
        //int[,] array1 = new int {1, 2};
        //bool a = Array.Exists( array1, element => element.Equals(5));
        //Console.WriteLine(a);
        //Console.ReadLine();
    }
}
class GuessingNum
{
    static void Main3(string[] args)
    {
        int g;
        int num = 7;
        Console.WriteLine("Guess a number between 0 and 10");
        System.ConsoleKeyInfo kp = Console.ReadKey();
        g = int.Parse(kp.KeyChar.ToString());
        while(g != num)
        {
            if (g > num)
            {
                Console.WriteLine(" Incorrect. Guess lower. ");
                kp = Console.ReadKey();
                g = int.Parse(kp.KeyChar.ToString());
            }
            else if (g < num)
            {
                Console.WriteLine(" Incorrect. Guess higher. ");
                kp = Console.ReadKey();
                g = int.Parse(kp.KeyChar.ToString());
            }
        }
        Console.WriteLine(" Correct! The answer is "+ g );
        Console.ReadLine();
    }
}

}

Built With

Share this project:
×

Updates