示例#1
0
        public static void Place(int curShipLength, int width, int height, ShipController ShipController)
        {
            bool isPlaced = false;

            while (!isPlaced)
            {
                try
                {
                    var direction = Random.Next(0, 3);
                    var x         = Random.Next(0, width - 1);
                    var y         = Random.Next(0, height - 1);
                    switch (direction)
                    {
                    case 0:
                        CriticalCheck(x - curShipLength < 0);
                        CreateShip(x - curShipLength, y, x, y, ShipController);
                        break;

                    case 1:
                        CriticalCheck(y - curShipLength < 0);
                        CreateShip(x, y - curShipLength, x, y, ShipController);
                        break;

                    case 2:
                        CriticalCheck(x + curShipLength > width);
                        CreateShip(x, y, x + curShipLength, y, ShipController);
                        break;

                    case 3:
                        CriticalCheck(y + curShipLength > height);
                        CreateShip(x, y, x, y + curShipLength, ShipController);
                        break;
                    }
                    isPlaced = true;
                }
                catch { }
            }
        }
示例#2
0
 private static void CreateShip(int x1, int y1, int x2, int y2, ShipController ShipController)
 {
     ShipController.Add(new Player2ShipBuilder(x1, y1, x2, y2));
 }
示例#3
0
        static void Main(string[] args)
        {
            var input          = "";
            var shipController = new ShipController();
            var game           = Game.getInstance();

            shipController.Subscribe(game as IObserver <Cell>);
            Stack <int> shipsLeft = new Stack <int>();
            var         tmp       = new List <int> {
                1, 2, 3
            };

            FillStack(shipsLeft, tmp);

            Console.WriteLine("This is sea battle");
            while (input.ToUpper() != "EXIT")
            {
                switch (game.GameStatus)
                {
                case GameStatus.boardSetting:
                    const int minWidth = 3, minHeight = 3, maxWidth = 20, maxHeight = 20;
                    game.SetWidth(CheckedInput($"width ({minWidth}-{maxWidth}) =", minWidth, maxWidth));
                    game.SetHeight(CheckedInput($"height ({minHeight}-{maxHeight}) =", minHeight, maxHeight));
                    game.NextTurn();
                    break;

                case GameStatus.shipPlacing:
                    int curShipLength = 0;
                    try
                    {
                        curShipLength = shipsLeft.Pop();
                    }
                    catch
                    {
                        game.NextTurn();
                        continue;
                    }
                    ComputerShipPlacer.Place(curShipLength - 1, game.GetWidth(), game.GetHeight(), shipController);
                    bool isPlacingCorrect = false;
                    while (!isPlacingCorrect)
                    {
                        Console.WriteLine($"Place ship with length ={curShipLength}");
                        shipController.ShowField(Player.player1, game.GetWidth(), game.GetHeight());
                        int x         = CheckedInput("x=", 0, game.GetWidth() - 1);
                        int y         = CheckedInput("y=", 0, game.GetHeight() - 1);
                        int direction = 4;
                        direction = CheckedInput("Enter direction: 0=left, 1=up, 2=right, 3= down", 0, 3);
                        Player1ShipBuilder player1ShipBuilder = null;
                        curShipLength--;
                        try
                        {
                            switch (direction)
                            {
                            case 0:
                                if (x - curShipLength < 0)
                                {
                                    throw new Exception();
                                }
                                player1ShipBuilder = new Player1ShipBuilder(x - curShipLength, y, x, y);
                                break;

                            case 1:
                                if (y - curShipLength < 0)
                                {
                                    throw new Exception();
                                }
                                player1ShipBuilder = new Player1ShipBuilder(x, y - curShipLength, x, y);
                                break;

                            case 2:
                                if (x + curShipLength > game.GetWidth())
                                {
                                    throw new Exception();
                                }
                                player1ShipBuilder = new Player1ShipBuilder(x, y, x + curShipLength, y);
                                break;

                            case 3:
                                if (y + curShipLength > game.GetHeight())
                                {
                                    throw new Exception();
                                }
                                player1ShipBuilder = new Player1ShipBuilder(x, y, x, y + curShipLength);
                                break;
                            }

                            shipController.Add(player1ShipBuilder);
                            isPlacingCorrect = true;
                        }
                        catch
                        {
                            Console.WriteLine("Incorrect placing");
                            curShipLength++;
                        }
                    }
                    break;

                case GameStatus.shootingP1:
                    var table         = game.GetShotsTable(Player.player1);
                    var isCorrectShot = false;
                    while (!isCorrectShot)
                    {
                        try
                        {
                            Console.WriteLine("You shoot now! Your shots:");
                            PrintShots(game, table);
                            int xTmp = CheckedInput("x=", 0, game.GetWidth() - 1);
                            int yTmp = CheckedInput("y=", 0, game.GetHeight() - 1);
                            if (table[xTmp, yTmp] != ShotsBoardCellState.Unknown)
                            {
                                throw new Exception();
                            }
                            var playerShotResult = shipController.Shoot(xTmp, yTmp, Player.player1);
                            Console.WriteLine(playerShotResult);
                            if (playerShotResult == ShootResult.Miss)
                            {
                                isCorrectShot = true;
                            }
                            if (shipController.P1Win())
                            {
                                game.EndGame(Player.player1); break;
                            }
                        }
                        catch
                        {
                            Console.WriteLine("Incorrect shot!");
                        }
                    }
                    game.NextTurn();

                    break;

                case GameStatus.shootingP2:
                    var comShotsTable = game.GetShotsTable(Player.player2);
                    Console.WriteLine("Computer shoots now! Computer shots:");
                    PrintShots(game, comShotsTable);
                    var iscCorrectShot = false;
                    IComputerStrategy computerStrategy = null;
                    while (!iscCorrectShot)
                    {
                        try
                        {
                            var woundsCount = 0;
                            foreach (ShotsBoardCellState c in comShotsTable)
                            {
                                if (c == ShotsBoardCellState.Wounded)
                                {
                                    woundsCount++;
                                }
                            }
                            if (woundsCount == 0)
                            {
                                computerStrategy = new NoHitsStategy();
                            }
                            if (woundsCount == 1)
                            {
                                computerStrategy = new OneHitStategy();
                            }
                            if (woundsCount > 1)
                            {
                                computerStrategy = new ManyHitsStategy();
                            }
                            var shotCell           = computerStrategy.Shoot(comShotsTable);
                            var computerShotResult = shipController.Shoot(shotCell.x, shotCell.y, Player.player2);
                            Console.WriteLine("Computer shot result" + computerShotResult);
                            if (computerShotResult == ShootResult.Miss)
                            {
                                iscCorrectShot = true;
                            }
                            if (shipController.P2Win())
                            {
                                game.EndGame(Player.player2); break;
                            }
                        }
                        catch { }
                    }
                    game.NextTurn();
                    break;

                case GameStatus.end:
                    Console.WriteLine("Game ended. type 'exit' to exit");
                    input = Console.ReadLine();
                    break;
                }
            }
        }