2
\$\begingroup\$

I did this to practice a bit without thinking about optimization due to simplicity of this progam.

It's a "maze game", the player is P and can step into the M by pressing Arrows on the Keyboard.

At the start a txt with the current level is loaded.

Anyway, how would you optimize this code?

I was thinking about making the string builder static.

Input file:

##############################
#PMMM#######MMM#MMM###########
####M#######M#M#M#M###########
#MMMMMMMMMMMM#MMM#M###########
#M####M###########M###########
#M####M###########M###########
#M####M#########MMM###########
#M####M#########M#############
#M####M#########M#############
#M####M#########M#############
#M####M#########M#############
#M####M#########M#############
#M####M#####MMMMM#############
#M####M####MM#################
#M###MM####M##################
#M##MM#####M##################
#M##M#MMMMMM##################
#M##M#E#######################

Code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Reflection;

namespace test
{
    class Program
    {
        // P - player
        // ^ - percurred
        // M - walkable maze
        // # - not walkable maze
        // E - end
        static List<string> Maze = new List<string>();
        static int PlayerX = 0;
        static int PlayerY = 0;
        static ConsoleKeyInfo KeyInfo;
        static void Main(string[] args)
        {
            Maze = File.ReadAllLines("maze.txt").ToList();
            UpdateOutput();
            while (true)
            {
                KeyInfo = Console.ReadKey();
                try
                {
                    WaitForInput();
                }
                catch
                {
                    Console.Clear();
                    Console.WriteLine("Lose!");
                    Console.ReadLine();
                    System.Diagnostics.Process.Start(Assembly.GetExecutingAssembly().Location);
                    Environment.Exit(0);
                }
            }
        }

        private static void UpdateOutput()
        {
            Console.Clear();
            for (int i = 0; i < Maze.Count; i++)
            {
                if (Maze[i].Contains("P"))
                {
                    PlayerY = i;
                    PlayerX = Maze[i].IndexOf("P");
                }
                Console.WriteLine(Maze[i]);
            }
        }

        private static void End()
        {
            Console.Clear();
            Console.WriteLine("Victory!");
            Console.ReadLine();
            System.Diagnostics.Process.Start(Assembly.GetExecutingAssembly().Location);
            Environment.Exit(0);
        }

        private static void WaitForInput()
        {
            if (KeyInfo.Key == ConsoleKey.DownArrow)
            {
                if (Maze[PlayerY + 1][PlayerX] == 'M')
                {
                    StringBuilder sb = new StringBuilder(Maze[PlayerY]);
                    sb[PlayerX] = '^';
                    Maze[PlayerY] = sb.ToString();
                    sb = new StringBuilder(Maze[PlayerY + 1]);
                    sb[PlayerX] = 'P';
                    Maze[PlayerY + 1] = sb.ToString();
                    UpdateOutput();
                }
                else if (Maze[PlayerY + 1][PlayerX] == 'E')
                {
                    End();
                }
            }

            if (KeyInfo.Key == ConsoleKey.UpArrow)
            {
                if (Maze[PlayerY - 1][PlayerX] == 'M')
                {
                    StringBuilder sb = new StringBuilder(Maze[PlayerY]);
                    sb[PlayerX] = '^';
                    Maze[PlayerY] = sb.ToString();
                    sb = new StringBuilder(Maze[PlayerY - 1]);
                    sb[PlayerX] = 'P';
                    Maze[PlayerY - 1] = sb.ToString();
                    UpdateOutput();
                }
                else if (Maze[PlayerY - 1][PlayerX] == 'E')
                {
                    End();
                }
            }

            if (KeyInfo.Key == ConsoleKey.RightArrow)
            {
                if (Maze[PlayerY][PlayerX + 1] == 'M')
                {
                    StringBuilder sb = new StringBuilder(Maze[PlayerY]);
                    sb[PlayerX] = '^';
                    Maze[PlayerY] = sb.ToString();
                    sb = new StringBuilder(Maze[PlayerY]);
                    sb[PlayerX + 1] = 'P';
                    Maze[PlayerY] = sb.ToString();
                    UpdateOutput();
                }
                else if (Maze[PlayerY][PlayerX + 1] == 'E')
                {
                    End();
                }
            }

            if (KeyInfo.Key == ConsoleKey.LeftArrow)
            {
                if (Maze[PlayerY][PlayerX - 1] == 'M')
                {
                    StringBuilder sb = new StringBuilder(Maze[PlayerY]);
                    sb[PlayerX] = '^';
                    Maze[PlayerY] = sb.ToString();
                    sb = new StringBuilder(Maze[PlayerY]);
                    sb[PlayerX - 1] = 'P';
                    Maze[PlayerY] = sb.ToString();
                    UpdateOutput();
                }
                else if (Maze[PlayerY][PlayerX - 1] == 'E')
                {
                    End();
                }
            }
        }

    }

}
\$\endgroup\$
1
  • \$\begingroup\$ I think you can get index out of range \$\endgroup\$
    – paparazzo
    Commented Sep 18, 2017 at 15:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.