Skip to main content
1 of 5
Ethan Bierlein
  • 15.8k
  • 4
  • 59
  • 146

Basic text-based rover game

I've made a small "rover game" similar to this one, except made in C#, using OOP, and prettier menus. This is also my first time programming in non-Unity C#, so any improvements are welcome.

using System;
using System.Collections.Generic;

namespace Rovers
{
    /* A class for creating a rover */
    class Rover
    {
        public long x;
        public long y;
        public string name;
        public int lifeTime;

        public Rover(long x, long y, string name, int lifeTime)
        {
            this.x = x;
            this.y = y;
            this.name = name;
            this.lifeTime = lifeTime;
        }

        /* Update the rover lifetime */
        public void UpdateRoverLifeTime(int xChange, int yChange)
        {
            this.lifeTime -= Math.Abs(xChange - yChange);
        }

        /* Move the rover a desired amount */
        public void MoveRover(int xChange, int yChange)
        {
            if (xChange <= 5 && yChange <= 5 && xChange >= -5 && yChange >= -5)
            {
                this.x += xChange;
                this.y += yChange;
                UpdateRoverLifeTime(xChange, yChange);
            }
            else
            {
                Console.WriteLine("\nPOSITION CHANGE MUST BE <= 5 or >= -5.");
            }
        }

        /* Get input for position changes of the rover */
        public void GetRoverPositionChange()
        {
            Console.WriteLine("\n-- Rover Position Changer Menu. --");

            Console.Write("xChange: ");
            int xChange = Int32.Parse(Console.ReadLine());

            Console.Write("yChange: ");
            int yChange = Int32.Parse(Console.ReadLine());

            MoveRover(xChange, yChange);
        }

        /* Output rover statistics */
        public void OutputRoverStats()
        {
            Console.WriteLine("\n-- Rover Stats Menu. --");
            Console.WriteLine("Rover: {0}", this.name);
            Console.WriteLine("x: {0}, y: {1}", this.x, this.y);
            Console.WriteLine("Life: {0}", this.lifeTime);
        }

        /* Is the rover dead? :( https://xkcd.com/695/ */
        public bool RoverDead()
        {
            return this.lifeTime > 0;
        }

        /* Main rover update "loop" */
        public void UpdateRover()
        {
            GetRoverPositionChange();
            OutputRoverStats();
        }
    }


    /* A class containing certain methods for rover managment */
    class RoverManager
    {
        public List<Rover> roverList = new List<Rover>();

        /* Add a rover */
        public void AddRover()
        {
            Console.WriteLine("\n-- Add Rover Menu. --");

            Console.Write("startX: ");
            long startX = (long)Int32.Parse(Console.ReadLine());

            Console.Write("startY: ");
            long startY = (long)Int32.Parse(Console.ReadLine());

            Console.Write("name: ");
            string name = Console.ReadLine();

            Console.Write("startingLifeTime: ");
            int startingLifeTime = Int32.Parse(Console.ReadLine());

            this.roverList.Add(new Rover(startX, startY, name, startingLifeTime));
        }

        /* Destroy a rover if it's dead */
        public void DestroyRovers()
        {
            for (int n = roverList.Count-1; n >= 0; n--)
            {
                Rover rover = roverList[n];

                if (rover.RoverDead())
                {
                    roverList.Remove(rover);
                }
            }
        }

        /* Update each rover */
        public void UpdateRovers()
        {
            for (int n = roverList.Count-1; n >= 0; n--)
            {
                Rover rover = roverList[n];
                rover.UpdateRover();
            }
        }

        /* Main rover manager loop */
        public void MainManagerLoop()
        {
            while (true)
            {
                AddRover();
                UpdateRovers();
                DestroyRovers();
            }
        }
    }


    public class MainProgram
    {
        private static RoverManager roverManager = new RoverManager();

        public static void Main(string[] args)
        {
            roverManager.MainManagerLoop();
        }
    }
}

Please keep in mind though that I am fairly new still to C#, so if you think a topic needs explaining, please do. Thanks!

Ethan Bierlein
  • 15.8k
  • 4
  • 59
  • 146