Пример #1
0
        public void ShotResult(ShotResult shotResult)
        {
            int  x      = shotResult.getCoordinates().X;
            int  y      = shotResult.getCoordinates().Y;
            bool result = shotResult.isHit();

            if (result)
            {
                ships[x, y].State = Ship.ShipState.HIT;
            }
            else
            {
                ships[x, y].State = Ship.ShipState.MISSED;
            }
        }
Пример #2
0
        public static String ToXMLString(ShotResult shotResult)
        {
            String result = "<shotResult isNull=\"";
            if (shotResult == null)
                result += "true\"";
            else
            {
                result += "false\"";
                result += " isHit=\"" + (shotResult.isHit() ? "true" : "false") + "\"";
                result += " isSunk=\"" + (shotResult.isSunk() ? "true" : "false") + "\"";
                result += " isGameEnded=\"" + (shotResult.isGameEnded() ? "true" : "false") + "\"";
                result += " matrix=\"";
                result += ((shotResult.getMatrix() == null) ? "null" : Global.boolArrayToString(shotResult.getMatrix())) + "\"";
            }

            result += ">" + Coordinates.ToXMLString(shotResult.getCoordinates()) + "</shotResult>";
            return result;
        }
Пример #3
0
        public static String ToXMLString(ShotResult shotResult)
        {
            String result = "<shotResult isNull=\"";

            if (shotResult == null)
            {
                result += "true\"";
            }
            else
            {
                result += "false\"";
                result += " isHit=\"" + (shotResult.isHit() ? "true" : "false") + "\"";
                result += " isSunk=\"" + (shotResult.isSunk() ? "true" : "false") + "\"";
                result += " isGameEnded=\"" + (shotResult.isGameEnded() ? "true" : "false") + "\"";
                result += " matrix=\"";
                result += ((shotResult.getMatrix() == null) ? "null" : Global.boolArrayToString(shotResult.getMatrix())) + "\"";
            }

            result += ">" + Coordinates.ToXMLString(shotResult.getCoordinates()) + "</shotResult>";
            return(result);
        }
Пример #4
0
        private void ReceivingThreadStart()
        {
            System.Diagnostics.Debug.WriteLine("ReceivingThread started");
            GamePacket packet;

            gameIsEnded = false;
            while (!gameIsEnded)
            {
                Thread.Sleep(250);

                packet = wifiService.receive();
                if (packet != null)
                {
                    System.Diagnostics.Debug.WriteLine(packet.GetType() + " packt received:" + System.Environment.NewLine + GamePacketSerialization.serialize(packet));

                    if (packet.Type == GamePacket.TYPE.WHO_STARTS)
                    {
                        meStartFirst = packet.getWhoStarts() == Global.CLIENT_FIRST;
                        BeginInvoke(new MethodInvoker(delegate() { mainBoard.setShootable(meStartFirst); }));
                    }
                    else if (packet.Type == GamePacket.TYPE.USER_NAME)
                    {
                    }
                    else if (packet.Type == GamePacket.TYPE.SHOT)
                    {
                        BeginInvoke(new MethodInvoker(delegate()
                        {
                            ShotResult result = prevBoard.Shoot(packet.getCoordinates());
                            if (prevBoard.isGameEnded())
                            {
                                wifiService.send(new GamePacket(new GameResult(Global.GAME_RESULT_WINNER)));    // opponent is winner because he sunk all of my ships
                                EndGame(false);
                            }
                            else
                            {
                                wifiService.send(new GamePacket(result));
                                if (!result.isHit() || result.isSunk())
                                {
                                    BeginInvoke(new MethodInvoker(delegate() { mainBoard.setShootable(true); }));
                                }
                            }
                        }));
                    }
                    else if (packet.Type == GamePacket.TYPE.RESULT)
                    {
                        bool result = packet.getShotResult().isHit();
                        BeginInvoke(new MethodInvoker(delegate()
                        {
                            mainBoard.ShotResult(packet.getShotResult());
                            mainBoard.setShootable(result && !packet.getShotResult().isSunk());
                        }));

                        currentTarget = null;
                        if (packet.getShotResult().isSunk())
                        {
                            BeginInvoke(new MethodInvoker(delegate() { mainBoard.setShipSunk(packet.getShotResult().getMatrix()); }));
                        }
                    }
                    else if (packet.Type == GamePacket.TYPE.GAME_RESULT)
                    {
                        BeginInvoke(new MethodInvoker(delegate() { EndGame(packet.getGameResult().isWinner()); }));
                    }
                }
            }
        }
Пример #5
0
        public void ShotResult(ShotResult shotResult)
        {
            int x = shotResult.getCoordinates().X;
            int y = shotResult.getCoordinates().Y;
            bool result = shotResult.isHit();

            if (result)
                ships[x, y].State = Ship.ShipState.HIT;
            else
                ships[x, y].State = Ship.ShipState.MISSED;
        }