Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class GameEngine {
- private bool success;
- private bool exploded;
- public GameEngine() {
- var play = true;
- while (play) {
- Console.Clear();
- success = false;
- exploded = false;
- play = DoGameLoop();
- }
- }
- private bool DoGameLoop() {
- var map = CreateMap();
- Console.Clear();
- Console.WriteLine("Your map:");
- Console.WriteLine();
- foreach (var mapFragment in map) {
- Console.WriteLine(mapFragment.Value);
- }
- var path = GetPath();
- var resultMap = MoveRobot(map, path);
- Console.Clear();
- if (success) {
- Console.WriteLine("Congratulations! You successfully navigated the robot through the minefield.\nYour path:");
- Console.WriteLine();
- foreach (var resultMapFragment in resultMap) {
- Console.WriteLine(resultMapFragment.Value);
- }
- }
- else {
- if (exploded) {
- Console.WriteLine("GAME OVER\nYour robot exploded.");
- Console.WriteLine();
- foreach (var resultMapFragment in resultMap) {
- Console.WriteLine(resultMapFragment.Value);
- }
- }
- else {
- if (!path.Contains("I")) {
- Console.WriteLine("Fail. You did not navigate the robot successfully through the minefield\nDid you start the robot's engine?\nYour path:");
- }
- else {
- Console.WriteLine("Fail. You did not navigate the robot successfully through the minefield.\nYour path:");
- }
- Console.WriteLine();
- foreach (var resultMapFragment in resultMap) {
- Console.WriteLine(resultMapFragment.Value);
- }
- }
- }
- Console.WriteLine();
- Console.WriteLine("Would you like to play again? (y/n)");
- var play = Console.ReadLine() == "y" ? true : false;
- return play;
- }
- private Dictionary<int, string> CreateMap() {
- 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.");
- Console.WriteLine("-----------------------------------------------------------------");
- Console.WriteLine();
- var map = new Dictionary<int, string>();
- var exit = false;
- var i = 0;
- while (!exit) {
- var mapFragment = Console.ReadLine();
- if (mapFragment == "exit") {
- break;
- }
- if (mapFragment == "clear") {
- i = 0;
- map.Clear();
- continue;
- }
- //Check for non allowed characters
- if (!Regex.IsMatch(mapFragment, "^[0+M*]+$")) {
- Console.WriteLine("You entered an invalid character. Please enter a new line.");
- continue;
- }
- //Double robot check
- var currLineDoubleRobot = mapFragment.ToCharArray()
- .GroupBy(x => x)
- .Where(y => y.Count() > 1)
- .Select(z => z.Key).Contains('M');
- var doubleRobot = false;
- foreach (var mapFrag in map) {
- if (mapFrag.Value.Contains("M")) {
- doubleRobot = true;
- }
- }
- if (currLineDoubleRobot || (doubleRobot && mapFragment.Contains("M"))) {
- Console.WriteLine("You already placed the robot. Please enter a new line:");
- continue;
- }
- map.Add(i, mapFragment);
- i++;
- }
- return map;
- }
- private string GetPath() {
- Console.WriteLine("-----------------------------------------------------------------");
- Console.WriteLine();
- Console.WriteLine("Please enter your path:");
- while (true) {
- var path = Console.ReadLine();
- if (Regex.IsMatch(path, @"^[I\-NSEO]+$")) {
- return path;
- }
- Console.WriteLine("Invalid characters in path. Enter path again:");
- }
- }
- private Dictionary<int, string> MoveRobot(Dictionary<int, string> map, string path) {
- var started = false;
- var mapArr = new char[map.Count, map.Values.OrderByDescending(s => s.Length).First().Length];
- var robotPos = new Tuple<int, int>(0, 0);
- var exitPos = new Tuple<int, int>(0, 0);
- //Convert Dictionary to char[], get robot position and exit position
- var x = 0;
- var y = 0;
- foreach (var mapFragment in map) {
- foreach (var character in mapFragment.Value) {
- mapArr[x, y] = character;
- if (character == 'M') {
- robotPos = new Tuple<int, int>(x, y);
- }
- if (x == 0 || y == 0 || x == (mapArr.GetLength(0) - 1) || y == (mapArr.GetLength(1) - 1)) {
- if (mapArr[x, y] == '0') {
- exitPos = new Tuple<int, int>(x, y);
- }
- }
- y++;
- }
- y = 0;
- x++;
- }
- foreach (var command in path) {
- switch (command) {
- case 'I':
- started = true;
- break;
- case '-':
- started = false;
- break;
- case 'N':
- if (!started) {
- continue;
- }
- if (mapArr[robotPos.Item1 - 1, robotPos.Item2] == '*') {
- exploded = true;
- }
- if (mapArr[robotPos.Item1 - 1, robotPos.Item2] == '+') {
- continue;
- }
- //Clear old position
- mapArr[robotPos.Item1, robotPos.Item2] = 'X';
- //Assign new position
- robotPos = new Tuple<int, int>(robotPos.Item1 - 1, robotPos.Item2);
- mapArr[robotPos.Item1, robotPos.Item2] = 'M';
- break;
- case 'S':
- if (!started) {
- continue;
- }
- if (mapArr[robotPos.Item1 + 1, robotPos.Item2] == '*') {
- exploded = true;
- }
- if (mapArr[robotPos.Item1 + 1, robotPos.Item2] == '+') {
- continue;
- }
- //Clear old position
- mapArr[robotPos.Item1, robotPos.Item2] = 'X';
- //Assign new position
- robotPos = new Tuple<int, int>(robotPos.Item1 + 1, robotPos.Item2);
- mapArr[robotPos.Item1, robotPos.Item2] = 'M';
- break;
- case 'E':
- if (!started) {
- continue;
- }
- if (mapArr[robotPos.Item1, robotPos.Item2 + 1] == '*') {
- exploded = true;
- }
- if (mapArr[robotPos.Item1, robotPos.Item2 + 1] == '+') {
- continue;
- }
- //Clear old position
- mapArr[robotPos.Item1, robotPos.Item2] = 'X';
- //Assign new position
- robotPos = new Tuple<int, int>(robotPos.Item1, robotPos.Item2 + 1);
- mapArr[robotPos.Item1, robotPos.Item2] = 'M';
- break;
- case 'O':
- if (!started) {
- continue;
- }
- if (mapArr[robotPos.Item1, robotPos.Item2 - 1] == '*') {
- exploded = true;
- }
- if (mapArr[robotPos.Item1, robotPos.Item2 - 1] == '+') {
- continue;
- }
- //Clear old position
- mapArr[robotPos.Item1, robotPos.Item2] = 'X';
- //Assign new position
- robotPos = new Tuple<int, int>(robotPos.Item1, robotPos.Item2 - 1);
- mapArr[robotPos.Item1, robotPos.Item2] = 'M';
- break;
- default:
- break;
- }
- if (exploded) {
- break;
- }
- }
- if (robotPos.Item1 == exitPos.Item1 && robotPos.Item2 == exitPos.Item2 && !exploded) {
- success = true;
- }
- var resultMap = new Dictionary<int, string>();
- for (int i = 0; i < mapArr.GetLength(0); i++) {
- var j = 0;
- var dimValue = "";
- for (j = 0; j < mapArr.GetLength(1); j++) {
- dimValue += mapArr[i, j].ToString();
- }
- resultMap.Add(i, dimValue);
- j = 0;
- }
- return resultMap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement