diff --git a/Chase.Engine/Game.cs b/Chase.Engine/Game.cs index 810f260..98e2452 100644 --- a/Chase.Engine/Game.cs +++ b/Chase.Engine/Game.cs @@ -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 GetAllMoves() @@ -214,6 +192,27 @@ public List 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 GetThreatenedPieces() { List threats = new List(); @@ -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("--------------------------------------"); + //} } } diff --git a/Chase.Engine/Position.cs b/Chase.Engine/Position.cs index 434bdf0..c36a645 100644 --- a/Chase.Engine/Position.cs +++ b/Chase.Engine/Position.cs @@ -354,6 +354,33 @@ public List 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; + } + /// /// 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. diff --git a/Chase.GUI/GameForm.Designer.cs b/Chase.GUI/GameForm.Designer.cs index adc8ba7..c180dec 100644 --- a/Chase.GUI/GameForm.Designer.cs +++ b/Chase.GUI/GameForm.Designer.cs @@ -72,6 +72,8 @@ private void InitializeComponent() this.showComputerAnalysisToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.showTileLabelsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.testToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.saveGameToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.saveCgn = new System.Windows.Forms.SaveFileDialog(); this.gamePanel.SuspendLayout(); this.csnPanel.SuspendLayout(); this.addPanel.SuspendLayout(); @@ -366,7 +368,8 @@ private void InitializeComponent() this.levelToolStripMenuItem, this.toolStripSeparator1, this.loadPositionFromCSNToolStripMenuItem, - this.copyCSNFromPositionToolStripMenuItem}); + this.copyCSNFromPositionToolStripMenuItem, + this.saveGameToolStripMenuItem}); this.gameToolStripMenuItem.Name = "gameToolStripMenuItem"; this.gameToolStripMenuItem.Size = new System.Drawing.Size(50, 20); this.gameToolStripMenuItem.Text = "&Game"; @@ -527,6 +530,20 @@ private void InitializeComponent() this.testToolStripMenuItem.Visible = false; this.testToolStripMenuItem.Click += new System.EventHandler(this.testToolStripMenuItem_Click); // + // saveGameToolStripMenuItem + // + this.saveGameToolStripMenuItem.Name = "saveGameToolStripMenuItem"; + this.saveGameToolStripMenuItem.Size = new System.Drawing.Size(203, 22); + this.saveGameToolStripMenuItem.Text = "&Save Game as CGN"; + this.saveGameToolStripMenuItem.Click += new System.EventHandler(this.saveGameToolStripMenuItem_Click); + // + // saveCgn + // + this.saveCgn.DefaultExt = "cgn"; + this.saveCgn.FileName = "game.cgn"; + this.saveCgn.Filter = "Chase Game Notation|*.cgn"; + this.saveCgn.Title = "Save Game"; + // // GameForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -607,6 +624,8 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem threeMovesDeep; private System.Windows.Forms.ToolStripMenuItem fiveSecondsMove; private System.Windows.Forms.ToolStripMenuItem twentySecondsMove; + private System.Windows.Forms.ToolStripMenuItem saveGameToolStripMenuItem; + private System.Windows.Forms.SaveFileDialog saveCgn; } } diff --git a/Chase.GUI/GameForm.cs b/Chase.GUI/GameForm.cs index ae2a128..731ac43 100644 --- a/Chase.GUI/GameForm.cs +++ b/Chase.GUI/GameForm.cs @@ -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; @@ -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()); + } + } + } } } diff --git a/Chase.GUI/GameForm.resx b/Chase.GUI/GameForm.resx index 7af31c9..ed9af71 100644 --- a/Chase.GUI/GameForm.resx +++ b/Chase.GUI/GameForm.resx @@ -132,6 +132,9 @@ 17, 17 + + 248, 17 +