Esempio n. 1
0
        private List <Square> validSquares(OpponentsBoard board)
        {
            List <Square> empties =
                board.getSquares(SquareState.Unknown)
                .Where(good)
                .ToList();

            empties.Shuffle();
            return(empties);
        }
Esempio n. 2
0
        public void HandleShotResult(IGridSquare square, bool wasHit)
        {
            Console.Write("(" + this.lastShot.x + "," + this.lastShot.y + ")" + ":" + wasHit);
            Console.WriteLine();
            board.print();

            if (wasHit)
            {
                hits++;
                board.processHit(this.lastShot);
                List <Square> sinkingShip = board.getContiguousHits(this.lastShot);
                bool          sunk        = isSunk(board, sinkingShip);
                int           size        = sinkingShip.Count;
                if (sunk)
                {
                    board.addMissesAboveAndBelow(sinkingShip);
                    board.addMissesToLeftAndRight(sinkingShip);
                    board.addMissesDiagonally(sinkingShip);
                    board.sunk(size);
                }
                else if (size >= 2)
                {
                    if (sinkingShip[0].y == sinkingShip[1].y)
                    {
                        board.addMissesAboveAndBelow(sinkingShip);
                        board.addMissesDiagonally(sinkingShip);
                    }
                    else
                    {
                        board.addMissesToLeftAndRight(sinkingShip);
                        board.addMissesDiagonally(sinkingShip);
                    }
                }

                if (hits == 17)
                {
                    // We win!
                    foreach (Square sq in board.getSquares(SquareState.Unknown))
                    {
                        board.addMissIfNotHit(sq);
                    }
                }
            }
            else
            {
                Square sq = this.lastShot;
                board.addMissIfNotHit(sq);
                foreach (Square nbr in OpponentsBoard.neighbours(sq))
                {
                    if (board.state(nbr) == SquareState.Hit)
                    {
                        List <Square> sinkingShip = board.getContiguousHits(nbr);
                        if (isSunk(board, sinkingShip))
                        {
                            board.sunk(sinkingShip.Count);
                            board.addMissesAboveAndBelow(sinkingShip);
                            board.addMissesToLeftAndRight(sinkingShip);
                            board.addMissesDiagonally(sinkingShip);
                        }
                    }
                }
            }
            hits++;;
            hits--;
        }