示例#1
0
        static void Main(string[] args)
        {
            Battleship battle = new Battleship();

            battle.Start();

            bool won = false;

            do
            {
                Console.Write("Enter coordinate: ");
                var key = Console.ReadLine();
                if (battle.ValidateCoordinates(key))
                {
                    battle.Fight(key.ToString(), out string result);
                    if (battle.AllHaveSunk())
                    {
                        won = true;
                        Console.WriteLine("You won. All ships are sunk.");
                    }
                    else
                    {
                        Console.WriteLine(result);
                    }
                }
                else
                {
                    Console.WriteLine("You're coordinates are not ok. Please try again");
                }
            }while (won == false);

            Console.ReadKey();
        }
示例#2
0
        static void Main(string[] args)
        {
            var Prog = new Battleship();

            Prog.Setup();
            Prog.StartGame();

            Prog.StartMainLoop();
        }
示例#3
0
 //resets both playerboards and clears the list of movesTaken
 public void NewGame()
 {
     foreach (Player player in players)
     {
         player.CreateGameBoard();
         player.MovesTaken.Clear();
     }
     bships = null;
     players.Clear();
     player1 = null;
     player2 = null;
 }
示例#4
0
        public void Play()
        {
            // Wait on pairings
            Ship[] ships  = new Ship[5];
            bool   placed = false;

            for (int i = 0; i < ships.Length; i++)
            {
                placed = false;
                if (i < 2)
                {
                    ships[i] = new Minesweeper(1, 1, true, yourMap);
                }
                else if (i < 3)// Feel the magic
                {
                    ships[i] = new Frigate(1, 1, true, yourMap);
                }
                else if (i < 4)
                {
                    ships[i] = new Cruiser(1, 1, true, yourMap);
                }
                else if (i < 5)
                {
                    ships[i] = new Battleship(1, 1, true, yourMap);
                }
                ships[i].Draw();
                while (!placed)
                {
                    ConsoleKey key = (ConsoleKey)Console.ReadKey(true).Key;
                    placed = ships[i].MoveShip(key);
                }
                // Place ships
            }

            shipsPlaced = true;

            foreach (Ship s in ships)
            {
                s.Draw();
            }
            // Send map to server
            while (true)
            {
                // Place coordinate on enemy map
                // Send point bto server
                // Recieve hit
                // Recieve enemy hit
                // If enemy ships/your ships destroyed - Break
            }
        }
示例#5
0
        public void Play()
        {
            // Wait on pairings
            Ship[] ships = new Ship[5];
            bool placed = false;
            for (int i = 0; i < ships.Length; i++ )
            {
                placed = false;
                if (i < 2)
                {
                    ships[i] = new Minesweeper(1, 1, true, yourMap);
                }
                else if (i < 3)// Feel the magic
                {
                    ships[i] = new Frigate(1, 1, true, yourMap);
                }
                else if (i < 4)
                {
                    ships[i] = new Cruiser(1, 1, true, yourMap);
                }
                else if (i < 5)
                {
                    ships[i] = new Battleship(1, 1, true, yourMap);
                }
                ships[i].Draw();
                while (!placed)
                {
                    ConsoleKey key = (ConsoleKey)Console.ReadKey(true).Key;
                    placed = ships[i].MoveShip(key);
                }
                // Place ships
            }

            shipsPlaced = true;

            foreach (Ship s in ships)
            {
                s.Draw();
            }
            // Send map to server
            while (true)
            {
                // Place coordinate on enemy map
                // Send point bto server
                // Recieve hit
                // Recieve enemy hit
                // If enemy ships/your ships destroyed - Break
            }
        }
示例#6
0
        public Player()
        {
            map      = new int[10, 10];
            enemyMap = new int[10, 10];
            ships    = new Battleship[5];

            Array.Clear(map, 0, map.Length);
            Array.Clear(enemyMap, 0, enemyMap.Length);

            ships[0] = new Battleship("Aircraft Carrier", 5);
            ships[1] = new Battleship("Battleship", 4);
            ships[2] = new Battleship("Submarine", 3);
            ships[3] = new Battleship("Cruiser", 3);
            ships[4] = new Battleship("Patrol Boat", 2);
        }
示例#7
0
        public Computer()
        {
            map       = new int[10, 10];
            playerMap = new int[10, 10];
            ships     = new Battleship[5];
            hunting   = true;

            Array.Clear(map, 0, map.Length);
            Array.Clear(playerMap, 0, playerMap.Length);

            ships[0] = new Battleship("Aircraft Carrier", 5);
            ships[1] = new Battleship("Battleship", 4);
            ships[2] = new Battleship("Submarine", 3);
            ships[3] = new Battleship("Cruiser", 3);
            ships[4] = new Battleship("Patrol Boat", 2);
        }
示例#8
0
        public static void Main(string[] args)
        {
            // Sample game, see unit tests for more examples
            PlayerController playerController = new PlayerController(new Board(10, 10), 2);

            Battleship a = new Battleship(2, new Coordinate(5, 5), BattleshipOrientation.Horizontal);
            Battleship b = new Battleship(1, new Coordinate(6, 6), BattleshipOrientation.Vertical);

            Console.WriteLine("Game state: " + playerController.GetState());

            playerController.PlaceShip(a);
            playerController.PlaceShip(b);

            Console.WriteLine("Incoming attack result: " + playerController.IncomingAttack(new Coordinate(0, 0)));
            Console.WriteLine("Game state: " + playerController.GetState());
            Console.WriteLine("Incoming attack result: " + playerController.IncomingAttack(new Coordinate(5, 5)));
            Console.WriteLine("Game state: " + playerController.GetState());
            Console.WriteLine("Incoming attack result: " + playerController.IncomingAttack(new Coordinate(6, 6)));
            Console.WriteLine("Game state: " + playerController.GetState());
            Console.WriteLine("Incoming attack result: " + playerController.IncomingAttack(new Coordinate(6, 5)));
            Console.WriteLine("Game state: " + playerController.GetState());
        }
示例#9
0
        static void Main(string[] args)
        {
            //setup the game (make the console bigger and recieve the player names)
            Console.SetWindowSize(80, 55);

            string input;

            Console.WriteLine("Player 1 will go first.");

            //Initialise Player1
            Console.WriteLine("What is player 1's name?");
            input = Console.ReadLine().ToUpper();
            Player player1;

            if (input == "")
            {
                input = "CPU";
            }
            player1 = new Player(input);

            //Initialise Player2
            Console.WriteLine("What is player 2's name? (Leave blank to play against the computer)");
            input = Console.ReadLine().ToUpper();
            Player player2;

            if (input == "")
            {
                input = "CPU";
            }
            player2 = new Player(input);

            Console.Clear();

            //Initialise a game of battleship between the two players and then begin the game.
            Battleship game = new Battleship(player1, player2);

            game.Start();
        }
示例#10
0
        public Battleship CreateBattleship()
        {
            Battleship battleship = new Battleship();

            return(battleship);
        }
示例#11
0
        public void TwoPlayer(bool load)
        {
            Console.Clear();
            playing = true;

            //sets the game state to that loaded from the file chosen
            if (load)
            {
                string name = "";

                while (name == "")
                {
                    Console.WriteLine("\nPlease enter the name of the game to load.\n");
                    name = Console.ReadLine();
                }
                while (!rw.LoadTest(name))
                {
                    Console.WriteLine("\nSave file not found or corrupt. Please try again.\n");
                    name = Console.ReadLine();
                }

                rw.LoadGame(name);

                player1 = new Player(rw.Player1Name);
                player2 = new Player(rw.Player2Name);
                players.Add(player1);
                players.Add(player2);
                bships             = new Battleship(player1, player2);
                player1.HitCount   = rw.Player1Hit;
                player2.HitCount   = rw.Player2Hit;
                player1.MovesTaken = rw.Player1Moves;
                player2.MovesTaken = rw.Player2Moves;
                player1.LoadGameBoard(rw.Player2Track);
                player2.LoadGameBoard(rw.Player1Track);

                Console.WriteLine("\nLoaded!");
                Console.ReadKey();
            }
            //allow the players to set up the gameboards if a game is not loaded
            else
            {
                do
                {
                    do
                    {
                        Console.Write("\nPlease Enter the name of player 1: ");
                        name1 = Console.ReadLine();
                    }while (name1 == "");

                    Console.Clear();

                    do
                    {
                        Console.Write("\nPlease Enter the name of player 2: ");
                        name2 = Console.ReadLine();
                    }while (name2 == "");

                    Console.Clear();

                    if (name1 == name2)
                    {
                        Console.WriteLine("\nTwo players cannot use the same name!\n");
                    }
                }while (name1 == name2);

                Console.Clear();

                player1 = new Player(name1);
                player2 = new Player(name2);
                players.Add(player1);
                players.Add(player2);
                bships = new Battleship(player1, player2);


                foreach (Player player in players)
                {
                    bships.PlaceShips(player);

                    Console.WriteLine("\nPress any button to continue...");
                    Console.ReadKey();
                    Console.Clear();
                }

                player1.TrackBoard = player2.PlayerBoard;
                player2.TrackBoard = player1.PlayerBoard;
            }

            //loop for the actual game
            while (playing == true)
            {
                foreach (Player player in players)
                {
                    if (GameOver())
                    {
                        Console.Clear();
                        rw.SaveLog(player1.PlayerName, player2.PlayerName);

                        playing = false;
                        break;
                    }
                    else
                    {
                        Console.Clear();
                        PlayerMove(player);

                        //if player2 has just taken his turn, allow the game to be saved by the players then exit
                        if (player == player2)
                        {
                            Console.WriteLine("\nPress the \"s\" key if you would like to save and quit, else press any key to continue. . . ");
                            ConsoleKeyInfo key = Console.ReadKey(false);
                            if (key.Key == ConsoleKey.S)
                            {
                                rw.SaveGame(player1, player2);
                                Console.WriteLine("\nSaved.\n");
                                Environment.Exit(0);
                            }
                        }

                        Console.Clear();
                        Console.WriteLine("Please pass to the next player.\n\nPress any button to start your turn.");
                        Console.ReadKey();
                    }
                }
            }
        }
示例#12
0
        public void OnePlayer()
        {
            Console.Clear();
            playing = true;

            do
            {
                Console.Write("\nPlease Enter the name of player 1: ");
                name1 = Console.ReadLine();
            }while (name1 == "");

            Console.Clear();

            player1 = new Player(name1);
            player2 = new Player("Robot");
            players.Add(player1);
            players.Add(player2);
            bships = new Battleship(player1, player2);

            bships.PlaceShips(player1);
            player2.AIBoard();

            Console.WriteLine("\nPress any button to continue...");
            Console.ReadKey();
            Console.Clear();

            player1.TrackBoard = player2.PlayerBoard;
            player2.TrackBoard = player1.PlayerBoard;

            //loop for the actual game
            while (playing == true)
            {
                foreach (Player player in players)
                {
                    if (GameOver())
                    {
                        Console.Clear();
                        rw.SaveLog(player1.PlayerName, player2.PlayerName);

                        playing = false;
                        break;
                    }
                    else
                    {
                        Console.Clear();
                        PlayerMove(player);

                        //if player2 has just taken his turn, allow the game to be saved by the players then exit
                        if (player == player2)
                        {
                            Console.WriteLine("\nPress the \"s\" key if you would like to save and quit, else press any key to continue. . . ");
                            ConsoleKeyInfo key = Console.ReadKey(false);
                            if (key.Key == ConsoleKey.S)
                            {
                                rw.SaveGame(player1, player2);
                                Console.WriteLine("\nSaved.\n");
                                Environment.Exit(0);
                            }
                        }

                        Console.Clear();
                        Console.WriteLine("Please pass to the next player.\n\nPress any button to start your turn.");
                        Console.ReadKey();
                    }
                }
            }
        }