13
Answers

need advice for movement on checkerboard

Adam

Adam

13y
1.7k
1
I have char[,] array Checkerboard full with symbols. Then I have done cursor movement through arrow keys. And now what I need to do is that when I pull Enter the currenct symbol save. And when now I start move with cursor, the current symbol will move also. It will be "save" in cursor. And when I pull Enter for the second time, it will save to current area when the cursor already is. Hope you understead what I want advice for. below my implemented code. Thanks

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Checkers
{
    class Checkers
    {
        public const int X = 8;
        public const int Y = 8;
        int switcher = 0;
        int num = 8;
        char i;
        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)
                            {
                                Console.SetCursorPosition(xpos, ypos);
                                Console.Write(Area[xpos, ypos]);
                                break;
                            }
                            Console.SetCursorPosition(xpos, ypos);
                            Console.Write(Area[xpos, ypos]);
                            ypos--;
                            break;
                        }
                    case ConsoleKey.DownArrow:
                        {
                            if (ypos + 1 == Y)
                            {
                                Console.SetCursorPosition(xpos, ypos);
                                Console.Write(Area[xpos, ypos]);
                                break;
                            }
                            Console.SetCursorPosition(xpos, ypos);
                            Console.Write(Area[xpos, ypos]);
                            ypos++;
                            break;
                        }
                    case ConsoleKey.LeftArrow:
                        {
                            if (xpos - 1 < 0)
                               
                            {
                                Console.SetCursorPosition(xpos, ypos);
                                Console.Write(Area[xpos, ypos]);
                                break;
                            }
                            Console.SetCursorPosition(xpos, ypos);
                            Console.Write(Area[xpos, ypos]);
                            xpos--;
                            break;
                        }
                    case ConsoleKey.RightArrow:
                        {
                            if (xpos + 1 == X)
                            {
                                Console.SetCursorPosition(xpos, ypos);
                                Console.Write(Area[xpos, ypos]);
                                break;
                            }
                            Console.SetCursorPosition(xpos, ypos);
                            Console.Write(Area[xpos, ypos]);
                            xpos++;
                            break;
                        }
                }
                Console.SetCursorPosition(xpos, ypos);
            }
        }




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

            Console.ReadLine();

        }
    }
}

Answers (13)