From f0eecdb3f623298b362fc0b473977c4b1fd77bb3 Mon Sep 17 00:00:00 2001 From: Scott Clayton Date: Fri, 16 Dec 2016 15:22:12 -0600 Subject: [PATCH] Added move history table --- Chase.Engine/Chase.Engine.csproj | 1 + Chase.Engine/Game.cs | 56 ++++++++++++++-- Chase.Engine/MoveHistory.cs | 17 +++++ Chase.GUI/GameForm.Designer.cs | 108 +++++++++++++++++++++++++++++-- Chase.GUI/GameForm.cs | 31 +++++++-- Chase.GUI/GameForm.resx | 9 +++ 6 files changed, 205 insertions(+), 17 deletions(-) create mode 100644 Chase.Engine/MoveHistory.cs diff --git a/Chase.Engine/Chase.Engine.csproj b/Chase.Engine/Chase.Engine.csproj index ac877a5..fe03095 100644 --- a/Chase.Engine/Chase.Engine.csproj +++ b/Chase.Engine/Chase.Engine.csproj @@ -46,6 +46,7 @@ + diff --git a/Chase.Engine/Game.cs b/Chase.Engine/Game.cs index 4dc502d..6fe7d2c 100644 --- a/Chase.Engine/Game.cs +++ b/Chase.Engine/Game.cs @@ -15,7 +15,9 @@ public class Game public Player PlayerToMove { get { return Board.PlayerToMove; } } - private List History; + private List BoardHistory; + + private List MoveHistory; private ISearchAlgorithm search; @@ -62,8 +64,10 @@ public void StartNew(Position position) { Board = position; - History = new List(); - History.Add(Board.Clone()); + BoardHistory = new List(); + BoardHistory.Add(Board.Clone()); + + MoveHistory = new List(); } private void Search_OnNewResult(object sender, SearchStatus e) @@ -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(); @@ -166,6 +171,45 @@ public List GetAllMoves() return Board.GetValidMoves(); } + public List GetMoveHistory() + { + List history = new List(); + 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 GetThreatenedPieces() { List threats = new List(); @@ -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("--------------------------------------"); diff --git a/Chase.Engine/MoveHistory.cs b/Chase.Engine/MoveHistory.cs new file mode 100644 index 0000000..43375d8 --- /dev/null +++ b/Chase.Engine/MoveHistory.cs @@ -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; } + } +} diff --git a/Chase.GUI/GameForm.Designer.cs b/Chase.GUI/GameForm.Designer.cs index 5359d46..8b5d017 100644 --- a/Chase.GUI/GameForm.Designer.cs +++ b/Chase.GUI/GameForm.Designer.cs @@ -28,6 +28,7 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(GameForm)); this.gamePanel = new System.Windows.Forms.Panel(); this.addPanel = new System.Windows.Forms.Panel(); @@ -58,6 +59,13 @@ private void InitializeComponent() this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); this.showThreatenedPiecesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.copyCSNFromPositionToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.moveHistory = new System.Windows.Forms.DataGridView(); + this.MoveNumber = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.player1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.player2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.showComputerAnalysisToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator(); + this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator(); this.gamePanel.SuspendLayout(); this.addPanel.SuspendLayout(); this.toolStripContainer1.BottomToolStripPanel.SuspendLayout(); @@ -71,6 +79,7 @@ private void InitializeComponent() this.splitContainer1.SuspendLayout(); this.menuStrip1.SuspendLayout(); this.csnPanel.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.moveHistory)).BeginInit(); this.SuspendLayout(); // // gamePanel @@ -218,6 +227,7 @@ private void InitializeComponent() // // splitContainer1.Panel2 // + this.splitContainer1.Panel2.Controls.Add(this.moveHistory); this.splitContainer1.Panel2.Controls.Add(this.infoLabel); this.splitContainer1.Size = new System.Drawing.Size(1095, 662); this.splitContainer1.SplitterDistance = 778; @@ -265,9 +275,12 @@ private void InitializeComponent() // optionsToolStripMenuItem // this.optionsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.highlightValidMovesToolStripMenuItem, this.computerPlaysBlueToolStripMenuItem, - this.showThreatenedPiecesToolStripMenuItem}); + this.toolStripSeparator2, + this.highlightValidMovesToolStripMenuItem, + this.showThreatenedPiecesToolStripMenuItem, + this.toolStripSeparator3, + this.showComputerAnalysisToolStripMenuItem}); this.optionsToolStripMenuItem.Name = "optionsToolStripMenuItem"; this.optionsToolStripMenuItem.Size = new System.Drawing.Size(61, 20); this.optionsToolStripMenuItem.Text = "&Options"; @@ -278,8 +291,8 @@ private void InitializeComponent() this.highlightValidMovesToolStripMenuItem.CheckOnClick = true; this.highlightValidMovesToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked; this.highlightValidMovesToolStripMenuItem.Name = "highlightValidMovesToolStripMenuItem"; - this.highlightValidMovesToolStripMenuItem.Size = new System.Drawing.Size(202, 22); - this.highlightValidMovesToolStripMenuItem.Text = "&Highlight Valid Moves"; + this.highlightValidMovesToolStripMenuItem.Size = new System.Drawing.Size(223, 22); + this.highlightValidMovesToolStripMenuItem.Text = "Highlight Valid &Moves"; // // computerPlaysBlueToolStripMenuItem // @@ -287,7 +300,7 @@ private void InitializeComponent() this.computerPlaysBlueToolStripMenuItem.CheckOnClick = true; this.computerPlaysBlueToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked; this.computerPlaysBlueToolStripMenuItem.Name = "computerPlaysBlueToolStripMenuItem"; - this.computerPlaysBlueToolStripMenuItem.Size = new System.Drawing.Size(202, 22); + this.computerPlaysBlueToolStripMenuItem.Size = new System.Drawing.Size(223, 22); this.computerPlaysBlueToolStripMenuItem.Text = "Computer Plays Blue"; // // testToolStripMenuItem @@ -367,8 +380,8 @@ private void InitializeComponent() this.showThreatenedPiecesToolStripMenuItem.CheckOnClick = true; this.showThreatenedPiecesToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked; this.showThreatenedPiecesToolStripMenuItem.Name = "showThreatenedPiecesToolStripMenuItem"; - this.showThreatenedPiecesToolStripMenuItem.Size = new System.Drawing.Size(202, 22); - this.showThreatenedPiecesToolStripMenuItem.Text = "&Show Threatened Pieces"; + this.showThreatenedPiecesToolStripMenuItem.Size = new System.Drawing.Size(223, 22); + this.showThreatenedPiecesToolStripMenuItem.Text = "Highlight &Threatened Pieces"; // // copyCSNFromPositionToolStripMenuItem // @@ -377,6 +390,79 @@ private void InitializeComponent() this.copyCSNFromPositionToolStripMenuItem.Text = "&Copy CSN from Position"; this.copyCSNFromPositionToolStripMenuItem.Click += new System.EventHandler(this.copyCSNFromPositionToolStripMenuItem_Click); // + // moveHistory + // + this.moveHistory.AllowUserToAddRows = false; + this.moveHistory.AllowUserToDeleteRows = false; + this.moveHistory.AllowUserToResizeColumns = false; + this.moveHistory.AllowUserToResizeRows = false; + this.moveHistory.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.moveHistory.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.MoveNumber, + this.player1, + this.player2}); + dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; + dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Window; + dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.ControlText; + dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Window; + dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.ControlText; + dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.False; + this.moveHistory.DefaultCellStyle = dataGridViewCellStyle1; + this.moveHistory.Location = new System.Drawing.Point(7, 30); + this.moveHistory.Name = "moveHistory"; + this.moveHistory.ReadOnly = true; + this.moveHistory.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None; + this.moveHistory.RowHeadersVisible = false; + this.moveHistory.Size = new System.Drawing.Size(294, 629); + this.moveHistory.TabIndex = 1; + // + // MoveNumber + // + this.MoveNumber.DataPropertyName = "Number"; + this.MoveNumber.HeaderText = "#"; + this.MoveNumber.Name = "MoveNumber"; + this.MoveNumber.ReadOnly = true; + this.MoveNumber.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; + this.MoveNumber.Width = 50; + // + // player1 + // + this.player1.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.player1.DataPropertyName = "RedMove"; + this.player1.HeaderText = "Red"; + this.player1.Name = "player1"; + this.player1.ReadOnly = true; + this.player1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; + // + // player2 + // + this.player2.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.player2.DataPropertyName = "BlueMove"; + this.player2.HeaderText = "Blue"; + this.player2.Name = "player2"; + this.player2.ReadOnly = true; + this.player2.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; + // + // showComputerAnalysisToolStripMenuItem + // + this.showComputerAnalysisToolStripMenuItem.Checked = true; + this.showComputerAnalysisToolStripMenuItem.CheckOnClick = true; + this.showComputerAnalysisToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked; + this.showComputerAnalysisToolStripMenuItem.Name = "showComputerAnalysisToolStripMenuItem"; + this.showComputerAnalysisToolStripMenuItem.Size = new System.Drawing.Size(223, 22); + this.showComputerAnalysisToolStripMenuItem.Text = "Show Computer &Analysis"; + // + // toolStripSeparator2 + // + this.toolStripSeparator2.Name = "toolStripSeparator2"; + this.toolStripSeparator2.Size = new System.Drawing.Size(220, 6); + // + // toolStripSeparator3 + // + this.toolStripSeparator3.Name = "toolStripSeparator3"; + this.toolStripSeparator3.Size = new System.Drawing.Size(220, 6); + // // GameForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -409,6 +495,7 @@ private void InitializeComponent() this.menuStrip1.PerformLayout(); this.csnPanel.ResumeLayout(false); this.csnPanel.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.moveHistory)).EndInit(); this.ResumeLayout(false); } @@ -443,6 +530,13 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; private System.Windows.Forms.ToolStripMenuItem showThreatenedPiecesToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem copyCSNFromPositionToolStripMenuItem; + private System.Windows.Forms.DataGridView moveHistory; + private System.Windows.Forms.DataGridViewTextBoxColumn MoveNumber; + private System.Windows.Forms.DataGridViewTextBoxColumn player1; + private System.Windows.Forms.DataGridViewTextBoxColumn player2; + private System.Windows.Forms.ToolStripMenuItem showComputerAnalysisToolStripMenuItem; + private System.Windows.Forms.ToolStripSeparator toolStripSeparator2; + private System.Windows.Forms.ToolStripSeparator toolStripSeparator3; } } diff --git a/Chase.GUI/GameForm.cs b/Chase.GUI/GameForm.cs index 6c0c747..76cd5d8 100644 --- a/Chase.GUI/GameForm.cs +++ b/Chase.GUI/GameForm.cs @@ -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); @@ -50,6 +51,11 @@ private void Game_OnFoundBestMove(SearchResult result) { game.MakeMove(result.BestMove); } + + if (!showComputerAnalysisToolStripMenuItem.Checked) + { + searchStatusLabel.Text = "Your turn!"; + } RefreshBoard(result.BestMove); @@ -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) @@ -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]; @@ -170,6 +189,10 @@ private void RefreshBoard(Move lastmove) } } + // Refresh move list + List history = game.GetMoveHistory(); + moveHistory.DataSource = history; + // Status information if (type == GameType.NotStarted) { diff --git a/Chase.GUI/GameForm.resx b/Chase.GUI/GameForm.resx index 8e24af7..7af31c9 100644 --- a/Chase.GUI/GameForm.resx +++ b/Chase.GUI/GameForm.resx @@ -120,6 +120,15 @@ 132, 17 + + True + + + True + + + True + 17, 17