Advertisement
Guest User

Robot mine game [C#]

a guest
Nov 16th, 2017
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.20 KB | None | 0 0
  1.     class GameEngine {
  2.         private bool success;
  3.         private bool exploded;
  4.  
  5.         public GameEngine() {
  6.             var play = true;
  7.  
  8.             while (play) {
  9.                 Console.Clear();
  10.                 success = false;
  11.                 exploded = false;
  12.                 play = DoGameLoop();
  13.             }
  14.         }
  15.  
  16.         private bool DoGameLoop() {
  17.             var map = CreateMap();
  18.  
  19.             Console.Clear();
  20.             Console.WriteLine("Your map:");
  21.             Console.WriteLine();
  22.             foreach (var mapFragment in map) {
  23.                 Console.WriteLine(mapFragment.Value);
  24.             }
  25.  
  26.             var path = GetPath();
  27.  
  28.             var resultMap = MoveRobot(map, path);
  29.  
  30.             Console.Clear();
  31.  
  32.             if (success) {
  33.                 Console.WriteLine("Congratulations! You successfully navigated the robot through the minefield.\nYour path:");
  34.                 Console.WriteLine();
  35.  
  36.                 foreach (var resultMapFragment in resultMap) {
  37.                     Console.WriteLine(resultMapFragment.Value);
  38.                 }
  39.             }
  40.             else {
  41.                 if (exploded) {
  42.                     Console.WriteLine("GAME OVER\nYour robot exploded.");
  43.                     Console.WriteLine();
  44.  
  45.                     foreach (var resultMapFragment in resultMap) {
  46.                         Console.WriteLine(resultMapFragment.Value);
  47.                     }
  48.                 }
  49.                 else {
  50.                     if (!path.Contains("I")) {
  51.                         Console.WriteLine("Fail. You did not navigate the robot successfully through the minefield\nDid you start the robot's engine?\nYour path:");
  52.                     }
  53.                     else {
  54.                         Console.WriteLine("Fail. You did not navigate the robot successfully through the minefield.\nYour path:");
  55.                     }
  56.                     Console.WriteLine();
  57.  
  58.                     foreach (var resultMapFragment in resultMap) {
  59.                         Console.WriteLine(resultMapFragment.Value);
  60.                     }
  61.                 }
  62.             }
  63.  
  64.             Console.WriteLine();
  65.             Console.WriteLine("Would you like to play again? (y/n)");
  66.             var play = Console.ReadLine() == "y" ? true : false;
  67.             return play;
  68.         }
  69.  
  70.  
  71.         private Dictionary<int, string> CreateMap() {
  72.             Console.WriteLine("Please enter the map, line by line.\nType \"exit\" to exit map creation mode.\nType \"clear\" to clear entered map fragments and start over.");
  73.             Console.WriteLine("-----------------------------------------------------------------");
  74.             Console.WriteLine();
  75.  
  76.             var map = new Dictionary<int, string>();
  77.             var exit = false;
  78.             var i = 0;
  79.  
  80.             while (!exit) {
  81.                 var mapFragment = Console.ReadLine();
  82.  
  83.                 if (mapFragment == "exit") {
  84.                     break;
  85.                 }
  86.  
  87.                 if (mapFragment == "clear") {
  88.                     i = 0;
  89.                     map.Clear();
  90.                     continue;
  91.                 }
  92.  
  93.                 //Check for non allowed characters
  94.                 if (!Regex.IsMatch(mapFragment, "^[0+M*]+$")) {
  95.                     Console.WriteLine("You entered an invalid character. Please enter a new line.");
  96.                     continue;
  97.                 }
  98.  
  99.                 //Double robot check
  100.                 var currLineDoubleRobot = mapFragment.ToCharArray()
  101.                                                 .GroupBy(x => x)
  102.                                                 .Where(y => y.Count() > 1)
  103.                                                 .Select(z => z.Key).Contains('M');
  104.  
  105.                 var doubleRobot = false;
  106.  
  107.                 foreach (var mapFrag in map) {
  108.                     if (mapFrag.Value.Contains("M")) {
  109.                         doubleRobot = true;
  110.                     }
  111.                 }
  112.  
  113.                 if (currLineDoubleRobot || (doubleRobot && mapFragment.Contains("M"))) {
  114.                     Console.WriteLine("You already placed the robot. Please enter a new line:");
  115.                     continue;
  116.                 }
  117.  
  118.  
  119.                 map.Add(i, mapFragment);
  120.                 i++;
  121.             }
  122.  
  123.             return map;
  124.         }
  125.  
  126.         private string GetPath() {
  127.             Console.WriteLine("-----------------------------------------------------------------");
  128.             Console.WriteLine();
  129.             Console.WriteLine("Please enter your path:");
  130.  
  131.  
  132.             while (true) {
  133.                 var path = Console.ReadLine();
  134.                 if (Regex.IsMatch(path, @"^[I\-NSEO]+$")) {
  135.                     return path;
  136.                 }
  137.  
  138.                 Console.WriteLine("Invalid characters in path. Enter path again:");
  139.             }
  140.         }
  141.  
  142.         private Dictionary<int, string> MoveRobot(Dictionary<int, string> map, string path) {
  143.             var started = false;
  144.             var mapArr = new char[map.Count, map.Values.OrderByDescending(s => s.Length).First().Length];
  145.             var robotPos = new Tuple<int, int>(0, 0);
  146.             var exitPos = new Tuple<int, int>(0, 0);
  147.  
  148.             //Convert Dictionary to char[], get robot position and exit position
  149.             var x = 0;
  150.             var y = 0;
  151.  
  152.             foreach (var mapFragment in map) {
  153.                 foreach (var character in mapFragment.Value) {
  154.                     mapArr[x, y] = character;
  155.                     if (character == 'M') {
  156.                         robotPos = new Tuple<int, int>(x, y);
  157.                     }
  158.  
  159.                     if (x == 0 || y == 0 || x == (mapArr.GetLength(0) - 1) || y == (mapArr.GetLength(1) - 1)) {
  160.                         if (mapArr[x, y] == '0') {
  161.                             exitPos = new Tuple<int, int>(x, y);
  162.                         }
  163.                     }
  164.                     y++;
  165.                 }
  166.                 y = 0;
  167.                 x++;
  168.             }
  169.  
  170.             foreach (var command in path) {
  171.                 switch (command) {
  172.                     case 'I':
  173.                         started = true;
  174.                         break;
  175.  
  176.                     case '-':
  177.                         started = false;
  178.                         break;
  179.  
  180.                     case 'N':
  181.                         if (!started) {
  182.                             continue;
  183.                         }
  184.  
  185.                         if (mapArr[robotPos.Item1 - 1, robotPos.Item2] == '*') {
  186.                             exploded = true;
  187.                         }
  188.  
  189.                         if (mapArr[robotPos.Item1 - 1, robotPos.Item2] == '+') {
  190.                             continue;
  191.                         }
  192.  
  193.                         //Clear old position
  194.                         mapArr[robotPos.Item1, robotPos.Item2] = 'X';
  195.  
  196.                         //Assign new position
  197.                         robotPos = new Tuple<int, int>(robotPos.Item1 - 1, robotPos.Item2);
  198.                         mapArr[robotPos.Item1, robotPos.Item2] = 'M';
  199.  
  200.                         break;
  201.  
  202.                     case 'S':
  203.                         if (!started) {
  204.                             continue;
  205.                         }
  206.  
  207.                         if (mapArr[robotPos.Item1 + 1, robotPos.Item2] == '*') {
  208.                             exploded = true;
  209.                         }
  210.  
  211.                         if (mapArr[robotPos.Item1 + 1, robotPos.Item2] == '+') {
  212.                             continue;
  213.                         }
  214.  
  215.                         //Clear old position
  216.                         mapArr[robotPos.Item1, robotPos.Item2] = 'X';
  217.  
  218.                         //Assign new position
  219.                         robotPos = new Tuple<int, int>(robotPos.Item1 + 1, robotPos.Item2);
  220.                         mapArr[robotPos.Item1, robotPos.Item2] = 'M';
  221.  
  222.                         break;
  223.  
  224.                     case 'E':
  225.                         if (!started) {
  226.                             continue;
  227.                         }
  228.  
  229.                         if (mapArr[robotPos.Item1, robotPos.Item2 + 1] == '*') {
  230.                             exploded = true;
  231.                         }
  232.  
  233.                         if (mapArr[robotPos.Item1, robotPos.Item2 + 1] == '+') {
  234.                             continue;
  235.                         }
  236.  
  237.                         //Clear old position
  238.                         mapArr[robotPos.Item1, robotPos.Item2] = 'X';
  239.  
  240.                         //Assign new position
  241.                         robotPos = new Tuple<int, int>(robotPos.Item1, robotPos.Item2 + 1);
  242.                         mapArr[robotPos.Item1, robotPos.Item2] = 'M';
  243.  
  244.                         break;
  245.  
  246.                     case 'O':
  247.                         if (!started) {
  248.                             continue;
  249.                         }
  250.  
  251.                         if (mapArr[robotPos.Item1, robotPos.Item2 - 1] == '*') {
  252.                             exploded = true;
  253.                         }
  254.  
  255.                         if (mapArr[robotPos.Item1, robotPos.Item2 - 1] == '+') {
  256.                             continue;
  257.                         }
  258.  
  259.                         //Clear old position
  260.                         mapArr[robotPos.Item1, robotPos.Item2] = 'X';
  261.  
  262.                         //Assign new position
  263.                         robotPos = new Tuple<int, int>(robotPos.Item1, robotPos.Item2 - 1);
  264.                         mapArr[robotPos.Item1, robotPos.Item2] = 'M';
  265.  
  266.                         break;
  267.                     default:
  268.                         break;
  269.                 }
  270.  
  271.                 if (exploded) {
  272.                     break;
  273.                 }
  274.  
  275.             }
  276.  
  277.             if (robotPos.Item1 == exitPos.Item1 && robotPos.Item2 == exitPos.Item2 && !exploded) {
  278.                 success = true;
  279.             }
  280.  
  281.             var resultMap = new Dictionary<int, string>();
  282.  
  283.             for (int i = 0; i < mapArr.GetLength(0); i++) {
  284.                 var j = 0;
  285.                 var dimValue = "";
  286.  
  287.                 for (j = 0; j < mapArr.GetLength(1); j++) {
  288.                     dimValue += mapArr[i, j].ToString();
  289.                 }
  290.                 resultMap.Add(i, dimValue);
  291.  
  292.                 j = 0;
  293.             }
  294.  
  295.             return resultMap;
  296.         }
  297.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement