Exemplo n.º 1
0
        private bool fitsHorizontally(Square square, int length, int displacement)
        {
            int squaresToLeft  = displacement;
            int squaresToRight = length - displacement - 1;

            if (!square.hasLeft(squaresToLeft))
            {
                return(false);
            }
            if (!square.hasRight(squaresToRight))
            {
                return(false);
            }
            for (int i = 0; i <= squaresToLeft; i++)
            {
                if (state(square.left(i)) != SquareState.Unknown)
                {
                    return(false);
                }
            }
            for (int i = 1; i <= squaresToRight; i++)
            {
                if (state(square.right(i)) != SquareState.Unknown)
                {
                    return(false);
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 private void addMissesToLeftAndRight(Square sq)
 {
     if (sq.hasRight())
     {
         addMissIfNotHit(sq.right());
     }
     if (sq.hasLeft())
     {
         addMissIfNotHit(sq.left());
     }
 }
Exemplo n.º 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);
            }
        }