Exemple #1
0
 static void printBoards(CompetitorBoard competitorBoard, PlayerBoard playerBoard)
 {
     Console.Clear();
     Console.WriteLine("Enemy board:");
     Console.WriteLine(competitorBoard);
     Console.WriteLine("\nYour board");
     Console.WriteLine(playerBoard);
 }
Exemple #2
0
        static CompetitorBoard makeBoard()
        {
            CompetitorBoard     gameBoard    = new CompetitorBoard();
            List <List <Ship> > shipLists    = new List <List <Ship> >();
            List <Ship>         shipsLayout0 = new List <Ship>();
            List <Ship>         shipsLayout1 = new List <Ship>();
            List <Ship>         shipsLayout2 = new List <Ship>();
            List <Ship>         shipsLayout3 = new List <Ship>();


            shipsLayout0.Add(new Ship(7, 10, 8, 10));
            shipsLayout0.Add(new Ship(1, 1, 1, 4));
            shipsLayout0.Add(new Ship(10, 3, 10, 5));
            shipsLayout0.Add(new Ship(5, 1, 5, 3));
            shipsLayout0.Add(new Ship(4, 5, 8, 5));

            shipsLayout1.Add(new Ship(2, 1, 3, 1));
            shipsLayout1.Add(new Ship(2, 3, 6, 3));
            shipsLayout1.Add(new Ship(9, 4, 9, 6));
            shipsLayout1.Add(new Ship(3, 5, 3, 6));
            shipsLayout1.Add(new Ship(5, 6, 5, 9));

            shipsLayout2.Add(new Ship(2, 1, 2, 3));
            shipsLayout2.Add(new Ship(9, 1, 9, 2));
            shipsLayout2.Add(new Ship(6, 4, 6, 8));
            shipsLayout2.Add(new Ship(4, 8, 8, 8));
            shipsLayout2.Add(new Ship(6, 10, 8, 10));

            shipsLayout3.Add(new Ship(9, 1, 9, 2));
            shipsLayout3.Add(new Ship(10, 1, 10, 3));
            shipsLayout3.Add(new Ship(6, 2, 6, 6));
            shipsLayout3.Add(new Ship(4, 7, 7, 7));
            shipsLayout3.Add(new Ship(7, 9, 9, 9));

            shipLists.Add(shipsLayout0);
            shipLists.Add(shipsLayout1);
            shipLists.Add(shipsLayout2);
            shipLists.Add(shipsLayout3);

            foreach (Ship ship in shipLists[new Random().Next(shipLists.Count)])
            {
                AddShip(gameBoard, ship);
            }

            return(gameBoard);
        }
Exemple #3
0
        //static void enemyCompAttack(CompetitorBoard competitorBoard, PlayerBoard playerBoard)
        //{

        //    int[] coords = new int[2];
        //    coords[0] = competitorBoard.LastHitCoords[0];
        //    coords[1] = competitorBoard.LastHitCoords[1];

        //    if (competitorBoard.HitStreak > 1)
        //    {
        //        if (competitorBoard.LastAttackCoords[0] - competitorBoard.savedHit[0] > 0)
        //        {
        //            if (competitorBoard.LastAttackCoords[0] > competitorBoard.savedHit[0])
        //            {
        //                coords[0] = competitorBoard.LastAttackCoords[0]++;
        //                coords[1] = competitorBoard.LastAttackCoords[1];
        //            }
        //            else
        //            {
        //                coords[0] = competitorBoard.savedHit[0]++;
        //                coords[1] = competitorBoard.savedHit[1];
        //            }
        //        }
        //        else if (competitorBoard.LastAttackCoords[1] - competitorBoard.savedHit[1] > 0)
        //        {
        //            if (competitorBoard.LastAttackCoords[0] > competitorBoard.savedHit[0])
        //            {
        //                coords[0] = competitorBoard.LastAttackCoords[0];
        //                coords[1] = competitorBoard.LastAttackCoords[1]++;
        //            }
        //            else
        //            {
        //                coords[0] = competitorBoard.savedHit[0];
        //                coords[1] = competitorBoard.savedHit[1]++;
        //            }
        //        }
        //        else if (competitorBoard.LastAttackCoords[0] - competitorBoard.savedHit[0] < 0)
        //        {
        //            if (competitorBoard.LastAttackCoords[0] > competitorBoard.savedHit[0])
        //            {
        //                coords[0] = competitorBoard.LastAttackCoords[0]--;
        //                coords[1] = competitorBoard.LastAttackCoords[1];
        //            }
        //            else
        //            {
        //                coords[0] = competitorBoard.savedHit[0]--;
        //                coords[1] = competitorBoard.savedHit[1];
        //            }
        //        }
        //        else if (competitorBoard.LastAttackCoords[1] - competitorBoard.savedHit[1] < 0)
        //        {
        //            if (competitorBoard.LastAttackCoords[0] > competitorBoard.savedHit[0])
        //            {
        //                coords[0] = competitorBoard.LastAttackCoords[0];
        //                coords[1] = competitorBoard.LastAttackCoords[1]--;
        //            }
        //            else
        //            {
        //                coords[0] = competitorBoard.savedHit[0];
        //                coords[1] = competitorBoard.savedHit[1]--;
        //            }
        //        }

        //    }
        //    else if (competitorBoard.IsShipHit)
        //    {
        //        int[] stored1 = new int[2];
        //        int[] stored2 = new int[2];
        //        int[] stored3 = new int[2];
        //        int[] stored4 = new int[2];
        //        competitorBoard.StoredCoords = new List<int[]>();
        //        if (competitorBoard.LastHitCoords[0] < 10)
        //        {
        //            stored1[0] = coords[0] + 1;
        //            stored1[1] = coords[1];
        //            competitorBoard.StoredCoords.Add(stored1);
        //        }
        //        if (competitorBoard.LastHitCoords[0] > 1)
        //        {
        //            stored2[0] = coords[0] - 1;
        //            stored2[1] = coords[1];
        //            competitorBoard.StoredCoords.Add(stored2);
        //        }

        //        if (competitorBoard.LastHitCoords[1] < 10)
        //        {
        //            stored3[0] = coords[0];
        //            stored3[1] = coords[1] + 1;
        //            competitorBoard.StoredCoords.Add(stored3);
        //        }

        //        if (competitorBoard.LastHitCoords[1] > 1)
        //        {
        //            stored4[0] = coords[0];
        //            stored4[1] = coords[1] - 1;
        //            competitorBoard.StoredCoords.Add(stored4);
        //        }


        //        coords = competitorBoard.StoredCoords[new Random().Next(0, competitorBoard.StoredCoords.Count)];
        //    }
        //    else if ((!competitorBoard.IsShipHit) && (competitorBoard.StoredCoords != null))
        //    {
        //        for (int i = 0; i < competitorBoard.StoredCoords.Count; i++)
        //        {
        //            if (competitorBoard.StoredCoords[i] == competitorBoard.LastAttackCoords)
        //            {
        //                competitorBoard.StoredCoords.RemoveAt(i);
        //            }
        //        }
        //        if (competitorBoard.StoredCoords.Count != 0)
        //        {
        //            coords = competitorBoard.StoredCoords[new Random().Next(0, competitorBoard.StoredCoords.Count)];
        //        }
        //        else
        //        {
        //            competitorBoard.StoredCoords = null;
        //            coords[0] = new Random().Next(1, 11);
        //            System.Threading.Thread.Sleep(35);
        //            coords[1] = new Random().Next(1, 11);
        //        }
        //    }

        //    else
        //    {
        //        competitorBoard.StoredCoords = null;
        //        coords[0] = new Random().Next(1, 11);
        //        System.Threading.Thread.Sleep(35);
        //        coords[1] = new Random().Next(1, 11);
        //    }

        //    if (playerBoard.rows[coords[1]][coords[0]] == playerBoard.Ship)
        //    {

        //        playerBoard.rows[coords[1]][coords[0]] = playerBoard.Hit;
        //        competitorBoard.HitStreak++;
        //        competitorBoard.IsShipHit = true;
        //        competitorBoard.LastHitCoords = coords;
        //        if (competitorBoard.HitStreak < 2)
        //        {
        //            competitorBoard.savedHit = coords;
        //        }
        //    }

        //    else
        //    {
        //        if (playerBoard.rows[coords[1]][coords[0]] == playerBoard.Hit ||
        //            playerBoard.rows[coords[1]][coords[0]] == playerBoard.Miss)
        //        {
        //            coords[0] = new Random().Next(1, 11);
        //            System.Threading.Thread.Sleep(35);
        //            coords[1] = new Random().Next(1, 11);
        //            competitorBoard.HitStreak = 0;

        //            if (playerBoard.rows[coords[1]][coords[0]] == playerBoard.Ship)
        //            {

        //                playerBoard.rows[coords[1]][coords[0]] = playerBoard.Hit;
        //                competitorBoard.HitStreak++;
        //                competitorBoard.IsShipHit = true;
        //                competitorBoard.LastHitCoords = coords;
        //                if (competitorBoard.HitStreak < 2)
        //                {
        //                    competitorBoard.savedHit = coords;
        //                }
        //            }

        //            else
        //            {
        //                if (playerBoard.rows[coords[1]][coords[0]] == playerBoard.Hit ||
        //                    playerBoard.rows[coords[1]][coords[0]] == playerBoard.Miss)
        //                {
        //                    coords[0] = new Random().Next(1, 11);
        //                    System.Threading.Thread.Sleep(35);
        //                    coords[1] = new Random().Next(1, 11);
        //                    competitorBoard.HitStreak = 0;


        //                }
        //                else
        //                {
        //                    playerBoard.rows[coords[1]][coords[0]] = playerBoard.Miss;
        //                    competitorBoard.IsShipHit = false;
        //                    competitorBoard.HitStreak = 0;
        //                    competitorBoard.LastAttackCoords = coords;
        //                }
        //            }

        //        }
        //        else
        //        {
        //            playerBoard.rows[coords[1]][coords[0]] = playerBoard.Miss;
        //            competitorBoard.IsShipHit = false;
        //            competitorBoard.HitStreak = 0;
        //            competitorBoard.LastAttackCoords = coords;
        //        }
        //    }

        //}

        static void enemyCompAttack(CompetitorBoard competitorBoard, PlayerBoard playerBoard, int difficulty)
        {
            int[] coords = new int[2];
            if (new Random().Next(101) < difficulty)
            {
                for (int i = 0; i < playerBoard.rows.Length; i++)
                {
                    for (int j = 0; j < playerBoard.rows.Length; j++)
                    {
                        if (playerBoard.rows[i][j] == playerBoard.Ship)
                        {
                            coords[0] = i;
                            coords[1] = j;
                            i        += 100;
                            j        += 100;
                        }
                    }
                }
            }
            else
            {
                coords[0] = new Random().Next(1, 11);
                System.Threading.Thread.Sleep(35);
                coords[1] = new Random().Next(1, 11);
            }

            if (playerBoard.rows[coords[0]][coords[1]] == playerBoard.Ship)
            {
                playerBoard.rows[coords[0]][coords[1]] = playerBoard.Hit;
            }
            else if (playerBoard.rows[coords[0]][coords[1]] == playerBoard.Hit || playerBoard.rows[coords[0]][coords[1]] == playerBoard.Miss)
            {
                enemyCompAttack(competitorBoard, playerBoard, difficulty);
            }
            else
            {
                playerBoard.rows[coords[0]][coords[1]] = playerBoard.Miss;
            }
        }
Exemple #4
0
        static void Main(string[] args)
        {
            Random rand = new Random();

            CompetitorBoard gameBoard       = new CompetitorBoard();
            Ship            playerDestroyer = new Ship(7, 10, 8, 10);
            //Ship playerBattleship = new Ship(1,1,1,4);
            //Ship playerCruiser = new Ship(10,3,10,5);
            //Ship playerSubmarine = new Ship(5, 1, 5, 3);
            //Ship playerCarrier = new Ship(4, 5, 8, 5);

            bool exit = true;

            do
            {
                Console.WriteLine("Do you want to play BattleShip? Y/N");
                ConsoleKey userChoice = Console.ReadKey(true).Key;
                bool       reload     = false;

                switch (userChoice)
                {
                case ConsoleKey.Y:
                    Console.WriteLine("Welcome to BattleShip!");
                    //TODO Build BAttle()
                    //Console.WriteLine("Enter letter from A-J: ");
                    //ConsoleKey userLetter = Console.ReadKey(true).Key;
                    //Console.WriteLine("Enter a number from 1-0 (0 = 10): ");
                    //ConsoleKey userNumber = Console.ReadKey(true).Key;
                    //switch (userLetter & userNumber)
                    //{


                    //    default:
                    //        Console.WriteLine("That is not a valid option. Enter another value.");
                    //        break;
                    //}
                    //AddShip(gameBoard, playerCarrier);
                    //AddShip(gameBoard, playerBattleship);
                    //AddShip(gameBoard, playerCruiser);
                    //AddShip(gameBoard, playerSubmarine);
                    AddShip(gameBoard, playerDestroyer);
                    while (!reload)
                    {
                        Console.Clear();
                        Console.WriteLine(gameBoard);

                        //Console.WriteLine("Enter first coord: (A2)");
                        //Ship newship = new Ship(getCoord(), getCoord());
                        //AddShip(gameBoard,newship);
                        //Console.WriteLine(gameBoard);
                        Console.WriteLine("Enter coords to attack: (A2)");
                        Attack(gameBoard);
                        if (WinCheck(gameBoard))
                        {
                            Console.WriteLine("You Win!");
                            Console.ReadLine();
                        }
                        exit = !WinCheck(gameBoard);
                    }
                    exit = reload;
                    break;

                case ConsoleKey.N:
                    Console.WriteLine("Good Bye");
                    exit = true;
                    break;

                default:
                    Console.WriteLine("Invalid option, please choose again");
                    break;
                }//end switch
            } while (exit);
        }
Exemple #5
0
        static void Main(string[] args)
        {
            int             easy             = 15;
            int             medium           = 25;
            int             hard             = 35;
            CompetitorBoard gameBoard        = makeBoard();
            PlayerBoard     playerBoard      = new PlayerBoard();
            Ship            playerDestroyer  = new Ship(7, 10, 8, 10);
            Ship            playerBattleship = new Ship(1, 1, 1, 4);
            Ship            playerCruiser    = new Ship(10, 3, 10, 5);
            Ship            playerSubmarine  = new Ship(5, 1, 5, 3);
            Ship            playerCarrier    = new Ship(4, 5, 8, 5);
            bool            exit             = false;

            while (!exit)
            {
                Console.WriteLine("Do you want to play BattleShip? Y/N");
                ConsoleKey userChoice = Console.ReadKey(true).Key;
                bool       reload     = false;

                switch (userChoice)
                {
                case ConsoleKey.Y:
                    Console.WriteLine("Welcome to BattleShip!");
                    break;

                case ConsoleKey.N:
                    Console.WriteLine("Good Bye");
                    exit = true;
                    break;

                default:
                    Console.WriteLine("Invalid option, please choose again");
                    break;
                }//end switch

                AddShip(playerBoard, playerCarrier);
                AddShip(playerBoard, playerBattleship);
                AddShip(playerBoard, playerCruiser);
                AddShip(playerBoard, playerSubmarine);
                AddShip(playerBoard, playerDestroyer);

                while (!reload && !exit)
                {
                    printBoards(gameBoard, playerBoard);

                    Attack(gameBoard);
                    if (WinCheck(gameBoard))
                    {
                        printBoards(gameBoard, playerBoard);
                        Console.WriteLine("You Win!");
                        Console.ReadLine();
                        exit = true;
                    }
                    else
                    {
                        enemyCompAttack(gameBoard, playerBoard, easy);
                        if (WinCheck(playerBoard))
                        {
                            printBoards(gameBoard, playerBoard);
                            Console.WriteLine("You Lose!");
                            Console.ReadLine();
                            exit = true;
                        }
                    }
                }
            }
        }