Advertisement
Hristo_B

Final version

Sep 3rd, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 35.96 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Linq;
  7.  
  8. namespace ConsoleGame
  9. {
  10.     struct Bonuses
  11.     {
  12.         public int X;
  13.         public int Y;
  14.         public char C;
  15.         public ConsoleColor Color;
  16.     }
  17.     class ConsoleGame
  18.     {
  19.         static int score = 0;
  20.         static int gameFieldWidth;
  21.         static int gameFieldHeight;
  22.  
  23.         // 1 is wall; 0 is path; 2 is Player; 3 path && bonuss can be generated; 4 bonus
  24.         static byte[,] gameField;
  25.         static int pathEndX;
  26.         static int pathEndY;
  27.  
  28.         static int torchLight = 5;
  29.  
  30.         static int[] playerCordinates = new int[2] { 0, 0 };
  31.         static int[] exitCoordinates = new int[2];
  32.         static char playerSymbol = (char)9787;
  33.  
  34.         static double gameFieldPercentWidth = 0.6;//original 0.6
  35.         static double gameFieldPercentHeight = 0.9;//original 0.9
  36.  
  37.         static double time = 20.00;
  38.         static bool gamePause = false;
  39.         static int countMaxScorePeople = 10;
  40.  
  41.         static int level = 1;
  42.         static double scoreMultiplier = 1.00;
  43.         static int bonusToPassLevel = 5;
  44.         static int bonusesGot = 0;
  45.  
  46.         static List<Bonuses> bonuses = new List<Bonuses>();
  47.         static Random randomGen = new Random();
  48.  
  49.         private static void SaveScore(int score, string name)
  50.         {
  51.             StreamWriter writer = new StreamWriter(@"..\..\result.txt", true);
  52.  
  53.             using (writer)
  54.             {
  55.                 writer.WriteLine("{0}-{1}", name, score);
  56.             }
  57.         }
  58.  
  59.         static void CreateBonuses(int bonusCount = 1)
  60.         {
  61.             for (int i = 0; i < bonusCount; i++)
  62.             {
  63.                 Bonuses newBonus = new Bonuses();
  64.                 newBonus.X = randomGen.Next(0, gameFieldHeight - 1);
  65.                 newBonus.Y = randomGen.Next(0, gameFieldWidth - 1);
  66.                 newBonus.C = '$';
  67.                 newBonus.Color = ConsoleColor.Red;
  68.  
  69.                 while (gameField[newBonus.X, newBonus.Y] != 3)
  70.                 {
  71.                     newBonus.X = randomGen.Next(0, gameFieldHeight);
  72.                     newBonus.Y = randomGen.Next(0, gameFieldWidth);
  73.                 }
  74.                 bonuses.Add(newBonus);
  75.                 gameField[newBonus.X, newBonus.Y] = 4;
  76.             }
  77.             //PrintBonuses(bonuses);
  78.         }
  79.  
  80.         static void PrintBonuses(List<Bonuses> bonuses)
  81.         {
  82.             foreach (var item in bonuses)
  83.             {
  84.                 Console.BackgroundColor = ConsoleColor.White;
  85.                 Console.SetCursorPosition(item.Y, item.X);
  86.                 Console.ForegroundColor = item.Color;
  87.                 Console.Write(item.C);
  88.             }
  89.         }
  90.  
  91.         private static void ConsoleWrite(int left, int top, string symbol, ConsoleColor foregroundColor = ConsoleColor.White, ConsoleColor backgroundColor = ConsoleColor.Black)
  92.         {
  93.             Console.SetCursorPosition(left, top);
  94.             Console.ForegroundColor = foregroundColor;
  95.             Console.BackgroundColor = backgroundColor;
  96.             Console.Write(symbol);
  97.         }
  98.  
  99.         private static void WelcomeScreen()
  100.         {
  101.  
  102.  
  103.             bool[,] logoSymbols = new bool[,]{{false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false},
  104. {true,true,false,false,false,false,false,false,false,false,false,false,true,true,false,false,false,false,false,false,false,false,false,false,false,true,true,false,false,false,false,false,false,false,false,false,false,false,true,true,true,true,true,true,true,true,true,true,true,true,false,false,true,true,true,true,true,true,true,true,true,true,true,true,true,false},
  105. {true,true,true,false,false,false,false,false,false,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,true,true,true,true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,true,true,true,false,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,false},
  106. {true,true,true,true,false,false,false,false,false,false,true,true,true,true,false,false,false,false,false,false,false,false,false,true,true,true,true,true,true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,true,true,true,false,false,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,false},
  107. {true,true,true,true,true,false,false,false,false,true,true,true,true,true,false,false,false,false,false,false,false,false,true,true,true,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,true,true,true,false,false,false,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,false},
  108. {true,true,true,true,true,true,false,false,true,true,true,true,true,true,false,false,false,false,false,false,false,true,true,true,false,false,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,false,false,true,true,true,false,false,false,false,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,false},
  109. {true,true,true,false,true,true,true,true,true,true,false,true,true,true,false,false,false,false,false,false,true,true,true,false,false,false,false,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,true,true,true,false,false,false,false,false,false,false,true,true,true,true,true,true,true,true,true,true,true,true,true,false},
  110. {true,true,true,false,false,true,true,true,true,false,false,true,true,true,false,false,false,false,false,true,true,true,true,true,true,true,true,true,true,true,true,true,true,false,false,false,false,false,false,false,false,true,true,true,false,false,false,false,false,false,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,false},
  111. {true,true,true,false,false,false,true,true,false,false,false,true,true,true,false,false,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,true,true,true,false,false,false,false,false,false,true,true,true,false,false,false,false,false,false,false,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,false},
  112. {true,true,true,false,false,false,false,false,false,false,false,true,true,true,false,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,false,false,true,true,true,false,false,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,false},
  113. {true,true,true,false,false,false,false,false,false,false,false,true,true,true,false,false,true,true,true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,true,true,true,false,false,true,true,true,true,true,true,true,true,true,true,true,true,false,false,true,true,true,true,true,true,true,true,true,true,true,true,true,false}};
  114.             bool[,] drawnSymbols = new bool[1000, 1000];
  115.             bool animate = true;
  116.             for (int row = 0; row < 11; row++)
  117.             {
  118.                 for (int col = 0; col < 66; col++)
  119.                 {
  120.                     if (Console.KeyAvailable)
  121.                     {
  122.                         animate = false;
  123.                     }
  124.                     bool sleep = false;
  125.                     if (logoSymbols[row, col])
  126.                     {
  127.                         ConsoleWrite(6 + col, 1 + row, " ", ConsoleColor.Black, ConsoleColor.Gray);
  128.                         drawnSymbols[1 + row, 6 + col] = true;
  129.                         ConsoleWrite(8 + col, 1 + row, " ", ConsoleColor.Black, ConsoleColor.Gray);
  130.                         drawnSymbols[1 + row, 8 + col] = true;
  131.                         sleep = true;
  132.                     }
  133.                     if ((drawnSymbols[1 + row, 8 + col] && drawnSymbols[1 + row, 6 + col]))
  134.                     {
  135.                         ConsoleWrite(6 + col, 1 + row, " ", ConsoleColor.DarkRed, ConsoleColor.DarkCyan);
  136.                     }
  137.                     if (sleep && animate)
  138.                     {
  139.                         Thread.Sleep(7);
  140.                         sleep = false;
  141.                     }
  142.                 }
  143.             }
  144.  
  145.             Console.ResetColor();
  146.             ConsoleWrite(5, 17, "Press \"S\" to start the game!", ConsoleColor.Gray);
  147.             ConsoleWrite(45, 17, "Press \"H\" to view high score!", ConsoleColor.Gray);
  148.             bool correctKeyPressed = false;
  149.             while (correctKeyPressed == false)
  150.             {
  151.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  152.                 if (pressedKey.Key == ConsoleKey.S)
  153.                 {
  154.                     Console.Clear();
  155.                     StartGame();
  156.                     correctKeyPressed = true;
  157.                 }
  158.                 else if (pressedKey.Key == ConsoleKey.H)
  159.                 {
  160.                     Console.Clear();
  161.                     PrintScore();
  162.                     correctKeyPressed = true;
  163.                 }
  164.             }
  165.         }
  166.  
  167.         static void StartGame()
  168.         {
  169.             //ThreadStart somemethod = new ThreadStart(Music);
  170.             //Thread somethread = new Thread(somemethod);
  171.             //somethread.Start();
  172.             RightSideBar();
  173.             GenerateField();
  174.             CreateBonuses(9);
  175.             DrawGameField();
  176.  
  177.             while (time >= 0.1) // apply logic here
  178.             {
  179.                 CheckForPressedKey();
  180.                 ConsoleWrite(gameFieldWidth + 2, 2, string.Format(" Score: {0}", score), ConsoleColor.Blue);
  181.                 ConsoleWrite(gameFieldWidth + 2, 0, string.Format(" Remaining Time: {0:0:00}", time), ConsoleColor.Blue);
  182.                 ConsoleWrite(gameFieldWidth + 2, 4, string.Format(" Level: {0}", level), ConsoleColor.Blue);
  183.                 ConsoleWrite(gameFieldWidth + 15, 4, string.Format(" Multiplier: {0}", scoreMultiplier), ConsoleColor.Blue);
  184.                 ConsoleWrite(gameFieldWidth + 2, 6, string.Format(" Bonuses to get: {0}    ", bonusToPassLevel - bonusesGot), ConsoleColor.Blue);
  185.                 time -= 0.1;
  186.                 Thread.Sleep(100);
  187.             }
  188.             Console.Clear();
  189.             ConsoleWrite(30, 4, "!!!GAME OVER!!!", ConsoleColor.Red);
  190.             ConsoleWrite(30, 5, "Enter your name: ", ConsoleColor.Gray);
  191.             string usersName = Console.ReadLine();
  192.             Console.SetCursorPosition(30, 6);
  193.             SaveScore(score, usersName);
  194.             try
  195.             {
  196.                 PrintScore();
  197.             }
  198.             catch (FileNotFoundException)
  199.             {
  200.  
  201.                 Console.WriteLine("The Score file can't be found!");
  202.             }
  203.             catch (DirectoryNotFoundException)
  204.             {
  205.                 Console.WriteLine("The directory of the score file can't be found!");
  206.             }
  207.             catch (FormatException)
  208.             {
  209.                 Console.WriteLine("Some error occured while trying to extract the score!");
  210.             }
  211.             Environment.Exit(0);
  212.         }
  213.  
  214.         private static void RightSideBar()
  215.         {
  216.             //Commands
  217.             ConsoleWrite(gameFieldWidth + 2, 10, " Commands: ", ConsoleColor.Gray);
  218.             ConsoleWrite(gameFieldWidth + 2, 12, " P - Pause", ConsoleColor.Gray);
  219.             ConsoleWrite(gameFieldWidth + 2, 14, " E - Exit", ConsoleColor.Gray);
  220.             ConsoleWrite(gameFieldWidth + 2, 16, " S - Save", ConsoleColor.Gray);
  221.             ConsoleWrite(gameFieldWidth + 2, 18, " L - Load game", ConsoleColor.Gray);
  222.             //horizontal red line
  223.             ConsoleWrite(gameFieldWidth + 1, 7, new string('\u2580', Console.WindowWidth - (gameFieldWidth + 1)), ConsoleColor.Red);
  224.             //vertical red line
  225.             for (int i = 0; i < gameFieldHeight; i++)
  226.             {
  227.                 ConsoleWrite(gameFieldWidth + 1, i, '\u2588'.ToString(), ConsoleColor.Red);
  228.             }
  229.         }
  230.  
  231.         static void Main()
  232.         {
  233.             GameSettings();
  234.             WelcomeScreen();
  235.         }
  236.  
  237.         private static void SaveGame()
  238.         {
  239.             ConsoleWrite(0, 23, "Please enter the name of the file: ");
  240.             string savedGameName = Console.ReadLine();
  241.             StreamWriter writer = new StreamWriter(@"..\..\" + savedGameName + ".txt");
  242.  
  243.             using (writer)
  244.             {
  245.                 writer.WriteLine(time);
  246.                 writer.WriteLine(score);
  247.                 writer.WriteLine(playerCordinates[0]);
  248.                 writer.WriteLine(playerCordinates[1]);
  249.  
  250.                 for (int loopRow = 0; loopRow < gameFieldHeight; loopRow++)
  251.                 {
  252.                     for (int loopCol = 0; loopCol < gameFieldWidth; loopCol++)
  253.                     {
  254.                         writer.Write(gameField[loopRow, loopCol] + " ");
  255.                     }
  256.                     writer.WriteLine();
  257.                 }
  258.             }
  259.  
  260.             //clear output text
  261.             ConsoleWrite(0, 23, "                                                                ");
  262.         }
  263.  
  264.         private static void LoadSavedGame()
  265.         {
  266.             try
  267.             {
  268.                 Console.SetCursorPosition(0, 23);
  269.                 Console.Write("Please enter the name of the file with the saved game: ");
  270.                 string savedGame = Console.ReadLine();
  271.                 StreamReader reader = new StreamReader(@"..\..\" + savedGame + ".txt");
  272.  
  273.                 using (reader)
  274.                 {
  275.                     time = double.Parse(reader.ReadLine());
  276.                     score = int.Parse(reader.ReadLine());
  277.                     playerCordinates[0] = int.Parse(reader.ReadLine());
  278.                     playerCordinates[1] = int.Parse(reader.ReadLine());
  279.  
  280.                     for (int loopRow = 0; loopRow < gameFieldHeight; loopRow++)
  281.                     {
  282.                         string[] currentLine = reader.ReadLine().Split(' ');
  283.  
  284.                         for (int loopCol = 0; loopCol < gameFieldWidth; loopCol++)
  285.                         {
  286.                             gameField[loopRow, loopCol] = byte.Parse(currentLine[loopCol]);
  287.                         }
  288.                     }
  289.                 }
  290.  
  291.                 //clear output text
  292.                 ConsoleWrite(0, 23, "                                                                ");
  293.             }
  294.             catch (FileNotFoundException)
  295.             {
  296.                 Console.WriteLine("This file can not me found!");
  297.             }
  298.         }
  299.  
  300.         private static void CheckForPressedKey()
  301.         {
  302.             if (Console.KeyAvailable)
  303.             {
  304.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  305.                 while (Console.KeyAvailable)
  306.                 {
  307.                     Console.ReadKey(true);
  308.                 }
  309.                 if (pressedKey.Key == ConsoleKey.LeftArrow && gamePause == false)
  310.                 {
  311.                     MovePlayer(0, -1);
  312.                     CleanBattleField("left");
  313.                     DrawGameField();
  314.                 }
  315.                 else if (pressedKey.Key == ConsoleKey.RightArrow && gamePause == false)
  316.                 {
  317.                     MovePlayer(0, 1);
  318.                     CleanBattleField("right");
  319.                     DrawGameField();
  320.                 }
  321.                 else if (pressedKey.Key == ConsoleKey.UpArrow && gamePause == false)
  322.                 {
  323.                     MovePlayer(-1, 0);
  324.                     CleanBattleField("up");
  325.                     DrawGameField();
  326.                
  327.                 }
  328.                 else if (pressedKey.Key == ConsoleKey.DownArrow && gamePause == false)
  329.                 {
  330.                     MovePlayer(1, 0);
  331.                     CleanBattleField("down");
  332.                     DrawGameField();
  333.                    
  334.                
  335.                 }
  336.                 else if (pressedKey.Key == ConsoleKey.E && gamePause == false)
  337.                 {
  338.                     Console.BackgroundColor = ConsoleColor.Black;
  339.                     Console.Clear();
  340.                     Console.ForegroundColor = ConsoleColor.Red;
  341.                     Console.WriteLine("Good bye!");
  342.                     Console.ForegroundColor = ConsoleColor.Gray;
  343.                     Environment.Exit(0);
  344.                 }
  345.                 else if (pressedKey.Key == ConsoleKey.P)
  346.                 {
  347.                     if (gamePause)
  348.                     {
  349.                         gamePause = false;
  350.                     }
  351.                     else
  352.                     {
  353.                         gamePause = true;
  354.                         while (gamePause)
  355.                         {
  356.                             CheckForPressedKey();
  357.                         }
  358.                     }
  359.                 }
  360.                 else if (pressedKey.Key == ConsoleKey.S)
  361.                 {
  362.                     SaveGame();
  363.                 }
  364.                 else if (pressedKey.Key == ConsoleKey.L)
  365.                 {
  366.                     LoadSavedGame();
  367.                     DrawGameField();
  368.                     //CreateBonuses();
  369.                 }
  370.                 else if (pressedKey.Key == ConsoleKey.N)
  371.                 {
  372.                     time += 20;
  373.                 }
  374.                 else if (pressedKey.Key == ConsoleKey.M)
  375.                 {
  376.                     time -= 20;
  377.                 }
  378.             }
  379.         }
  380.  
  381.         private static void CleanBattleField(string direction)
  382.         {
  383.             if (direction == "left")
  384.             {
  385.                 if (playerCordinates[1] + torchLight < gameFieldWidth)
  386.                 {
  387.                     for (int i = 0; i < gameFieldHeight; i++)
  388.                     {
  389.                         ConsoleWrite(playerCordinates[1] + torchLight + 1, i, " ", ConsoleColor.Black);
  390.                     }
  391.                 }
  392.             }
  393.             else if (direction == "right")
  394.             {
  395.                 if (playerCordinates[1] - torchLight > 0)
  396.                 {
  397.                     for (int i = 0; i < gameFieldHeight; i++)
  398.                     {
  399.                         ConsoleWrite(playerCordinates[1] - torchLight - 1, i, " ", ConsoleColor.Black);
  400.                     }
  401.                 }
  402.             }
  403.             else if (direction == "up")
  404.             {
  405.                 if (playerCordinates[0] + torchLight < gameFieldHeight)
  406.                 {
  407.                     for (int i = 0; i < gameFieldWidth; i++)
  408.                     {
  409.                         ConsoleWrite(i, playerCordinates[0] + torchLight + 1, " ", ConsoleColor.Black);
  410.                     }
  411.                 }
  412.             }
  413.             else if (direction == "down")
  414.             {
  415.                 if (playerCordinates[0] - torchLight > 0)
  416.                 {
  417.                     for (int i = 0; i < gameFieldWidth; i++)
  418.                     {
  419.                         ConsoleWrite(i, playerCordinates[0] - torchLight - 1, " ", ConsoleColor.Black);
  420.                     }
  421.                 }
  422.             }
  423.         }
  424.  
  425.         private static void MovePlayer(int rowChange, int colChange)
  426.         {
  427.             int newRowAfterMove = playerCordinates[0] + rowChange;
  428.             int newColAfterMove = playerCordinates[1] + colChange;
  429.             bool bonusGot = false;
  430.             if (newRowAfterMove >= 0 && newColAfterMove >= 0 && newRowAfterMove < gameFieldHeight && newColAfterMove < gameFieldWidth && gameField[newRowAfterMove, newColAfterMove] != 1)
  431.             {
  432.                 if (gameField[newRowAfterMove, newColAfterMove] == 4)
  433.                 {
  434.                     score += (int)(100 * scoreMultiplier);
  435.                     time += 5;
  436.                     gameField[newRowAfterMove, newColAfterMove] = 0;
  437.                     bonusGot = true;
  438.                     if (bonusesGot < bonusToPassLevel)
  439.                     {
  440.                         bonusesGot++;
  441.                     }
  442.                     if (bonusesGot == bonusToPassLevel)
  443.                     {
  444.                         gameField[exitCoordinates[0], exitCoordinates[1]] = 5;
  445.                     }
  446.                 }
  447.                 if (gameField[newRowAfterMove, newColAfterMove] == 5)
  448.                 {
  449.                     gameField[newRowAfterMove, newColAfterMove] = 0;
  450.                     Console.Clear();
  451.                     bonuses.Clear();
  452.                     playerCordinates[0] = 0;
  453.                     playerCordinates[1] = 0;
  454.                     scoreMultiplier += 0.2;
  455.                     level++;
  456.                     bonusesGot = 0;
  457.                     bonusToPassLevel += 5;
  458.                     StartGame();
  459.  
  460.                 }
  461.                 ConsoleWrite(playerCordinates[1], playerCordinates[0], " ", ConsoleColor.White, ConsoleColor.White);
  462.                 playerCordinates[0] = newRowAfterMove;
  463.                 playerCordinates[1] = newColAfterMove;
  464.                 if (bonusGot)
  465.                 {
  466.                     foreach (var item in bonuses)
  467.                     {
  468.                         if (item.X == playerCordinates[0] && item.Y == playerCordinates[1])
  469.                         {
  470.                             bonuses.Remove(item);
  471.                             break;
  472.                         }
  473.                     }
  474.                     CreateBonuses();
  475.                     PrintBonuses(bonuses);
  476.                     gameField[newRowAfterMove, newColAfterMove] = 0;
  477.                     bonusGot = false;
  478.                 }
  479.                 ConsoleWrite(playerCordinates[1], playerCordinates[0], playerSymbol.ToString(), ConsoleColor.Blue, ConsoleColor.White);
  480.             }
  481.         }
  482.  
  483.         private static void GameSettings()
  484.         {
  485.             Console.BufferWidth = Console.WindowWidth;
  486.             Console.BufferHeight = Console.WindowHeight;
  487.             gameFieldWidth = (int)(Console.WindowWidth * gameFieldPercentWidth);
  488.             gameFieldHeight = (int)(Console.WindowHeight * gameFieldPercentHeight);
  489.             gameField = new byte[gameFieldHeight, gameFieldWidth];
  490.             Console.CursorVisible = false;
  491.             Console.Title = "-=MaZe Game=-";
  492.         }
  493.  
  494.         private static void DrawGameField()
  495.         {
  496.             for (int a = -torchLight; a <= torchLight; a++)
  497.             {
  498.                 for (int b = -torchLight; b <= torchLight; b++)
  499.                 {
  500.                     if ((playerCordinates[0] + a) < 0 || (playerCordinates[1] + b) < 0 || (playerCordinates[1] + b) >= gameFieldWidth || (playerCordinates[0] + a) >= gameFieldHeight)
  501.                     {
  502.                         continue;
  503.                     }
  504.                     WriteSymbolOnGameFieldPosition(playerCordinates[0] + a, playerCordinates[1] + b);
  505.                 }
  506.             }
  507.         }
  508.  
  509.         private static void WriteSymbolOnGameFieldPosition(int loopRow, int loopCol)
  510.         {
  511.             bool teleportFound = false;
  512.             ConsoleColor backgroundColor = ConsoleColor.Black;
  513.             ConsoleColor foregroundColor = ConsoleColor.White;
  514.             string printedSymbol = string.Empty;
  515.             if (gameField[loopRow, loopCol] == 4)
  516.             {
  517.                 backgroundColor = ConsoleColor.White;
  518.                 foregroundColor = ConsoleColor.Red;
  519.                 printedSymbol = "$";
  520.             }
  521.             else
  522.             {
  523.                 if (gameField[loopRow, loopCol] == 0 || gameField[loopRow, loopCol] == 3)
  524.                 {
  525.                     if (loopRow == pathEndY && loopCol == pathEndX)
  526.                     {
  527.                         backgroundColor = ConsoleColor.Blue;
  528.                         foregroundColor = ConsoleColor.White;
  529.  
  530.  
  531.                         teleportFound = true;
  532.                     }
  533.                     else
  534.                     {
  535.                         backgroundColor = ConsoleColor.White;
  536.                     }
  537.                 }
  538.                 if (loopCol == 0 && loopRow == 0)
  539.                 {
  540.                     backgroundColor = ConsoleColor.White;
  541.                     printedSymbol = " ";
  542.                 }
  543.                 else
  544.                 {
  545.                     //Variable "teleportFound" is true if exit(teleport to next lvl) is found.
  546.                     printedSymbol = " ";
  547.                     if (teleportFound)
  548.                     {
  549.                         printedSymbol = "@";
  550.                         exitCoordinates[0] = loopRow;
  551.                         exitCoordinates[1] = loopCol;
  552.                         gameField[loopRow, loopCol] = 1;
  553.                     }
  554.                 }
  555.             }
  556.             ConsoleWrite(loopCol, loopRow, printedSymbol, foregroundColor, backgroundColor);
  557.             ConsoleWrite(playerCordinates[1], playerCordinates[0], playerSymbol.ToString(), ConsoleColor.Blue, ConsoleColor.White);
  558.         }
  559.  
  560.         private static void GenerateField()
  561.         {
  562.             bool[,] visitedGameField = new bool[gameFieldHeight, gameFieldWidth];
  563.             Random randomPicker = new Random();
  564.  
  565.             Stack<int> rowStack = new Stack<int>();
  566.             Stack<int> colStack = new Stack<int>();
  567.  
  568.             int currentCelRow = 0;
  569.             int currentCelCol = 0;
  570.             int countVisits = 1;
  571.  
  572.             while (countVisits > 0)
  573.             {
  574.  
  575.                 if (!visitedGameField[currentCelRow, currentCelCol])
  576.                 {
  577.                     visitedGameField[currentCelRow, currentCelCol] = true;
  578.                     gameField[currentCelRow, currentCelCol] = 3;
  579.                 }
  580.                 List<int[]> neighbours = GetNeighbours(currentCelRow, currentCelCol, visitedGameField);
  581.                 if (neighbours.Count > 0)
  582.                 {
  583.                     //Pushing random cell to the stack
  584.                     int[] randomCell = neighbours[randomPicker.Next(0, neighbours.Count)];
  585.                     rowStack.Push(randomCell[0]);
  586.                     colStack.Push(randomCell[1]);
  587.                     currentCelCol = randomCell[1];
  588.                     currentCelRow = randomCell[0];
  589.                     List<int[]> neighboursToMakeUnavailable = GetNeighbours(currentCelRow, currentCelCol, visitedGameField);
  590.                     if (neighboursToMakeUnavailable.Count > 1)
  591.                     {
  592.                         int[] randomtoUnavailable = neighboursToMakeUnavailable[randomPicker.Next(0, neighboursToMakeUnavailable.Count)];
  593.                         visitedGameField[randomtoUnavailable[0], randomtoUnavailable[1]] = true;
  594.                         gameField[randomtoUnavailable[0], randomtoUnavailable[1]] = 1;
  595.                     }
  596.                     if (currentCelCol > (gameFieldWidth / 4) && currentCelRow > (gameFieldHeight / 4))
  597.                     {
  598.                         pathEndX = currentCelCol;
  599.                         pathEndY = currentCelRow;
  600.                     }
  601.                 }
  602.                 else if (rowStack.Count > 0)
  603.                 {
  604.                     currentCelCol = colStack.Pop();
  605.                     currentCelRow = rowStack.Pop();
  606.                 }
  607.                 else
  608.                 {
  609.                     countVisits = 0;
  610.                 }
  611.             }
  612.         }
  613.  
  614.         private static List<int[]> GetNeighbours(int currentCelRow, int currentCelCol, bool[,] visitedGameField)
  615.         {
  616.             int[][] possibilities = new int[4][] { new int[2] { 0, -1 }, new int[2] { -1, 0 }, new int[2] { 0, 1 }, new int[2] { 1, 0 } };
  617.  
  618.             List<int[]> neighbours = new List<int[]>();
  619.  
  620.             foreach (var cell in possibilities)
  621.             {
  622.                 if ((currentCelCol + cell[1]) >= 0 && (currentCelCol + cell[1]) < gameFieldWidth && (currentCelRow + cell[0]) >= 0 && (currentCelRow + cell[0]) < gameFieldHeight && !visitedGameField[currentCelRow + cell[0], currentCelCol + cell[1]])
  623.                 {
  624.                     neighbours.Add(new int[] { currentCelRow + cell[0], currentCelCol + cell[1] });
  625.                 }
  626.             }
  627.  
  628.             return neighbours;
  629.         }
  630.  
  631.         private static void PrintScore()
  632.         {
  633.  
  634.             StreamReader reader = new StreamReader(@"..\..\result.txt");
  635.  
  636.             var items = new List<KeyValuePair<string, int>>();
  637.  
  638.             using (reader)
  639.             {
  640.                 string line = reader.ReadLine();
  641.                 while (line != null)
  642.                 {
  643.                     int indexOfDash = line.IndexOf('-');
  644.                     StringBuilder name = new StringBuilder();
  645.                     StringBuilder scoreString = new StringBuilder();
  646.                     for (int i = 0; i < line.Length - 1; i++)
  647.                     {
  648.                         if (i < indexOfDash)
  649.                         {
  650.                             name.Append(line[i]);
  651.                         }
  652.                         else
  653.                         {
  654.                             scoreString.Append(line[i + 1]);
  655.                         }
  656.                     }
  657.                     if (scoreString.ToString() != "")
  658.                     {
  659.                         int currentScore = int.Parse(scoreString.ToString());
  660.                         items.Add(new KeyValuePair<string, int>(name.ToString(), currentScore));
  661.                     }
  662.  
  663.                     line = reader.ReadLine();
  664.                 }
  665.                 var sortedList = items.OrderByDescending(x => x.Value).Take(countMaxScorePeople);
  666.                 Console.SetCursorPosition(0, 7);
  667.                 Console.WriteLine("Top {0} Score:", countMaxScorePeople);
  668.                 foreach (var pair in sortedList)
  669.                 {
  670.                     Console.WriteLine("{0} - {1}", pair.Key, pair.Value);
  671.                 }
  672.             }
  673.  
  674.         }
  675.  
  676.         private static void Music()
  677.         {
  678.             Console.Beep(659, 125);
  679.             Console.Beep(659, 125);
  680.             Thread.Sleep(125);
  681.             Console.Beep(659, 125);
  682.             Thread.Sleep(167);
  683.             Console.Beep(523, 125);
  684.             Console.Beep(659, 125);
  685.             Thread.Sleep(125);
  686.             Console.Beep(784, 125);
  687.             Thread.Sleep(375);
  688.             Console.Beep(392, 125);
  689.             Thread.Sleep(375);
  690.             Console.Beep(523, 125);
  691.             Thread.Sleep(250);
  692.             Console.Beep(392, 125);
  693.             Thread.Sleep(250);
  694.             Console.Beep(330, 125);
  695.             Thread.Sleep(250);
  696.             Console.Beep(440, 125);
  697.             Thread.Sleep(125);
  698.             Console.Beep(494, 125);
  699.             Thread.Sleep(125);
  700.             Console.Beep(466, 125);
  701.             Thread.Sleep(42);
  702.             Console.Beep(440, 125);
  703.             Thread.Sleep(125);
  704.             Console.Beep(392, 125);
  705.             Thread.Sleep(125);
  706.             Console.Beep(659, 125);
  707.             Thread.Sleep(125);
  708.             Console.Beep(784, 125);
  709.             Thread.Sleep(125);
  710.             Console.Beep(880, 125);
  711.             Thread.Sleep(125);
  712.             Console.Beep(698, 125);
  713.             Console.Beep(784, 125);
  714.             Thread.Sleep(125);
  715.             Console.Beep(659, 125);
  716.             Thread.Sleep(125);
  717.             Console.Beep(523, 125);
  718.             Thread.Sleep(125);
  719.             Console.Beep(587, 125);
  720.             Console.Beep(494, 125);
  721.             Thread.Sleep(125);
  722.             Console.Beep(523, 125);
  723.             Thread.Sleep(250);
  724.             Console.Beep(392, 125);
  725.             Thread.Sleep(250);
  726.             Console.Beep(330, 125);
  727.             Thread.Sleep(250);
  728.             Console.Beep(440, 125);
  729.             Thread.Sleep(125);
  730.             Console.Beep(494, 125);
  731.             Thread.Sleep(125);
  732.             Console.Beep(466, 125);
  733.             Thread.Sleep(42);
  734.             Console.Beep(440, 125);
  735.             Thread.Sleep(125);
  736.             Console.Beep(392, 125);
  737.             Thread.Sleep(125);
  738.             Console.Beep(659, 125);
  739.             Thread.Sleep(125);
  740.             Console.Beep(784, 125);
  741.             Thread.Sleep(125);
  742.             Console.Beep(880, 125);
  743.             Thread.Sleep(125);
  744.             Console.Beep(698, 125);
  745.             Console.Beep(784, 125);
  746.             Thread.Sleep(125);
  747.             Console.Beep(659, 125);
  748.             Thread.Sleep(125);
  749.             Console.Beep(523, 125);
  750.             Thread.Sleep(125);
  751.             Console.Beep(587, 125);
  752.             Console.Beep(494, 125);
  753.             Thread.Sleep(375);
  754.             Console.Beep(784, 125);
  755.             Console.Beep(740, 125);
  756.             Console.Beep(698, 125);
  757.             Thread.Sleep(42);
  758.             Console.Beep(622, 125);
  759.             Thread.Sleep(125);
  760.             Console.Beep(659, 125);
  761.             Thread.Sleep(167);
  762.             Console.Beep(415, 125);
  763.             Console.Beep(440, 125);
  764.             Console.Beep(523, 125);
  765.             Thread.Sleep(125);
  766.             Console.Beep(440, 125);
  767.             Console.Beep(523, 125);
  768.             Console.Beep(587, 125);
  769.             Thread.Sleep(250);
  770.             Console.Beep(784, 125);
  771.             Console.Beep(740, 125);
  772.             Console.Beep(698, 125);
  773.             Thread.Sleep(42);
  774.             Console.Beep(622, 125);
  775.             Thread.Sleep(125);
  776.             Console.Beep(659, 125);
  777.             Thread.Sleep(167);
  778.             Console.Beep(698, 125);
  779.             Thread.Sleep(125);
  780.             Console.Beep(698, 125);
  781.             Console.Beep(698, 125);
  782.             Thread.Sleep(625);
  783.             Console.Beep(784, 125);
  784.             Console.Beep(740, 125);
  785.             Console.Beep(698, 125);
  786.             Thread.Sleep(42);
  787.             Console.Beep(622, 125);
  788.             Thread.Sleep(125);
  789.             Console.Beep(659, 125);
  790.             Thread.Sleep(167);
  791.             Console.Beep(415, 125);
  792.             Console.Beep(440, 125);
  793.             Console.Beep(523, 125);
  794.             Thread.Sleep(125);
  795.             Console.Beep(440, 125);
  796.             Console.Beep(523, 125);
  797.             Console.Beep(587, 125);
  798.             Thread.Sleep(250);
  799.             Console.Beep(622, 125);
  800.             Thread.Sleep(250);
  801.             Console.Beep(587, 125);
  802.             Thread.Sleep(250);
  803.             Console.Beep(523, 125);
  804.             Thread.Sleep(1125);
  805.             Console.Beep(784, 125);
  806.             Console.Beep(740, 125);
  807.             Console.Beep(698, 125);
  808.             Thread.Sleep(42);
  809.             Console.Beep(622, 125);
  810.             Thread.Sleep(125);
  811.             Console.Beep(659, 125);
  812.             Thread.Sleep(167);
  813.             Console.Beep(415, 125);
  814.             Console.Beep(440, 125);
  815.             Console.Beep(523, 125);
  816.             Thread.Sleep(125);
  817.             Console.Beep(440, 125);
  818.             Console.Beep(523, 125);
  819.             Console.Beep(587, 125);
  820.             Thread.Sleep(250);
  821.             Console.Beep(784, 125);
  822.             Console.Beep(740, 125);
  823.             Console.Beep(698, 125);
  824.             Thread.Sleep(42);
  825.             Console.Beep(622, 125);
  826.             Thread.Sleep(125);
  827.             Console.Beep(659, 125);
  828.             Thread.Sleep(167);
  829.             Console.Beep(698, 125);
  830.             Thread.Sleep(125);
  831.             Console.Beep(698, 125);
  832.             Console.Beep(698, 125);
  833.             Thread.Sleep(625);
  834.             Console.Beep(784, 125);
  835.             Console.Beep(740, 125);
  836.             Console.Beep(698, 125);
  837.             Thread.Sleep(42);
  838.             Console.Beep(622, 125);
  839.             Thread.Sleep(125);
  840.             Console.Beep(659, 125);
  841.             Thread.Sleep(167);
  842.             Console.Beep(415, 125);
  843.             Console.Beep(440, 125);
  844.             Console.Beep(523, 125);
  845.             Thread.Sleep(125);
  846.             Console.Beep(440, 125);
  847.             Console.Beep(523, 125);
  848.             Console.Beep(587, 125);
  849.             Thread.Sleep(250);
  850.             Console.Beep(622, 125);
  851.             Thread.Sleep(250);
  852.             Console.Beep(587, 125);
  853.             Thread.Sleep(250);
  854.             Console.Beep(523, 125);
  855.             Thread.Sleep(625);
  856.             Music();
  857.         }
  858.     }
  859. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement