So basically, in game, I have a "wall validator", which checks if wall meets all of the requirements. Then, based on which requirement have failed, program calls appropriate message function.
Here is the code, where I call wall validator:
internal void PlaceWall(Vector2 wallStartPosition, Vector2 wallEndPosition)
{
_wallStartPosition = wallStartPosition;
_wallEndPosition = wallEndPosition;
_wallValidator.InitializeVectors(wallStartPosition, wallEndPosition);
if (_wallValidator.WallDoesNotMeetTheRequirements())
{
_wallValidator.SendAppropriateMessage();
return;
}
PlaceNewWall();
}
Right now I check all requirements two times in two different functions here they are in wall validator:
internal bool WallDoesNotMeetTheRequirements()
{
if (WallPositionIsBeyondBoard()||
PlayerUsedAllAvailableWalls()||
WallIsNotOnTheSameLine() ||
WallIsTooLong() ||
WallTilesHavePairCoordinates() ||
WallDoesNotCoverTwoSolidTiles() ||
WallInterceptsWithOtherWall())
return true;
return false;
}
internal void SendAppropriateMessage()
{
if (WallPositionIsBeyondBoard())
_player.output?.DisplayWallHasPositionBeyondBoardMessage();
if (PlayerUsedAllAvailableWalls())
_player.output?.DisplayNotEnoughWallsMessage();
if (WallIsNotOnTheSameLine())
_player.output?.DisplayWallIsNotOnTheSameLineMessage();
if (WallIsTooLong())
_player.output?.DisplayWallIsTooLongMessage();
if (WallTilesHavePairCoordinates())
_player.output?.DisplayWallTilesHavePairCoordinatesMessage();
if (WallDoesNotCoverTwoSolidTiles())
_player.output?.DisplayWallDoesNotCoverTwoSolidTilesMessage();
if (WallInterceptsWithOtherWall())
_player.output?.DisplayWallInterceptsWithOtherWallMessage();
}
How would you refactor these functions?
I've tried making a "Requirement" class, which accepts "check function" and "message to send" in it's constructor, but have failed. This option does not work in testing environment. The reason is that I am using NUnit testing framework, and when I tried to test wall placement without assigning "ConsoleOutput" (Class, which implements all of the message functions), I would always get a "NullPointer" exception at the point of "Requirement" class initialization.
So, basically, what Im trying to say in code is "Send appropriate message to the output if it is not null, but if it is, then just return true". But with every new requirement in current state, i would need to add check function in two different places.
I don't really like this option of "double checking requirement and sending message" and I feel like I'm missing a simple answer to this problem.
P.S. Sorry for my broken english ;D, hope I explained everything that I could in a way that you can understand.
Aucun commentaire:
Enregistrer un commentaire