Advertisement
Hristo_B

djv

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