Adam

Adam

  • NA
  • 131
  • 30.6k

Need help with ConsoleCursor please

Mar 24 2011 12:20 PM
Hi
Below is my C# code, and I have problem with SetCursorPsotion in switch loop(Keys();). When I pull key, it sets cursor to request area but it has deleted a symbol where the cursor was before.So when I go through all symbols with cursor, result is empty Area(checkerboard). Could anybody help me pls, can you see a mistake? Thanks a lot

namespace Checkers
{
    class Checkers
    {
        public const int X = 8;
        public const int Y = 8;
        int switcher = 0;
        int num = 8;
        public int xpos = 0;
        public int ypos = 0;
        public char[,] Area = new char[X, Y];
       
       

        void Checkerboard()
        {
            Cursor();
            for (int i = 0; i < X; i++)
            {
                for (int y = 0; y < Y; y++)
                {
                    if (Area[i, y] == Area[0, 0])
                        Area[i, y] = (char)5;
                    else if (switcher % 2 == 0)
                        Area[i, y] = 'X';
                    else
                        Area[i, y] = 'Y';
                    switcher++;
                    Console.Write(Area[i, y]);
                }
                Console.WriteLine(num);
                num--;
                switcher++;
             }
            Console.WriteLine("ABCDEFGH");
        }
       
        void Cursor()
        {
            bool saveCursorVisibile;
            int saveCursorSize;

            Console.CursorVisible = true;
            saveCursorVisibile = Console.CursorVisible;
            saveCursorSize = Console.CursorSize;
            Console.CursorSize = 100;
        }

     
        void Keys()
        {
            while (true)
            {
                var ch = Console.ReadKey().Key;
                switch (ch)
                {
                    case ConsoleKey.Enter:
                       
                        break;

                    case ConsoleKey.UpArrow:
                        {
                            if (ypos-1 < 0)
                                break;
                            ypos--;
                            break;
                        }
                    case ConsoleKey.DownArrow:
                        {
                            if (ypos+1 == Y)
                                break;
                            Area[xpos, ypos] = Area[xpos, ypos + 1];
                            ypos++;
                        break;
                        }
                    case ConsoleKey.LeftArrow:
                        {
                            if (xpos-1 < 0)
                                break;
                            xpos--;
                            break;
                        }
                    case ConsoleKey.RightArrow:
                        {
                            if (xpos+1 == X)
                                break;
                            xpos++;
                            break;
                        }
                }
                //Console.Write(Area[xpos - 1, ypos]); presouvani znaku
                Console.SetCursorPosition(xpos, ypos);
            }
        }




        static void Main(string[] args)
        {
            Checkers check = new Checkers();
            check.Checkerboard();
            Console.SetCursorPosition(0, 0);
            check.Keys();

            Console.ReadLine();

        }
    }
}


Answers (4)