Advertisement
Hristo_B

Console Game

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