Ejemplo n.º 1
0
 public bool validPlacement(Tuple<int, int> start, Tuple<int, int> end, Board p)
 {
     if (start.Item1 < 10 && start.Item1 >= 0 && start.Item2 >= 0 && start.Item2 < 10 && end.Item1 < 10 && end.Item1 >= 0 && end.Item2 < 10 && end.Item1 >= 0)
     {
         if ((start.Item1 == end.Item1) && (start.Item2 != end.Item2) || (start.Item1 != end.Item1) && (start.Item2 == end.Item2))
         {
             for (int y = start.Item1; y <= end.Item1; y++)
             {
                 for (int x = start.Item2; x <= end.Item2; x++)
                 {
                     if (p.isShip(y, x))
                     {
                         return false;
                     }
                 }
             }
             if (start.Item1 == end.Item1)
             {
                 //Check above ship
                 if (start.Item2 > 0)
                 {
                     if (p.isShip(start.Item1, start.Item2 - 1))
                     {
                         return false;
                     }
                 }
                 //Check below ship
                 if (start.Item2 < 9)
                 {
                     if (p.isShip(end.Item1, end.Item2 + 1))
                     {
                         return false;
                     }
                 }
                 //Check at either side of ship
                 for (int i = start.Item2; i <= end.Item2; ++i)
                 {
                     if (start.Item1 < 9)
                     {
                         if (p.isShip(start.Item1 + 1, i))
                         {
                             return false;
                         }
                     }
                     if (start.Item1 > 0)
                     {
                         if (p.isShip(start.Item1 - 1, i))
                         {
                             return false;
                         }
                     }
                 }
             }
             else if (start.Item2 == end.Item2)
             {
                 //Check left of ship
                 if (start.Item1 > 0)
                 {
                     if (p.isShip(start.Item1 - 1, start.Item2))
                     {
                         return false;
                     }
                 }
                 //Check right of ship
                 if (start.Item1 < 9)
                 {
                     if (p.isShip(end.Item1 + 1, end.Item2))
                     {
                         return false;
                     }
                 }
                 //Check above and below ship
                 for (int i = start.Item1; i <= end.Item1; ++i)
                 {
                     if (start.Item2 < 9)
                     {
                         //Console.WriteLine("Checking below ship at ["+i+", "+end.Item2+"]");
                         if (p.isShip(i, start.Item2 + 1))
                         {
                             return false;
                         }
                     }
                     if (start.Item2 > 0)
                     {
                         if (p.isShip(i, start.Item2 - 1))
                         {
                             return false;
                         }
                     }
                 }
             }
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 2
0
        public void playTurn(Board targetBoard)
        {
            Console.WriteLine("Playing AI turn");
            Tuple<int, int> target;
            do
            {
                target = chooseFireCoords();
                Console.WriteLine("AI-Target: " + target);
            } while (!targetBoard.fire(target.Item1, target.Item2));

            if (targetBoard.isShip(target.Item1, target.Item2))
            {
                updateKnownBoard(target.Item1, target.Item2, 2);    //Hit a ship

                if (target.Item1 < 9)
                {
                    if (knownBoard[target.Item2][target.Item1 + 1] == 2)
                    {
                        Tuple<int, int> start = new Tuple<int, int>(target.Item1, target.Item2);
                        Tuple<int, int> end = new Tuple<int, int>(target.Item1 + 1, target.Item2);
                        damagedShipAt(start, end);
                    }
                }
                if (target.Item1 > 0)
                {
                    if (knownBoard[target.Item2][target.Item1 - 1] == 2)
                    {
                        Tuple<int, int> end = new Tuple<int, int>(target.Item1, target.Item2);
                        Tuple<int, int> start = new Tuple<int, int>(target.Item1 - 1, target.Item2);
                        damagedShipAt(start, end);
                    }
                }
                if (target.Item2 < 9)
                {
                    if (knownBoard[target.Item2 + 1][target.Item1] == 2)
                    {
                        Tuple<int, int> start = new Tuple<int, int>(target.Item1, target.Item2);
                        Tuple<int, int> end = new Tuple<int, int>(target.Item1, target.Item2 + 1);
                        damagedShipAt(start, end);

                    }
                }
                if (target.Item2 > 0)
                {
                    if (knownBoard[target.Item2 - 1][target.Item1] == 2)
                    {
                        Tuple<int, int> end = new Tuple<int, int>(target.Item1, target.Item2);
                        Tuple<int, int> start = new Tuple<int, int>(target.Item1, target.Item2 - 1);
                        damagedShipAt(start, end);
                    }
                }
                updateKnownBoard(target.Item1 + 1, target.Item2, 3);  //Update the surrounding nodes
                updateKnownBoard(target.Item1 - 1, target.Item2, 3);
                updateKnownBoard(target.Item1, target.Item2 + 1, 3);
                updateKnownBoard(target.Item1, target.Item2 - 1, 3);

                if (targetBoard.getShip(target.Item1, target.Item2).getDead())
                {
                    deadShipAt(targetBoard.getShip(target.Item1, target.Item2).getStart(), targetBoard.getShip(target.Item1, target.Item2).getEnd());
                    updateKnownBoard(target.Item1, target.Item2, 4);
                }

            }
            else
            {
                updateKnownBoard(target.Item1, target.Item2, 1);
            }
        }