Beispiel #1
0
            public Backstatus Move(List<IShip> ships)
        {

            states status = states.miss;
            Random rnd1 = new Random();
            Random rnd2 = new Random();
            do
            {

                shot.x = rnd1.Next(10);
                shot.y = rnd1.Next(10);

            } while (contain(shot));


            foreach (IShip ship in ships)
            {
                observator = new ShipObservator(ship);
                ship.Register(observator);
                ship.set_move(shot);
                ship.shootIt();
                observator.update();
                status = observator.getState();
                ship.UnRegister(observator);
                returnvalue = new Backstatus();
                returnvalue.status = status;
                returnvalue.hit = new Coordinates(shot.x,shot.y);
                if (returnvalue.status == states.hit || returnvalue.status == states.dead)
                {
                    fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                    return returnvalue;
                }               
            }
            fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
            return returnvalue;
        }
Beispiel #2
0
        public Backstatus Move(List<IShip> ships)
        {
        states status = states.miss;
        int temp = list.Count;
            Precise temp2 = new Precise();
            if (temp < 4)
                elo = 0;
            else elo = temp-4;

                Random rnd1 = new Random();
                Random rnd2 = new Random();
                do
                {
                    shot.x = rnd1.Next(10);
                    shot.y = rnd2.Next(10);
                } while (contain(shot));

            for (int i = list.Count - 1; i >= elo; i--)
            {
                if (list[i].lastshot == states.dead)
                {
                    break;
                }
                if (list[i].lastshot == states.hit && list[i].fired.x + 1 < 10 && !contain(new Coordinates(list[i].fired.x + 1, list[i].fired.y)))
                {
                    shot.x = list[i].fired.x + 1;
                    shot.y = list[i].fired.y;
                    break;
                }
                if (i > 0)
                {
                    if (list[i - 1].lastshot == states.hit && list[i].fired.x - 1 >=0 && !contain(new Coordinates(list[i].fired.x - 1, list[i].fired.y)))
                    {
                        shot.x = list[i - 1].fired.x - 1;
                        shot.y = list[i - 1].fired.y;
                        break;
                    }
                }
                if (i > 1)
                {
                    if (list[i - 2].lastshot == states.hit && list[i].fired.y + 1 < 10 && !contain(new Coordinates(list[i].fired.x, list[i].fired.y+1)))
                    {
                        shot.x = list[i - 2].fired.x;
                        shot.y = list[i - 2].fired.y + 1;
                        break;
                    }
                }
                if (i > 2)
                {
                    if (list[i - 3].lastshot == states.hit && list[i].fired.y - 1 >=0 && !contain(new Coordinates(list[i].fired.x , list[i].fired.y-1)))
                    {
                        shot.x = list[i - 3].fired.x;
                        shot.y = list[i - 3].fired.y - 1;
                        break;
                    }
                }
                // TODO od nowa trzeba bo stara była chujowa
            }

            foreach (IShip ship in ships)
            {
                observator = new ShipObservator(ship);
                ship.Register(observator);
                ship.set_move(shot);
                ship.shootIt();
                observator.update();
                status = observator.getState();
                ship.UnRegister(observator);
                returnvalue = new Backstatus();
                returnvalue.status = status;
                returnvalue.hit = new Coordinates(shot.x, shot.y);              
                temp2.lastshot = status;
                temp2.fired = new Coordinates(returnvalue.hit.x, returnvalue.hit.y);             

                if (returnvalue.status == states.hit || returnvalue.status == states.dead)
                {
                    list.Add(temp2);
                    return returnvalue;
                }
            }
            list.Add(temp2);
            return returnvalue;
        }
Beispiel #3
0
        public Backstatus Move(List<IShip> ships)
        {

            if (!tempFired.Any())
            {
                //hunter mode - random 
                states status = states.miss;
                Random rnd1 = new Random();
                do
                {

                    shot.x = rnd1.Next(10);
                    shot.y = rnd1.Next(10);

                } while (contain(shot));


                foreach (IShip ship in ships)
                {
                    observator = new ShipObservator(ship);
                    ship.Register(observator);
                    ship.set_move(shot);
                    ship.shootIt();
                    observator.update();
                    status = observator.getState();
                    ship.UnRegister(observator);
                    returnvalue = new Backstatus();
                    returnvalue.status = status;
                    returnvalue.hit = new Coordinates(shot.x, shot.y);
                    //if (returnvalue.status == states.hit || returnvalue.status == states.dead)
                    if (returnvalue.status == states.hit)
                    {
                        fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                        tempFired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                        return returnvalue;
                    }
                }
                fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                return returnvalue;
            }
            else
            {
                //target mode
                states status = states.miss;

                if (tempFired.Count() == 1)
                {

                    //probuj w gore
                    tryShot.x = tempFired.First().x;
                    tryShot.y = tempFired.First().y - 1;

                    if (tryShot.y >= 0 && !contain(tryShot))
                    {
                        foreach (IShip ship in ships)
                        {
                            observator = new ShipObservator(ship);
                            ship.Register(observator);
                            ship.set_move(tryShot);
                            ship.shootIt();
                            observator.update();
                            status = observator.getState();
                            ship.UnRegister(observator);
                            returnvalue = new Backstatus();
                            returnvalue.status = status;
                            returnvalue.hit = new Coordinates(tryShot.x, tryShot.y);
                            //if (returnvalue.status == states.hit || returnvalue.status == states.dead)
                            if (returnvalue.status == states.hit)
                            {
                                fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                tempFired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                return returnvalue;
                            }
                            else if (returnvalue.status == states.dead)
                            {
                                fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                tempFired.Clear();
                                //tryShot = null;
                                return returnvalue;
                            }
                        }
                        fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                        return returnvalue;
                    }

                    //probuj w dol
                    tryShot.x = tempFired.First().x;
                    tryShot.y = tempFired.First().y + 1;

                    if (tryShot.y <= 9 && !contain(tryShot))
                    {
                        foreach (IShip ship in ships)
                        {
                            observator = new ShipObservator(ship);
                            ship.Register(observator);
                            ship.set_move(tryShot);
                            ship.shootIt();
                            observator.update();
                            status = observator.getState();
                            ship.UnRegister(observator);
                            returnvalue = new Backstatus();
                            returnvalue.status = status;
                            returnvalue.hit = new Coordinates(tryShot.x, tryShot.y);
                            //if (returnvalue.status == states.hit || returnvalue.status == states.dead)
                            if (returnvalue.status == states.hit)
                            {
                                fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                tempFired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                return returnvalue;
                            }
                            else if (returnvalue.status == states.dead)
                            {
                                fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                tempFired.Clear();
                                //tryShot = null;
                                return returnvalue;
                            }
                        }
                        fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                        return returnvalue;
                    }

                    //probuj w prawo
                    tryShot.x = tempFired.First().x + 1;
                    tryShot.y = tempFired.First().y;

                    if (tryShot.x <= 9 && !contain(tryShot))
                    {
                        foreach (IShip ship in ships)
                        {
                            observator = new ShipObservator(ship);
                            ship.Register(observator);
                            ship.set_move(tryShot);
                            ship.shootIt();
                            observator.update();
                            status = observator.getState();
                            ship.UnRegister(observator);
                            returnvalue = new Backstatus();
                            returnvalue.status = status;
                            returnvalue.hit = new Coordinates(tryShot.x, tryShot.y);
                            //if (returnvalue.status == states.hit || returnvalue.status == states.dead)
                            if (returnvalue.status == states.hit)
                            {
                                fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                tempFired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                return returnvalue;
                            }
                            else if (returnvalue.status == states.dead)
                            {
                                fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                tempFired.Clear();
                                //tryShot = null;
                                return returnvalue;
                            }
                        }
                        fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                        return returnvalue;
                    }

                    //probuj w lewo
                    tryShot.x = tempFired.First().x - 1;
                    tryShot.y = tempFired.First().y;

                    //niepotrzebne sprawdzanie - nie ma innej możliwości
                    //if (tryShot.x >= 0 && !contain(tryShot))
                    //{
                    foreach (IShip ship in ships)
                    {
                        observator = new ShipObservator(ship);
                        ship.Register(observator);
                        ship.set_move(tryShot);
                        ship.shootIt();
                        observator.update();
                        status = observator.getState();
                        ship.UnRegister(observator);
                        returnvalue = new Backstatus();
                        returnvalue.status = status;
                        returnvalue.hit = new Coordinates(tryShot.x, tryShot.y);
                        //if (returnvalue.status == states.hit || returnvalue.status == states.dead)
                        if (returnvalue.status == states.hit)
                        {
                            fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                            tempFired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                            return returnvalue;
                        }
                        else if (returnvalue.status == states.dead)
                        {
                            fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                            tempFired.Clear();
                            //tryShot = null;
                            return returnvalue;
                        }
                    }
                    fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                    return returnvalue;
                    //}


                }
                else
                {
                    if (tempFired[0].x == tempFired[1].x)
                    {
                        Coordinates temp = MaxValueY(tempFired);
                        tryShot.x = temp.x;
                        tryShot.y = temp.y + 1;

                        if (tryShot.y <= 9  && !contain(tryShot))
                        {
                            //wal w tryShota, jesli trafiony to dodaj do tempFired i fired, jesli nie to tylko do fired
                            //jesli zatopiony to oproznij liste tempFired i znulluj tryShot
                            foreach (IShip ship in ships)
                            {
                                observator = new ShipObservator(ship);
                                ship.Register(observator);
                                ship.set_move(tryShot);
                                ship.shootIt();
                                observator.update();
                                status = observator.getState();
                                ship.UnRegister(observator);
                                returnvalue = new Backstatus();
                                returnvalue.status = status;
                                returnvalue.hit = new Coordinates(tryShot.x, tryShot.y);
                                //if (returnvalue.status == states.hit || returnvalue.status == states.dead)
                                if (returnvalue.status == states.hit)
                                {
                                    fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                    tempFired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                    return returnvalue;
                                }
                                else if (returnvalue.status == states.dead)
                                {
                                    fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                    tempFired.Clear();
                                    //tryShot = null;
                                    return returnvalue;
                                }
                            }
                            fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                            return returnvalue;
                        }

                        temp = MinValueY(tempFired);
                        tryShot.x = temp.x;
                        tryShot.y = temp.y - 1;

                        //wal w tryShota, jesli trafiony to dodaj do tempFired i fired, jesli nie to tylko do fired
                        //jesli zatopiony to oproznij liste tempFired i znulluj tryShot
                        foreach (IShip ship in ships)
                        {
                            observator = new ShipObservator(ship);
                            ship.Register(observator);
                            ship.set_move(tryShot);
                            ship.shootIt();
                            observator.update();
                            status = observator.getState();
                            ship.UnRegister(observator);
                            returnvalue = new Backstatus();
                            returnvalue.status = status;
                            returnvalue.hit = new Coordinates(tryShot.x, tryShot.y);
                            //if (returnvalue.status == states.hit || returnvalue.status == states.dead)
                            if (returnvalue.status == states.hit)
                            {
                                fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                tempFired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                return returnvalue;
                            }
                            else if (returnvalue.status == states.dead)
                            {
                                fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                tempFired.Clear();
                                //tryShot = null;
                                return returnvalue;
                            }
                        }
                        fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                        return returnvalue;
                    }
                    else
                    {
                        Coordinates temp = MaxValueX(tempFired);
                        tryShot.x = temp.x + 1;
                        tryShot.y = temp.y;

                        if (tryShot.x <= 9 && !contain(tryShot))
                        {
                            //wal w tryShota, jesli trafiony to dodaj do tempFired i fired, jesli nie to tylko do fired
                            //jesli zatopiony to oproznij liste tempFired i znulluj tryShot
                            foreach (IShip ship in ships)
                            {
                                observator = new ShipObservator(ship);
                                ship.Register(observator);
                                ship.set_move(tryShot);
                                ship.shootIt();
                                observator.update();
                                status = observator.getState();
                                ship.UnRegister(observator);
                                returnvalue = new Backstatus();
                                returnvalue.status = status;
                                returnvalue.hit = new Coordinates(tryShot.x, tryShot.y);
                                //if (returnvalue.status == states.hit || returnvalue.status == states.dead)
                                if (returnvalue.status == states.hit)
                                {
                                    fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                    tempFired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                    return returnvalue;
                                }
                                else if (returnvalue.status == states.dead)
                                {
                                    fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                    tempFired.Clear();
                                    //tryShot = null;
                                    return returnvalue;
                                }
                            }
                            fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                            return returnvalue;
                        }

                        temp = MinValueX(tempFired);
                        tryShot.x = temp.x - 1;
                        tryShot.y = temp.y;

                        //wal w tryShota, jesli trafiony to dodaj do tempFired i fired, jesli nie to tylko do fired
                        //jesli zatopiony to oproznij liste tempFired i znulluj tryShot
                        foreach (IShip ship in ships)
                        {
                            observator = new ShipObservator(ship);
                            ship.Register(observator);
                            ship.set_move(tryShot);
                            ship.shootIt();
                            observator.update();
                            status = observator.getState();
                            ship.UnRegister(observator);
                            returnvalue = new Backstatus();
                            returnvalue.status = status;
                            returnvalue.hit = new Coordinates(tryShot.x, tryShot.y);
                            //if (returnvalue.status == states.hit || returnvalue.status == states.dead)
                            if (returnvalue.status == states.hit)
                            {
                                fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                tempFired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                return returnvalue;
                            }
                            else if (returnvalue.status == states.dead)
                            {
                                fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                                tempFired.Clear();
                                //tryShot = null;
                                return returnvalue;
                            }
                        }
                        fired.Add(new Coordinates(returnvalue.hit.x, returnvalue.hit.y));
                        return returnvalue;
                    }

                }



            }
        }
        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();
            }



        }