Skip to content

Commit

Permalink
Added ability to play back saved games
Browse files Browse the repository at this point in the history
  • Loading branch information
skotz committed Dec 17, 2016
1 parent 683c73e commit 5507913
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 14 deletions.
56 changes: 55 additions & 1 deletion Chase.Engine/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class Game
private List<Move> MoveHistory;

private ISearchAlgorithm search;

private string loadedCgn;

public delegate void SearchProgress(SearchStatus status);

Expand Down Expand Up @@ -68,6 +70,8 @@ public void StartNew(Position position)
BoardHistory.Add(Board.Clone());

MoveHistory = new List<Move>();

loadedCgn = "";
}

private void Search_OnNewResult(object sender, SearchStatus e)
Expand Down Expand Up @@ -125,8 +129,9 @@ public bool IsValidMove(Move move)

public void MakeMove(string move)
{
Board.MakeMove(move);
Move parsed = Board.MakeMove(move);
BoardHistory.Add(Board.Clone());
MoveHistory.Add(parsed);
}

public void MakeMove(Move move)
Expand Down Expand Up @@ -192,6 +197,55 @@ public List<MoveHistory> GetMoveHistory()
return history;
}

public Move RecallState(int move)
{
// TODO: there's got to be a better way to load a state than to re-parse and re-play all past moves from a clean state...
LoadFromGameNotationString(loadedCgn, move);

if (MoveHistory.Count >= 1)
{
return MoveHistory[MoveHistory.Count - 1];
}
else
{
return null;
}
}

public int LoadFromGameNotationString(string cgn)
{
return LoadFromGameNotationString(cgn, int.MaxValue);
}

public int LoadFromGameNotationString(string cgn, int stopOnMove)
{
StartNew();

int moveNum = 0;
foreach (string line in cgn.Split(new string[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries))
{
if (!line.StartsWith("["))
{
string[] parts = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 1; i < parts.Length; i++)
{
if (moveNum < stopOnMove)
{
MakeMove(parts[i]);
moveNum++;
}
}
}
}

if (string.IsNullOrEmpty(loadedCgn))
{
loadedCgn = cgn;
}

return moveNum;
}

public string GetGameNotationString()
{
StringBuilder sb = new StringBuilder();
Expand Down
63 changes: 63 additions & 0 deletions Chase.Engine/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class Move

public int Evaluation { get; set; }

public bool IsValid { get { return FromIndex != -1 || ToIndex != -1 || Increment != -1; } }

/// <summary>
/// The direction the piece was moving when it stopped
/// </summary>
Expand All @@ -46,6 +48,67 @@ public static int GetIndexFromTile(string tile)
return IndexLookup.ContainsKey(tile) ? IndexLookup[tile] : Constants.InvalidTile;
}

public static Move ParseMove(string move)
{
// There are three types of moves...
// Move from one tile to another: A1-A2
// Add points after a capture: A1+=1
// Add points to an adjacent piece: A1-A2+=1

try
{
string[] parts = move.ToUpper().Split(new string[] { "+=" }, StringSplitOptions.None);
if (parts.Length == 2)
{
int increment = int.Parse(parts[1]);
string[] moves = parts[0].Split('-');
int fromTile = GetIndexFromTile(moves[0]);
if (moves.Length == 2)
{
int toTile = GetIndexFromTile(moves[1]);

return new Move()
{
FromIndex = fromTile,
ToIndex = toTile,
Increment = increment
};
}
else
{
return new Move()
{
FromIndex = -1,
ToIndex = fromTile,
Increment = increment
};
}
}
else
{
string[] moves = parts[0].Split('-');
int fromTile = GetIndexFromTile(moves[0]);
int toTile = GetIndexFromTile(moves[1]);

return new Move()
{
FromIndex = fromTile,
ToIndex = toTile,
Increment = 0
};
}
}
catch (Exception)
{
return new Move()
{
FromIndex = -1,
ToIndex = -1,
Increment = -1
};
}
}

public static string GetTileFromIndex(int index)
{
// Indexes of each piece on the board...
Expand Down
18 changes: 16 additions & 2 deletions Chase.Engine/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,23 @@ public Position()
}
}

public void MakeMove(string move)
public Move MakeMove(string move)
{
// TODO: parse string moves and validate before making the move
Move parse = Move.ParseMove(move);
if (parse.IsValid)
{
foreach (Move m in GetValidMoves())
{
if (m.FromIndex == parse.FromIndex && m.ToIndex == parse.ToIndex && m.Increment == parse.Increment)
{
// We can't use the parsed move since it doesn't contain the final direction information
MakeMove(m);
return m;
}
}
}

return null;
}

public void MakeMove(Move move)
Expand Down
86 changes: 76 additions & 10 deletions Chase.GUI/GameForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5507913

Please sign in to comment.