Beispiel #1
0
        private GridSquare TryGridDirection(HitTracker currentlyTracked, List <GridSquare> availableGuesses)
        {
            var nameArray = currentlyTracked.gridSquare._rectangle.Name.Split('_');
            var xcoord    = Int32.Parse(nameArray[1]);
            var ycoord    = Int32.Parse(nameArray[2]);

            //find adjacent gridsquares
            var left  = availableGuesses.Where(x => x._rectangle.Name == $"fleet_{xcoord - 1}_{ycoord}");
            var right = availableGuesses.Where(x => x._rectangle.Name == $"fleet_{xcoord + 1}_{ycoord}");
            var up    = availableGuesses.Where(x => x._rectangle.Name == $"fleet_{xcoord}_{ycoord - 1}");
            var down  = availableGuesses.Where(x => x._rectangle.Name == $"fleet_{xcoord}_{ycoord + 1}");

            //first pass
            if (currentlyTracked.direction == HitTracker.TrackingDirection.Neutral)
            {
                //try new directions if the last try didnt succeed
                if (left.Any() && !currentlyTracked.hasTriedLeft)
                {
                    var retval = left.First();
                    currentlyTracked.direction    = HitTracker.TrackingDirection.Left;
                    currentlyTracked.hasTriedLeft = true;
                    return(retval);
                }
                if (up.Any() && !currentlyTracked.hasTriedUp)
                {
                    var retval = up.First();
                    currentlyTracked.direction  = HitTracker.TrackingDirection.Up;
                    currentlyTracked.hasTriedUp = true;
                    return(retval);
                }
                if (right.Any() && !currentlyTracked.hasTriedRight)
                {
                    var retval = right.First();
                    currentlyTracked.direction     = HitTracker.TrackingDirection.Right;
                    currentlyTracked.hasTriedRight = true;
                    return(retval);
                }
                if (down.Any() && !currentlyTracked.hasTriedDown)
                {
                    var retval = down.First();
                    currentlyTracked.direction    = HitTracker.TrackingDirection.Down;
                    currentlyTracked.hasTriedDown = true;
                    return(retval);
                }
            }
            else
            {
                //is currently tracking a direction

                //if the last try was successful keep tracking that direction
                if (left.Any() && currentlyTracked.direction == HitTracker.TrackingDirection.Left && currentlyTracked.lastTryWasSuccessful)
                {
                    return(left.First());
                }
                if (up.Any() && currentlyTracked.direction == HitTracker.TrackingDirection.Up && currentlyTracked.lastTryWasSuccessful)
                {
                    return(up.First());
                }
                if (right.Any() && currentlyTracked.direction == HitTracker.TrackingDirection.Right && currentlyTracked.lastTryWasSuccessful)
                {
                    return(right.First());
                }
                if (down.Any() && currentlyTracked.direction == HitTracker.TrackingDirection.Down && currentlyTracked.lastTryWasSuccessful)
                {
                    return(down.First());
                }

                //if the last try wasn't successful guess in a clockwise direction
                if (up.Any() && currentlyTracked.direction == HitTracker.TrackingDirection.Left && !currentlyTracked.lastTryWasSuccessful)
                {
                    currentlyTracked.direction = HitTracker.TrackingDirection.Up;
                    return(up.First());
                }
                if (right.Any() && currentlyTracked.direction == HitTracker.TrackingDirection.Up && !currentlyTracked.lastTryWasSuccessful)
                {
                    currentlyTracked.direction = HitTracker.TrackingDirection.Right;
                    return(right.First());
                }
                if (down.Any() && currentlyTracked.direction == HitTracker.TrackingDirection.Right && !currentlyTracked.lastTryWasSuccessful)
                {
                    currentlyTracked.direction = HitTracker.TrackingDirection.Down;
                    return(down.First());
                }
                if (left.Any() && currentlyTracked.direction == HitTracker.TrackingDirection.Down && !currentlyTracked.lastTryWasSuccessful)
                {
                    currentlyTracked.direction = HitTracker.TrackingDirection.Left;
                    return(left.First());
                }
            }

            //if we fell all the way through just make a new random guess
            return(ResetTrackingState(availableGuesses));
        }
Beispiel #2
0
        public Tuple <bool, bool, string, bool> CheckFireResult(Player player, int xcoord, int ycoord)
        {
            //Tuple definition:
            //bool was a hit
            //bool was a ship sunk
            //string the ship name that was sunk
            //victory condtion was reached

            var c = new Coordinate(xcoord, ycoord, new HitState());

            foreach (Ship s in player.Fleet)
            {
                if (s.CoordinatesList.Where(x => x.X == c.X && x.Y == c.Y).Any())
                {
                    s.CoordinatesList.Where(x => x.X == c.X && x.Y == c.Y).First().hitState.WasHit = HitState.HitValue.Hit;

                    //player 1s ship was hit
                    if (player.PlayerName == "Player 1")
                    {
                        var gridSquare = GridSquares.Where(x => x._rectangle.Name == $"fleet_{c.X}_{c.Y}").Single();
                        gridSquare.WasHit = HitState.HitValue.Hit;
                        IsTrackingTarget  = true;
                        var hitTracker = new HitTracker(gridSquare);
                        TrackingStack.Push(hitTracker);
                    }

                    var wasSunk = WasShipSunk(s);
                    if (wasSunk)
                    {
                        //player 1's ship was sunk
                        if (player.PlayerName == "Player 1")
                        {
                            IsTrackingTarget = false;
                            TrackingStack    = new Stack <HitTracker>();
                        }
                        bool victoryState = CheckVictoryState(player);
                        return(Tuple.Create(true, true, s.ShipName, victoryState));
                    }
                    else
                    {
                        //was hit but did not sink
                        if (player.PlayerName == "Player 1" && IsTrackingTarget)
                        {
                            if (TrackingStack.Any())
                            {
                                var stackTop = TrackingStack.Peek();
                                stackTop.lastTryWasSuccessful = true;
                            }
                        }
                        return(Tuple.Create(true, false, String.Empty, false));
                    }
                }
            }

            //was a miss
            if (TrackingStack.Any())
            {
                var topOfStack = TrackingStack.Peek();
                topOfStack.lastTryWasSuccessful = false;
            }

            return(Tuple.Create(false, false, String.Empty, false));
        }