Skip to content

Commit

Permalink
Added way to save game to file
Browse files Browse the repository at this point in the history
  • Loading branch information
skotz committed Dec 17, 2016
1 parent 67f8afc commit 683c73e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 31 deletions.
61 changes: 31 additions & 30 deletions Chase.Engine/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,29 +145,7 @@ public void MakeMove(Move move)

public Player GetWinner()
{
int bluePieces = 0;
int redPieces = 0;
for (int i = 0; i < Constants.BoardSize; i++)
{
if (Board[i] > 0)
{
bluePieces++;
}
else if (Board[i] < 0)
{
redPieces++;
}
}

if (bluePieces < Constants.MinimumPieceCount)
{
return Player.Red;
}
else if (redPieces < Constants.MinimumPieceCount)
{
return Player.Blue;
}
return Player.None;
return Board.GetWinner();
}

public List<Move> GetAllMoves()
Expand Down Expand Up @@ -214,6 +192,27 @@ public List<MoveHistory> GetMoveHistory()
return history;
}

public string GetGameNotationString()
{
StringBuilder sb = new StringBuilder();

sb.AppendLine("[Version \"1.0\"]");
sb.AppendLine("[Date \"" + DateTime.Now.ToString("yyyy.MM.dd") + "\"]");
if (BoardHistory.Count > 0)
{
Player winner = BoardHistory[BoardHistory.Count - 1].GetWinner();
sb.AppendLine("[Result \"" + (winner != Player.None ? winner + " Won" : "?") + "\"]");
}
sb.AppendLine();

foreach (MoveHistory move in GetMoveHistory())
{
sb.AppendLine((move.Number.ToString() + ".").PadRight(5, ' ') + (move.RedMove ?? "").PadRight(9, ' ') + move.BlueMove);
}

return sb.ToString();
}

public List<int> GetThreatenedPieces()
{
List<int> threats = new List<int>();
Expand Down Expand Up @@ -257,13 +256,15 @@ public void SaveGameToFile(string file)
{
using (StreamWriter w = new StreamWriter(file))
{
w.WriteLine("Moves: " + Board.MovesHistory);
w.WriteLine("--------------------------------------");
foreach (Position position in BoardHistory)
{
w.Write(GetStringVisualization(position));
w.WriteLine("--------------------------------------");
}
w.Write(GetGameNotationString());

//w.WriteLine("Moves: " + Board.MovesHistory);
//w.WriteLine("--------------------------------------");
//foreach (Position position in BoardHistory)
//{
// w.Write(GetStringVisualization(position));
// w.WriteLine("--------------------------------------");
//}
}
}

Expand Down
27 changes: 27 additions & 0 deletions Chase.Engine/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,33 @@ public List<Move> GetValidMoves(Player player)
return moves;
}

public Player GetWinner()
{
int bluePieces = 0;
int redPieces = 0;
for (int i = 0; i < Constants.BoardSize; i++)
{
if (Board[i] > 0)
{
bluePieces++;
}
else if (Board[i] < 0)
{
redPieces++;
}
}

if (bluePieces < Constants.MinimumPieceCount)
{
return Player.Red;
}
else if (redPieces < Constants.MinimumPieceCount)
{
return Player.Blue;
}
return Player.None;
}

/// <summary>
/// Check if it's possible to move a piece from a given tile in a given direction a given number of tiles.
/// If the move is valid then the destination index will be returned.
Expand Down
21 changes: 20 additions & 1 deletion Chase.GUI/GameForm.Designer.cs

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

13 changes: 13 additions & 0 deletions Chase.GUI/GameForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -490,5 +491,17 @@ private void depthToolStripMenuItem_Click(object sender, EventArgs e)

((ToolStripMenuItem)sender).Checked = true;
}

private void saveGameToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult dr = saveCgn.ShowDialog();
if (dr == DialogResult.OK)
{
using (StreamWriter w = new StreamWriter(saveCgn.FileName))
{
w.Write(game.GetGameNotationString());
}
}
}
}
}
3 changes: 3 additions & 0 deletions Chase.GUI/GameForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="saveCgn.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>248, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
Expand Down

0 comments on commit 683c73e

Please sign in to comment.