# Organizing interfaces for player movement in chess game

I'm trying to figure out how to organize interfaces in my game to make future development easier, by making my code more modular.

Notice that my Rook class implements the IPlayer interface, which will move it left and right, but not up and down (I had to add that in seperate methods). My question is, should I make an interface per method of movement that I want to do (IMoveLeft, IMoveRight, IMoveUp, IMoveDown)? This makes composing future players more modular, perhaps there is a better way.

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

namespace Sandbox
{
class Program
{
static void Main(string[] args)
{
IPlayer bob = new Person();
IPlayer rook = new Rook();
List<IPlayer> iPlayerList = new List<IPlayer>();

foreach (var player in iPlayerList)
{
player.MoveLeft(1);
player.MoveRight(1);
}
}
}

public class Person : IPlayer
{
public int XDistanceFromOrigin { get; set; }

public void MoveLeft(int distanceToMove)
{
XDistanceFromOrigin = XDistanceFromOrigin - 1;
}

public void MoveRight(int distanceToMove)
{
XDistanceFromOrigin = XDistanceFromOrigin + 1;
}
}

interface IPlayer
{
void MoveLeft(int distanceToMove);
void MoveRight(int distanceToMove);
}

public class Rook : IPlayer
{
public int XDistance { get; set; }
public int YDistance { get; set; }

public void MoveLeft(int distanceToMove)
{
XDistance = XDistance - 1;
}

public void MoveRight(int distanceToMove)
{
XDistance = XDistance + 1;
}

public void MoveUp(int distanceToMove)
{
YDistance = YDistance + 1;
}

public void MoveDown(int distanceToMove)
{
YDistance = YDistance - 1;
}
}
}

• Do the movement functions have actual implementations at the moment? If they do, I'd recommend including them just to make sure that this doesn't get closed as stub code. – Ethan Bierlein Nov 2 '15 at 4:13
• @EthanBierlein, I abstracted that away from the code, because it is irrelevant to the question I was asking. Do you want me to include the using statements, and the namespace as well? – Nathvi Nov 4 '15 at 23:18
• Well, in order for your question to be on topic, we'd need all the code for the methods. The using statements and the namespaces would also be helpful as well. – Ethan Bierlein Nov 4 '15 at 23:19
• @EthanBierlein, ok, I'll try to be more explicit. – Nathvi Nov 4 '15 at 23:36
• Maybe this would be better on another stackexchange site. Idk. – Nathvi Nov 4 '15 at 23:48

1. A player doesn't move left or right, they move a game piece
2. A rook is not a player. A player has a rook (2 actually, but I digress).

When thinking about interfaces/class hierarchies it's useful to think in terms of "is a" or "has a" relationships. Bob is a player. A rook is a piece. A player has a rook.

I would modify your interfaces a bit.

interface IPlayer
{
string Name { get; }
bool MovePiece(IPiece piece, int row, int rank);
}

interface IPiece
{
int Row { get; set; }
int Rank { get; set; }

bool CanMoveTo(int row, int rank);
}


You may want to consider using standard chess notation for clarity and cohesion with the problem domain, but using Cartesian coordinates will make the code simpler to write. I added a CanMoveTo method so each piece can store its own logic for checking to see if it's allowed to move to a position that the player attempts to move it to. This design may not be perfect (or even good), but should get you pointed in the right direction.