private void StartGame(object sender, EventArgs e)
        {
            if (Global.GameMode == GAME_MODE.HOST)
                wifiService = new HostWiFiService();
            else
                wifiService = new ClientWiFiService();

            ConnectDialog dialog = new ConnectDialog(wifiService);
            dialog.ShowDialog();

            if (!wifiService.isConnected())
                return;

            this.Controls.Remove(randomButton);
            this.Controls.Remove(backButton);
            this.Controls.Remove(nextButton);

            if (endGameButton == null)
            {
                endGameButton = new ImageButton("End game");
                endGameButton.Click += delegate(object o, EventArgs ev) { EndGame(false); };
                int widthSpaceForEndButton = ClientSize.Width - prevBoard.Margin - prevBoard.Width;
                endGameButton.Location = new System.Drawing.Point(prevBoard.Margin + prevBoard.Width + widthSpaceForEndButton / 2 - endGameButton.Width / 2,
                                                                  prevBoard.Margin + prevBoard.Height / 2 - endGameButton.Height / 2);
            }

            this.Controls.Add(endGameButton);
            bool[,] matrix = mainBoard.getShips();
            mainBoard.setShips(GameBoard.empty);
            prevBoard.setShips(matrix);
            prevBoard.Visible = true;

            prevBoard.Enabled = false;
            mainBoard.Enabled = false;
            mainBoard.Click += onShipClick;   // add action to each Ship in mainBoard
            mainBoard.DoubleClick += onShipClick;

            if (Global.GameMode == GAME_MODE.HOST)
            {
                bool whoStarts = (new Random()).Next() % 2 == 0;
                meStartFirst = whoStarts == Global.HOST_FIRST;
                GamePacket packet = new GamePacket(whoStarts);
                wifiService.send(packet);

                mainBoard.setShootable(meStartFirst);
            }
            else
                mainBoard.setShootable(false);

            this.FormClosing += delegate(object s, FormClosingEventArgs ev) { gameIsEnded = true; };

            Thread ReceivingThread = new Thread(new ThreadStart(ReceivingThreadStart));
            ReceivingThread.Name = "Main game loop ReceivingThread";
            ReceivingThread.Start();
        }
Beispiel #2
0
        private void StartGame(object sender, EventArgs e)
        {
            if (Global.GameMode == GAME_MODE.HOST)
            {
                wifiService = new HostWiFiService();
            }
            else
            {
                wifiService = new ClientWiFiService();
            }

            ConnectDialog dialog = new ConnectDialog(wifiService);

            dialog.ShowDialog();

            if (!wifiService.isConnected())
            {
                return;
            }

            this.Controls.Remove(randomButton);
            this.Controls.Remove(backButton);
            this.Controls.Remove(nextButton);

            if (endGameButton == null)
            {
                endGameButton        = new ImageButton("End game");
                endGameButton.Click += delegate(object o, EventArgs ev) { EndGame(false); };
                int widthSpaceForEndButton = ClientSize.Width - prevBoard.Margin - prevBoard.Width;
                endGameButton.Location = new System.Drawing.Point(prevBoard.Margin + prevBoard.Width + widthSpaceForEndButton / 2 - endGameButton.Width / 2,
                                                                  prevBoard.Margin + prevBoard.Height / 2 - endGameButton.Height / 2);
            }

            this.Controls.Add(endGameButton);
            bool[,] matrix = mainBoard.getShips();
            mainBoard.setShips(GameBoard.empty);
            prevBoard.setShips(matrix);
            prevBoard.Visible = true;

            prevBoard.Enabled      = false;
            mainBoard.Enabled      = false;
            mainBoard.Click       += onShipClick; // add action to each Ship in mainBoard
            mainBoard.DoubleClick += onShipClick;

            if (Global.GameMode == GAME_MODE.HOST)
            {
                bool whoStarts = (new Random()).Next() % 2 == 0;
                meStartFirst = whoStarts == Global.HOST_FIRST;
                GamePacket packet = new GamePacket(whoStarts);
                wifiService.send(packet);

                mainBoard.setShootable(meStartFirst);
            }
            else
            {
                mainBoard.setShootable(false);
            }

            this.FormClosing += delegate(object s, FormClosingEventArgs ev) { gameIsEnded = true; };

            Thread ReceivingThread = new Thread(new ThreadStart(ReceivingThreadStart));

            ReceivingThread.Name = "Main game loop ReceivingThread";
            ReceivingThread.Start();
        }