Пример #1
0
        private static string ApplyCardsToLine(SampleMatch match, bool isBorderLine, int row, string orgline, int cardrow)
        {
            int numCards = match.GetNumberOfCards(row);

            if (numCards == 0)
            {
                return(orgline);
            }

            int startX = GetFirstCardPositionX(match, row);

            string line      = "+";
            string emptyLine = "|";

            for (int j = 0; j < numCards; j++)
            {
                line += "=====+";

                SampleMinionCard card = match.Playfield[j, row] as SampleMinionCard;

                if (card != null)
                {
                    switch (cardrow)
                    {
                    case 1:
                        if (card.IsSleeping)
                        {
                            emptyLine += " Zzz |";
                        }
                        else
                        {
                            emptyLine += "   " + card.CurrentMovesPerRound + " |";
                        }
                        break;

                    case 2:
                        emptyLine += "A" + card.Attack.ToString("D3") + " |";
                        break;

                    case 3:
                        emptyLine += "L" + card.CurrentHealth.ToString("D3") + " |";
                        break;

                    default:
                        emptyLine += "     |";
                        break;
                    }
                }
                else
                {
                    emptyLine += "     |";
                }
            }

            string lineToUse = isBorderLine ? line : emptyLine;

            return(orgline.Remove(startX, lineToUse.Length).Insert(startX, lineToUse));
        }
Пример #2
0
        private static int GetFirstCardPositionX(SampleMatch match, int row)
        {
            int numberOfCardsInRow = match.GetNumberOfCards(row);

            int widthOfCard = 6;
            int fullWidth   = widthOfCard * match.PlayfieldWidth;
            int xPos        = (fullWidth / 2) - (widthOfCard / 2) * numberOfCardsInRow;

            return(xPos);
        }
Пример #3
0
        private static string GetFullLine(SampleMatch match, bool isBorderline)
        {
            string line = isBorderline ? "+" : "|";

            for (int i = 0; i < (match.CardWidth * match.PlayfieldWidth); i++)
            {
                line += isBorderline ? "=" : " ";
            }
            line += isBorderline ? "+" : "|";
            return(line);
        }
Пример #4
0
        private static void LogHero(SampleMatch match, SamplePlayer player, bool isUpper)
        {
            int    heroStartIndex = (int)((float)match.PlayfieldWidth / 2.0f - 0.5f);
            string emptyLine      = new String(' ', heroStartIndex * match.CardWidth);

            if (isUpper == true)
            {
                Logger.Log(emptyLine + "+=====+");
            }

            string manaSubstring = player.CurrentMana.ToString("D2") + "/" + player.MaxMana.ToString("D2");
            string manaString    = "        Mana: " + manaSubstring;

            Logger.Log(emptyLine + "|     |");
            Logger.Log(ReplaceWithName(emptyLine, player.Name, emptyLine.Length - 1) + "| " + player.CurrentHealth.ToString("D3") + " |" + manaString);
            Logger.Log(emptyLine + "|     |");

            if (isUpper == false)
            {
                Logger.Log(emptyLine + "+=====+");
            }
        }
Пример #5
0
        private static Command ParseCommand(string cmd, SampleMatch match)
        {
            string[] split = cmd.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (split.Length == 0)
            {
                return(Command.None);
            }

            string command = split[0];

            string[] parms = new string[split.Length - 1];
            Array.Copy(split, 1, parms, 0, parms.Length);

            switch (command)
            {
            case "play":
            {
                if (parms.Length < 2)
                {
                    Console.WriteLine("'play' command must have at least two parameters");
                    return(Command.None);
                }

                int param1 = 0;
                int param2 = 0;
                int param3 = -1;
                try
                {
                    param1 = int.Parse(parms[0]);
                    param2 = int.Parse(parms[1]);
                }
                catch
                {
                    Console.WriteLine("'play' parameters must be valid integers");
                    return(Command.None);
                }

                if (parms.Length > 2)
                {
                    if (int.TryParse(parms[2], out param3) == false)
                    {
                        param3 = -1;
                    }
                }

                int row = param3;
                if (row == -1)
                {
                    row = match.GetRowForPlayer(match.ActiveSamplePlayer);
                }

                bool res = match.ActiveSamplePlayer.PlayCardFromHandUsingIndex(param1, param2, row);
                if (res == false)
                {
                    Console.WriteLine("Failed to play card '" + param1 + "' from hand to position '" + param2 + "'");
                    return(Command.None);
                }
            }
                return(Command.Play);

            case "attack":
            {
                if (parms.Length != 2)
                {
                    Console.WriteLine("'attack' command must have two parameters");
                    return(Command.None);
                }

                int param1 = 0;
                int param2 = 0;
                try
                {
                    param1 = int.Parse(parms[0]);
                    param2 = int.Parse(parms[1]);
                }
                catch
                {
                    Console.WriteLine("'attack' parameters must be valid integers");
                    return(Command.None);
                }

                bool res = match.AttackWithCardUsingIndexes(match.GetRowForPlayer(match.ActiveSamplePlayer), param1,
                                                            match.GetRowForPlayer(match.InactiveSamplePlayer), param2);
                if (res == false)
                {
                    Console.WriteLine("Failed to attack victim '" + param2 + "' with card '" + param2 + "'");
                    return(Command.None);
                }
            }
                return(Command.Attack);

            case "help":
                PrintHelp();
                return(Command.None);

            case "hand":
                // end turn
                return(Command.PrintHand);

            case "end":
                // end turn
                return(Command.End);

            case "concede":
                match.ActivePlayer.Concede();
                return(Command.Concede);
            }

            Console.WriteLine("Unknown command: " + command);
            return(Command.None);
        }
Пример #6
0
        public static void Main(string[] args)
        {
            MainClass cls = new MainClass();

            Logger.LogWriter = cls;

            Console.WriteLine("Hello SampleTCG!");
            Console.WriteLine();
            Console.WriteLine("Choose player setup:");
            Console.WriteLine("1 - Human vs. Human");
            Console.WriteLine("2 - Human vs. CPU");
            Console.WriteLine("3 - CPU vs. CPU");
            Console.WriteLine();
            string mode = Console.ReadLine();

            while (isValidPlayerSetup(mode) == false)
            {
                Console.WriteLine("Please enter either 1, 2 or 3.");
                mode = Console.ReadLine();
            }

            SamplePlayer player1 = null;
            SamplePlayer player2 = null;

            switch (mode)
            {
            case "1":
                player1      = new SampleHumanPlayer();
                player1.Name = "Human1";
                player2      = new SampleHumanPlayer();
                player2.Name = "Human2";
                break;

            case "2":
                player1      = new SampleCPUPlayer();
                player1.Name = "CPU";
                player2      = new SampleHumanPlayer();
                player2.Name = "Human2";
                break;

            case "3":
                player1      = new SampleCPUPlayer();
                player1.Name = "CPU1";
                player2      = new SampleCPUPlayer();
                player2.Name = "CPU2";
                break;
            }

            Random rnd = new Random();

            SampleGame game = new SampleGame();

            game.InitCollection();

            SampleMatch match = new SampleMatch(rnd);

            match.Player1 = player1;
            match.Player2 = player2;

            player1.CreateRandomDeck(game.CardCollection, 20, match.Random);
            player2.CreateRandomDeck(game.CardCollection, 20, match.Random);

            match.PreStart();
            match.Start();

            while (match.IsRunning)
            {
                SampleMatchPrinter.PrintSampleMatch(match);

                if (match.ActivePlayer.IsLocallyControlled)
                {
                    string  line = Console.ReadLine();
                    Command cmd  = ParseCommand(line, match);
                    while (cmd == Command.None)
                    {
                        line = Console.ReadLine();
                        cmd  = ParseCommand(line, match);
                    }

                    if (cmd == Command.PrintHand)
                    {
                        match.ActivePlayer.LogHand();
                    }

                    if (cmd == Command.End)
                    {
                        match.StartNextRound();
                    }
                }
                else
                {
                    SampleCPUPlayer cpu = match.ActiveSamplePlayer as SampleCPUPlayer;
                    if (cpu != null)
                    {
                        cpu.PerformActions();
                    }

                    match.StartNextRound();
                }
            }

            Console.WriteLine("Press return to exit");
            Console.ReadLine();
        }
Пример #7
0
        public static void PrintSampleMatch(SampleMatch match)
        {
            ILogWriter prevWriter = Logger.LogWriter;

            SampleMatchPrinterBuffer buffer = new SampleMatchPrinterBuffer();

            try
            {
                Logger.LogWriter = buffer;

                Logger.Log("========================================================================================================");
                Logger.Log("Round: " + match.CurrentRound);
                Logger.Log("Active Player: " + match.ActivePlayer.Name);
                Logger.Log("--------------------------------------------------------------------------------------------------------");

                // Draw upper hero
                LogHero(match, match.Player1, true);

                string borderLine = GetFullLine(match, true);
                string middleLine = GetFullLine(match, false);

                Logger.Log(ApplyCardsToLine(match, true, 0, borderLine, 0));
                for (int i = 0; i < match.PlayfieldHeight; i++)
                {
                    for (int y = 0; y < match.CardHeight - 1; y++)
                    {
                        Logger.Log(ApplyCardsToLine(match, false, i, middleLine, y + 1));
                    }
                    Logger.Log(ApplyCardsToLine(match, true, i, borderLine, 0));
                }

                // Draw upper hero
                LogHero(match, match.Player2, false);

                // Add hand to buffer
                int             maxLineLength = borderLine.Length + match.CardWidth;
                List <BaseCard> handCards     = match.ActivePlayer.Hand.Cards;

                int    linePos   = 4;
                int    gapLength = maxLineLength - buffer.buffer[linePos].Length;
                string gap       = new String(' ', gapLength);

                buffer.buffer[linePos] += gap + "Hand of " + match.ActivePlayer.Name;

                for (int i = 0; i < handCards.Count; i++)
                {
                    linePos++;

                    if (linePos >= buffer.buffer.Count)
                    {
                        gapLength = maxLineLength;

                        gap = new String(' ', gapLength);
                        buffer.buffer.Add(gap + i + ": " + handCards[i]);
                    }
                    else
                    {
                        gapLength = maxLineLength - buffer.buffer[linePos].Length;
                        if (gapLength < 0)
                        {
                            gapLength = 0;
                        }

                        gap = new String(' ', gapLength);
                        buffer.buffer[linePos] += gap + i + ": " + handCards[i];
                    }
                }
            }
            finally
            {
                Logger.LogWriter = prevWriter;
            }

            // Write buffer to real output
            if (Logger.LogWriter != buffer)
            {
                for (int i = 0; i < buffer.buffer.Count; i++)
                {
                    Logger.Log(buffer.buffer[i]);
                }
            }
        }