Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ChiamataLibrary.GameData"/> class.
        /// </summary>
        /// <param name="time">The date on which the game was played.</param>
        /// <param name="cards">The matrix of <see cref="ChiamataLibrary.Card"/> of the game.</param>
        /// <param name="players">The array of <see cref="ChiamataLibrary.Player"/>.</param>
        /// <param name="bids">The list of bids.</param>
        /// <param name="type">The type of game.</param>
        /// <param name="calledCard">The called <see cref="ChiamataLibrary.Card"/>.</param>
        /// <param name="winningPoint">The points that the team composed of CHIAMANTE and SOCIO had to do in order to win the game.</param>
        internal GameData(DateTime time, Card [,] cards, Player [] players, List <BidBase> bids, EnGameType type, Card calledCard, int winningPoint)
        {
            this.time = time;

            for (int i = 0; i < Board.Instance.nSemi; ++i)
            {
                for (int j = 0; j < Board.Instance.nNumber; ++j)
                {
                    this._cards [i, j] = cards [i, j];
                }
            }

            for (int i = 0; i < Board.PLAYER_NUMBER; ++i)
            {
                this._players [i] = players [i];
            }

            this._bids = new List <BidBase> (bids);

            this.gameType     = type;
            this.calledCard   = calledCard;
            this.winningPoint = winningPoint;
        }
Пример #2
0
        /// <summary>
        /// Updates the game. must be continuously called in order to continue with the game. Stopping would mean a pause in the game.
        /// </summary>
        public void Update()
        {
            if (IsAuctionPhase)                 //auction
            {
                BidBase bid = _activeAuctionPlayer.InvokeChooseBid();

                if (bid != null)
                {
                    //place a bid
                    _listBid.Add(bid);

                    if (bid is NotPassBidBase)
                    {
                        _currentWinningBid = (NotPassBidBase)bid;
                    }

                    List <Player> pass = new List <Player> ();

                    foreach (BidBase b in _listBid)
                    {
                        if (b is PassBid)
                        {
                            pass.Add(b.bidder);
                        }
                    }

                    //find the next bidder
                    _activeAuctionPlayer = _activeAuctionPlayer + 1;

                    while (pass.Contains(_activeAuctionPlayer))
                    {
                        _activeAuctionPlayer++;
                    }

                    //event place a bid.
                    if (eventIPlaceABid != null && bid.bidder == Me)
                    {
                        eventIPlaceABid(bid);
                    }

                    if (eventSomeonePlaceABid != null && bid.bidder != Me)
                    {
                        eventSomeonePlaceABid(bid);
                    }



                    //check if the auction is still open
                    if (pass.Count >= PLAYER_NUMBER - 1 && _listBid.Count >= PLAYER_NUMBER)
                    {
                        _t = -1;
                        if (_currentWinningBid == null)
                        {
                            _gameType = EnGameType.MONTE;
                            _t        = 41;
                        }
                        else if (_currentWinningBid is CarichiBid)                              //carichi
                        {
                            _gameType = EnGameType.CARICHI;

                            _currentWinningBid.bidder.Role = EnRole.CHIAMANTE;
                            _winningPoint = ((CarichiBid)_currentWinningBid).point;

                            _t = 0;

                            if (eventPlaytimeStart != null)
                            {
                                eventPlaytimeStart();
                            }
                        }
                    }
                }
            }
            else if (IsFinalizePhase)                   //finalize
            {
                EnSemi?seme = _currentWinningBid.bidder.InvokeChooseSeme();

                if (seme.HasValue)
                {
                    _gameType = EnGameType.STANDARD;

                    _calledCard = GetCard(seme.Value, ((NormalBid)_currentWinningBid).number);

                    _winningPoint = ((NormalBid)_currentWinningBid).point;

                    //set the roles
                    _calledCard.initialPlayer.Role       = EnRole.SOCIO;
                    currentAuctionWinningBid.bidder.Role = EnRole.CHIAMANTE;

                    _t = 0;

                    if (eventPlaytimeStart != null)
                    {
                        eventPlaytimeStart();
                    }
                }
            }
            else if (IsPlayTime)                        //playtime

            {
                if (numberOfCardOnBoard == PLAYER_NUMBER)
                {
                    Card max = _cardOnBoard [0];
                    for (int i = 1; i < PLAYER_NUMBER; i++)
                    {
                        if (_cardOnBoard [i] > max)
                        {
                            max = _cardOnBoard [i];
                        }
                    }

                    _lastWinner = max.initialPlayer;

                    foreach (Card c in _cardOnBoard)
                    {
                        c.FinalPlayer = max.initialPlayer;
                    }

                    //event pick up
                    if (eventPickTheBoard != null)
                    {
                        eventPickTheBoard(_lastWinner, _cardOnBoard);
                    }

                    _cardOnBoard.Clear();

                    if (IsLastTurn)
                    {
                        if (eventPlaytimeEnd != null)
                        {
                            eventPlaytimeEnd();
                        }

                        //save last game in the archive
                        Archive.Instance.Add(new GameData(DateTime.Now, _cardGrid, _players, _listBid, _gameType, _calledCard, _winningPoint));
                        Archive.Instance.SaveLastGame();

                        _t = 41;
                    }
                }
                else
                {
                    Card card = ActivePlayer.InvokeChooseCard();
                    if (card != null)
                    {
                        card.PlayingTime = _t;
                        _cardOnBoard.Add(card);

                        //Events play a card
                        if (eventIPlayACard != null && ActivePlayer == Me)
                        {
                            eventIPlayACard(ActivePlayer, card);
                        }

                        if (eventSomeonePlayACard != null && ActivePlayer != Me)
                        {
                            eventSomeonePlayACard(ActivePlayer, card);
                        }

                        ++_t;
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ChiamataLibrary.GameData"/> class.
        /// </summary>
        /// <param name="path">The path of the XML file from which will be reading the <see cref="ChiamataLibrary.GameData"/>.</param>
        internal GameData(Stream s)
        {
            Board.Instance.Reset();
            _players = new Player[Board.PLAYER_NUMBER];
            _cards   = new Card[Board.Instance.nSemi, Board.Instance.nNumber];
            _bids    = new List <BidBase> ();

            //create the xml reader
            XmlReaderSettings setting = new XmlReaderSettings();

            setting.IgnoreComments = true;

            using (XmlReader reader = XmlReader.Create(s, setting)) {
                reader.ReadToFollowing("Game");                 //game

                reader.MoveToFirstAttribute();                  //datetime
                this.time = DateTime.Parse(reader.Value.ToString());

                reader.MoveToNextAttribute();                   //gametype
                this.gameType = (EnGameType)Enum.Parse(typeof(EnGameType), reader.Value, true);

                reader.MoveToNextAttribute();                   //winningPoint
                this.winningPoint = int.Parse(reader.Value);

                //Players
                reader.ReadToFollowing("Players");
                for (int i = 0; i < Board.PLAYER_NUMBER; i++)
                {
                    reader.ReadToFollowing("Player");

                    reader.ReadToFollowing("Name");                             //name
                    string name = reader.ReadElementContentAsString();

                    reader.ReadToFollowing("Role");                             //role
                    EnRole role = (EnRole)Enum.Parse(typeof(EnRole), reader.ReadElementContentAsString(), true);

                    reader.ReadToFollowing("Order");                            //order

                    int order = reader.ReadElementContentAsInt();

                    _players [i]      = new Player(name, order);
                    _players [i].Role = role;
                }

                //bids
                reader.ReadToFollowing("BidList");

                reader.MoveToFirstAttribute();                  //bid's number
                int nBids = int.Parse(reader.Value);

                for (int i = 0; i < nBids; i++)
                {
                    reader.ReadToFollowing("Bid");

                    reader.MoveToFirstAttribute();                      //bid's type
                    string type = reader.Value;

                    reader.ReadToFollowing("Bidder");                           //bidder

                    int bidder = reader.ReadElementContentAsInt();

                    if (type == "Pass")
                    {
                        _bids.Add(new PassBid(_players [bidder]));
                    }
                    else
                    {
                        reader.ReadToFollowing("Point");                                //point

                        int point = reader.ReadElementContentAsInt();

                        if (type == "Carichi")
                        {
                            _bids.Add(new CarichiBid(_players [bidder], point));
                        }
                        else
                        {
                            reader.ReadToFollowing("Number");                                   //number
                            EnNumbers number = (EnNumbers)Enum.Parse(typeof(EnNumbers), reader.ReadElementContentAsString(), true);

                            _bids.Add(new NormalBid(_players [bidder], number, point));
                        }
                    }
                }

                //Cards
                reader.ReadToFollowing("Cards");

                for (int seme = 0; seme < Board.Instance.nSemi; seme++)
                {
                    for (int number = 0; number < Board.Instance.nNumber; number++)
                    {
                        reader.ReadToFollowing("Card");
                        reader.MoveToFirstAttribute();                          //seme
                        reader.MoveToNextAttribute();                           //number
                        reader.MoveToNextAttribute();                           //called card
                        bool cc = bool.Parse(reader.Value);

                        reader.ReadToFollowing("InitialPlayer");
                        Player ip = _players [reader.ReadElementContentAsInt()];

                        _cards [seme, number] = new Card((EnNumbers)number, (EnSemi)seme, ip);

                        reader.ReadToFollowing("FinalPlayer");
                        Player fp = _players [reader.ReadElementContentAsInt()];

                        reader.ReadToFollowing("PlayingTime");
                        int tp = reader.ReadElementContentAsInt();

                        _cards [seme, number].PlayingTime = tp;
                        _cards [seme, number].FinalPlayer = fp;

                        if (cc)
                        {
                            calledCard = _cards [seme, number];
                        }
                    }
                }
            }
        }