Exemplo n.º 1
0
        public void GetTurn(int x, int y)
        {
            if (_game != null)
            {
                if (_game.field.GetCell(x, y) == Cell.Empty)
                {
                    _game.field.SetCell(x, y, Cell.Missed);
                    try
                    {
                        _callback.UpdateYourField(x, y, Cell.Missed);
                    }
                    catch (Exception) { }
                    try
                    {
                        _game.Opponent.UpdateOpponentField(x, y, Cell.Missed);
                    }
                    catch (Exception) { }
                    YouTurn();
                    return;
                }
                if (_game.field.GetCell(x, y) == Cell.DeadShip || _game.field.GetCell(x, y) == Cell.Fire || _game.field.GetCell(x, y) == Cell.Missed)
                {
                    _game.Opponent.AlreadyClicked();
                    _game.Opponent.YouTurn();
                    return;
                }
                List<Point> list = new List<Point>();
                list.Add(new Point(x, y));
                _game.field.SetCell(x, y, Cell.Fire);
                bool killed = true;
                for (int i = 0; i < list.Count; i++)
                {
                    int lx = list[i].x;
                    int ly = list[i].y;
                    if (lx > 0)
                    {
                        if (_game.field.GetCell(lx-1, ly) == Cell.Ship)
                        {
                            killed = false;
                            break;
                        }
                        if (_game.field.GetCell(lx-1, ly) == Cell.Fire && !ListContained(list, lx-1, ly))
                        {
                            list.Add(new Point(lx - 1, ly));
                        }
                    }
                    if (lx < 9)
                    {
                        if (_game.field.GetCell(lx + 1, ly) == Cell.Ship)
                        {
                            killed = false;
                            break;
                        }
                        if (_game.field.GetCell(lx + 1, ly) == Cell.Fire && !ListContained(list, lx + 1, ly))
                        {
                            list.Add(new Point(lx + 1, ly));
                        }
                    }
                    if (ly > 0)
                    {
                        if (_game.field.GetCell(lx, y-1) == Cell.Ship)
                        {
                            killed = false;
                            break;
                        }
                        if (_game.field.GetCell(lx, ly-1) == Cell.Fire && !ListContained(list, lx, ly-1))
                        {
                            list.Add(new Point(lx, ly-1));
                        }
                    }
                    if (ly < 9)
                    {
                        if (_game.field.GetCell(lx, ly+1) == Cell.Ship)
                        {
                            killed = false;
                            break;
                        }
                        if (_game.field.GetCell(lx, ly+1) == Cell.Fire && !ListContained(list, lx, ly+1))
                        {
                            list.Add(new Point(lx, ly+1));
                        }
                    }
                }
                if (!killed)
                {
                    try
                    {
                        _callback.UpdateYourField(x, y, Cell.Fire);
                    }
                    catch (Exception) { }
                    try
                    {
                        _game.Opponent.UpdateOpponentField(x, y, Cell.Fire);
                    }
                    catch (Exception) { }
                    _game.Opponent.YouTurn();
                    return;
                }
                for (int i = 0; i < list.Count; i++)
                {
                    int lx = list[i].x;
                    int ly = list[i].y;
                    if (lx > 0)
                    {
                        if (_game.field.GetCell(lx - 1, ly) == Cell.Empty)
                        {
                            _game.field.SetCell(lx - 1, ly, Cell.Missed);
                            try
                            {
                                _callback.UpdateYourField(lx - 1, ly, Cell.Missed);
                            }
                            catch (Exception) { }
                            try
                            {
                                _game.Opponent.UpdateOpponentField(lx - 1, ly, Cell.Missed);
                            }
                            catch (Exception) { }
                        }
                    }
                    if (lx > 0 && ly > 0)
                    {
                        if (_game.field.GetCell(lx - 1, ly-1) == Cell.Empty)
                        {
                            _game.field.SetCell(lx - 1, ly-1, Cell.Missed);
                            try
                            {
                                _callback.UpdateYourField(lx - 1, ly-1, Cell.Missed);
                            }
                            catch (Exception) { }
                            try
                            {
                                _game.Opponent.UpdateOpponentField(lx - 1, ly-1, Cell.Missed);
                            }
                            catch (Exception) { }
                        }
                    }
                    if (ly > 0)
                    {
                        if (_game.field.GetCell(lx, ly-1) == Cell.Empty)
                        {
                            _game.field.SetCell(lx, ly-1, Cell.Missed);
                            try
                            {
                                _callback.UpdateYourField(lx, ly-1, Cell.Missed);
                            }
                            catch (Exception) { }
                            try
                            {
                                _game.Opponent.UpdateOpponentField(lx, ly-1, Cell.Missed);
                            }
                            catch (Exception) { }
                        }
                    }
                    if (ly > 0 && lx < 9)
                    {
                        if (_game.field.GetCell(lx+1, ly-1) == Cell.Empty)
                        {
                            _game.field.SetCell(lx+1, ly-1, Cell.Missed);
                            try
                            {
                                _callback.UpdateYourField(lx+1, ly-1, Cell.Missed);
                            }
                            catch (Exception) { }
                            try
                            {
                                _game.Opponent.UpdateOpponentField(lx+1, ly-1, Cell.Missed);
                            }
                            catch (Exception) { }
                        }
                    }
                    if (lx < 9)
                    {
                        if (_game.field.GetCell(lx+1, ly) == Cell.Empty)
                        {
                            _game.field.SetCell(lx+1, ly, Cell.Missed);
                            try
                            {
                                _callback.UpdateYourField(lx+1, ly, Cell.Missed);
                            }
                            catch (Exception) { }
                            try
                            {
                                _game.Opponent.UpdateOpponentField(lx+1, ly, Cell.Missed);
                            }
                            catch (Exception) { }
                        }
                    }

                    if (lx < 9 && ly < 9)
                    {
                        if (_game.field.GetCell(lx+1, ly+1) == Cell.Empty)
                        {
                            _game.field.SetCell(lx+1, ly+1, Cell.Missed);
                            try
                            {
                                _callback.UpdateYourField(lx+1, ly+1, Cell.Missed);
                            }
                            catch (Exception) { }
                            try
                            {
                                _game.Opponent.UpdateOpponentField(lx+1, ly+1, Cell.Missed);
                            }
                            catch (Exception) { }
                        }
                    }
                    if (ly < 9)
                    {
                        if (_game.field.GetCell(lx, ly+1) == Cell.Empty)
                        {
                            _game.field.SetCell(lx, ly+1, Cell.Missed);
                            try
                            {
                                _callback.UpdateYourField(lx, ly+1, Cell.Missed);
                            }
                            catch (Exception) { }
                            try
                            {
                                _game.Opponent.UpdateOpponentField(lx, ly+1, Cell.Missed);
                            }
                            catch (Exception) { }
                        }
                    }
                    if (lx > 0 && ly < 9)
                    {
                        if (_game.field.GetCell(lx-1, ly+1) == Cell.Empty)
                        {
                            _game.field.SetCell(lx-1, ly+1, Cell.Missed);
                            try
                            {
                                _callback.UpdateYourField(lx-1, ly+1, Cell.Missed);
                            }
                            catch (Exception) { }
                            try
                            {
                                _game.Opponent.UpdateOpponentField(lx-1, ly+1, Cell.Missed);
                            }
                            catch (Exception) { }
                        }
                    }
                    _game.field.SetCell(lx, ly, Cell.DeadShip);
                    try
                    {
                        _callback.UpdateYourField(lx, ly, Cell.DeadShip);
                    }
                    catch (Exception) { }
                    try
                    {
                        _game.Opponent.UpdateOpponentField(lx, ly, Cell.DeadShip);
                    }
                    catch (Exception) { }
                }
                _game.ships--;
                if (_game.ships == 0)
                {
                    try
                    {
                        _game.Opponent.Win();
                    }
                    catch (Exception) { }
                    try
                    {
                        _callback.Loose();
                    }
                    catch (Exception) { }
                    Client client = _game.Opponent;
                    _game = null;
                    client.JoinTo(this);
                    return;
                }
                _game.Opponent.YouTurn();
                return;
            }
        }
Exemplo n.º 2
0
        public Client(IClientCallback callback, string name)
        {
            this._callback = callback;
            this._name = name;
            this._GUID = Guid.NewGuid().ToString();

            callback.LogIn(this._GUID);
            foreach (var room in Client.Rooms)
            {
                callback.RoomCreated(room);
            }
            _room = null;
            _game = null;
            _ready = false;
        }
Exemplo n.º 3
0
 public void YouCheated()
 {
     _game = null;
     try
     {
         Callback.YouCheated();
     }
     catch (Exception) { };
 }
Exemplo n.º 4
0
 public Game LetsPlay(Client opponent)
 {
     if (!HaveGame)
     {
         _game = new Game(opponent);
     }
     _ready = false;
     return new Game(this);
 }
Exemplo n.º 5
0
 public void LeaveGame()
 {
     if (_game != null)
     {
         _game.Opponent.YouCheated();
     }
     _game = null;
 }
Exemplo n.º 6
0
 public void JoinTo(Client opponent)
 {
     _game = opponent.LetsPlay(this);
     _ready = false;
 }