コード例 #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
ファイル: 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;
        }
コード例 #3
0
ファイル: ShipObservator.cs プロジェクト: gornikp/ZTP
 public ShipObservator(IShip watched)
 {
     this.watched = watched;
 }
コード例 #4
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;

                }
            }

        }