示例#1
0
文件: MovePlayer.cs 项目: gornikp/ZTP
 public MovePlayer(Coordinates shoot,IShip ship)
 {       
     shot = new Coordinates();
     shot.x = shoot.x;
     shot.y = shoot.y;
     
     this.ship = ship;
 }
示例#2
0
 public MovePrecise()
 {
     //fired = new List<Coordinates>();
     //lastshot = new List<states>();
     shot = new Coordinates();
     list = new List<Precise>();
     //Precise temp2 = new Precise();
     //temp.lastshot = states.miss;
     //temp.fired = new Coordinates(11, 11);
    // list.Add(temp);
 }
示例#3
0
 public bool contain(Coordinates shot)
 {
     foreach (Coordinates shots in fired)
     {
         if (shots.x == shot.x && shots.y == shot.y)
         {
             return true;
         }
     }
     return false;
 }
示例#4
0
 public ShipThreeMast(List<Coordinates> shipcords, string color)
 {
     length = 3;
     Coords = new List<Coordinates>();
     for (int i = 0; i < shipcords.Count; i++)
     {
         Coordinates temp = new Coordinates();
         temp.x = shipcords.ElementAt(i).x;
         temp.y = shipcords.ElementAt(i).y;
         Coords.Add(temp);
     }
     this.color = color;
     observators = new List<IObservator>();
 }
示例#5
0
文件: Computer.cs 项目: gornikp/ZTP
        public List<IShip> PutComputerShips()
        {

            //choice = rnd.Next(0, 1);


            //stawianie czteromasztowych statków
            for (int i = 0; i < 2;)
            {
                randX = rnd.Next(0, 9);
                randY = rnd.Next(0, 9);
                //choice = rnd.Next(0, 3);
                choice = rnd.Next(0, 2);
                coords = new Coordinates(randX, randY);
                temp.Add(coords);

                //choice == 1, stawia statek do góry
                if (choice == 1 && coords.y > 2 && !coordsChosen.Contains(coords))
                {
                    coords = new Coordinates(randX, --randY);
                    temp.Add(coords);
                    coords = new Coordinates(randX, --randY);
                    temp.Add(coords);
                    coords = new Coordinates(randX, --randY);
                    temp.Add(coords);
                    computerShip = ShipFactory.createShip(temp, computerShipsAdded);

                    if (computerShip != null)
                    {
                        ++i;
                        this.computerShipsAdded.Add(computerShip);
                        computerShip = null;
                        coordsChosen.AddRange(temp);
                    }
                    temp.Clear();
                }
                //choice == 0, stawia statek w lewo
                else if (choice == 0 && coords.x > 2 && !coordsChosen.Contains(coords))
                {
                    coords = new Coordinates(--randX, randY);
                    temp.Add(coords);
                    coords = new Coordinates(--randX, randY);
                    temp.Add(coords);
                    coords = new Coordinates(--randX, randY);
                    temp.Add(coords);
                    computerShip = ShipFactory.createShip(temp, computerShipsAdded);

                    if (computerShip != null)
                    {
                        ++i;
                        this.computerShipsAdded.Add(computerShip);
                        computerShip = null;
                        coordsChosen.AddRange(temp);
                    }
                    temp.Clear();
                }

                temp.Clear();
            }


            //stawianie trzymasztowych statków
            for (int i = 0; i < 3;)
            {
                randX = rnd.Next(0, 9);
                randY = rnd.Next(0, 9);
                //choice = rnd.Next(0, 3);
                choice = rnd.Next(0, 2);
                coords = new Coordinates(randX, randY);
                temp.Add(coords);

                //choice == 1, stawia statek do góry
                if (choice == 1 && coords.y > 1 && !coordsChosen.Contains(coords))
                {
                    coords = new Coordinates(randX, --randY);
                    temp.Add(coords);
                    coords = new Coordinates(randX, --randY);
                    temp.Add(coords);
                    computerShip = ShipFactory.createShip(temp, computerShipsAdded);

                    if (computerShip != null)
                    {
                        ++i;
                        this.computerShipsAdded.Add(computerShip);
                        computerShip = null;
                        coordsChosen.AddRange(temp);
                    }
                    temp.Clear();
                }
                else if (choice == 0 && coords.x > 1 && !coordsChosen.Contains(coords))
                {
                    coords = new Coordinates(--randX, randY);
                    temp.Add(coords);
                    coords = new Coordinates(--randX, randY);
                    temp.Add(coords);
                    computerShip = ShipFactory.createShip(temp, computerShipsAdded);

                    if (computerShip != null)
                    {
                        ++i;
                        this.computerShipsAdded.Add(computerShip);
                        computerShip = null;
                        coordsChosen.AddRange(temp);
                    }
                    temp.Clear();
                }

                temp.Clear();
            }

            //stawianie dwumasztowych statków
            for (int i = 0; i < 4;)
            {
                randX = rnd.Next(0, 9);
                randY = rnd.Next(0, 9);
                //choice = rnd.Next(0, 3);
                choice = rnd.Next(0, 2);
                coords = new Coordinates(randX, randY);
                temp.Add(coords);

                //choice == 1, stawia statek do góry
                if (choice == 1 && coords.y != 0 && !coordsChosen.Contains(coords))
                {
                    coords = new Coordinates(randX, --randY);
                    temp.Add(coords);
                    computerShip = ShipFactory.createShip(temp, computerShipsAdded);

                    if (computerShip != null)
                    {
                        ++i;
                        this.computerShipsAdded.Add(computerShip);
                        computerShip = null;
                        coordsChosen.AddRange(temp);
                    }
                    temp.Clear();
                }
                else if (choice == 0 && coords.x != 0 && !coordsChosen.Contains(coords))
                {

                    coords = new Coordinates(--randX, randY);
                    temp.Add(coords);
                    computerShip = ShipFactory.createShip(temp, computerShipsAdded);

                    if (computerShip != null)
                    {
                        ++i;

                        this.computerShipsAdded.Add(computerShip);
                        computerShip = null;
                        coordsChosen.AddRange(temp);
                    }
                    temp.Clear();
                }

                temp.Clear();
            }

            return computerShipsAdded;
        }
示例#6
0
 public bool contain(Coordinates shot)
 {
     for (int i = 0; i < list.Count; i++)
     {
         if (list.ElementAt(i).fired.x == shot.x && list.ElementAt(i).fired.y == shot.y)
         {
             return true;
         }
     }
     return false;
 }
示例#7
0
文件: iShip.cs 项目: gornikp/ZTP
 public void set_move(Coordinates shot)
 {
     this.shot = shot;
 }
示例#8
0
        private void PutShip(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            ++movesMade;
            int x = Grid.GetColumn(btn) - 1;
            int y = Grid.GetRow(btn) - 1;

            Coordinates coord = new Coordinates(x, y);

            if (shipsCreated < 4)
            {
                if (movesMade <= 1)
                {
                    this.tempCoords.Add(coord);
                    DisableUserBoardButton(x, y);
                    btn.Background = Brushes.Green;
                }
                else
                {
                    btn.Background = Brushes.Green;
                    this.tempCoords.Add(coord);
                    DisableUserBoardButton(x, y);

                    ship = ShipFactory.createShip(tempCoords, shipsAdded);
                    if (ship != null)
                    {
                        shipsCreated++;
                        MessageBox.AppendText(Environment.NewLine + "Statek został pomyślnie dodany.");
                        if (shipsCreated == 4) MessageBox.AppendText(Environment.NewLine + "Proszę dodać trzymasztowy statek.");
                        else MessageBox.AppendText(Environment.NewLine + "Proszę ponownie dodać dwumasztowy statek.");
                        movesMade = 0;
                        //foreach (Coordinates crd in tempCoords)
                        //{
                        //    DisableButton(crd.x, crd.y);
                        //}
                        shipsAdded.Add(ship);

                    }
                    else
                    {
                        MessageBox.AppendText(Environment.NewLine + "Statek został źle dodany.");
                        MessageBox.AppendText(Environment.NewLine + "Proszę ponownie dodać dwumasztowy statek.");
                        foreach (Coordinates crd in tempCoords)
                        {
                            SetUserBoardButtonDefault(crd.x, crd.y);
                        }

                    }
                    ship = null;
                    this.tempCoords.Clear();
                    movesMade = 0;
                }
            }
            else if (shipsCreated >= 4 && shipsCreated < 7)
            {
                if (movesMade <= 2)
                {
                    this.tempCoords.Add(coord);
                    DisableUserBoardButton(x, y);
                    btn.Background = Brushes.Blue;
                }
                else
                {
                    btn.Background = Brushes.Blue;
                    this.tempCoords.Add(coord);
                    DisableUserBoardButton(x, y);
                    ship = ShipFactory.createShip(tempCoords, shipsAdded);
                    if (ship != null)
                    {
                        shipsCreated++;
                        MessageBox.AppendText(Environment.NewLine + "Statek został pomyślnie dodany.");
                        if (shipsCreated == 7) MessageBox.AppendText(Environment.NewLine + "Proszę dodać czteromasztowy statek.");
                        else MessageBox.AppendText(Environment.NewLine + "Proszę ponownie dodać trzymasztowy statek.");
                        movesMade = 0;
                        //foreach (Coordinates crd in tempCoords)
                        //{
                        //    DisableButton(crd.x, crd.y);
                        //}
                        shipsAdded.Add(ship);
                    }
                    else
                    {
                        MessageBox.AppendText(Environment.NewLine + "Statek został źle dodany.");
                        MessageBox.AppendText(Environment.NewLine + "Proszę ponownie dodać trzymasztowy statek.");
                        foreach (Coordinates crd in tempCoords)
                        {
                            SetUserBoardButtonDefault(crd.x, crd.y);
                        }
                    }
                    ship = null;
                    this.tempCoords.Clear();
                    movesMade = 0;
                }
            }
            else
            {
                if (movesMade <= 3)
                {
                    this.tempCoords.Add(coord);
                    DisableUserBoardButton(x, y);
                    btn.Background = Brushes.Red;
                }
                else
                {
                    btn.Background = Brushes.Red;
                    this.tempCoords.Add(coord);
                    DisableUserBoardButton(x, y);
                    ship = ShipFactory.createShip(tempCoords, shipsAdded);
                    if (ship != null)
                    {
                        shipsCreated++;
                        MessageBox.AppendText(Environment.NewLine + "Statek został pomyślnie dodany.");
                        if (shipsCreated == 9)
                        {
                            MessageBox.AppendText(Environment.NewLine + "Wszystkie statki dodane.");
                            DisableUserBoard();
                            EnableComputerBoard();

                        }
                        else MessageBox.AppendText(Environment.NewLine + "Proszę ponownie dodać czteromasztowy statek.");
                        movesMade = 0;
                        //foreach (Coordinates crd in tempCoords)
                        //{
                        //    DisableButton(crd.x, crd.y);
                        //}
                        shipsAdded.Add(ship);
                    }
                    else
                    {
                        MessageBox.AppendText(Environment.NewLine + "Statek został źle dodany.");
                        MessageBox.AppendText(Environment.NewLine + "Proszę ponownie dodać czteromasztowy statek.");
                        foreach (Coordinates crd in tempCoords)
                        {
                            SetUserBoardButtonDefault(crd.x, crd.y);
                        }
                    }
                    ship = null;
                    this.tempCoords.Clear();
                    movesMade = 0;

                }
            }

        }
示例#9
0
        private void HitShip(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            int x = Grid.GetColumn(btn) - 1;
            int y = Grid.GetRow(btn) - 1;
            Coordinates hit = new Coordinates(x, y);
            states status = states.miss;


            foreach (IShip ship in computerShipsAdded)
            {

                observator = new ShipObservator(ship);
                ship.Register(observator);
                ship.set_move(hit);
                ship.shootIt();
                observator.update();
                status = observator.getState();
                if (status == states.hit)
                {
                    MessageBox.AppendText(Environment.NewLine + "Trafiłeś w statek przeciwnika.");
                    btn.Background = Brushes.IndianRed;
                    btn.Click -= HitShip;
                    break;
                }
                if (status == states.dead)
                {
                    shipcounter--;
                    MessageBox.AppendText(Environment.NewLine + "Zniszczyłeś statek przeciwnika. Zostało: " + shipcounter);
                    btn.Background = Brushes.IndianRed;
                    btn.Click -= HitShip;
                    break;
                }
                ship.UnRegister(observator);

            }

            if (status == states.miss)
            {
                MessageBox.AppendText(Environment.NewLine + "Nie trafiłeś.");
                btn.Content = "X";
                btn.FontSize = 25;
                btn.Click -= HitShip;
            }
            Backstatus temp = computer.HitShip(shipsAdded);
            if (temp.status == states.hit)
            {
                MessageBox.AppendText(Environment.NewLine + "Przeciwnik trafił Twój statek.");
                ChangeUserButtonToHit(temp.hit.x, temp.hit.y);

            }
            else if (temp.status == states.miss)
            {
                MessageBox.AppendText(Environment.NewLine + "Przeciwnik nie trafił.");
                ChangeUserButtonToMiss(temp.hit.x, temp.hit.y);
            }
            else if (temp.status == states.dead)
            {
                usershipelementcounter--;
                MessageBox.AppendText(Environment.NewLine + "Przeciwnik zniszczył Twój statek. Zostało: " + usershipelementcounter);
                ChangeUserButtonToHit(temp.hit.x, temp.hit.y);
            }

            if (usershipelementcounter == 0)
            {
                MessageBox.AppendText(Environment.NewLine + "Przegrałeś.");
                DisableComputerBoard();
            }

            if (shipcounter == 0)
            {
                MessageBox.AppendText(Environment.NewLine + "Wygrałeś.");
                DisableComputerBoard();
            }



        }