Skip to content

Commit 3330ab2

Browse files
committed
initial import
0 parents  commit 3330ab2

30 files changed

+16588
-0
lines changed

RobotWars.App/AzimuthEnum.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace RobotWars.App
7+
{
8+
public enum OrientationEnum
9+
{
10+
North,
11+
East,
12+
South,
13+
West
14+
}
15+
}

RobotWars.App/Grid.cs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace RobotWars.App
5+
{
6+
public class Grid : List<IRobot>, IGrid
7+
{
8+
public int Width { get; set; }
9+
public int Height { get; set; }
10+
11+
public Grid(int width, int height)
12+
{
13+
Width = width;
14+
Height = height;
15+
}
16+
17+
public Grid(string initialiseString)
18+
{
19+
string[] parts = initialiseString.Split(' ');
20+
if (parts.Length != 2)
21+
throw new ArgumentException("Setup string must be in the format [Width] [Height] separated by a space e.g: 10 10");
22+
23+
int tryWidth, tryHeight;
24+
if (!int.TryParse(parts[0], out tryWidth) || tryWidth < 0)
25+
throw new ArgumentException(string.Format("{0} is not a valid value for Width", parts[0]));
26+
if (!int.TryParse(parts[1], out tryHeight) || tryHeight < 0)
27+
throw new ArgumentException(string.Format("{0} is not a valid value for Height", parts[1]));
28+
29+
Width = tryWidth;
30+
Height = tryHeight;
31+
}
32+
}
33+
}

RobotWars.App/IGrid.cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
3+
namespace RobotWars.App
4+
{
5+
public interface IGrid : IList<IRobot>
6+
{
7+
int Width { get; set; }
8+
int Height { get; set; }
9+
}
10+
}

RobotWars.App/IRobot.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace RobotWars.App
2+
{
3+
public interface IRobot
4+
{
5+
int X { get; }
6+
int Y { get; }
7+
OrientationEnum Orientation { get; }
8+
void CommandLeft();
9+
void CommandRight();
10+
void CommandMove();
11+
void ExecuteInstructions(string instructions);
12+
}
13+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace RobotWars.App
2+
{
3+
public interface IRobotCommandMarshaller
4+
{
5+
void ExecuteInstructions(string instructions);
6+
void SetRobot(IRobot robot);
7+
}
8+
}

RobotWars.App/Program.cs

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace RobotWars.App
7+
{
8+
class Program
9+
{
10+
private static IGrid _grid = null;
11+
private static List<IRobot> _robots = null;
12+
static void Main(string[] args)
13+
{
14+
Exception e = null;
15+
16+
// initialise a grid
17+
do
18+
{
19+
try
20+
{
21+
Console.Write("Grid size (Width Height): ");
22+
_grid = new Grid(Console.ReadLine());
23+
e = null;
24+
} catch (Exception exception)
25+
{
26+
Console.WriteLine(exception.Message);
27+
e = exception;
28+
}
29+
} while (e != null);
30+
31+
List<IRobot> robots = new List<IRobot>();
32+
33+
// set up two robots
34+
Console.Write("Robot 1 initialisation (X Y [N|E|S|W}): ");
35+
robots.Add(InitialiseRobot());
36+
Console.Write("Robot 2 initialisation (X Y [N|E|S|W}): ");
37+
robots.Add(InitialiseRobot());
38+
39+
do
40+
{
41+
for (int i = 0; i < robots.Count;i++ )
42+
{
43+
Console.Write(string.Format("Robot {0} instructions: ", (i+1)));
44+
ExecuteTurn(robots[i]);
45+
}
46+
} while (true);
47+
48+
#if DEBUG
49+
Console.WriteLine("Finished, press any key to continue.");
50+
Console.ReadKey();
51+
#endif
52+
}
53+
54+
private static IRobot InitialiseRobot()
55+
{
56+
IRobot robot=null;
57+
Exception e;
58+
do
59+
{
60+
try
61+
{
62+
robot = new Robot(Console.ReadLine(), _grid);
63+
e = null;
64+
}
65+
catch (Exception exception)
66+
{
67+
Console.WriteLine(exception.Message);
68+
e = exception;
69+
}
70+
} while (e != null);
71+
return robot;
72+
}
73+
74+
private static void ExecuteTurn(IRobot robot)
75+
{
76+
Exception e;
77+
do
78+
{
79+
try
80+
{
81+
robot.ExecuteInstructions(Console.ReadLine());
82+
e = null;
83+
} catch (Exception exception)
84+
{
85+
Console.WriteLine(exception.Message);
86+
e = exception;
87+
}
88+
89+
} while (e != null);
90+
Console.WriteLine("{0} {1} {2}", robot.X, robot.Y, Enum.GetName(typeof(OrientationEnum), robot.Orientation).Substring(0,1));
91+
}
92+
}
93+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("RobotWars.App")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("RobotWars.App")]
13+
[assembly: AssemblyCopyright("Copyright © 2011")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("5163ff2c-33f9-4be0-b14b-f5ebb394cdc9")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

RobotWars.App/Robot.cs

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace RobotWars.App
7+
{
8+
public class Robot : IRobot
9+
{
10+
private readonly IRobotCommandMarshaller _commandMarshaller;
11+
private readonly IGrid _grid;
12+
public int X { get; set; }
13+
public int Y { get; set; }
14+
public OrientationEnum Orientation { get; set; }
15+
16+
public Robot(int initialX, int initialY, OrientationEnum orientation, IGrid grid)
17+
: this(initialX, initialY, orientation, grid, new RobotCommandMarshaller()){}
18+
19+
public Robot(int initialX, int initialY, OrientationEnum orientation, IGrid grid, IRobotCommandMarshaller commandMarshaller)
20+
{
21+
_commandMarshaller = commandMarshaller;
22+
_commandMarshaller.SetRobot(this);
23+
24+
_grid = grid;
25+
X = initialX;
26+
Y = initialY;
27+
Orientation = orientation;
28+
}
29+
30+
public Robot(string setupString, IGrid grid) : this(setupString, grid, new RobotCommandMarshaller()){}
31+
public Robot(string setupString, IGrid grid, IRobotCommandMarshaller commandMarshaller)
32+
{
33+
_commandMarshaller = commandMarshaller;
34+
commandMarshaller.SetRobot(this);
35+
36+
_grid = grid;
37+
string[] setupParts = setupString.Split(' ');
38+
if (setupParts.Length != 3)
39+
throw new ArgumentException("To set up a robot provide X Y [N|E|S|W] values seperated by spaces\ne.g: 1 1 N");
40+
41+
int tryX = 0, tryY = 0;
42+
if (!int.TryParse(setupParts[0], out tryX))
43+
throw new ArgumentException(string.Format("{0} is not a valid value for X", setupParts[0]), "setupString");
44+
if (!int.TryParse(setupParts[1], out tryY))
45+
throw new ArgumentException(string.Format("{0} is not a valid value for Y", setupParts[1]), "setupString");
46+
if (tryX < 0 || tryX >= grid.Width || tryY < 0 || tryY >= grid.Height)
47+
throw new ArgumentException(string.Format("The coordinates {0},{1} are outside the grid", setupParts[0],
48+
setupParts[1]), "setupString");
49+
50+
X = tryX;
51+
Y = tryY;
52+
switch(setupParts[2])
53+
{
54+
case "N":
55+
Orientation = OrientationEnum.North;
56+
break;
57+
case "E":
58+
Orientation = OrientationEnum.East;
59+
break;
60+
case "S":
61+
Orientation = OrientationEnum.South;
62+
break;
63+
case "W":
64+
Orientation = OrientationEnum.West;
65+
break;
66+
default:
67+
throw new ArgumentException(string.Format("{0} is not a valid value for Orientation", setupParts[2]), "setupString");
68+
}
69+
}
70+
71+
public void CommandLeft()
72+
{
73+
--Orientation;
74+
if (!Enum.IsDefined(typeof(OrientationEnum), (int)Orientation))
75+
Orientation = Enum.GetValues(typeof(OrientationEnum)).Cast<OrientationEnum>().Last();
76+
}
77+
78+
public void CommandRight()
79+
{
80+
++Orientation;
81+
if (!Enum.IsDefined(typeof(OrientationEnum), (int)Orientation))
82+
Orientation = 0;
83+
}
84+
85+
public void CommandMove()
86+
{
87+
switch(Orientation)
88+
{
89+
case OrientationEnum.North:
90+
if (Y+1==_grid.Height) throw new InvalidOperationException("Cannot move outside the grid!");
91+
++Y;
92+
break;
93+
case OrientationEnum.East:
94+
if (X+1 == _grid.Width) throw new InvalidOperationException("Cannot move outside the grid!");
95+
++X;
96+
break;
97+
case OrientationEnum.West:
98+
if (X == 0) throw new InvalidOperationException("Cannot move outside the grid!");
99+
--X;
100+
break;
101+
case OrientationEnum.South:
102+
if (Y == 0) throw new InvalidOperationException("Cannot move outside the grid!");
103+
--Y;
104+
break;
105+
}
106+
}
107+
108+
public void ExecuteInstructions(string instructions)
109+
{
110+
_commandMarshaller.ExecuteInstructions(instructions);
111+
}
112+
}
113+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
3+
namespace RobotWars.App
4+
{
5+
public class RobotCommandMarshaller : IRobotCommandMarshaller
6+
{
7+
private IRobot _robot;
8+
9+
public RobotCommandMarshaller(IRobot robot)
10+
{
11+
_robot = robot;
12+
}
13+
14+
public RobotCommandMarshaller()
15+
{
16+
}
17+
18+
public void ExecuteInstructions(string instructions)
19+
{
20+
instructions = instructions.ToLower();
21+
foreach (char i in instructions)
22+
{
23+
switch (i)
24+
{
25+
case 'l':
26+
_robot.CommandLeft();
27+
break;
28+
case 'r':
29+
_robot.CommandRight();
30+
break;
31+
case 'm':
32+
_robot.CommandMove();
33+
break;
34+
default: throw new ArgumentException("Invalid instruction " + i);
35+
}
36+
}
37+
38+
}
39+
40+
public void SetRobot(IRobot robot)
41+
{
42+
_robot = robot;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)