예제 #1
0
        private Position GetRandomPos(Sea sea)
        {
            Position robotShotPos = new Position();

            robotShotPos.X = generator.Next(0, 9);
            robotShotPos.Y = generator.Next(0, 9);


            while (sea[robotShotPos.X, robotShotPos.Y] == State.MissedHit ||
                   sea[robotShotPos.X, robotShotPos.Y] == State.TargetHit)
            {
                robotShotPos.X = generator.Next(0, 9);
                robotShotPos.Y = generator.Next(0, 9);
            }


            return(robotShotPos);
        }
예제 #2
0
        //protected Position GenerateRobotShoot()
        //{
        //    Position robotShot=new Position();
        //    robotShot.X=randGenerator.Next(0, seas[0].GetBody().GetLength(0));
        //    robotShot.Y  = randGenerator.Next(0, seas[0].GetBody().GetLength(0));
        //    return robotShot;
        //}

        public void ConfigureGameField(Sea someSea, int shipMaxLength)
        {
            if (shipMaxLength > someSea.GetBody().GetLength(0) - 2)
            {
                throw new OutOfShipLengthException("Exceeded maximal ship length.", someSea.GetBody().GetLength(0) - 2);
            }

            for (int shipLength = shipMaxLength; shipLength >= 2; shipLength--)
            {
                while (true)
                {
                    Ship currentShip;
                    if (someSea is PlayerSea)
                    {
                        string message = string.Format("Ship`s length: {0}. Enter ships parameters in format: \r\nStart position: X Y:\r\nAlignment:",shipLength);
                        renderer.AddMessageToRender(message);
                        renderer.RenderAll();
                        renderer.ClearRenderObjects();
                        currentShip = this.userInput.ReadInputShip(shipLength);
                    }
                    else
                    {
                        currentShip = this.GenerateShip(shipLength, someSea.GetBody().GetLength(0));
                    }

                    Position[] currentShipBody = currentShip.GetBody() as Position[];
                    int freePositions = 0;

                    foreach (var position in currentShipBody)
                    {
                        if (someSea is PlayerSea)
                        {
                            if ((position.X < someSea.GetBody().GetLength(0) && position.Y < someSea.GetBody().GetLength(1)) && (someSea[position.X, position.Y] == State.Empty))
                            {
                                freePositions++;
                            }
                        }
                        else if(someSea[position.X, position.Y] == State.Empty)
                        {
                            freePositions++;
                        }
                    }

                    if (freePositions == shipLength)
                    {
                        foreach (var position in currentShipBody)
                        {
                            someSea[position.X, position.Y] = State.Ship;
                        }

                        if (someSea is PlayerSea)
                        {
                            this.AddPlayerShips(currentShip);
                            string message = string.Format("Ship with length {0} was created",shipLength);
                            renderer.AddMessageToRender(message);
                            renderer.RenderAll();
                            renderer.ClearRenderObjects();
                        }
                        else
                        {
                            this.AddRobotShips(currentShip);
                        }

                        break;
                    }
                    else if (someSea is PlayerSea)
                    {
                        string message = string.Format("Ship with length {0} was NOT created", shipLength);
                        renderer.AddMessageToRender(message);
                        renderer.RenderAll();
                        renderer.ClearRenderObjects();
                    }
                }
            }

            this.AddSea(someSea);
        }
예제 #3
0
 public void AddSea(Sea sea)
 {
     this.seas.Add(sea);
 }//will add two seas in Main (one of player and one of robot)
예제 #4
0
        private void ProcessShot(Sea sea, Position shot)
        {
            List<Ship> fleet= new List<Ship>();
            if (sea is BootSea)
            {
                fleet = robotShips;
            }
            else
            {
                fleet = playerShips;
            }

            
            bool isShip = sea.RespondToHit(shot);
            if (isShip)
            {
                foreach (Ship ship in fleet)
                {
                    foreach (Position position in ship.GetBody())
                    {
                        if (position.Equals(shot))
                        {
                            bool destoroyed = ship.RespondToHit(shot);
                            if (destoroyed)
                            {
                                if (sea is BootSea)
                                {
                                    robotFleet++;
                                }
                                else
                                {
                                    playerFleet++;
                                }
                            }
                        }

                    }
                }
                //    foreach (Gift gift in robotGifts)
                //    {
                //        if (gift.StartPosition.Equals(playerShotPosition))
                //        {
                //            gift.RespondToHit();
                //        }
                //    }
            }

        }
        public void AddSeaToRender(Sea sea)
        {
            State[,] field = sea.GetBody() as State[,];
            fieldRows = field.GetLength(0);
            fieldCols = field.GetLength(1);

            scene.Append(string.Format("   0 1 2 3 4 5 6 7 8 9" + Environment.NewLine));
            scene.Append(string.Format("   -------------------" + Environment.NewLine));

            for (int row = 0; row < fieldRows; row++)
            {
                scene.Append(string.Format("{0} |", row));
                for (int col = 0; col < fieldCols; col++)
                {
                    if (sea is PlayerSea)
                    {
                        if (field[row, col] == State.Empty)
                        {
                            scene.Append((char)State.Empty);
                            scene.Append(" ");
                        }
                        else if (field[row, col] == State.MissedHit)
                        {
                            scene.Append((char)State.MissedHit);
                            scene.Append(" ");
                        }
                        else if (field[row, col] == State.Ship)
                        {
                            scene.Append((char)State.Ship);
                            scene.Append(" ");
                        }
                        else if (field[row, col] == State.TargetHit)
                        {
                            scene.Append((char)State.TargetHit);
                            scene.Append(" ");
                        }
                        else
                        {
                            throw new ArgumentException("Invalid State in Sea matrix.");
                        }
                    }
                    else if (sea is BootSea)
                    {
                        if (field[row, col] == State.Empty || field[row, col] == State.Ship)
                        {
                            scene.Append((char)State.Empty);
                            scene.Append(" ");
                        }
                        else if (field[row, col] == State.MissedHit)
                        {
                            scene.Append((char)State.MissedHit);
                            scene.Append(" ");
                        }
                        else if (field[row, col] == State.TargetHit)
                        {
                            scene.Append((char)State.TargetHit);
                            scene.Append(" ");
                        }
                        else
                        {
                            throw new ArgumentException("Invalid State in Sea matrix.");
                        }
                    }
                }

                scene.Append(Environment.NewLine);
            }
        }
            public Position GetNextPosition(Sea sea)
            {
                Position robotShotPos = new Position();
          

                if(sea[lastHitPosition.X, lastHitPosition.Y] != State.TargetHit && 
                    targetHit == false)
                {
                    robotShotPos = GetRandomPos(sea);
                   

                    lastHitPosition = robotShotPos; 
                    return robotShotPos;
                }
                    
                else if (sea[lastHitPosition.X , lastHitPosition.Y] == State.TargetHit)
                {
                    targetHit = true;
                    robotShotPos.X = lastHitPosition.X + addition;        
                    robotShotPos.Y = lastHitPosition.Y;

                    if (robotShotPos.X > 9 || robotShotPos.X < 0
                         || sea[robotShotPos.X, robotShotPos.Y] == State.TargetHit)
                    {
                        robotShotPos = GetRandomPos(sea);
                        targetHit = false;
                        count = 4;
                        lastHitPosition = robotShotPos;

                        return robotShotPos;
                    }


                    count--;
                    lastHitPosition = robotShotPos;

                    return robotShotPos;
                }   
   
                else if (targetHit == true && count > 3)
                {
                    robotShotPos.Y = lastHitPosition.Y;
                    robotShotPos.X = lastHitPosition.X + addition;

                    if (robotShotPos.X > 9 || robotShotPos.X < 0
                        || sea[robotShotPos.X, robotShotPos.Y] == State.TargetHit)
                    {
                        robotShotPos = GetRandomPos(sea);
                        targetHit = false;
                        count = 4;
                        lastHitPosition = robotShotPos;

                        return robotShotPos;
                    }

                    
                    count--;

                    lastHitPosition = robotShotPos;

                    return robotShotPos;
                   
                }

                else if (targetHit == true && count <=3 )
                {
                    robotShotPos.X = lastHitPosition.X ;
                    robotShotPos.Y = lastHitPosition.Y + addition;
                    if (robotShotPos.Y > 9 || robotShotPos.Y < 0
                        || sea[robotShotPos.X, robotShotPos.Y] == State.TargetHit)
                    {
                       robotShotPos = GetRandomPos(sea);
                       targetHit = false;
                       count = 4;
                       lastHitPosition = robotShotPos;

                       return robotShotPos;
                    }

                  
                    count--;
                    addition *= -1;

                    if (count==0)
                    {
                        count = 4;
                        targetHit = false;
                    }

                    lastHitPosition = robotShotPos;
                    return robotShotPos;
                }
                else
                {
                    robotShotPos = GetRandomPos(sea);
                    lastHitPosition = robotShotPos;

                    return robotShotPos;
                }

               
                
            }
            private Position GetRandomPos(Sea sea)
            {
                
                    Position robotShotPos = new Position();
                    
                    robotShotPos.X = generator.Next(0, 9);
                    robotShotPos.Y = generator.Next(0, 9);


                    while (sea[robotShotPos.X, robotShotPos.Y] == State.MissedHit
                        || sea[robotShotPos.X, robotShotPos.Y] == State.TargetHit)
                    {
                        robotShotPos.X = generator.Next(0, 9);
                        robotShotPos.Y = generator.Next(0, 9);
                    }

            
                    return robotShotPos;
                }
예제 #8
0
        public Position GetNextPosition(Sea sea)
        {
            Position robotShotPos = new Position();


            if (sea[lastHitPosition.X, lastHitPosition.Y] != State.TargetHit &&
                targetHit == false)
            {
                robotShotPos = GetRandomPos(sea);


                lastHitPosition = robotShotPos;
                return(robotShotPos);
            }

            else if (sea[lastHitPosition.X, lastHitPosition.Y] == State.TargetHit)
            {
                targetHit      = true;
                robotShotPos.X = lastHitPosition.X + addition;
                robotShotPos.Y = lastHitPosition.Y;

                if (robotShotPos.X > 9 || robotShotPos.X < 0 ||
                    sea[robotShotPos.X, robotShotPos.Y] == State.TargetHit)
                {
                    robotShotPos    = GetRandomPos(sea);
                    targetHit       = false;
                    count           = 4;
                    lastHitPosition = robotShotPos;

                    return(robotShotPos);
                }


                count--;
                lastHitPosition = robotShotPos;

                return(robotShotPos);
            }

            else if (targetHit == true && count > 3)
            {
                robotShotPos.Y = lastHitPosition.Y;
                robotShotPos.X = lastHitPosition.X + addition;

                if (robotShotPos.X > 9 || robotShotPos.X < 0 ||
                    sea[robotShotPos.X, robotShotPos.Y] == State.TargetHit)
                {
                    robotShotPos    = GetRandomPos(sea);
                    targetHit       = false;
                    count           = 4;
                    lastHitPosition = robotShotPos;

                    return(robotShotPos);
                }


                count--;

                lastHitPosition = robotShotPos;

                return(robotShotPos);
            }

            else if (targetHit == true && count <= 3)
            {
                robotShotPos.X = lastHitPosition.X;
                robotShotPos.Y = lastHitPosition.Y + addition;
                if (robotShotPos.Y > 9 || robotShotPos.Y < 0 ||
                    sea[robotShotPos.X, robotShotPos.Y] == State.TargetHit)
                {
                    robotShotPos    = GetRandomPos(sea);
                    targetHit       = false;
                    count           = 4;
                    lastHitPosition = robotShotPos;

                    return(robotShotPos);
                }


                count--;
                addition *= -1;

                if (count == 0)
                {
                    count     = 4;
                    targetHit = false;
                }

                lastHitPosition = robotShotPos;
                return(robotShotPos);
            }
            else
            {
                robotShotPos    = GetRandomPos(sea);
                lastHitPosition = robotShotPos;

                return(robotShotPos);
            }
        }