private bool fitsVertically(Square square, int length, int displacement)
        {
            int squaresBelow = displacement;
            int squaresAbove = length - displacement - 1;

            if (!square.hasDown(squaresBelow))
            {
                return(false);
            }
            if (!square.hasUp(squaresAbove))
            {
                return(false);
            }
            for (int i = 0; i <= squaresBelow; i++)
            {
                if (state(square.down(i)) != SquareState.Unknown)
                {
                    return(false);
                }
            }
            for (int i = 1; i <= squaresAbove; i++)
            {
                if (state(square.up(i)) != SquareState.Unknown)
                {
                    return(false);
                }
            }
            return(true);
        }
        internal List <Square> getContiguousHits(Square sq)
        {
            bool horizontal = false;

            if (sq.hasLeft() && (state(sq.left()) == SquareState.Hit))
            {
                horizontal = true;
            }
            if (sq.hasRight() && (state(sq.right()) == SquareState.Hit))
            {
                horizontal = true;
            }

            List <Square> answer = new List <Square>();

            if (horizontal)
            {
                Square current = sq;
                while (current.hasLeft() && state(current.left()) == SquareState.Hit)
                {
                    current = current.left();
                }

                answer.Add(current);
                while (current.hasRight() && state(current.right()) == SquareState.Hit)
                {
                    current = current.right();
                    answer.Add(current);
                }
            }
            else
            {
                Square current = sq;
                while (current.hasDown() && state(current.down()) == SquareState.Hit)
                {
                    current = current.down();
                }

                answer.Add(current);
                while (current.hasUp() && state(current.up()) == SquareState.Hit)
                {
                    current = current.up();
                    answer.Add(current);
                }
            }
            return(answer);
        }
 private void addMissesAboveAndBelow(Square sq)
 {
     if (sq.hasUp())
     {
         addMissIfNotHit(sq.up());
     }
     if (sq.hasDown())
     {
         addMissIfNotHit(sq.down());
     }
 }
示例#4
0
        private bool isSunk(OpponentsBoard board, List <Square> sinkingShip)
        {
            if (sinkingShip.Count < 2)
            {
                return(false);
            }
            if (sinkingShip.Count == board.maxRemaining())
            {
                return(true);
            }

            Square first = sinkingShip[0];
            Square last  = sinkingShip[sinkingShip.Count - 1];

            bool horizontal = first.y == last.y;

            // logger(horizontal ? "Horizontal" : "Vertical");


            if (horizontal)
            {
                bool leftOk  = !first.hasLeft() || board.state(first.left()) == SquareState.Miss;
                bool rightOk = !last.hasRight() || board.state(last.right()) == SquareState.Miss;
                //logger(leftOk ? "Left is done" : "Could still be left");
                //logger(rightOk ? "Right is done" : "Could still be right");
                return(leftOk && rightOk);
            }
            else
            {
                bool downOk = !first.hasDown() || board.state(first.down()) == SquareState.Miss;
                bool upOk   = !last.hasUp() || board.state(last.up()) == SquareState.Miss;
                //logger(downOk ? "Down is done" : "Could still be down");
                //logger(upOk ? "Up is done" : "Could still be up");
                return(downOk && upOk);
            }
        }