Skip to content

Commit

Permalink
Added move history table
Browse files Browse the repository at this point in the history
  • Loading branch information
skotz committed Dec 16, 2016
1 parent 7fe7469 commit f0eecdb
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 17 deletions.
1 change: 1 addition & 0 deletions Chase.Engine/Chase.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<Compile Include="Game.cs" />
<Compile Include="Interfaces\ISearchAlgorithm.cs" />
<Compile Include="Move.cs" />
<Compile Include="MoveHistory.cs" />
<Compile Include="SearchResult.cs" />
<Compile Include="Player.cs" />
<Compile Include="Position.cs" />
Expand Down
56 changes: 50 additions & 6 deletions Chase.Engine/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public class Game

public Player PlayerToMove { get { return Board.PlayerToMove; } }

private List<Position> History;
private List<Position> BoardHistory;

private List<Move> MoveHistory;

private ISearchAlgorithm search;

Expand Down Expand Up @@ -62,8 +64,10 @@ public void StartNew(Position position)
{
Board = position;

History = new List<Position>();
History.Add(Board.Clone());
BoardHistory = new List<Position>();
BoardHistory.Add(Board.Clone());

MoveHistory = new List<Move>();
}

private void Search_OnNewResult(object sender, SearchStatus e)
Expand Down Expand Up @@ -118,13 +122,14 @@ public bool IsValidMove(Move move)
public void MakeMove(string move)
{
Board.MakeMove(move);
History.Add(Board.Clone());
BoardHistory.Add(Board.Clone());
}

public void MakeMove(Move move)
{
Board.MakeMove(move);
History.Add(Board.Clone());
BoardHistory.Add(Board.Clone());
MoveHistory.Add(move);

// See if the game is over
Player winner = GetWinner();
Expand Down Expand Up @@ -166,6 +171,45 @@ public List<Move> GetAllMoves()
return Board.GetValidMoves();
}

public List<MoveHistory> GetMoveHistory()
{
List<MoveHistory> history = new List<MoveHistory>();
MoveHistory mh = new MoveHistory();
int num = 1;

for (int i = 0; i < MoveHistory.Count; i++)
{
mh.Number = num;
if (BoardHistory[i].PlayerToMove == Player.Red)
{
mh.RedMove = MoveHistory[i].ToString();

if (i + 1 < MoveHistory.Count && BoardHistory[i + 1].PlayerToMove == Player.Red)
{
history.Add(mh);
mh = new MoveHistory();
num++;
}
}
else
{
mh.BlueMove = MoveHistory[i].ToString();

history.Add(mh);
mh = new MoveHistory();

num++;
}
}

if (mh.Number > 0)
{
history.Add(mh);
}

return history;
}

public List<int> GetThreatenedPieces()
{
List<int> threats = new List<int>();
Expand Down Expand Up @@ -211,7 +255,7 @@ public void SaveGameToFile(string file)
{
w.WriteLine("Moves: " + Board.MovesHistory);
w.WriteLine("--------------------------------------");
foreach (Position position in History)
foreach (Position position in BoardHistory)
{
w.Write(GetStringVisualization(position));
w.WriteLine("--------------------------------------");
Expand Down
17 changes: 17 additions & 0 deletions Chase.Engine/MoveHistory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chase.Engine
{
public class MoveHistory
{
public string RedMove { get; set; }

public string BlueMove { get; set; }

public int Number { get; set; }
}
}
108 changes: 101 additions & 7 deletions Chase.GUI/GameForm.Designer.cs

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

31 changes: 27 additions & 4 deletions Chase.GUI/GameForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public GameForm()
private void selfPlayToolStripMenuItem_Click(object sender, EventArgs e)
{
type = GameType.ComputerSelfPlay;
infoLabel.Text = "Thinking...";

game.StartNew();
game.BeginGetBestMove(depth);
Expand All @@ -50,6 +51,11 @@ private void Game_OnFoundBestMove(SearchResult result)
{
game.MakeMove(result.BestMove);
}

if (!showComputerAnalysisToolStripMenuItem.Checked)
{
searchStatusLabel.Text = "Your turn!";
}

RefreshBoard(result.BestMove);

Expand All @@ -71,10 +77,17 @@ private void Game_OnFoundBestMove(SearchResult result)

private void Game_OnSearchProgress(SearchStatus status)
{
searchStatusLabel.Text = "best: " + status.BestMoveSoFar.BestMove.ToString() +
" score: " + status.BestMoveSoFar.Score +
" nps: " + status.NodesPerSecond.ToString("0") +
" pv: " + status.BestMoveSoFar.PrimaryVariation;
if (showComputerAnalysisToolStripMenuItem.Checked)
{
searchStatusLabel.Text = "best: " + status.BestMoveSoFar.BestMove.ToString() +
" score: " + status.BestMoveSoFar.Score +
" nps: " + status.NodesPerSecond.ToString("0") +
" pv: " + status.BestMoveSoFar.PrimaryVariation;
}
else
{
searchStatusLabel.Text = "Thinking...";
}
}

private void testToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -104,6 +117,12 @@ private void RefreshBoard()

private void RefreshBoard(Move lastmove)
{
if (gamePanel.Controls["tile0"] == null)
{
// Probably in the process of closing the program
return;
}

for (int i = 0; i < 81; i++)
{
int piece = game.Board[i];
Expand Down Expand Up @@ -170,6 +189,10 @@ private void RefreshBoard(Move lastmove)
}
}

// Refresh move list
List<MoveHistory> history = game.GetMoveHistory();
moveHistory.DataSource = history;

// Status information
if (type == GameType.NotStarted)
{
Expand Down
9 changes: 9 additions & 0 deletions Chase.GUI/GameForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>132, 17</value>
</metadata>
<metadata name="MoveNumber.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="player1.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="player2.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
Expand Down

0 comments on commit f0eecdb

Please sign in to comment.