Thursday, 22 August 2013

Can someone help me improve speed?

Can someone help me improve speed?

As of now this generates a dictionary full of points that a player can
move to and the cost of that move. My problem is that it takes ages to
process.
I understand that although a square is already checked as added my method
checks the same square again, but when I dont do this than the moveable
area gets all screwed up and it does not return what it should.
Can i get some pointers on how to speed this up, Please include
examplecode. I was thinking about a few extra threads but that would leave
me processing this in quadrents and if i need to form movements around an
object on the grid then I dont think ill be able to build off of cells
belonging to other threads.
This code works right now, it is just slow as snot and its unacceptable
for my program.
Please note that This is not path finding, I am only looking for all
possible moves, not the shortest way to get to a spot.
class MoveCompiler
{
public Board Board;
public MoveCompiler(Board board)
{
Board = board;
}
Dictionary<Coordinate, int> MovesList = new Dictionary<Coordinate,
int>(new Coordinate.EqualityComparer());
public Dictionary<Coordinate, int> getMoves( int startRow, int
startColumn, int actionPointsUsed,int maxActionPoints, bool
firstDiagnol)
{
int moveCost = 1;
if (!firstDiagnol)
{
moveCost = 2;
}
if (actionPointsUsed + moveCost <= maxActionPoints)
{
if (IfCheck(startRow + 1, startColumn + 1))
{
if (!Board.cellAt(new Coordinate(startRow + 1,
(startColumn + 1))).InUse)
{
MovesList.Add(new Coordinate(startRow + 1,
(startColumn + 1)), actionPointsUsed + moveCost);
Board.cellAt(new Coordinate(startRow + 1, (startColumn
+ 1))).InUse = true;
}getMoves(startRow + 1, startColumn + 1, actionPointsUsed
+ moveCost, maxActionPoints, !firstDiagnol);
}
if (IfCheck(startRow + 1, startColumn - 1))
{
if (!Board.cellAt(new Coordinate(startRow + 1,
(startColumn - 1))).InUse)
{
MovesList.Add(new Coordinate(startRow + 1,
(startColumn - 1)), actionPointsUsed + moveCost);
Board.cellAt(new Coordinate(startRow + 1, (startColumn
- 1))).InUse = true;
}getMoves(startRow + 1, startColumn - 1, actionPointsUsed
+ moveCost, maxActionPoints, !firstDiagnol);
}
if (IfCheck(startRow - 1, startColumn - 1))
{
if (!Board.cellAt(new Coordinate(startRow - 1,
(startColumn - 1))).InUse)
{
MovesList.Add(new Coordinate(startRow - 1,
(startColumn - 1)), actionPointsUsed + moveCost);
Board.cellAt(new Coordinate(startRow - 1, (startColumn
- 1))).InUse = true;
}getMoves(startRow - 1, startColumn - 1, actionPointsUsed
+ moveCost, maxActionPoints, !firstDiagnol);
}
if (IfCheck(startRow - 1, startColumn + 1))
{
if (!Board.cellAt(new Coordinate(startRow - 1,
(startColumn + 1))).InUse)
{
MovesList.Add(new Coordinate(startRow - 1,
(startColumn + 1)), actionPointsUsed + moveCost);
Board.cellAt(new Coordinate(startRow - 1, (startColumn
+ 1))).InUse = true;
}getMoves(startRow - 1, startColumn + 1, actionPointsUsed
+ moveCost, maxActionPoints, !firstDiagnol);
}
}
if (actionPointsUsed + 1 <= maxActionPoints)
{
if (IfCheck(startRow - 1, startColumn))
{
if (!Board.cellAt(new Coordinate(startRow - 1,
(startColumn))).InUse)
{
MovesList.Add(new Coordinate(startRow - 1,
(startColumn)), actionPointsUsed + moveCost);
Board.cellAt(new Coordinate(startRow - 1,
(startColumn))).InUse = true;
}getMoves(startRow - 1, startColumn, actionPointsUsed + 1,
maxActionPoints, firstDiagnol);
}
if (IfCheck(startRow + 1, startColumn))
{
if (!Board.cellAt(new Coordinate(startRow + 1,
(startColumn))).InUse)
{
MovesList.Add(new Coordinate(startRow + 1,
(startColumn)), actionPointsUsed + moveCost);
Board.cellAt(new Coordinate(startRow + 1,
(startColumn))).InUse = true;
}getMoves(startRow + 1, startColumn, actionPointsUsed + 1,
maxActionPoints, firstDiagnol);
}
if (IfCheck(startRow, startColumn + 1))
{
if (!Board.cellAt(new Coordinate(startRow, (startColumn +
1))).InUse)
{
MovesList.Add(new Coordinate(startRow, (startColumn +
1)), actionPointsUsed + moveCost);
Board.cellAt(new Coordinate(startRow, (startColumn +
1))).InUse = true;
}getMoves(startRow, startColumn + 1, actionPointsUsed + 1,
maxActionPoints, firstDiagnol);
}
if (IfCheck(startRow, startColumn - 1))
{
if (!Board.cellAt(new Coordinate(startRow, (startColumn -
1))).InUse)
{
MovesList.Add(new Coordinate(startRow, (startColumn -
1)), actionPointsUsed + moveCost);
Board.cellAt(new Coordinate(startRow, (startColumn -
1))).InUse = true;
}getMoves(startRow, startColumn - 1, actionPointsUsed + 1,
maxActionPoints, firstDiagnol);
}
}
return MovesList;
}
private bool IfCheck(int desiredRow, int desiredColumn)
{
bool ReturnMe= false;
if (desiredRow <= Board.BOARDHEIGHT && desiredRow >= 1)
if (desiredColumn + 1 <= Board.BOARDWIDTH && desiredColumn >= 1)
if (!Board.cellAt(new Coordinate(desiredRow,
desiredColumn)).hasCharacter)
//if (!Board.cellAt(new Coordinate(desiredRow,
(desiredColumn))).InUse)
if (Board.cellAt(new Coordinate(desiredRow,
desiredColumn)).isPassable)
ReturnMe = true;
return ReturnMe;
}
private void VH()
{
}
}

No comments:

Post a Comment