Ejemplo n.º 1
0
 public Helper()
 {
     String[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));
 }
Ejemplo n.º 2
0
    private static void Main()
    {
        Console.BufferHeight = Console.WindowHeight = 20; // Field:Buffer cut.
        Console.BufferWidth  = Console.WindowWidth = 40;
        int  playfield = Console.WindowWidth - 10;
        int  scores    = 0;
        int  level     = 0;
        int  speed     = 150;
        byte lives     = 5;

        // Initial dwarf creation - (0)
        string  dwarfSymbol = "(0)";
        Objects dwarf       = new Objects();

        {
            dwarf.Ycoord = Console.WindowHeight - 1;
            dwarf.Xcoord = (playfield / 2) - 1;
            dwarf.Color  = ConsoleColor.White;
            dwarf.Symbol = dwarfSymbol;
        }

        //// Draw menu
        PrintMenu(playfield);

        // Define random generator variable
        Random randomGenerator = new Random();

        // Creation of list that stores all rocks
        List <Objects> rocks = new List <Objects>();

        // List array of console color names
        List <string> colorNames = new List <string>(ConsoleColor.GetNames(typeof(ConsoleColor)));

        colorNames.Remove("Black");
        colorNames.Remove("Red");
        colorNames.Remove("White");

        // Get colorNames length in int
        int numColors = colorNames.Count;

        // String array of rock symbols
        string[] rockSymbol = { "^", "@", "*", "&", "+", "%", "$", "#", "!", ".", ";", "\u2665" };

        // Get rock symbols length in int
        int numRockSymbols = rockSymbol.Length;

        while (true)
        {
            // Lower rock creation rate
            int densityLength = 13 - (level % 1000);
            if (densityLength <= 2)
            {
                densityLength = 2;
            }
            int density = randomGenerator.Next(1, densityLength);
            if (density < 6)
            {
                int densityRand = randomGenerator.Next(1, 11);
                switch (densityRand)
                {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                {
                    density = 1;
                }
                break;

                case 6:
                case 7:
                case 8:
                {
                    density = 2;
                }
                break;

                case 9:
                case 10:
                {
                    density = 3;
                }
                break;

                default:
                    break;
                }

                // Creation of binary mask to store x coordinates for all rocks on current row
                int mask = new int();

                // Creation of more than one in a line rocks
                while (density > 0)
                {
                    // Random rock generator - ^, @, *, &, +, %, $, #, !, ., ;,
                    string rockSelector = rockSymbol[randomGenerator.Next(numRockSymbols)];

                    // Make rock symbol long
                    StringBuilder currentRock = new StringBuilder();
                    int           rockLength  = new int();
                    if (rockSelector != "\u2665")
                    {
                        rockLength = randomGenerator.Next(1, 4);
                    }
                    else
                    {
                        rockLength = 1;
                    }

                    for (int curRockIndex = 0; curRockIndex < rockLength; curRockIndex++)
                    {
                        currentRock.Append(rockSelector);
                    }

                    // Get random ConsoleColor string
                    string colorName = colorNames[randomGenerator.Next(numColors)];

                    // Get ConsoleColor from string name
                    ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorName);

                    // New rock cration block
                    // Ensure that next rock will not overlap previous one
                    int newXcoord = randomGenerator.Next(0, playfield + 1 - currentRock.Length);

                    // Using binary mask 00011110000010000110000
                    int tempMask  = new int();
                    int freeSpace = 3;
                    for (int index = newXcoord - freeSpace; index < newXcoord + currentRock.Length + freeSpace; index++)
                    {
                        if (index >= 0 && index <= playfield)
                        {
                            tempMask |= 536870912 >> index; // 536870912 means one on bit 30 - x = 0
                        }
                    }

                    // Check if there is a match in ones bits
                    if ((mask & tempMask) != 0)
                    {
                        continue;
                    }
                    else
                    {
                        mask |= tempMask;
                    }

                    if (rockSelector != "\u2665")
                    {
                        Objects newRock = new Objects();
                        {
                            newRock.Color  = color;
                            newRock.Xcoord = newXcoord;
                            newRock.Ycoord = 0;
                            newRock.Symbol = currentRock.ToString();
                            rocks.Add(newRock);
                        }
                    }
                    else
                    {
                        Objects newRock = new Objects();
                        {
                            newRock.Color  = ConsoleColor.Red;
                            newRock.Xcoord = newXcoord;
                            newRock.Ycoord = 0;
                            newRock.Symbol = rockSelector;
                            rocks.Add(newRock);
                        }
                    }

                    density--;
                }
            }

            // Draw dwarf
            PrintStringOnPosition(dwarf.Xcoord, dwarf.Ycoord, dwarf.Symbol, dwarf.Color);

            // Move dwarf
            while (Console.KeyAvailable)
            {
                ConsoleKeyInfo keyPressed = Console.ReadKey();

                // Keeps dwarf in borders while moving left and right
                if (keyPressed.Key == ConsoleKey.LeftArrow)
                {
                    if (dwarf.Xcoord - 1 >= 0)
                    {
                        // Remove old dwarf
                        PrintStringOnPosition(dwarf.Xcoord, dwarf.Ycoord, new string(' ', dwarf.Symbol.Length), ConsoleColor.Black);
                        dwarf.Xcoord = dwarf.Xcoord - 1;
                    }
                }
                else if (keyPressed.Key == ConsoleKey.RightArrow)
                {
                    if (dwarf.Xcoord + dwarf.Symbol.Length < playfield)
                    {
                        // Remove old dwarf
                        PrintStringOnPosition(dwarf.Xcoord, dwarf.Ycoord, " ", ConsoleColor.Black);
                        dwarf.Xcoord = dwarf.Xcoord + 1;
                    }
                }

                // Draw dwarf
                PrintStringOnPosition(dwarf.Xcoord, dwarf.Ycoord, dwarf.Symbol, dwarf.Color);
            }

            bool levelUp = new bool();

            // Move rocks
            List <Objects> newList     = new List <Objects>();
            List <Objects> formerState = new List <Objects>();
            for (int count = 0; count < rocks.Count; count++)
            {
                Objects oldRock = rocks[count];
                Objects newRock = new Objects();
                {
                    newRock.Xcoord = oldRock.Xcoord;
                    newRock.Ycoord = oldRock.Ycoord + 1;
                    newRock.Color  = oldRock.Color;
                    newRock.Symbol = oldRock.Symbol;
                }

                // Save former state of rocks to remove them from screen
                formerState.Add(oldRock);

                // If moved rock is inside playfield borders (top-bottom) add it to new list for drawing
                // else adds score if a rock reaches the end of playfield
                if (newRock.Ycoord < Console.WindowHeight)
                {
                    newList.Add(newRock);
                }
                else if (newRock.Ycoord == Console.WindowHeight)
                {
                    scores += 10;

                    // Draw scores
                    PrintOnPosition(playfield + 2, 9, scores, ConsoleColor.White);
                    if (scores % 1000 == 0)
                    {
                        levelUp = true;
                    }
                }

                // Colission detection
                if (newRock.Ycoord == dwarf.Ycoord)
                {
                    bool rightEdge = newRock.Xcoord < (dwarf.Xcoord + dwarf.Symbol.Length);
                    bool leftEdge  = (newRock.Xcoord + newRock.Symbol.Length) > dwarf.Xcoord;
                    if (leftEdge && rightEdge)
                    {
                        if (newRock.Symbol != "\u2665")
                        {
                            lives--;
                            newList.Clear();
                            rocks.Clear();
                            dwarf.Xcoord = (playfield / 2) - 1;
                            Console.Clear(); // Clear console

                            //// Draw menu
                            PrintMenu(playfield);
                        }
                        else
                        {
                            lives++;
                        }
                    }
                }
            }

            rocks = newList;

            // Draw lives
            PrintOnPosition(playfield + 2, 6, lives, ConsoleColor.White);

            // Draw level
            PrintOnPosition(playfield + 2, 12, level, ConsoleColor.White);

            // Draw scores
            PrintOnPosition(playfield + 2, 9, scores, ConsoleColor.White);

            // Clear old rocks
            foreach (var oldRock in formerState)
            {
                PrintStringOnPosition(oldRock.Xcoord, oldRock.Ycoord, new string(' ', oldRock.Symbol.Length), ConsoleColor.Black);
            }

            // Draw rocks
            foreach (Objects rock in rocks)
            {
                PrintStringOnPosition(rock.Xcoord, rock.Ycoord, rock.Symbol, rock.Color);
            }

            // End of game condition
            if (lives == 0)
            {
                string gameOver = "GAME OVER!!!";
                PrintStringOnPosition((playfield / 2) - (gameOver.Length / 2), Console.WindowHeight / 2, gameOver, ConsoleColor.Red);
                Console.ReadKey();
                return;
            }

            // Make it harder
            if (levelUp)
            {
                level++;
            }

            Thread.Sleep(speed); // Game speed
        }
    }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            //variables
            sbyte  right     = 3;
            sbyte  left      = -3;
            int    direction = right;
            int    i         = 0;
            bool   contains  = false;
            ushort score     = 0;

            try
            {
                String[] signs = new String[]
                {
                    "*", "?", ",", ".", "#", ";", ":", "<", "&", "+", "!", "^",
                    "%", "$", "-", "@", "TELERIK", "≤≥", ">", "=", "()", "[]", "{}"
                };
                String[]       colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));
                ConsoleColor[] color      = new ConsoleColor[16];
                for (i = 0; i < 16; i++)
                {
                    color[i] = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[i]);
                }
                Random rand = new Random();

                //Remove the crollbars
                Console.BufferHeight = Console.WindowHeight;
                Console.BufferWidth  = Console.WindowWidth;

                //The dwarf in the begining
                Position         dwarf     = new Position(24, 38, color[12], "(0)");
                Queue <Position> rockDwarf = new Queue <Position>();
                rockDwarf.Enqueue(dwarf);
                Console.SetCursorPosition(dwarf.col, dwarf.row);
                Console.ForegroundColor = dwarf.color;
                Console.Write(dwarf.sign);

                //Rocks in the begining
                //Creating a list of rocks
                List <Position> startRocks = new List <Position>();
                for (i = 0; i < 25; i++)
                {
                    startRocks.Add(new Position(rand.Next(0, 20), rand.Next(0, 78), color[rand.Next(1, 16)], signs[rand.Next(0, 23)]));
                }
                //Putting them in a queue
                Queue <Position> rocks = new Queue <Position>();
                foreach (Position startRock in startRocks)
                {
                    rocks.Enqueue(startRock);
                }
                //Drawing them on the screen
                foreach (Position rock in rocks)
                {
                    Console.ForegroundColor = rock.color;
                    Console.SetCursorPosition(rock.col, rock.row);
                    Console.Write(rock.sign);
                }

                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.SetCursorPosition(0, 0);
                Console.WriteLine("Catch the yellow rocks to increase your score.\nPress any key to continue...");
                ConsoleKeyInfo userInput = Console.ReadKey();
                //The game itself
                while (true)
                {
                    Console.Clear();

                    direction = 0;
                    if (Console.KeyAvailable)
                    {
                        userInput = Console.ReadKey();
                        if (userInput.Key == ConsoleKey.LeftArrow)
                        {
                            direction = left;
                        }
                        if (userInput.Key == ConsoleKey.RightArrow)
                        {
                            direction = right;
                        }
                    }

                    //The rocks are moving
                    for (i = 0; i < 25; i++)
                    {
                        startRocks[i] = rocks.Dequeue();
                        if (startRocks[i].row + 1 != 25)
                        {
                            startRocks[i] = new Position(startRocks[i].row + 1, startRocks[i].col, startRocks[i].color, startRocks[i].sign);
                            rocks.Enqueue(startRocks[i]);
                        }
                        else
                        {
                            startRocks.Remove(startRocks[i]);
                            startRocks.Insert(i, new Position(0, rand.Next(0, 78), color[rand.Next(1, 16)], signs[rand.Next(0, 23)]));
                            rocks.Enqueue(startRocks[i]);
                        }
                    }
                    foreach (Position rock in rocks)
                    {
                        Console.ForegroundColor = rock.color;
                        Console.SetCursorPosition(rock.col, rock.row);
                        Console.Write(rock.sign);
                    }

                    //The dwarf is moving
                    dwarf = rockDwarf.Dequeue();
                    if ((direction == right && dwarf.col < 75) || (direction == left && dwarf.col > 3))
                    {
                        dwarf = new Position(dwarf.row, dwarf.col + direction, dwarf.color, dwarf.sign);
                    }
                    rockDwarf.Enqueue(dwarf);
                    Console.SetCursorPosition(dwarf.col, dwarf.row);
                    Console.ForegroundColor = dwarf.color;
                    Console.Write(dwarf.sign);

                    //Colision detection
                    //Keeping score
                    foreach (Position rock in rocks)
                    {
                        contains = rock.row == 24 && (dwarf.col == rock.col || (dwarf.col + 1) == rock.col || (dwarf.col + 2) == rock.col);
                        if (rock.color != color[14] && contains)
                        {
                            Console.Clear();
                            throw new Exception("Game Over...");
                        }
                        if (contains && rock.color == color[14])
                        {
                            score++;
                        }
                    }
                    Thread.Sleep(150);
                }
            }
            catch (Exception ex)
            {
                Console.SetCursorPosition(0, 0);
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(ex.Message);
                Console.WriteLine("Your score is: {0}", score);
            }
        }
Ejemplo n.º 4
0
    static void Main()
    {
        RemoveScrollBars();

        Object dwarf = new Object();

        dwarf.x     = playfieldWidth / 2 - playerPadSize / 2;
        dwarf.y     = Console.WindowHeight - 1;
        dwarf.c     = "(0)";
        dwarf.color = ConsoleColor.Yellow;

        Random        randomGenerator = new Random();
        List <Object> rocks           = new List <Object>();

        // Get a string array with the names of ConsoleColor enumeration members.
        string[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));

        while (true)
        {
            bool hitted = false;
            indexRocksSymbols = randomGenerator.Next(0, rocksSymbols.Length);
            ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[randomGenerator.Next(0, colorNames.Length)]);

            Object newRock = new Object();
            newRock.color = color;
            newRock.c     = rocksSymbols[indexRocksSymbols];
            newRock.x     = randomGenerator.Next(0, playfieldWidth);
            newRock.y     = 0;
            rocks.Add(newRock);

            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo pressedKey = Console.ReadKey(true);
                while (Console.KeyAvailable)
                {
                    Console.ReadKey(true);
                }

                if (pressedKey.Key == ConsoleKey.LeftArrow)
                {
                    if (dwarf.x - 1 >= 0)
                    {
                        dwarf.x = dwarf.x - 1;
                    }
                }
                else if (pressedKey.Key == ConsoleKey.RightArrow)
                {
                    if (dwarf.x + 1 < playfieldWidth)
                    {
                        dwarf.x = dwarf.x + 1;
                    }
                }
            }

            for (int i = 0; i < rocks.Count; i++)
            {
                Object oldObject = rocks[i];
                Object newObject = new Object();
                newObject.x     = oldObject.x;
                newObject.y     = oldObject.y + 1;
                newObject.c     = oldObject.c;
                newObject.color = oldObject.color;
                rocks.Remove(oldObject);

                if (newObject.y == Console.WindowHeight)
                {
                    pointsCount++;
                }

                if ((newObject.y == dwarf.y && newObject.x == dwarf.x) ||
                    (newObject.y == dwarf.y && newObject.x == dwarf.x + 1) ||
                    (newObject.y == dwarf.y && newObject.x == dwarf.x + 2))
                {
                    livesCount--;
                    hitted = true;

                    if (livesCount <= 0)
                    {
                        PrintStringOnPosition(15, 7, "GAME OVER", ConsoleColor.Red);
                        PrintStringOnPosition(15, 9, "Press Enter to continue", ConsoleColor.Red);
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                }

                if (newObject.y < Console.WindowHeight)
                {
                    rocks.Add(newObject);
                }
            }

            Console.Clear();

            // Print the dwarf
            if (hitted)
            {
                rocks.Clear();
                PrintStringOnPosition(dwarf.x, dwarf.y, "XXX", ConsoleColor.Red);
            }
            else
            {
                PrintStringOnPosition(dwarf.x, dwarf.y, dwarf.c, dwarf.color);
            }

            // Print the rocks
            foreach (Object rock in rocks)
            {
                PrintStringOnPosition(rock.x, rock.y, rock.c, rock.color);
            }

            PrintStringOnPosition(41, 5, "Lives: " + livesCount, ConsoleColor.White);
            PrintStringOnPosition(41, 7, "Score: " + pointsCount, ConsoleColor.White);

            Thread.Sleep(300);
        }
    }
Ejemplo n.º 5
0
    static void Main()
    {
        int playFieldWidth = 60;
        int livesCount = 5;
        string[] rocksSymbols = { "^", "@", "*", "+++", "+", "+", "&", ";", "%", "$", "#", "!", ".." };
        int indexRocksSymbols;
        double speed = 100.0;
        Console.BufferHeight = Console.WindowHeight = 25;
        Console.BufferWidth = Console.WindowWidth = 60;
        Object user = new Object();
        user.x = 2;
        user.y = Console.WindowHeight - 1;
        user.c = "(0)";
        user.color = ConsoleColor.Yellow;

        Random randomGenerator = new Random();
        List<Object> objects = new List<Object>();
        string[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));

        while (true)
        {
            bool hitted = false;
            {
                indexRocksSymbols = randomGenerator.Next(0, rocksSymbols.Length);
                ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[randomGenerator.Next(0, colorNames.Length)]);

                Object newObject = new Object();
                newObject.color = color;
                newObject.x = randomGenerator.Next(0, playFieldWidth);
                newObject.y = 5;
                newObject.c = rocksSymbols[indexRocksSymbols];
                objects.Add(newObject);
            }
            //Move rocks
            List<Object> newList = new List<Object>();

            for (int i = 0; i < objects.Count; i++)
            {
                Object oldObject = objects[i];
                Object newObject = new Object();
                newObject.x = oldObject.x;
                newObject.y = oldObject.y + 1;
                newObject.c = oldObject.c;
                newObject.color = oldObject.color;

                if ((newObject.c != "*" && newObject.y == user.y && newObject.x == user.x)
                            || (newObject.y == user.y && newObject.x == user.x + 1)
                            || (newObject.y == user.y && newObject.x == user.x + 2))
                {
                    livesCount--;
                    hitted = true;
                    if (livesCount <= 0)
                    {
                        printStringOnPosition(3, 3, "GAME OVER!!!", ConsoleColor.Red);
                        printStringOnPosition(25, 3, "Pres [enter] to exit", ConsoleColor.Red);
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                }
                if (newObject.y < Console.WindowHeight)
                {
                    newList.Add(newObject);
                }
            }
            objects = newList;

            while (Console.KeyAvailable)
            {
                ConsoleKeyInfo pressedKey = Console.ReadKey(true);
                if (pressedKey.Key == ConsoleKey.LeftArrow)
                {
                    if (user.x - 1 >= 0)
                    {
                        user.x = user.x - 1;
                    }
                }
                else if (pressedKey.Key == ConsoleKey.RightArrow)
                {
                    if (user.x + 1 < playFieldWidth)
                    {
                        user.x = user.x + 1;
                    }
                }
            }

            //Clear the console
            Console.Clear();

            //Redraw playfield
            if (hitted)
            {
                objects.Clear();
                printStringOnPosition(user.x, user.y, "XXX", ConsoleColor.Red);
            }
            else
            {
                printStringOnPosition(user.x, user.y, user.c, user.color);
            }
            foreach (Object rocks in objects)
            {
                printStringOnPosition(rocks.x, rocks.y, rocks.c, rocks.color);
            }

            //Draw info
            printStringOnPosition(3, 1, "*FALLING ROCKS*", ConsoleColor.Yellow);
            printStringOnPosition(28, 1, "Lives: " + livesCount, ConsoleColor.White);
            printStringOnPosition(40, 1, "Speed: " + speed, ConsoleColor.White);

            //Slow down the program
            Thread.Sleep(150);
        }
    }
Ejemplo n.º 6
0
        //instadd varible is used for checking the instruction address
        public void ABBAS(string infix, int instadd)
        {
            String[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));
            if (infix.Length == 2)
            {
                throw new ProgramException("This infix expression does not fulfill the condition of required no of operators and operands");
            }
            check(infix);
            Heterogeneous_Brackets_Validity obj1 = new Heterogeneous_Brackets_Validity();

            if (obj1.check(infix) == 0)//Checking the infix expression for hetrogeneous validity
            {
                throw new ProgramException("This infix expression does not fulfill the condition of Hetrogeneous");
            }
            postfix obj     = new postfix();
            string  poststr = obj.conversion(infix);//Converting the infix expression into postfix expression

            if (instadd == 0)
            {
                zeroadd obj2 = new zeroadd();
                obj2.evaluate(poststr, infix);
            }
            else if (instadd == 1)
            {
                oneadd obj2 = new oneadd();
                obj2.evaluate(poststr, infix);
            }
            else if (instadd == 2)
            {
                twoaddress obj2 = new twoaddress();
                obj2.evaluate(poststr, infix);
            }
            else if (instadd == 3)
            {
                ThreeAdd obj2 = new ThreeAdd();
                obj2.evaluate(poststr, infix);
            }
            else//if instruction address will greater than 3 or less than zero
            {
                throw new ProgramException("\n\tThis address instruction is invalid\n\tThis exception is thrown from start class");
            }
            string       inst;                                                   //for printing the file reading
            FileStream   aFile = new FileStream("C:/Output.txt", FileMode.Open); //opening the output file in open mode for read purpose
            StreamReader sr    = new StreamReader(aFile);                        //for reading the file

            inst = sr.ReadLine();
            color cf = new color();

            cf.change("");
            Console.BackgroundColor = ConsoleColor.Magenta;
            Console.Clear();
            while (inst != null)//for reading the whole file
            {
                Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[13]);
                for (int i = 0; i < inst.Length; i++)
                {
                    Console.Write(inst[i]);
                    Thread.Sleep(20);
                }
                Console.WriteLine();
                inst = sr.ReadLine();
            }
            cf.Rchange("");
            Console.BackgroundColor = ConsoleColor.Cyan;
            sr.Close();//closing the file after reading the whole file
        }