Esempio n. 1
0
        /// <summary>
        /// Handles when the done button has been clicked
        /// </summary>
        /// <param name="bClickedByHuman"></param>
        public void DoneClicked(bool bClickedByHuman)
        {
            Round curRound = m_Rounds[m_Rounds.Count - 1];

            curRound.Expand();
            Turn curTurn  = curRound.GetCurrentTurn();
            Turn prevTurn = (Turn)curTurn.Clone();

            prevTurn--;
            Player curPlayer  = curTurn.GetPlayer();
            Player prevPlayer = prevTurn.GetPlayer();
            Hand   prevHand   = prevTurn.GetHand();
            Hand   curHand    = curTurn.GetHand();

            if (curTurn.GetPlayer().ID == prevTurn.GetPlayer().ID)
            {
                if (!curTurn.isDefending())//impossible to defend on the first turn
                {
                    if (!bClickedByHuman)
                    {
                        curHand = curPlayer.Attack(prevHand);//a player object may attack, only if it's not human
                        foreach (PlayingCard card in curHand)
                        {
                            m_Gg.m_BattleArea.AddAttackCard(new CardBox(card, Orientation.Vertical, curTurn.GetHashCode()));
                            curPlayer.m_Hand -= card;//remove the card from the player's hand
                            WriteToLog("Player " + prevTurn.GetPlayer().ID.ToString() + " " + ((!(PlayerType.human == prevTurn.GetPlayer().GetMode())) ? "computer" : "human")
                                       + " attacked using " + card.ToString());
                        }
                        curTurn.SetHand(curHand);
                        if (0 == curHand.Count)
                        {
                            WriteToLog("Player " + prevTurn.GetPlayer().ID.ToString() + " " + ((!(PlayerType.human == prevTurn.GetPlayer().GetMode())) ? "computer" : "human")
                                       + " player (prev turn) sucessfully attacked/defended");
                        }
                        m_Gg.UpdateElements(m_Players);//update UI
                    }
                }
                if (false == btnDoneGuard)
                {
                    Timed_Response(ResponseType.next_turn);//timed response gives time for the user to view cards on the table
                }
            }
            else
            {
                int iLoserId = 0;//used to track loser of this round. The loser will have the first turn in next round
                if (!bClickedByHuman)
                {
                    if (false == btnDoneGuard)
                    {
                        if (curTurn.GetPlayer().CanDefend(prevHand))
                        {
                            curHand = curPlayer.MakeDefense(prevHand);
                            foreach (PlayingCard card in curHand)
                            {
                                m_Gg.m_BattleArea.AddDefenseCard(new CardBox(card, Orientation.Vertical, curTurn.GetHashCode()));
                                curPlayer.m_Hand -= card;
                                WriteToLog("Player " + prevTurn.GetPlayer().ID.ToString() + " " + ((!(PlayerType.human == prevTurn.GetPlayer().GetMode())) ? "computer" : "human")
                                           + " defended using " + card.ToString());
                            }
                            curTurn.SetHand(curHand);//override any existing hand
                            if (0 == curHand.Count)
                            {
                                WriteToLog("Player " + prevTurn.GetPlayer().ID.ToString() + " " + ((!(PlayerType.human == prevTurn.GetPlayer().GetMode())) ? "computer" : "human")
                                           + " player (prev turn) sucessfully attacked/defended");
                                iLoserId = prevTurn.GetPlayer().ID;
                            }
                        }
                        else
                        {
                            iLoserId = ProcessLoser(prevTurn, prevHand, prevPlayer.ID, curTurn, curHand, bClickedByHuman);
                        }
                    }
                }
                //need to get the current count of cards in each player's hands
                int  ranksInPrevHand = GameUtil.getCountMaxRanksInHand(prevHand);
                int  ranksInCurHand  = GameUtil.getCountMaxRanksInHand(curHand);
                bool bTemp           = GameUtil.doHandSuitsMatch(prevHand, curHand);
                if (ranksInPrevHand == ranksInCurHand)//test for equal hands, do nothing if not equals
                {
                    if (false == btnDoneGuard)
                    {
                        if (prevHand > curHand)//compare
                        {
                            iLoserId = ProcessLoser(prevTurn, prevHand, prevPlayer.ID, curTurn, curHand, bClickedByHuman);
                        }
                        else if (curHand > prevHand)
                        {
                            iLoserId = ProcessLoser(curTurn, curHand, curPlayer.ID, prevTurn, prevHand, bClickedByHuman);
                        }
                        else
                        {//draw
                            MessageBox.Show("Draw");
                        }
                    }
                }
                else
                {
                    if (curTurn.isDefending()) //attacking humans are free to place as many cards on the board as they want, errors to be caught by Cardbox_Clicked method
                    {
                        if (bClickedByHuman)   //notify user of their mistake
                        {
                            if (GameUtil.ShallIDoThisForYou("You did not place the correct amount of cards in the battle area. Forfeit?"))
                            {
                                WriteToLog("Player " + prevTurn.GetPlayer().ID.ToString() + " " + ((!(PlayerType.human == prevTurn.GetPlayer().GetMode())) ? "computer" : "human")
                                           + " player (prev turn) sucessfully attacked/defended");
                                iLoserId = prevTurn.GetPlayer().ID;
                            }
                        }
                    }
                }
                m_Gg.UpdateElements(m_Players); //refresh gui with new placements
                DoneWithRound(iLoserId);        //iLoserId used to set player order in the next round
                curRound.m_CurrentTurn--;
            }
            curRound.m_CurrentTurn++;
            curRound.outputStatusToWindow(ref m_Gg);
            curRound.updateBoldedStatus(curRound.GetCurrentTurn(), ref m_Gg);
        }
Esempio n. 2
0
        internal FoolGame36Cards(IConfigurationSetter languageSet, IDeck deck, IGameSetter gameSetter, IConsoleReadWrap comReadWrap, IUserIdetifier userIdetifier)
        {
            if (languageSet == null || deck == null || gameSetter == null || comReadWrap == null)
            {
                throw new NullReferenceException(nameof(Table));
            }

            _languageSet = languageSet;
            _deck        = deck;
            _gameSetter  = gameSetter;
            _comReadWrap = comReadWrap;

            _players = _gameSetter.Players;

            MinCards = _languageSet.Constant.numberOfCards_1_;
            string wantToContinue = _languageSet.Constant.WantToContinue_7_;
            string flag           = wantToContinue;

            SetTurnToPlayers();

            while (flag == wantToContinue)
            {
                Console.Clear();
                Console.WriteLine("{0}", _languageSet.Message.welcome_11_);                       //Welcome to DURAK game!!!
                Console.WriteLine("-----------------------------------------------------------------------------------------------");
                Console.WriteLine($"{_languageSet.Message.hello_13_} {userIdetifier.UserName}!"); //Hello
                Console.WriteLine("-----------------------------------------------------------------------------------------------");

                Console.WriteLine($"{_languageSet.Message.trumpCardIs_17_} {_deck.ShowTrumpCard()}"); //This time your Trump card is
                foreach (var player in _players)
                {
                    _deck.GiveCardFromDeck(MinCards, player); // Give 6 cards to every player
                }
                Console.WriteLine("-----------------------------------------------------------------------------------------------");


                while (_players[0].HowManyCardsOnHands() > 0 && _players[1].HowManyCardsOnHands() > 0)
                {
                    //tempdefence = null;
                    Console.WriteLine("-----------------------------------------------------------------------------------------------");
                    Console.WriteLine($"{_languageSet.Message.yourTurn_18_}");                                                                               //
                    _players[0].ShowOnHands();
                    Console.WriteLine($"{_languageSet.Message.thereIs_19_} {_players[1].HowManyCardsOnHands()} {_languageSet.Message.cardsInCpuHands_20_}"); //There is(are) XX card(s) in CPU hands
                                                                                                                                                             //CPU.ShowOnHands();
                    CardsOnTableAre();

                    Card Attack = player1.Attack(_cardsOnTable);
                    if (Attack != null)
                    {
                        player1.RemoveCardFromHands(Attack);
                        _cardsOnTable.Add(Attack);
                        Card defCard = player2.Defend(_cardsOnTable, Attack);
                        if (defCard != null)
                        {
                            player2.RemoveCardFromHands(defCard);
                            _cardsOnTable.Add(defCard);
                            CardsOnTableAre();
                        }
                        else // (tempdefence == null)
                        {
                            _deck.GiveCardFromDeck(player1.CardsToTake(), player1);
                            for (int i = 0; i <= _cardsOnTable.Count - 1; i++)
                            {
                                player2.AddCardToHands(_cardsOnTable[i]);
                            }
                            _cardsOnTable.Clear();
                            Console.WriteLine($"{_languageSet.Message.thereIs_19_} {_deck.HowManyCardsInDeck()} {_languageSet.Message.cardsInDeck_21_}"); //There is(are) XX card(s) in Deck
                        }
                    }
                    else
                    {
                        Console.WriteLine($"{_languageSet.Message.turnChanges_22_}"); //Turn changes
                        _deck.GiveCardFromDeck(player1.CardsToTake(), player1);
                        _deck.GiveCardFromDeck(player2.CardsToTake(), player2);
                        _cardsOnTable.Clear();
                        Console.WriteLine($"{_languageSet.Message.thereIs_19_} {_deck.HowManyCardsInDeck()} {_languageSet.Message.cardsInDeck_21_}"); //
                        Player tempPlayer = player1;
                        player1 = player2;
                        player2 = tempPlayer;
                    }
                }

                if (player1.HowManyCardsOnHands() == 0 && player2.HowManyCardsOnHands() == 0)
                {
                    Console.WriteLine("-----------------------------------------------------------------------------------------------");
                    Console.WriteLine($"{_languageSet.Message.drawThisTime_24_}"); //It is draw in this Game!!!!!!
                    Console.WriteLine("-----------------------------------------------------------------------------------------------");
                }
                else if (player1.HowManyCardsOnHands() == 0 && player2.HowManyCardsOnHands() > 0)
                {
                    Console.WriteLine("-----------------------------------------------------------------------------------------------");
                    Console.WriteLine($"{_languageSet.Message.congratulations_25_} {player1.PlayerName}, {_languageSet.Message.youAreWinner_26_}"); //Congratulations XX you are winner!!!!!!
                    Console.WriteLine("-----------------------------------------------------------------------------------------------");
                }
                else
                {
                    Console.WriteLine("-----------------------------------------------------------------------------------------------");
                    Console.WriteLine($"{player1.PlayerName}, {_languageSet.Message.youLoseThisGame_27_} "); //you lose this time
                    Console.WriteLine("-----------------------------------------------------------------------------------------------");
                }
                Console.WriteLine("-----------------------------------------------------------------------------------------------");
                Console.WriteLine($"--------------{_languageSet.Message.press_28_} {wantToContinue} {_languageSet.Message.toStartAgain_29_}-------------------------------------");
                //Press __ to start again or any button to EXIT
                flag = (Console.ReadLine())?.ToUpper();
            }
            _comReadWrap.ReturnExit();
        }