コード例 #1
0
        public Savegame(string fileLoc)
        {
            this._playerShips = new ObservableCollection <Ship>();
            this._shipyards   = new ObservableCollection <Shipyard>();
            this._stations    = new ObservableCollection <Station>();

            Path = fileLoc;
            Data = XElement.Load(_path, LoadOptions.PreserveWhitespace);

            IEnumerable <XElement> ships = from ship in _data.Descendants().Elements("component")
                                           where (string)ship.Attribute("owner") == "player" && (string)ship.Attribute("state") != "wreck" && ((string)ship.Attribute("class") == "ship_xl" | (string)ship.Attribute("class") == "ship_l" | (string)ship.Attribute("class") == "ship_s" | (string)ship.Attribute("class") == "ship_xs")
                                           orderby(string) ship.Attribute("name")
                                           select ship;

            foreach (XElement ship in ships)
            {
                PlayerShips.Add(new Ship(ship));
            }

            IEnumerable <XElement> shipyards = this._data.Descendants().Elements("component").Where(delegate(XElement shipyard)
            {
                bool result = false;
                if (shipyard.Attribute("macro") != null && shipyard.Attribute("macro").Value.ToString().EndsWith("yard_macro"))
                {
                    result = true;
                }
                return(result);
            }
                                                                                                    );

            foreach (XElement shipyard in shipyards)
            {
                Shipyards.Add(new Shipyard(shipyard));
            }


            IEnumerable <XElement> stations = this._data.Descendants().Elements("component").Where(delegate(XElement ele)
            {
                bool result = false;
                if (ele.Attribute("owner") != null && ele.Attribute("class") != null && (ele.Attribute("owner").Value.ToString() == "player") && ele.Attribute("class").Value.Contains("station"))
                {
                    result = true;
                }

                return(result);
            });

            foreach (XElement station in stations)
            {
                Stations.Add(new Station(station));
            }
        }
コード例 #2
0
        private void HandleShipRequest(object sender, ShipMessage msg)
        {
            ShipPeer    peer    = sender as ShipPeer;
            ShipRequest request = msg as ShipRequest;

            JoinShipResponce responce = new JoinShipResponce();

            PlayerShip shipToLink = null;

            if (peer.LinkedShip != null)
            {
                shipToLink = peer.LinkedShip;
            }
            else
            {
                if (request.Join)
                {
                    shipToLink = PlayerShips.Find((x) => x.LinkedShip.GUID == request.RequestedShipID);
                    if (shipToLink == null || shipToLink.Locked && shipToLink.Password != request.Password)
                    {
                        responce.Error   = true;
                        responce.Message = "NoShipToJoin";
                        peer.Send(responce);
                        return; // nope!
                    }
                    else
                    {
                        if (!shipToLink.ControlingPeers.Contains(peer))
                        {
                            shipToLink.ControlingPeers.Add(peer);
                        }

                        peer.LinkedShip = shipToLink;
                    }
                }
            }

            if (shipToLink == null)
            {
                var template = TemplateDatabase.GetTemplate(request.RequestedShipID);
                int shipID   = ActiveScenario.SpawnPlayableShip(peer.Connection.RemoteUniqueIdentifier.ToString(), template == null ? string.Empty : template.Name, request.Name);

                var ship = ZoneManager.GetFirstMatch(new PlayableShipFinder(shipID)) as Ship;

                shipToLink            = new PlayerShip();
                shipToLink.LinkedShip = ship;
                shipToLink.ControlingPeers.Add(peer);
                shipToLink.Locked   = request.Password != string.Empty;
                shipToLink.Password = request.Password;

                peer.LinkedShip = shipToLink;
                PlayerShips.Add(shipToLink);
            }

            // send back the responce with the ship they are on
            responce.Error   = false;
            responce.Message = shipToLink.LinkedShip.Name;
            responce.ShipID  = shipToLink.LinkedShip.GUID;
            peer.Send(responce);

            // send an info and status update
        }
コード例 #3
0
ファイル: Game.cs プロジェクト: maolen/WarShip
        public void ProccessArrangement()
        {
            int type = 0;


            bool isTruePunct = false;

            Console.WriteLine("Вы должны расставить корабли. В запасе:");
            Console.WriteLine($"1. Катер (однапалубный) - {boat}");
            Console.WriteLine($"2. Эсминцы (двухпалубный) - {destroyers}");
            Console.WriteLine($"3. Крейсеры (трёхпалубный) - {cruisers}");
            Console.WriteLine($"4. Линкор (четырёхпалубный) - {battleship}\n");
            while (!isTruePunct)
            {
                Console.Write($"Введите номер корабля: ");
                var isDigit = int.TryParse(Console.ReadLine(), out int punct);
                if (isDigit && (punct > 0 || punct < 5))
                {
                    switch (punct)
                    {
                    case 1:
                        type = 1;
                        if (boat == 0)
                        {
                            Console.WriteLine("Кораблей такого типа нет в запасе");
                        }
                        else
                        {
                            isTruePunct = true;
                        }
                        break;

                    case 2:
                        type = 2;
                        if (destroyers == 0)
                        {
                            Console.WriteLine("Кораблей такого типа нет в запасе");
                        }
                        else
                        {
                            isTruePunct = true;
                        }
                        break;

                    case 3:
                        type = 3;
                        if (cruisers == 0)
                        {
                            Console.WriteLine("Кораблей такого типа нет в запасе");
                        }
                        else
                        {
                            isTruePunct = true;
                        }
                        break;

                    case 4:
                        type = 4;
                        if (battleship == 0)
                        {
                            Console.WriteLine("Кораблей такого типа нет в запасе");
                        }
                        else
                        {
                            isTruePunct = true;
                        }
                        break;

                    default:
                        Console.WriteLine("Введите число от 1 до 4");
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("Введите число от 1 до 4");
                }
            }
            string alph       = "";
            bool   isTrueAlph = false;

            while (!isTrueAlph)
            {
                Console.Write("Введите букву: ");
                alph = Console.ReadLine();
                if (alph != "A" && alph == "B" && alph != "C" && alph != "D" && alph != "E" && alph != "F" && alph != "G" && alph != "H" && alph != "I" && alph != "J")
                {
                    Console.WriteLine("Вы должны ввести букву от A-J");
                }
                else
                {
                    isTrueAlph = true;
                }
            }
            string number       = string.Empty;
            bool   isTrueNumber = false;

            while (!isTrueNumber)
            {
                Console.Write("Введите число: ");
                number = Console.ReadLine();
                if (number != "1" && number != "2" && number != "3" && number != "4" && number != "5" && number != "6" && number != "7" && number != "8" && number != "9" && number != "10")
                {
                    Console.WriteLine("Вы должны ввести число от 1-10");
                }
                else
                {
                    isTrueNumber = true;
                }
            }
            int alphDigit = 0;

            if (alph == "A")
            {
                alphDigit = 1;
            }
            else if (alph == "B")
            {
                alphDigit = 2;
            }
            else if (alph == "C")
            {
                alphDigit = 3;
            }
            else if (alph == "D")
            {
                alphDigit = 4;
            }
            else if (alph == "E")
            {
                alphDigit = 5;
            }
            else if (alph == "F")
            {
                alphDigit = 6;
            }
            else if (alph == "G")
            {
                alphDigit = 7;
            }
            else if (alph == "H")
            {
                alphDigit = 8;
            }
            else if (alph == "I")
            {
                alphDigit = 9;
            }
            else if (alph == "J")
            {
                alphDigit = 10;
            }
            if (type == 1)
            {
                PositionShip boatPos = new PositionShip
                {
                    Alph   = alphDigit,
                    Number = Int32.Parse(number)
                };
                Console.WriteLine("Корабль поставлен!");
                Console.Clear();
                PlayerShips.Add(boatPos);
            }
            else
            {
                Console.WriteLine("1. Вверх");
                Console.WriteLine("2. Вниз");
                Console.WriteLine("3. Вправо");
                Console.WriteLine("4. Влево");
                string direction       = "";
                bool   isTrueDirection = false;
                while (!isTrueDirection)
                {
                    Console.Write("Выберите направление для корабля: ");
                    direction = Console.ReadLine();
                    if (direction != "1" && direction != "2" && direction != "3" && direction != "4")
                    {
                        Console.WriteLine("Вы должны ввести число от 1-4");
                    }
                    else
                    {
                        if (direction == "1")
                        {
                            if (Int32.Parse(number) - type < 0)
                            {
                                Console.WriteLine("Корабль невозможно поставить");
                            }
                            else
                            {
                                isTrueDirection = true;
                            }
                        }
                        else if (direction == "2")
                        {
                            if (Int32.Parse(number) + type > 10)
                            {
                                Console.WriteLine("Корабль невозможно поставить");
                            }
                            else
                            {
                                isTrueDirection = true;
                            }
                        }
                        else if (direction == "3")
                        {
                            if (Char.Parse(number) + type > 74)
                            {
                                Console.WriteLine("Корабль невозможно поставить");
                            }
                            else
                            {
                                isTrueDirection = true;
                            }
                        }
                        else if (direction == "4")
                        {
                            if (Char.Parse(number) - type < 65)
                            {
                                Console.WriteLine("Корабль невозможно поставить");
                            }
                            else
                            {
                                isTrueDirection = true;
                            }
                        }
                        else
                        {
                            isTrueNumber = true;
                        }
                    }
                }

                for (int i = 0; i < type; i++)
                {
                    PositionShip position = new PositionShip();
                    if (direction == "1")
                    {
                        position = new PositionShip
                        {
                            Alph   = alphDigit,
                            Number = Int32.Parse(number) - i
                        };
                    }
                    else if (direction == "2")
                    {
                        position = new PositionShip
                        {
                            Alph   = alphDigit,
                            Number = Int32.Parse(number) + i
                        };
                    }
                    else if (direction == "3")
                    {
                        position = new PositionShip
                        {
                            Alph   = alphDigit + i,
                            Number = Int32.Parse(number)
                        };
                    }
                    else if (direction == "4")
                    {
                        position = new PositionShip
                        {
                            Alph   = alphDigit - i,
                            Number = Int32.Parse(number)
                        };
                    }

                    PlayerShips.Add(position);
                }

                Console.WriteLine("Корабль поставлен!");
                Console.Clear();
            }
            if (type == 1)
            {
                boat--;
            }
            else if (type == 2)
            {
                destroyers--;
            }
            else if (type == 3)
            {
                cruisers--;
            }
            else if (type == 4)
            {
                battleship--;
            }
            Start();
        }