예제 #1
0
 public static void PrintStatus(List <int> config, Track.Point Player, ConsoleKeyInfo userInput, int data)
 {
     Console.SetCursorPosition(0, config[1] + 2);
     Console.WriteLine("#######################################################################################");
     Console.SetCursorPosition(0, config[1] + 3);
     Console.WriteLine("###### USE ARROW KEYS TO MOVE INDICES, SPACE TO CHANGE TILE, ESCAPE OR Q TO QUIT ######");
     Console.SetCursorPosition(0, config[1] + 4);
     Console.WriteLine("#######################################################################################");
     Console.SetCursorPosition(0, config[1] + 6);
     Console.Write("Step Number: N/A");
 }
예제 #2
0
        public static int[][] PopulateGrid(Track.Point Play)
        {
            Random rnd             = new Random();
            int    innerXDimension = Play.xEnd / 2;
            int    innerYDimension = Play.yEnd;

            int[][] grid = new int[innerXDimension][];
            for (int i = 0; i < grid.Length; ++i)
            {
                grid[i] = new int[innerYDimension];
            }
            for (int i = 0; i < grid.Length; ++i)
            {
                for (int j = 0; j < grid[i].Length; ++j)
                {
                    grid[i][j] = rnd.Next(0, 99) % 2;
                }
            }
            return(grid);
        }
예제 #3
0
 public static void PrintDebug(Track.Point Player, List <int> config, int[][] grid)
 {
     //Tests Player.xEnd, Player.xStart, Player.yEnd, and Player.yStart values
     Console.SetCursorPosition(Player.xEnd, Player.yEnd);
     Console.Write("O");
     Console.SetCursorPosition(Player.xStart, Player.yStart);
     Console.Write("O");
     Console.SetCursorPosition(0, config[1] + 8);
     Console.WriteLine("Last input: N/A");
     Console.SetCursorPosition(0, config[1] + 10);
     Console.WriteLine("xStart: " + Player.xStart + " xEnd: " + Player.xEnd + "        ");
     Console.SetCursorPosition(0, config[1] + 12);
     Console.WriteLine("xTrack: " + Player.xTrack + " xTrackLine: " + Player.xTrackLine + "        ");
     Console.SetCursorPosition(0, config[1] + 14);
     Console.WriteLine("yStart: " + Player.yStart + " yEnd: " + Player.yEnd + "        ");
     Console.SetCursorPosition(0, config[1] + 16);
     Console.WriteLine("yTrack: " + Player.yTrack + " yTrackLine: " + Player.yTrackLine + "        ");
     //Prints Grid inner-dimensions
     Console.SetCursorPosition(0, 25);
     Console.WriteLine("grid.Length: " + grid.Length + "\ngrid[0].Length: " + grid[0].Length);
 }
예제 #4
0
        public static List <Coords> GetConnectedNodes(int[][] grid, Track.Point Player)
        {
            int           xCurrent      = Player.xTrack / 2 - 1;
            int           yCurrent      = Player.yTrack - 1;
            int           top           = yCurrent - 1;
            int           bottom        = yCurrent + 1;
            int           right         = xCurrent + 1;
            int           left          = xCurrent - 1;
            int           currentVal    = GetCurrentValue(Player, grid);
            bool          newCoord      = false;
            List <Coords> trackedCoords = new List <Coords>();
            Coords        current       = new Coords(xCurrent, yCurrent);

            trackedCoords.Add(current);
            List <Coords> coordsToProcess = new List <Coords>();

            //TOP POINT
            if (GridPositionExists(grid, xCurrent, top))
            {
                if (SameValue(xCurrent, top, grid, currentVal))
                {
                    Coords evaluateCoord = new Coords(xCurrent, top);
                    if (!AlreadyRecorded(evaluateCoord, trackedCoords))
                    {
                        coordsToProcess.Add(evaluateCoord);
                        trackedCoords.Add(evaluateCoord);
                        newCoord = true;
                    }
                }
            }
            //BOTTOM POINT
            if (GridPositionExists(grid, xCurrent, bottom))
            {
                if (SameValue(xCurrent, bottom, grid, currentVal))
                {
                    Coords evaluateCoord = new Coords(xCurrent, bottom);
                    if (!AlreadyRecorded(evaluateCoord, trackedCoords))
                    {
                        coordsToProcess.Add(evaluateCoord);
                        trackedCoords.Add(evaluateCoord);
                        newCoord = true;
                    }
                }
            }
            //LEFT POINT
            if (GridPositionExists(grid, left, yCurrent))
            {
                if (SameValue(left, yCurrent, grid, currentVal))
                {
                    Coords evaluateCoord = new Coords(left, yCurrent);
                    if (!AlreadyRecorded(evaluateCoord, trackedCoords))
                    {
                        coordsToProcess.Add(evaluateCoord);
                        trackedCoords.Add(evaluateCoord);
                        newCoord = true;
                    }
                }
            }
            //RIGHT POINT
            if (GridPositionExists(grid, right, yCurrent))
            {
                if (SameValue(right, yCurrent, grid, currentVal))
                {
                    Coords evaluateCoord = new Coords(right, yCurrent);
                    if (!AlreadyRecorded(evaluateCoord, trackedCoords))
                    {
                        coordsToProcess.Add(evaluateCoord);
                        trackedCoords.Add(evaluateCoord);
                        newCoord = true;
                    }
                }
            }
            List <Coords> coordsProcessed = new List <Coords>();

            coordsProcessed.Add(current);
            foreach (Coords set in coordsToProcess)
            {
                if (coordsProcessed.Contains(set))
                {
                    continue;
                }
                else
                {
                    coordsProcessed.Add(set);
                    RecursiveCoordCollection(grid, set, trackedCoords, coordsProcessed);
                }
            }
            return(trackedCoords);
        }
예제 #5
0
 public static int GetCurrentValue(Track.Point Player, int[][] grid)
 {
     return(grid[Player.xTrack / 2 - 1][Player.yTrack - 1]);
 }
예제 #6
0
        public static void UpdateGridValues(int[][] grid, List <Coords> trackedCoords, Track.Point Player)
        {
            int    xPlayer = Player.xTrack / 2 - 1;
            int    yPlayer = Player.yTrack - 1;
            string newVal;
            int    newGridVal;

            if (grid[trackedCoords[0].x][trackedCoords[0].y] == 0)
            {
                newGridVal = 1;
                newVal     = "+";
            }
            else
            {
                newGridVal = 0;
                newVal     = "O";
            }
            //Prints in random sequence
            //Random rnd = new Random();
            //while(trackedCoords.Count > 0)
            //{
            //    int x = rnd.Next(0, trackedCoords.Count);
            //    grid[trackedCoords[x].x][trackedCoords[x].y] = newGridVal;
            //    Console.SetCursorPosition((trackedCoords[x].x + 1) * 2, trackedCoords[x].y + 1);
            //    Console.SetWindowPosition(0, 0);
            //    Console.Write(newVal);
            //    trackedCoords.RemoveAt(x);
            //    Thread.Sleep(15);
            //
            //}


            //Prints procedurally in the order the nodes were collected
            //foreach(Coords set in trackedCoords)
            //{
            //    grid[set.x][set.y] = newGridVal;
            //    Console.SetCursorPosition((set.x + 1) * 2, set.y + 1);
            //    Console.SetWindowPosition(0, 0);
            //    Console.Write(newVal);
            //    Thread.Sleep(15);
            //}

            //Prints radially based on closest point to Player Coords
            //Simple Insertion sort method -> O(n^2)
            for (int i = 0; i < trackedCoords.Count - 1; ++i)
            {
                for (int j = i + 1; j > 0; --j)
                {
                    double distA = GetDistance(xPlayer, yPlayer, trackedCoords[j].x, trackedCoords[j].y);
                    double distB = GetDistance(xPlayer, yPlayer, trackedCoords[j - 1].x, trackedCoords[j - 1].y);
                    if (distB > distA)
                    {
                        Coords temp = new Coords(trackedCoords[j - 1].x, trackedCoords[j - 1].y);
                        trackedCoords[j - 1] = trackedCoords[j];
                        trackedCoords[j]     = temp;
                    }
                }
            }
            for (int i = 0; i < trackedCoords.Count; ++i)
            {
                grid[trackedCoords[i].x][trackedCoords[i].y] = newGridVal;
                Console.SetCursorPosition((trackedCoords[i].x + 1) * 2, trackedCoords[i].y + 1);
                Console.SetWindowPosition(0, 0);
                Console.Write(newVal);
                Thread.Sleep(2);
            }

            //Poorly optimized radial version
            //List<Coords> orderedList = new List<Coords>();
            //double distanceMin = 99999;
            //int distanceMinIndex = 0;
            //while(trackedCoords.Count > 0)
            //{
            //    distanceMin = 99999;
            //    for (int i = 0; i < trackedCoords.Count; ++i)
            //    {
            //        double x = trackedCoords[i].x - xPlayer;
            //        double y = trackedCoords[i].y - yPlayer;
            //        x = Math.Pow(x, 2);
            //        y = Math.Pow(y, 2);
            //        double numToSquare = x + y;
            //        double dist = Math.Sqrt(numToSquare);
            //         if(dist < distanceMin)
            //        {
            //            distanceMin = dist;
            //            distanceMinIndex = i;
            //        }
            //    }
            //    orderedList.Add(trackedCoords[distanceMinIndex]);
            //    trackedCoords.RemoveAt(distanceMinIndex);
            //}
            //for(int i = 0; i < orderedList.Count; ++i)
            //{
            //    grid[orderedList[i].x][orderedList[i].y] = newGridVal;
            //    Console.SetCursorPosition((orderedList[i].x + 1) * 2, orderedList[i].y + 1);
            //    Console.SetWindowPosition(0, 0);
            //    Console.Write(newVal);
            //    Thread.Sleep(2);
            //}
        }
예제 #7
0
        static void Main(string[] args)
        {
            //######################################### INITIALIZATION #########################################
            //INCREASE WINDOW SIZE AND TAKE IN USER INPUT ON GRID SPECIFICATIONS
            Console.SetWindowSize(consoleWidth, consoleHeight);
            List <int>    config  = Init.InitializeBoard();
            List <string> borders = Init.InitializeBorders(config);

            Init.DrawBorders(config, borders);
            Init.InitializePlayerCursor(config);
            //CREATE PLAYER TRACKING OBJECT
            Track.Point Player = new Track.Point(config[0], config[1]);
            //POPULATES GRID VALUES AND PRINTS
            int[][] grid = Init.PopulateGrid(Player);
            GridManip.PrintGrid(grid);
            //HIDES CURSOR FROM BLINKING
            Console.CursorVisible = false;
            //##################################################################################################

            //######################################### DEBUG & INTERFACE #########################################
            //SET UP INTERFACE
            if (initInterface == 1)
            {
                ConsoleKeyInfo placeholder = new ConsoleKeyInfo();
                Init.PrintStatus(config, Player, placeholder, 0);
            }
            //DEBUG INITIALIZING
            if (initDebug == 1)
            {
                Init.PrintDebug(Player, config, grid);
            }
            //#####################################################################################################
            //MAY IMPLEMENT STOPWATCH LATER; MIGHT REQUIRE MULTITHREADING IN ORDER TO COUNT TIME WHILE WAITING FOR USER INPUT
            //System.Diagnostics.Stopwatch clock = new System.Diagnostics.Stopwatch();
            //clock.Start();
            //Program Run Loop, waits for user input, breaks on Escape or Q
            //#####################################################################################################

            //COUNTS INPUTS BY USER
            int data = 1;

            //##################################### MAIN GAME LOOP #####################################
            while (true)
            {
                Console.SetCursorPosition(Player.xTrack, Player.yTrack);
                Console.CursorVisible = true;
                var userInput = Console.ReadKey(true);
                Console.CursorVisible = false;
                //MOVE TRACKERS
                if (userInput.Key == ConsoleKey.RightArrow || userInput.Key == ConsoleKey.LeftArrow || userInput.Key == ConsoleKey.UpArrow || userInput.Key == ConsoleKey.DownArrow)
                {
                    bool moved = false;
                    moved = Player.MoveTracker(userInput.Key.ToString());
                    if (moved == true)
                    {
                        data++;
                    }
                }
                else if (userInput.Key == ConsoleKey.Spacebar)
                {
                    List <GridManip.Coords> coordsToChange = new List <GridManip.Coords>();
                    coordsToChange = GridManip.GetConnectedNodes(grid, Player);
                    GridManip.UpdateGridValues(grid, coordsToChange, Player);
                    data++;
                }
                else if (userInput.Key == ConsoleKey.Escape || userInput.Key == ConsoleKey.Q)
                {
                    break;
                }
                else if (userInput.Key == ConsoleKey.T) //TEST CASE
                {
                    Console.SetCursorPosition(0, 20);
                    List <GridManip.Coords> coordsToChange = new List <GridManip.Coords>();
                    coordsToChange = GridManip.GetConnectedNodes(grid, Player);
                    for (int i = 0; i < coordsToChange.Count; ++i)
                    {
                        Console.SetCursorPosition(0, i + 20);
                        Console.WriteLine("Need to change: (" + coordsToChange[i].x + "," + coordsToChange[i].y + ")                       ");
                    }
                    GridManip.UpdateGridValues(grid, coordsToChange, Player);
                }
                if (initInterface == 1)
                {
                    Init.PrintStatus(config, Player, userInput, data);
                }
                //WAS PRINTING STOPWATCH STATUS
                //Console.SetCursorPosition(0, config[1] + 7);
                //Console.Write("Running Time: " + clock.Elapsed.TotalSeconds.ToString());
            }
            //##########################################################################################
        }