I've made a small "rover game" similar to this one, except made in C#, using OOP, and prettier menus. Here's how it works:
There is one Rover
class which is instantiated into an array containing all rovers. This class contains certain actions that a rover might need like, MoveRover
, or RoverDead
. There is also a RoverManager
class where actions such as updating rovers are executed by looping through the array where the rover data is stored. This is also where rover addition and deletion is managed as well.
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();
}
}
}