Skip to content

Instantly share code, notes, and snippets.

@DeepSky8
Created December 22, 2015 19:52
Show Gist options
  • Save DeepSky8/af44c33beaf2976a8815 to your computer and use it in GitHub Desktop.
Save DeepSky8/af44c33beaf2976a8815 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbstractRobots
{
class Program
{
static void Main(string[] args)
{
}
}
public class SafeInt
{
public static SafeInt Create(int n, int min, int max)
{
if (min > max)
{
throw new ArgumentOutOfRangeException(string.Format("The lowest possible value '{0}' must be lower than the largest possible value '{1}'", min, max));
}
else if (n < min)
{
throw new ArgumentOutOfRangeException(string.Format("The value '{0}' supplied is below '{1},' the minimum value", n, min));
}
else if (n > max)
{
throw new ArgumentOutOfRangeException(string.Format("The value '{0}' supplied is higher than '{1},' the maximum value", n, max));
}
else
{
return new SafeInt(n, min, max);
}
}
public int Value { get; private set; }
public int Min { get; private set; }
public int Max { get; private set; }
private SafeInt(int n, int min, int max)
{
Value = n;
Min = min;
Max = max;
}
public static SafeInt operator +(SafeInt s1, int s2)
{
return SafeInt.Create(s1.Value + s2, s1.Min, s1.Max);
}
public static SafeInt operator -(SafeInt s1, int s2)
{
return SafeInt.Create(s1.Value - s2, s1.Min, s1.Max);
}
public static bool operator <(SafeInt s1, int s2)
{
return s1.Value < s2;
}
public static bool operator >(SafeInt s1, int s2)
{
return s1.Value > s2;
}
public override string ToString()
{
return string.Format("{0} <= {1} <= {2}", Min, Value, Max);
}
//End of SafeInt
}
public enum MoveShoot { Default, Move, Shoot };
public enum WhichDirection { Default, Up, Down, Left, Right };
abstract class Robot
{
public string Name { get; protected set; }
public SafeInt Energy { get; protected set; }
public SafeInt Bullets { get; protected set; }
public SafeInt X { get; protected set; }
public SafeInt Y { get; protected set; }
public Robot(string name, SafeInt energy, SafeInt bullets, SafeInt x, SafeInt y)
{
Name = name;
Energy = energy;
Bullets = bullets;
X = x;
Y = y;
}
/// <summary>
/// Requires direction (selected from Enum WhichDirection), and a SafeInt for how many units to move. Modifies the Robot's X or Y position, and decrements the robot's energy by an appropriate amount of Energy.
/// </summary>
/// <param name="direction"></param>
/// <param name="thisFar"></param>
/// <returns></returns>
public void Move(WhichDirection direction, SafeInt thisFar)
{
int distance = thisFar.Value;
if ((Energy.Value - distance) < 0)
{
Console.WriteLine("\n{0} does not have enough energy to move that far!", Name);
}
else
{
switch (direction)
{
case WhichDirection.Up:
X += distance;
Energy -= distance;
break;
case WhichDirection.Down:
X -= distance;
Energy -= distance;
break;
case WhichDirection.Left:
Y -= distance;
Energy -= distance;
break;
case WhichDirection.Right:
Y += distance;
Energy -= distance;
break;
default:
break;
}
}
//End of 'Move'
}
public abstract void Shoot(WhichDirection direction);
public override string ToString()
{
return string.Format("\n{0} is located at postion({1}, {2}), with {3} energy and {4} bullets.\n", Name, X.Value, Y.Value, Energy.Value, Bullets.Value);
}
}
class Sniper : Robot
{
/// <summary>
/// Requires a direction (selected from Enum WhichDirection). Modifies the robot which called this method by decrementing Energy by 2, and Bullets by 1.
/// </summary>
/// <param name="robot"></param>
/// <param name="direction"></param>
/// <returns></returns>
public override void Shoot(WhichDirection direction)
{
if (Energy < 2)
{
Console.WriteLine("\n{0} don't have enough energy to fire that shot!", Name);
}
else if (Bullets.Value <= 0)
{
Console.WriteLine("\n{0} don't have enough bullets to fire that shot!", Name);
}
else
{
Energy -= 2;
Bullets -= 1;
}
//End of 'Shoot'
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment