public void PlaceAllShips(Board board)
        {
            var ships = new List<Ship>();

            ships.Add(new Ship("Patrol boat"));
            ships.Add(new Ship("Destroyer"));
            ships.Add(new Ship("Submarine"));
            ships.Add(new Ship("Battleship"));
            ships.Add(new Ship("Carrier"));

            foreach (var ship in ships)
            {
                bool shipIsNotPlaced = true;
                while (shipIsNotPlaced)
                {
                    try
                    {
                        board.Occupy(ship, Orientationgenerator.Create(), GridReferenceGenerator.Create());
                        shipIsNotPlaced = false;
                    }
                    catch (Exception)
                    {
                        shipIsNotPlaced = true;
                    }
                }
            }
        }
 public void PlaceAllShips(Board board)
 {
     foreach (var shipPlacement in GetAllShips())
     {
         board.Occupy(shipPlacement.Ship, shipPlacement.Orientation, shipPlacement.GridReference);
     }
 }
Beispiel #3
0
        public void Start()
        {
            board = new Board();
            var player = new HumanPlayer();
            var computer = new ComputerPlayer();

            board.Initialize(player, computer);

            do
            {
                gameOver = TakeMove(player, computer);

                if (!gameOver)
                {
                    gameOver = TakeMove(computer, player);
                }
                else
                {
                    Console.WriteLine("CONGRATULATIONS YOU WON!!!");
                    Console.ReadLine();
                    return;
                }
            } while (!gameOver);

            Console.WriteLine("COMPUTER HAS WON!!!");
            Console.ReadLine();
        }
Beispiel #4
0
        public GameScreen(GameEngine ge, Board p1, Board p2)
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(Form_MouseMove);
            this.MouseClick += new System.Windows.Forms.MouseEventHandler(Form_MouseClick);

            this.ge = ge;
            this.p1 = p1;
            this.p2 = p2;
            boardSize = p1.getSize();
            shipListOne = p1.getShipList();
            shipListTwo = p2.getShipList();
            turn = ge.getTurn();
            phase = ge.getPhase();

            Console.WriteLine("Turn: " + turn + "  Phase: " + phase);
        }
Beispiel #5
0
        static void Main()
        {
            Storage xmlStorage = new Storage();

            Form splashScreen = new SplashScreen();
            splashScreen.Show();
            Thread.Sleep(1000);
            splashScreen.Close();

            Menu menu = new Menu(xmlStorage.previousGame());
            menu.ShowDialog();
            string menuChoice = menu.buttonEvent;

            if (menuChoice != "EXIT")
            {
                GameEngine GE;
                if (menuChoice == "PLAYER_VS_PLAYER")
                {
                    xmlStorage.clearData();
                    xmlStorage.setPvP("Yes");
                    Board p1 = new Board(true, "Player1", xmlStorage); //Player 1 is human
                    Board p2 = new Board(true, "Player2", xmlStorage); //PLayer 2 is human
                    GE = new GameEngine(p1, p2, xmlStorage);
                    GameScreen gameScreen = new GameScreen(GE, p1, p2);
                    gameScreen.ShowDialog();
                }
                else if (menuChoice == "PLAYER_VS_PC")
                {
                    xmlStorage.clearData();
                    xmlStorage.setPvP("No");
                    Board p1 = new Board(true, "Player1", xmlStorage); //Player 1 is human
                    Board p2 = new Board(false, "Player2", xmlStorage); //PLayer 2 is PC
                    GE = new GameEngine(p1, p2, xmlStorage);
                    GameScreen gameScreen = new GameScreen(GE, p1, p2);
                    gameScreen.ShowDialog();
                }
                else if (menuChoice == "LOAD_SAVED_GAME")
                {
                    //Load from xmlStorage
                    Board p1 = new Board("Player1", xmlStorage);
                    Board p2 = new Board("Player2", xmlStorage);
                    GE = new GameEngine(p1, p2, xmlStorage);
                    GE.setTurn(xmlStorage.getTurn());
                    GE.setPhase(xmlStorage.getPhase());
                    GameScreen gameScreen = new GameScreen(GE, p1, p2);
                    gameScreen.ShowDialog();

                }
            }
        }
Beispiel #6
0
        public GameEngine(Board p1, Board p2, Storage xmlStorage)
        {
            this.p1 = p1;
            this.p2 = p2;
            this.xmlStorage = xmlStorage;

            watcher = new FileSystemWatcher();
            watcher.Path = xmlStorage.getDirectory();
            watcher.NotifyFilter = NotifyFilters.LastWrite;
            watcher.Filter = "*.xml";
            watcher.EnableRaisingEvents = false;
            watcher.Changed += (s, e) => OnChanged(p1, p2);
        }
Beispiel #7
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;
 }
Beispiel #8
0
        public bool validFire(int x, int y, Board b)
        {
            if ((x < 0 || x > 10 || y < 0 || y > 10) || (b.isHit(x, y)))
            {
                return false;
            }

            return true;
        }
Beispiel #9
0
 public bool lost(Board p)
 {
     if (p.getShipList().Count() == 0)
     {
         return true;
     }
     else
     {
         return false;
     }
 }
Beispiel #10
0
        //Place each ship
        void placeShipsPhase(Board p)
        {
            List<Ship> newShipList = new List<Ship>(p.getShipList());

            for (int i = 0; i < newShipList.Count; ++i)
            {
                if (!actionPlaceShip(p, newShipList.ElementAt(i)))
                {
                    --i;
                }
            }

            p.printBoard();
            Console.WriteLine("All ships placed, leaving phase");
            phase = 2;
        }
Beispiel #11
0
        private void OnChanged(Board p1, Board p2)
        {
            watcher.EnableRaisingEvents = false;
            DateTime lastWriteTime = File.GetLastWriteTime(xmlStorage.getPath());

            lastWriteTime = lastWriteTime.AddMilliseconds(-lastWriteTime.Millisecond);
            lastReadTime = lastReadTime.AddMilliseconds(-lastReadTime.Millisecond);

            if (lastWriteTime != lastReadTime)
            {
                Console.WriteLine("Watcher detected change in file, loading it now");
                p1.loadGame();
                p2.loadGame();

                lastReadTime = lastWriteTime;
            }

            watcher.EnableRaisingEvents = true;
        }
Beispiel #12
0
        bool actionPlaceShip(Board p, Ship s)
        {
            if (p.getIsHuman())
            {
                int x1, y1, x2, y2;
                Console.WriteLine("Enter coordinates to place a ship at, cant be longer or shorter than " + s.getSize());
                Console.Write("x: ");
                x1 = int.Parse(Console.ReadLine());
                Console.Write("y: ");
                y1 = int.Parse(Console.ReadLine());

                Console.Write("x: ");
                x2 = int.Parse(Console.ReadLine());
                Console.Write("y: ");
                y2 = int.Parse(Console.ReadLine());

                Tuple<int, int> posStart = new Tuple<int, int>(x1, y1);
                Tuple<int, int> posEnd = new Tuple<int, int>(x2, y2);
                if (rulebook.validPlacement(posStart, posEnd, p))
                {
                    s.setStartEnd(posStart, posEnd);
                    p.placeShip(posStart, posEnd, s);
                    Console.WriteLine("Ship Placed!");
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                Tuple<Tuple<int, int>, Tuple<int, int>> startEnd = p.ai.placeShip(s);
                Tuple<int, int> posStart = new Tuple<int, int>(startEnd.Item1.Item1, startEnd.Item1.Item2);
                Tuple<int, int> posEnd = new Tuple<int, int>(startEnd.Item2.Item1, startEnd.Item2.Item2);

                if (rulebook.validPlacement(posStart, posEnd, p))
                {
                    s.setStartEnd(posStart, posEnd);
                    p.placeShip(posStart, posEnd, s);
                    Console.WriteLine("Ship Placed!");
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
Beispiel #13
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);
            }
        }