Beispiel #1
0
        private PlayerShot GetRandomShot()
        {
            int        x        = -1;
            int        y        = -1;
            PlayerShot shotTemp = new PlayerShot(this.PlayerID, this.GameID, x, y, false);
            Random     random   = new Random();

            while (CheckIfMoveExist(x, y))
            {
                x = random.Next(0, 10);
                y = random.Next(0, 10);
            }

            shotTemp.XCoordinate = x;
            shotTemp.YCoordinate = y;
            return(shotTemp);
        }
Beispiel #2
0
        private PlayerShot TryNewShotNearShip()
        {
            int x, y;

            if (this.ShotsMade.Last().IsShip)
            {
                x = this.ShotsMade.Last().XCoordinate;
                y = this.ShotsMade.Last().YCoordinate;
            }
            else
            {
                x = this.shotShips.Last().Fields.First().XCoordinate;
                y = this.shotShips.Last().Fields.First().YCoordinate;
            }

            PlayerShot shotTemp = new PlayerShot(this.PlayerID, this.GameID, x, y, false);

            if (CheckIfVerticalShip() || CheckIfFirstShot())
            {
                x = x - 1;
                if (!CheckIfMoveExist(x, y) && this.ShotsMade.Last().IsShip)
                {
                    triedLeft            = true;
                    shotTemp.XCoordinate = x;
                    return(shotTemp);
                }

                if (triedLeft || !this.ShotsMade.Last().IsShip)
                {
                    x          = this.shotShips.Last().Fields.First().XCoordinate + 1;
                    triedRight = true;
                }
                else if (triedLeft || !this.ShotsMade.Last().IsShip)
                {
                    x          = this.ShotsMade.Last().XCoordinate + 1;
                    triedRight = true;
                }

                if (CheckIfMoveExist(x, y) && triedLeft && triedRight)
                {
                    this.triedVertical = true;
                }
            }

            if (this.triedVertical || !CheckIfVerticalShip())
            {
                if (this.ShotsMade.Last().IsShip)
                {
                    x = this.ShotsMade.Last().XCoordinate;
                    y = this.ShotsMade.Last().YCoordinate - 1;
                }
                else
                {
                    x = this.shotShips.Last().Fields.First().XCoordinate;
                    y = this.shotShips.Last().Fields.First().YCoordinate - 1;
                }

                if (!CheckIfMoveExist(x, y))
                {
                    shotTemp.YCoordinate = y;
                    return(shotTemp);
                }

                if (CheckIfMoveExist(x, y) || !this.ShotsMade.Last().IsShip)
                {
                    y = this.shotShips.Last().Fields.First().YCoordinate + 1;
                }
                else if (CheckIfMoveExist(x, y) || this.ShotsMade.Last().IsShip)
                {
                    y = this.ShotsMade.Last().YCoordinate + 1;
                }
            }

            shotTemp.XCoordinate = x;
            shotTemp.YCoordinate = y;

            if (CheckIfMoveExist(x, y))
            {
                shotTemp = GetRandomShot();
                this.ResetConditions();
            }

            return(shotTemp);
        }