*********************************************************
Developer: Ranjeet kumar
Email id: ddkhune@gmail.com
*********************************************************
.......Main......Form(ChessBoard).....Coding
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ranjeet_chess_game {
public partial class frmCreatePGNGame : Form {
private List<ChessBoard.MovePosS> m_arrMoveList;
private ChessBoard.PlayerColorE m_eStartingColor;
private ChessBoard m_chessBoardStarting;
private string m_strWhitePlayerName;
private string m_strBlackPlayerName;
private PgnParser.PlayerTypeE m_eWhiteType;
private PgnParser.PlayerTypeE m_eBlackType;
private TimeSpan m_spanWhitePlayer;
private TimeSpan m_spanBlackPlayer;
private void butOk_Click(object sender, EventArgs e) {
string strGame;
PgnParser parser;
List<ChessBoard.MovePosS> listGame;
int iSkip;
int iTruncated;
strGame = textBox1.Text;
if (String.IsNullOrEmpty(strGame)) {
MessageBox.Show("No PGN text has been pasted.");
} else {
listGame = new List<ChessBoard.MovePosS>(256);
parser = new PgnParser(false);
if (!parser.ParseSingle(strGame,
false,
listGame,
out iSkip,
out iTruncated,
out m_chessBoardStarting,
out m_eStartingColor,
out m_strWhitePlayerName,
out m_strBlackPlayerName,
out m_eWhiteType,
out m_eBlackType,
out m_spanWhitePlayer,
out m_spanBlackPlayer)) {
MessageBox.Show("The specified board is invalid.");
} else if (iSkip != 0) {
MessageBox.Show("The game is incomplete. Paste another game.");
} else if (iTruncated != 0) {
MessageBox.Show("The selected game includes an unsupported pawn promotion (only pawn promotion to queen is supported).");
} else if (listGame.Count == 0 && m_chessBoardStarting == null) {
MessageBox.Show("Game is empty.");
} else {
m_arrMoveList = listGame;
DialogResult = DialogResult.OK;
Close();
}
}
}
public List<ChessBoard.MovePosS> MoveList {
get {
return(m_arrMoveList);
}
}
public ChessBoard.PlayerColorE StartingColor {
get {
return(m_eStartingColor);
}
}
public string WhitePlayerName {
get {
return(m_strWhitePlayerName);
}
}
public string BlackPlayerName {
get {
return(m_strBlackPlayerName);
}
}
public PgnParser.PlayerTypeE WhitePlayerType {
get {
return(m_eWhiteType);
}
}
public TimeSpan BlackTimer {
get {
return(m_spanBlackPlayer);
}
}
private void frmCreatePGNGame_Load(object sender, EventArgs e)
{
}
}
}
File ToolStripMenuItem_Click Form2....(create parameters).....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ranjeet_chess_game {
public partial class frmGameParameter : Form {
private frmChessBoard m_frmFather;
public frmGameParameter() {
InitializeComponent();
}
private frmGameParameter(frmChessBoard frmFather) : this() {
m_frmFather = frmFather;
switch(m_frmFather.PlayingMode) {
case frmChessBoard.PlayingModeE.DesignMode:
throw new System.ApplicationException("Must not be called in design mode.");
case frmChessBoard.PlayingModeE.PlayerAgainstComputer:
radioButtonPlayerAgainstComputer.Checked = true;
break;
case frmChessBoard.PlayingModeE.PlayerAgainstPlayer:
radioButtonPlayerAgainstPlayer.Checked = true;
break;
case frmChessBoard.PlayingModeE.ComputerAgainstComputer:
radioButtonComputerAgainstComputer.Checked = true;
break;
}
switch(m_frmFather.m_eComputerPlayingColor) {
case ChessBoard.PlayerColorE.Black:
radioButtonComputerPlayBlack.Checked = true;
break;
case ChessBoard.PlayerColorE.White:
radioButtonComputerPlayWhite.Checked = true;
break;
}
CheckState();
}
private void CheckState() {
groupBoxComputerPlay.Enabled = radioButtonPlayerAgainstComputer.Checked;
}
private void butOk_Click(object sender, EventArgs e) {
if (radioButtonPlayerAgainstComputer.Checked) {
m_frmFather.PlayingMode = frmChessBoard.PlayingModeE.PlayerAgainstComputer;
} else if (radioButtonPlayerAgainstPlayer.Checked) {
m_frmFather.PlayingMode = frmChessBoard.PlayingModeE.PlayerAgainstPlayer;
} else if (radioButtonComputerAgainstComputer.Checked) {
m_frmFather.PlayingMode = frmChessBoard.PlayingModeE.ComputerAgainstComputer;
}
m_frmFather.m_eComputerPlayingColor = (radioButtonComputerPlayBlack.Checked) ? ChessBoard.PlayerColorE.Black : ChessBoard.PlayerColorE.White;
}
private void radioButtonPlayerAgainstComputer_CheckedChanged(object sender, EventArgs e) {
CheckState();
}
private void radioButtonPlayerAgainstPlayer_CheckedChanged(object sender, EventArgs e) {
CheckState();
}
private void radioButtonComputerAgainstComputer_CheckedChanged(object sender, EventArgs e) {
CheckState();
}
public static bool AskGameParameter(frmChessBoard frmFather) {
bool bRetVal;
frmGameParameter frm;
frm = new frmGameParameter(frmFather);
using(frm) {
bRetVal = (frm.ShowDialog() == DialogResult.OK);
}
return(bRetVal);
}
private void frmGameParameter_Load(object sender, EventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ranjeet_chess_game {
/// <summary>Form use to filter the content of a PGN file</summary>
public partial class frmPGNFilter : Form {
///ry <summa>Represent an ELO range in the checked list control</summary>
private class RangeItem {
public int m_iRange;
public RangeItem(int iRange) { m_iRange = iRange; }
public override string ToString() {
return("Range " + m_iRange.ToString() + " - " + (m_iRange + 99).ToString());
}
}
public PgnUtil.FilterClause m_filterClause;
/// <summary>PGN utility class</summary>
private PgnUtil m_pgnUtil;
private Stream m_streamInp;
public frmPGNFilter() {
InitializeComponent();
m_filterClause = new PgnUtil.FilterClause();
}
public frmPGNFilter(PgnUtil pgnUtil, Stream streamInp, int iMinELO, int iMaxELO, string[] arrPlayers, string strInpFileName, int iGameCount) : this() {
m_pgnUtil = pgnUtil;
m_streamInp = streamInp;
iMinELO = iMinELO / 100 * 100;
checkedListBoxRange.Items.Clear();
for (int iIndex = iMinELO; iIndex < iMaxELO; iIndex += 100) {
checkedListBoxRange.Items.Add(new RangeItem(iIndex), true);
}
checkedListBoxPlayers.Items.Clear();
foreach (string strPlayer in arrPlayers) {
checkedListBoxPlayers.Items.Add(strPlayer, true);
}
checkedListBoxEnding.Items.Clear();
checkedListBoxEnding.Items.Add("White win", true);
checkedListBoxEnding.Items.Add("Black win", true);
checkedListBoxEnding.Items.Add("Draws", true);
checkBoxAllRanges.Checked = true;
checkBoxAllPlayer.Checked = true;
checkBoxAllEndGame.Checked = true;
checkedListBoxPlayers.Enabled = false;
checkedListBoxRange.Enabled = false;
checkedListBoxEnding.Enabled = false;
labelDesc.Text = iGameCount.ToString() + " games found in the file '" + strInpFileName + "'";
}
private void CheckAllItems(CheckedListBox checkedListBox, bool bChecked) {
int iCount;
checkedListBox.SuspendLayout();
iCount = checkedListBox.Items.Count;
for (int iIndex = 0; iIndex < iCount; iIndex++) {
checkedListBox.SetItemChecked(iIndex, bChecked);
}
checkedListBox.ResumeLayout();
}
private void butSelectAllRange_Click(object sender, EventArgs e) {
CheckAllItems(checkedListBoxRange, true);
}
private void buttonClearAllRange_Click(object sender, EventArgs e) {
CheckAllItems(checkedListBoxRange, false);
}
private void buttonSelectAllPlayers_Click(object sender, EventArgs e) {
CheckAllItems(checkedListBoxPlayers, true);
}
private void buttonClearAllPlayers_Click(object sender, EventArgs e) {
CheckAllItems(checkedListBoxPlayers, false);
}
private void buttonSelectAllEndGame_Click(object sender, EventArgs e) {
CheckAllItems(checkedListBoxEnding, true);
}
private void buttonClearAllEndGame_Click(object sender, EventArgs e) {
CheckAllItems(checkedListBoxEnding, false);
}
private void checkBoxAllRanges_CheckedChanged(object sender, EventArgs e) {
checkedListBoxRange.Enabled = !checkBoxAllRanges.Checked;
buttonClearAllRange.Enabled = !checkBoxAllRanges.Checked;
butSelectAllRange.Enabled = !checkBoxAllRanges.Checked;
}
private void checkBoxAllPlayer_CheckedChanged(object sender, EventArgs e) {
checkedListBoxPlayers.Enabled = !checkBoxAllPlayer.Checked;
buttonClearAllPlayers.Enabled = !checkBoxAllPlayer.Checked;
buttonSelectAllPlayers.Enabled = !checkBoxAllPlayer.Checked;
}
private void checkBoxAllEndGame_CheckedChanged(object sender, EventArgs e) {
checkedListBoxEnding.Enabled = !checkBoxAllEndGame.Checked;
buttonClearAllEndGame.Enabled = !checkBoxAllEndGame.Checked;
buttonSelectAllEndGame.Enabled = !checkBoxAllEndGame.Checked;
}
private bool SyncInfo() {
bool bRetVal = true;
if (checkBoxAllRanges.Checked || checkedListBoxRange.CheckedItems.Count == checkedListBoxRange.Items.Count) {
m_filterClause.m_bAllRanges = true;
m_filterClause.m_hashRanges = null;
} else {
m_filterClause.m_bAllEnding = false;
if (checkedListBoxRange.CheckedItems.Count == 0 && !checkBoxIncludeUnrated.Checked) {
MessageBox.Show("At least one range must be selected.");
bRetVal = false;
} else {
m_filterClause.m_hashRanges = new Dictionary<int,int>(checkedListBoxRange.CheckedItems.Count);
foreach (RangeItem rangeItem in checkedListBoxRange.CheckedItems) {
m_filterClause.m_hashRanges.Add(rangeItem.m_iRange, 0);
}
}
}
m_filterClause.m_bIncludesUnrated = checkBoxIncludeUnrated.Checked;
if (checkBoxAllPlayer.Checked || checkedListBoxPlayers.CheckedItems.Count == checkedListBoxPlayers.Items.Count) {
m_filterClause.m_bAllPlayers = true;
m_filterClause.m_hashPlayerList = null;
} else {
m_filterClause.m_bAllPlayers = false;
if (checkedListBoxPlayers.CheckedItems.Count == 0) {
MessageBox.Show("At least one player must be selected.");
bRetVal = false;
} else {
m_filterClause.m_hashPlayerList = new Dictionary<string,string>(checkedListBoxPlayers.CheckedItems.Count);
foreach (string strPlayer in checkedListBoxPlayers.CheckedItems) {
m_filterClause.m_hashPlayerList.Add(strPlayer, null);
}
}
}
if (checkBoxAllEndGame.Checked || checkedListBoxEnding.CheckedItems.Count == checkedListBoxEnding.Items.Count) {
m_filterClause.m_bAllEnding = true;
m_filterClause.m_bEndingWhiteWinning = true;
m_filterClause.m_bEndingBlackWinning = true;
m_filterClause.m_bEndingDraws = true;
} else {
m_filterClause.m_bAllEnding = false;
if (checkedListBoxEnding.CheckedItems.Count == 0) {
MessageBox.Show("At least one ending must be selected.");
bRetVal = false;
} else {
m_filterClause.m_bEndingWhiteWinning = checkedListBoxEnding.GetItemChecked(0);
m_filterClause.m_bEndingBlackWinning = checkedListBoxEnding.GetItemChecked(1);
m_filterClause.m_bEndingDraws = checkedListBoxEnding.GetItemChecked(2);
}
}
if (m_filterClause.m_bAllRanges &&
m_filterClause.m_bAllPlayers &&
m_filterClause.m_bAllEnding &&
m_filterClause.m_bIncludesUnrated) {
MessageBox.Show("At least one filtering option must be selected.");
bRetVal = false;
}
return(bRetVal);
}
public PgnUtil.FilterClause FilteringClause {
get {
return(m_filterClause);
}
}
private void butOk_Click(object sender, EventArgs e) {
if (SyncInfo()) {
DialogResult = DialogResult.OK;
Close();
}
}
private void butTest_Click(object sender, EventArgs e) {
int iCount;
if (SyncInfo()) {
iCount = m_pgnUtil.FilterPGN(m_streamInp, null, FilteringClause);
MessageBox.Show("The specified filter will result in " + iCount.ToString() + " game(s) selected.");
}
}
private void frmPGNFilter_Load(object sender, EventArgs e)
{
}
}
}
Developer: Ranjeet kumar
Email id: ddkhune@gmail.com
*********************************************************
.......Main......Form(ChessBoard).....Coding
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
namespace ranjeet_chess_game {
public partial class ChessBoard : Form, ChessControl.IMoveListUI {
private class LocalChessControl : ChessControl {
private ChessBoard m_frmFather;
public LocalChessControl() : base() {
}
public ChessBoard Father {
get {
return(m_frmFather);
}
set {
m_frmFather = value;
}
}
public override bool LoadGame(BinaryReader reader) {
bool bRetVal;
string strVersion;
PlayingModeE ePlayingMode;
strVersion = reader.ReadString();
if (strVersion == "SRCCHESS095") {
bRetVal = base.LoadGame(reader);
if (bRetVal) {
ePlayingMode = (PlayingModeE)reader.ReadInt32();
m_frmFather.PlayingMode = ePlayingMode;
} else {
bRetVal = false;
}
} else {
bRetVal = false;
}
return(bRetVal);
}
public override void SaveGame(BinaryWriter writer) {
writer.Write("SRCCHESS095");
base.SaveGame(writer);
writer.Write((int)m_frmFather.m_ePlayingMode);
}
public override void CreateGameFromMove(ChessBoard chessBoardStarting,
List<ChessBoard.MovePosS> listMove,
ChessBoard.PlayerColorE eNextMoveColor,
string strWhitePlayerName,
string strBlackPlayerName,
PgnParser.PlayerTypeE eWhitePlayerType,
PgnParser.PlayerTypeE eBlackPlayerType,
TimeSpan spanPlayerWhite,
TimeSpan spanPlayerBlack) {
base.CreateGameFromMove(chessBoardStarting,
listMove,
eNextMoveColor,
strWhitePlayerName,
strBlackPlayerName,
eWhitePlayerType,
eBlackPlayerType,
spanPlayerWhite,
spanPlayerBlack);
if (eWhitePlayerType == PgnParser.PlayerTypeE.Program) {
if (eBlackPlayerType == PgnParser.PlayerTypeE.Program) {
Father.PlayingMode = PlayingModeE.ComputerAgainstComputer;
} else {
Father.PlayingMode = PlayingModeE.PlayerAgainstComputer;
Father.m_eComputerPlayingColor = ChessBoard.PlayerColorE.White;
}
} else if (eBlackPlayerType == PgnParser.PlayerTypeE.Program) {
Father.PlayingMode = PlayingModeE.PlayerAgainstComputer;
Father.m_eComputerPlayingColor = ChessBoard.PlayerColorE.Black;
} else {
Father.PlayingMode = PlayingModeE.PlayerAgainstPlayer;
}
Father.SetCmdState();
}
}
private class ComputerPlayingStat {
public ComputerPlayingStat() { m_timeSpanMethod1 = TimeSpan.Zero; m_timeSpanMethod2 = TimeSpan.Zero; m_eResult = ChessBoard.MoveResultE.NoRepeat; m_iMethod1MoveCount = 0; m_iMethod2MoveCount = 0; m_bUserCancel = false; }
public TimeSpan m_timeSpanMethod1;
public TimeSpan m_timeSpanMethod2;
public ChessBoard.MoveResultE m_eResult;
public int m_iMethod1MoveCount;
public int m_iMethod2MoveCount;
public bool m_bUserCancel;
};
public enum MessageModeE {
Silent = 0,
CallEndGame = 1,
Verbose = 2
};
public enum PlayingModeE {
PlayerAgainstPlayer,
PlayerAgainstComputer,
ComputerAgainstComputer,
DesignMode
};
private LocalChessControl m_chessCtl;
private LostPiecesControl m_lostPieceBlack;
private LostPiecesControl m_lostPieceWhite;
private MoveViewer m_moveViewer;
private PlayingModeE m_ePlayingMode;
public ChessBoard.PlayerColorE m_eComputerPlayingColor;
private bool m_bSecondThreadBusy;
private SearchEngine.SearchMode m_searchMode;
private BoardEvaluationUtil m_boardEvalUtil;
public ChessBoard() {
SrcChess.Properties.Settings settings;
SearchEngine.SearchMode.OptionE eOption;
SearchEngine.SearchMode.ThreadingModeE eThreadingMode;
int iTransTableSize;
IBoardEvaluation boardEvalWhite;
IBoardEvaluation boardEvalBlack;
ChessControl.ChessControlColorInfo colorInfo;
InitializeComponent();
settings = SrcChess.Properties.Settings.Default;
colorInfo.m_colBlackPiece = NameToColor(settings.BlackPieceColor);
colorInfo.m_colWhitePiece = NameToColor(settings.WhitePieceColor);
colorInfo.m_colLiteCase = NameToColor(settings.LiteCaseColor);
colorInfo.m_colDarkCase = NameToColor(settings.DarkCaseColor);
iTransTableSize = (settings.TransTableSize < 5 || settings.TransTableSize > 256) ? 32 : settings.TransTableSize;
TransTable.TranslationTableSize = iTransTableSize / 32 * 1000000;
eOption = settings.UseAlphaBeta ? SearchEngine.SearchMode.OptionE.UseAlphaBeta : SearchEngine.SearchMode.OptionE.UseMinMax;
if (settings.UseBook) {
eOption |= SearchEngine.SearchMode.OptionE.UseBook;
}
if (settings.UseTransTable) {
eOption |= SearchEngine.SearchMode.OptionE.UseTransTable;
}
if (settings.UsePlyCountIterative) {
eOption |= SearchEngine.SearchMode.OptionE.UseIterativeDepthSearch;
}
switch(settings.UseThread) {
case 0:
eThreadingMode = SearchEngine.SearchMode.ThreadingModeE.Off;
break;
case 1:
eThreadingMode = SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch;
break;
default:
eThreadingMode = SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch;
break;
}
m_boardEvalUtil = new BoardEvaluationUtil();
boardEvalWhite = m_boardEvalUtil.FindBoardEvaluator(settings.WhiteBoardEval);
if (boardEvalWhite == null) {
boardEvalWhite = m_boardEvalUtil.BoardEvaluators[0];
}
boardEvalBlack = m_boardEvalUtil.FindBoardEvaluator(settings.BlackBoardEval);
if (boardEvalBlack == null) {
boardEvalBlack = m_boardEvalUtil.BoardEvaluators[0];
}
m_searchMode = new SearchEngine.SearchMode(boardEvalWhite,
boardEvalBlack,
eOption,
eThreadingMode,
settings.UsePlyCount | settings.UsePlyCountIterative ? ((settings.PlyCount > 1 && settings.PlyCount < 9) ? settings.PlyCount : 6) : 0, // Maximum depth
settings.UsePlyCount | settings.UsePlyCountIterative ? 0 : (settings.AverageTime > 0 && settings.AverageTime < 1000) ? settings.AverageTime : 15,
(settings.RandomMode >= 0 && settings.RandomMode <= 2) ? (SearchEngine.SearchMode.RandomModeE)settings.RandomMode : SearchEngine.SearchMode.RandomModeE.On); // Average time
m_chessCtl = new LocalChessControl();
m_chessCtl.Location = new Point(0, 0);
m_chessCtl.Size = new Size(60 * 9, 60 * 9);
m_chessCtl.UpdateCmdState += new EventHandler(m_chessCtl_UpdateCmdState);
m_chessCtl.Father = this;
m_chessCtl.ColorInfo = colorInfo;
panelBoard.Controls.Add(m_chessCtl);
PlayingMode = PlayingModeE.PlayerAgainstComputer;
m_eComputerPlayingColor = ChessBoard.PlayerColorE.Black;
m_lostPieceBlack = new LostPiecesControl();
m_lostPieceBlack.Location = new Point(0,0);
m_lostPieceBlack.Size = new Size(200, 200);
m_lostPieceBlack.Dock = DockStyle.Fill;
m_lostPieceBlack.ChessControl = m_chessCtl;
m_lostPieceBlack.Color = true;
panelBlackLostPiece.Controls.Add(m_lostPieceBlack);
m_lostPieceWhite = new LostPiecesControl();
m_lostPieceWhite.Location = new Point(0,0);
m_lostPieceWhite.Size = new Size(200, 200);
m_lostPieceWhite.Dock = DockStyle.Fill;
m_lostPieceWhite.ChessControl = m_chessCtl;
m_lostPieceWhite.Color = false;
panelWhiteLostPiece.Controls.Add(m_lostPieceWhite);
m_moveViewer = new MoveViewer();
m_moveViewer.Location = new Point(0,0);
m_moveViewer.Size = panelMoveList.Size;
m_moveViewer.Dock = DockStyle.Fill;
m_moveViewer.NewMoveSelected += new MoveViewer.NewMoveSelectedHandler(m_moveViewer_NewMoveSelected);
m_moveViewer.DisplayMode = (settings.MoveNotation == 0) ? MoveViewer.DisplayModeE.MovePos : MoveViewer.DisplayModeE.PGN;
panelMoveList.Controls.Add(m_moveViewer);
m_chessCtl.MoveListUI = this;
m_chessCtl.MoveSelected += new ChessControl.MoveSelectedEventHandler(m_chessCtl_MoveSelected);
m_chessCtl.QueryPiece += new ChessControl.QueryPieceEventHandler(m_chessCtl_QueryPiece);
m_chessCtl.QueryPawnPromotionType += new ChessControl.QueryPawnPromotionTypeEventHandler(m_chessCtl_QueryPawnPromotionType);
m_bSecondThreadBusy = false;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
ResizeChessCtl();
SetCmdState();
ShowSearchMode();
flashPieceToolStripMenuItem.Checked = settings.FlashPiece;
pgnNotationToolStripMenuItem.Checked = (m_moveViewer.DisplayMode == MoveViewer.DisplayModeE.PGN);
}
private Color NameToColor(string strName) {
Color colRetVal;
int iVal;
if (strName.Length == 8 && (Char.IsLower(strName[0]) || Char.IsDigit(strName[0])) &&
Int32.TryParse(strName, System.Globalization.NumberStyles.HexNumber, null, out iVal)) {
colRetVal = Color.FromArgb((iVal >> 24) & 255, (iVal >> 16) & 255, (iVal >> 8) & 255, iVal & 255);
} else {
colRetVal = Color.FromName(strName);
}
return(colRetVal);
}
private void ResizeChessCtl() {
Rectangle rect;
int iCtlSize;
int iXPos;
int iYPos;
if (m_chessCtl != null) {
rect = panelBoard.ClientRectangle;
iCtlSize = (rect.Width < rect.Height) ? rect.Width : rect.Height;
iXPos = (rect.Width - iCtlSize) / 2;
iYPos = (rect.Height - iCtlSize) / 2;
m_chessCtl.Location = new Point(iXPos, iYPos);
m_chessCtl.Size = new Size(iCtlSize, iCtlSize);
}
}
private void panelBoard_Resize(object sender, EventArgs e) {
ResizeChessCtl();
}
public PlayingModeE PlayingMode {
get {
return(m_ePlayingMode);
}
set {
m_ePlayingMode = value;
playerAgainstPlayerToolStripMenuItem.Checked = (m_ePlayingMode == PlayingModeE.PlayerAgainstPlayer);
switch(value) {
case PlayingModeE.PlayerAgainstPlayer:
m_chessCtl.WhitePlayerType = PgnParser.PlayerTypeE.Human;
m_chessCtl.BlackPlayerType = PgnParser.PlayerTypeE.Human;
break;
case PlayingModeE.PlayerAgainstComputer:
if (m_eComputerPlayingColor == ChessBoard.PlayerColorE.White) {
m_chessCtl.WhitePlayerType = PgnParser.PlayerTypeE.Program;
m_chessCtl.BlackPlayerType = PgnParser.PlayerTypeE.Human;
} else {
m_chessCtl.WhitePlayerType = PgnParser.PlayerTypeE.Human;
m_chessCtl.BlackPlayerType = PgnParser.PlayerTypeE.Program;
}
break;
default:
m_chessCtl.WhitePlayerType = PgnParser.PlayerTypeE.Program;
m_chessCtl.BlackPlayerType = PgnParser.PlayerTypeE.Program;
break;
}
}
}
private void SetPlayingMode(PlayingModeE ePlayingMode) {
PlayingMode = ePlayingMode;
}
private void StartAsyncComputing() {
bool bDifferentThreadForUI;
if (m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch) {
bDifferentThreadForUI = true;
} else if (m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch) {
bDifferentThreadForUI = true;
} else {
bDifferentThreadForUI = false;
}
if (bDifferentThreadForUI) {
m_bSecondThreadBusy = true;
SetCmdState();
}
toolStripStatusLabelMove.Text = "Finding Best Move...";
toolStripStatusLabelPerm.Text = "";
Cursor = Cursors.WaitCursor;
}
private void ShowMoveInStatusBar(ChessBoard.PlayerColorE ePlayerColor, ChessBoard.MovePosS move, int iPermCount, int iDepth, int iCacheHit) {
string strPermCount;
System.Globalization.CultureInfo ci;
ci = new System.Globalization.CultureInfo("en-US");
switch(iPermCount) {
case -1:
strPermCount = "Found in Book.";
break;
case 0:
strPermCount = "---";
break;
default:
strPermCount = iPermCount.ToString("C0", ci).Replace("$", "") + " permutations evaluated. " + iCacheHit.ToString("C0", ci).Replace("$","") + " found in cache.";
break;
}
if (iDepth != -1) {
strPermCount += " " + iDepth.ToString() + " ply.";
}
strPermCount += " " + m_chessCtl.LastFindBestMoveTimeSpan.TotalSeconds.ToString() + " sec(s).";
toolStripStatusLabelMove.Text = ((ePlayerColor == ChessBoard.PlayerColorE.Black) ? "Black " : "White ") + ChessBoard.GetHumanPos(move);
toolStripStatusLabelPerm.Text = strPermCount;
}
private void ShowSearchMode() {
string str;
if ((m_searchMode.m_eOption & SearchEngine.SearchMode.OptionE.UseAlphaBeta) == SearchEngine.SearchMode.OptionE.UseAlphaBeta) {
str = "Alpha-Beta ";
} else {
str = "Min-Max ";
}
if (m_searchMode.m_iSearchDepth == 0) {
str += "(Iterative " + m_searchMode.m_iTimeOutInSec.ToString() + " secs) ";
} else if ((m_searchMode.m_eOption & SearchEngine.SearchMode.OptionE.UseIterativeDepthSearch) == SearchEngine.SearchMode.OptionE.UseIterativeDepthSearch) {
str += "(Iterative " + m_searchMode.m_iSearchDepth.ToString() + " ply) ";
} else {
str += "(" + m_searchMode.m_iSearchDepth.ToString() + " ply) ";
}
if (m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch) {
str += "using " + Environment.ProcessorCount.ToString() + " processor";
if (Environment.ProcessorCount > 1) {
str += "s";
}
str += ". ";
} else {
str += "using 1 processor. ";
}
toolStripStatusLabelSearchMode.Text = str;
}
void ChessControl.IMoveListUI.Reset(ChessBoard chessBoard) {
m_moveViewer.Reset(chessBoard);
}
void ChessControl.IMoveListUI.NewMoveDone(int iPermCount, int iDepth, int iCacheHit) {
ChessBoard.MovePosS movePos;
ChessBoard.PlayerColorE eMoveColor;
m_moveViewer.NewMoveDone(iPermCount, iDepth, iCacheHit);
movePos = m_chessCtl.ChessBoard.MovePosStack.CurrentMove;
eMoveColor = m_chessCtl.ChessBoard.CurrentMoveColor;
ShowMoveInStatusBar(eMoveColor, movePos, iPermCount, iDepth, iCacheHit);
}
private bool DisplayMessage(ChessBoard.MoveResultE eMoveResult, MessageModeE eMessageMode) {
bool bRetVal;
string strOpponent;
if (m_ePlayingMode == PlayingModeE.PlayerAgainstComputer) {
if (m_chessCtl.ChessBoard.NextMoveColor == m_eComputerPlayingColor) {
strOpponent = "Computer is ";
} else {
strOpponent = "You are ";
}
} else {
strOpponent = (m_chessCtl.ChessBoard.NextMoveColor == ChessBoard.PlayerColorE.White) ? "White player is " : "Black player is ";
}
switch(eMoveResult) {
case ChessBoard.MoveResultE.NoRepeat:
bRetVal = false;
break;
case ChessBoard.MoveResultE.TieNoMove:
if (eMessageMode != MessageModeE.Silent) {
MessageBox.Show("Draw. " + strOpponent + "unable to move.");
}
bRetVal = true;
break;
case ChessBoard.MoveResultE.TieNoMatePossible:
if (eMessageMode != MessageModeE.Silent) {
MessageBox.Show("Draw. Not enough pieces to make a checkmate.");
}
bRetVal = true;
break;
case ChessBoard.MoveResultE.ThreeFoldRepeat:
if (eMessageMode != MessageModeE.Silent) {
MessageBox.Show("Draw. 3 times the same board.");
}
bRetVal = true;
break;
case ChessBoard.MoveResultE.FiftyRuleRepeat:
if (eMessageMode != MessageModeE.Silent) {
MessageBox.Show("Draw. 50 moves without moving a pawn or eating a piece.");
}
bRetVal = true;
break;
case ChessBoard.MoveResultE.Check:
if (eMessageMode == MessageModeE.Verbose) {
MessageBox.Show(strOpponent + "in check.");
}
bRetVal = false;
break;
case ChessBoard.MoveResultE.Mate:
if (eMessageMode != MessageModeE.Silent) {
MessageBox.Show(strOpponent + "checkmate.");
}
bRetVal = true;
break;
default:
bRetVal = false;
break;
}
return(bRetVal);
}
void m_moveViewer_NewMoveSelected(object sender, MoveViewer.NewMoveSelectedEventArg e) {
ChessBoard.MoveResultE eResult;
bool bSucceed;
if (PlayingMode == PlayingModeE.PlayerAgainstPlayer) {
eResult = m_chessCtl.SelectMove(e.NewIndex, out bSucceed);
DisplayMessage(eResult, MessageModeE.Verbose);
e.Cancel = !bSucceed;
} else {
e.Cancel = true;
}
}
void m_chessCtl_UpdateCmdState(object sender, EventArgs e) {
m_lostPieceBlack.Invalidate();
m_lostPieceWhite.Invalidate();
SetCmdState();
}
private void SetCmdState() {
bool bDesignMode;
bDesignMode = (PlayingMode == PlayingModeE.DesignMode);
m_chessCtl.AutoSelection = !m_bSecondThreadBusy;
newGameToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
createAGameFromPGNToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
loadGameToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
toolStripButtonLoad.Enabled = !(m_bSecondThreadBusy || bDesignMode);
saveGameToolStripMenuItem1.Enabled = !(m_bSecondThreadBusy || bDesignMode);
toolStripButtonSave.Enabled = !(m_bSecondThreadBusy || bDesignMode);
quitToolStripMenuItem.Enabled = !m_bSecondThreadBusy;
hintToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
toolStripButtonHint.Enabled = !(m_bSecondThreadBusy || bDesignMode);
undoToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode || m_chessCtl.UndoCount < ((m_ePlayingMode == PlayingModeE.PlayerAgainstPlayer) ? 1 : 2));
toolStripButtonUndo.Enabled = !(m_bSecondThreadBusy || bDesignMode || m_chessCtl.UndoCount < ((m_ePlayingMode == PlayingModeE.PlayerAgainstPlayer) ? 1 : 2));
redoToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode || m_chessCtl.RedoCount == 0);
toolStripButtonRedo.Enabled = !(m_bSecondThreadBusy || bDesignMode || m_chessCtl.RedoCount == 0);
playerAgainstPlayerToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
searchModeToolStripMenuItem.Enabled = !m_bSecondThreadBusy;
flashPieceToolStripMenuItem.Enabled = !m_bSecondThreadBusy;
automaticPlayToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
fastAutomaticPlayToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
designModeToolStripMenuItem.Enabled = !m_bSecondThreadBusy;
revertBoardToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
createABookToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
filterAPGNFileToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
cancelPlayToolStripMenuItem.Enabled = m_bSecondThreadBusy;
toolStripButtonStop.Enabled = m_bSecondThreadBusy;
}
private void UnlockBoard() {
m_bSecondThreadBusy = false;
Cursor = Cursors.Arrow;
SetCmdState();
}
private bool PlayComputerMove(bool bFlash, MessageModeE eMessageMode, ChessBoard.MovePosS move, int iPermCount, int iDepth, int iCacheHit, ComputerPlayingStat stat) {
bool bRetVal;
ChessBoard.MoveResultE eResult;
ChessBoard.PlayerColorE eColorToPlay;
eColorToPlay = m_chessCtl.NextMoveColor;
eResult = m_chessCtl.DoMove(move,
bFlash,
iPermCount,
iDepth,
iCacheHit);
if (stat != null) {
stat.m_eResult = eResult;
}
bRetVal = DisplayMessage(eResult, eMessageMode);
return(bRetVal);
}
private void PlayComputerAsync(bool bFlash, SearchEngine.SearchMode searchMode) {
Func<bool> delPlayComputerMove;
Action delUnlockBoard;
Action delSetPlayingMode;
ChessBoard chessBoard;
ChessBoard.MovePosS move;
int iPermCount;
int iCacheHit;
int iMaxDepth;
bool bEndOfGame;
bool bMoveFound;
bool bMultipleThread;
chessBoard = m_chessCtl.ChessBoard.Clone();
bMultipleThread = (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
bMoveFound = m_chessCtl.FindBestMove(searchMode,
chessBoard,
out move,
out iPermCount,
out iCacheHit,
out iMaxDepth);
if (bMoveFound) {
if (bMultipleThread) {
delPlayComputerMove = () => PlayComputerMove(bFlash, MessageModeE.CallEndGame, move, iPermCount, iMaxDepth, iCacheHit, null);
bEndOfGame = (bool)Invoke(delPlayComputerMove);
} else {
bEndOfGame = PlayComputerMove(bFlash, MessageModeE.CallEndGame, move, iPermCount, iMaxDepth, iCacheHit, null);
}
} else {
bEndOfGame = DisplayMessage(m_chessCtl.ChessBoard.CheckNextMove(), MessageModeE.CallEndGame);
}
if (bMultipleThread) {
delUnlockBoard = () => UnlockBoard();
Invoke(delUnlockBoard);
if (bEndOfGame) {
delSetPlayingMode = () => SetPlayingMode(PlayingModeE.PlayerAgainstPlayer);
Invoke(delSetPlayingMode);
}
} else {
UnlockBoard();
if (bEndOfGame) {
SetPlayingMode(PlayingModeE.PlayerAgainstPlayer);
}
}
}
private void PlayComputer(bool bFlash, bool bSilent) {
bool bMultipleThread;
bMultipleThread = (m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
StartAsyncComputing();
if (bMultipleThread) {
Task.Factory.StartNew(() => PlayComputerAsync(bFlash, m_searchMode));
} else {
PlayComputerAsync(bFlash, m_searchMode);
}
}
private void PlayComputerAgainstComputerAsync(bool bFlash, SearchEngine.SearchMode searchMode) {
bool bEndOfGame;
int iCount;
Func<ChessBoard.MovePosS,int,int,int,bool> delPlayComputerMove = null;
Action delUnlockBoard;
Action delSetPlayingMode;
ChessBoard chessBoard;
ChessBoard.MovePosS move;
int iPermCount;
int iCacheHit;
int iMaxDepth;
bool bMoveFound;
bool bMultipleThread;
bMultipleThread = (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
if (bMultipleThread) {
delPlayComputerMove = (a1,a2,a3,a4) => { return(PlayComputerMove(bFlash, MessageModeE.CallEndGame, a1, a2, a3, a4, null)); };
}
iCount = 0;
do {
chessBoard = m_chessCtl.ChessBoard.Clone();
bMoveFound = m_chessCtl.FindBestMove(searchMode,
chessBoard,
out move,
out iPermCount,
out iCacheHit,
out iMaxDepth);
if (bMoveFound) {
if (bMultipleThread) {
bEndOfGame = (bool)Invoke(delPlayComputerMove, new object[] { move, iPermCount, iMaxDepth, iCacheHit });
} else {
bEndOfGame = PlayComputerMove(bFlash, MessageModeE.CallEndGame, move, iPermCount, iMaxDepth, iCacheHit, null);
}
} else {
bEndOfGame = DisplayMessage(m_chessCtl.ChessBoard.CheckNextMove(), MessageModeE.Verbose);
}
iCount++;
} while (!bEndOfGame && PlayingMode == PlayingModeE.ComputerAgainstComputer && iCount < 500);
if (PlayingMode != PlayingModeE.ComputerAgainstComputer) {
MessageBox.Show("Automatic play canceled");
} else {
if (bMultipleThread) {
delSetPlayingMode = () => SetPlayingMode(PlayingModeE.PlayerAgainstPlayer);
Invoke(delSetPlayingMode);
} else {
SetPlayingMode(PlayingModeE.PlayerAgainstPlayer);
}
if (iCount >= 500) {
MessageBox.Show("Tie!");
}
}
if (bMultipleThread) {
delUnlockBoard = () => UnlockBoard();
Invoke(delUnlockBoard);
} else {
UnlockBoard();
}
}
private void PlayComputerAgainstComputer(bool bFlash) {
bool bMultipleThread;
bMultipleThread = (m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
PlayingMode = PlayingModeE.ComputerAgainstComputer;
StartAsyncComputing();
if (bMultipleThread) {
Task.Factory.StartNew(() => PlayComputerAgainstComputerAsync(bFlash, m_searchMode));
} else {
PlayComputerAgainstComputerAsync(bFlash, m_searchMode);
}
}
private void TestShowResult(int iGameCount, SearchEngine.SearchMode searchMode, ComputerPlayingStat stat, int iMethod1Win, int iMethod2Win) {
string strMsg;
string strMethod1;
string strMethod2;
int iTimeMethod1;
int iTimeMethod2;
strMethod1 = searchMode.m_boardEvaluationWhite.Name;
strMethod2 = searchMode.m_boardEvaluationBlack.Name;
iTimeMethod1 = (stat.m_iMethod1MoveCount == 0) ? 0 : stat.m_timeSpanMethod1.Milliseconds / stat.m_iMethod1MoveCount;
iTimeMethod2 = (stat.m_iMethod2MoveCount == 0) ? 0 : stat.m_timeSpanMethod2.Milliseconds / stat.m_iMethod2MoveCount;
strMsg = iGameCount.ToString() + " game(s) played.\r\n" +
iMethod1Win.ToString() + " win(s) for method #1 (" + strMethod1 + "). Average time = " + iMethod1Win.ToString() + " ms per move.\r\n" +
iMethod2Win.ToString() + " win(s) for method #2 (" + strMethod2 + "). Average time = " + iMethod2Win.ToString() + " ms per move.\r\n" +
(iGameCount - iMethod1Win - iMethod2Win).ToString() + " draw(s).";
MessageBox.Show(strMsg);
}
private void TestComputerAgainstComputerAsync(int iGameCount, SearchEngine.SearchMode searchMode) {
int iCount;
Func<ChessBoard.MovePosS,int,int,int,ComputerPlayingStat,bool> delPlayComputerMove = null;
Action delResetBoard = null;
Action delUnlockBoard;
Action delSetPlayingMode;
Action delShowResultDel;
ChessBoard chessBoard;
ChessBoard.MovePosS move;
int iPermCount;
int iCacheHit;
int iMaxDepth;
int iGameIndex;
int iMethod1Win = 0;
int iMethod2Win = 0;
DateTime dateTime;
bool bMoveFound;
bool bMultipleThread;
bool bEven;
bool bEndOfGame;
IBoardEvaluation boardEvaluation1;
IBoardEvaluation boardEvaluation2;
ComputerPlayingStat stat;
stat = new ComputerPlayingStat();
bMultipleThread = (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
boardEvaluation1 = searchMode.m_boardEvaluationWhite;
boardEvaluation2 = searchMode.m_boardEvaluationBlack;
if (bMultipleThread) {
delPlayComputerMove = (a1, a2, a3, a4, a5) => PlayComputerMove(false, MessageModeE.Silent, a1, a2, a3, a4, a5);
delResetBoard = () => ResetBoard();
}
iGameIndex = 0;
while (iGameIndex < iGameCount && !stat.m_bUserCancel) {
bEven = ((iGameIndex & 1) == 0);
searchMode.m_boardEvaluationWhite = bEven ? boardEvaluation1 : boardEvaluation2;
searchMode.m_boardEvaluationBlack = bEven ? boardEvaluation2 : boardEvaluation1;
if (bMultipleThread) {
Invoke(delResetBoard);
} else {
ResetBoard();
}
iCount = 0;
do {
chessBoard = m_chessCtl.ChessBoard.Clone();
dateTime = DateTime.Now;
bMoveFound = m_chessCtl.FindBestMove(searchMode,
chessBoard,
out move,
out iPermCount,
out iCacheHit,
out iMaxDepth);
if (bMoveFound) {
if ((m_chessCtl.NextMoveColor == ChessBoard.PlayerColorE.White && bEven) ||
(m_chessCtl.NextMoveColor == ChessBoard.PlayerColorE.Black && !bEven)) {
stat.m_timeSpanMethod1 += DateTime.Now - dateTime;
stat.m_iMethod1MoveCount++;
} else {
stat.m_timeSpanMethod2 += DateTime.Now - dateTime;
stat.m_iMethod2MoveCount++;
}
if (bMultipleThread) {
bEndOfGame = (bool)Invoke(delPlayComputerMove, new object[] { move, iPermCount, iMaxDepth, iCacheHit, stat } );
} else {
bEndOfGame = PlayComputerMove(false, MessageModeE.Silent, move, iPermCount, iMaxDepth, iCacheHit, stat);
}
} else {
bEndOfGame = true;
}
iCount++;
} while (!bEndOfGame && PlayingMode == PlayingModeE.ComputerAgainstComputer && iCount < 250);
if (PlayingMode != PlayingModeE.ComputerAgainstComputer) {
stat.m_bUserCancel = true;
} else if (iCount < 250) {
if (stat.m_eResult == ChessBoard.MoveResultE.Mate) {
if ((m_chessCtl.NextMoveColor == ChessBoard.PlayerColorE.Black && bEven) ||
(m_chessCtl.NextMoveColor == ChessBoard.PlayerColorE.White && !bEven)) {
iMethod1Win++;
} else {
iMethod2Win++;
}
}
}
iGameIndex++;
}
searchMode.m_boardEvaluationWhite = boardEvaluation1;
searchMode.m_boardEvaluationBlack = boardEvaluation2;
if (bMultipleThread) {
delSetPlayingMode = () => SetPlayingMode(PlayingModeE.PlayerAgainstPlayer);
delUnlockBoard = () => UnlockBoard();
delShowResultDel = () => TestShowResult(iGameIndex, searchMode, stat, iMethod1Win, iMethod2Win);
Invoke(delShowResultDel);
Invoke(delSetPlayingMode);
Invoke(delUnlockBoard);
} else {
TestShowResult(iGameIndex, searchMode, stat, iMethod1Win, iMethod2Win);
SetPlayingMode(PlayingModeE.PlayerAgainstPlayer);
UnlockBoard();
}
}
private void TestComputerAgainstComputer(int iGameCount, SearchEngine.SearchMode searchMode) {
bool bMultipleThread;
bMultipleThread = (m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
PlayingMode = PlayingModeE.ComputerAgainstComputer;
StartAsyncComputing();
if (bMultipleThread) {
Task.Factory.StartNew(() => TestComputerAgainstComputerAsync(iGameCount, searchMode));
} else {
TestComputerAgainstComputerAsync(iGameCount, searchMode);
}
}
private void ShowHintMove(ChessBoard.MovePosS move, int iPermCount, int iDepth, int iCacheHit) {
ShowMoveInStatusBar(m_chessCtl.NextMoveColor, move, iPermCount, iDepth, iCacheHit);
m_chessCtl.ShowHintMove(move);
}
private void ShowHintAsync(SearchEngine.SearchMode searchMode) {
Action delShowHintMove;
Action delUnlockBoard;
ChessBoard chessBoard;
ChessBoard.MovePosS move;
int iPermCount;
int iCacheHit;
int iMaxDepth;
bool bMoveFound;
bool bMultipleThread;
chessBoard = m_chessCtl.ChessBoard.Clone();
bMultipleThread = (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
bMoveFound = m_chessCtl.FindBestMove(searchMode,
chessBoard,
out move,
out iPermCount,
out iCacheHit,
out iMaxDepth);
if (bMultipleThread) {
if (bMoveFound) {
delShowHintMove = () => ShowHintMove(move, iPermCount, iMaxDepth, iCacheHit);
Invoke(delShowHintMove);
}
delUnlockBoard = () => UnlockBoard();
Invoke(delUnlockBoard);
} else {
if (bMoveFound) {
ShowHintMove(move, iPermCount, iMaxDepth, iCacheHit);
}
UnlockBoard();
}
}
private void ShowHint() {
bool bMultipleThread;
bMultipleThread = (m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
StartAsyncComputing();
if (bMultipleThread) {
Task.Factory.StartNew(() => ShowHintAsync(m_searchMode));
} else {
ShowHintAsync(m_searchMode);
}
}
private void Undo(bool bPlayerAgainstComputer) {
bool bFlash;
bFlash = flashPieceToolStripMenuItem.Checked;
if (bPlayerAgainstComputer) {
if (m_chessCtl.UndoCount > 1) {
m_chessCtl.UndoMove(bFlash); // Computer
m_chessCtl.UndoMove(bFlash); // User
}
} else {
if (m_chessCtl.UndoCount != 0) {
m_chessCtl.UndoMove(bFlash);
}
}
}
private ChessBoard.MoveResultE RedoMove(bool bPlayerAgainstComputer) {
ChessBoard.MoveResultE eRetVal = ChessBoard.MoveResultE.NoRepeat;
bool bFlash;
bFlash = flashPieceToolStripMenuItem.Checked;
if (m_chessCtl.RedoCount != 0) {
eRetVal = m_chessCtl.RedoMove(bFlash); // Computer
}
if (bPlayerAgainstComputer && m_chessCtl.RedoCount != 0) {
eRetVal = m_chessCtl.RedoMove(bFlash); // User
}
return(eRetVal);
}
private void CheckIfComputerMustPlay() {
switch(PlayingMode) {
case PlayingModeE.ComputerAgainstComputer:
Update();
PlayComputerAgainstComputer(true);
break;
case PlayingModeE.PlayerAgainstComputer:
if (m_chessCtl.NextMoveColor == m_eComputerPlayingColor) {
Update();
PlayComputer(flashPieceToolStripMenuItem.Checked, false);
}
break;
default:
break;
}
}
void m_chessCtl_MoveSelected(object sender, ChessControl.MoveSelectedEventArgs e) {
bool bFlash;
bool bEndOfGame;
ChessBoard.MoveResultE eMoveResult;
bFlash = flashPieceToolStripMenuItem.Checked;
eMoveResult = m_chessCtl.DoMove(e.Move, bFlash, -1, 0, 0);
bEndOfGame = DisplayMessage(eMoveResult, MessageModeE.Verbose);
if (!bEndOfGame) {
CheckIfComputerMustPlay();
}
}
private void ToggleDesignMode() {
if (PlayingMode == PlayingModeE.DesignMode) {
PlayingMode = PlayingModeE.PlayerAgainstPlayer;
if (frmGameParameter.AskGameParameter(this)) {
m_chessCtl.BoardDesignMode = false;
if (m_chessCtl.BoardDesignMode) {
PlayingMode = PlayingModeE.DesignMode;
MessageBox.Show("Invalid board configuration. Correct or reset.");
} else {
m_lostPieceBlack.BoardDesignMode = false;
panelWhiteLostPiece.Visible = true;
CheckIfComputerMustPlay();
}
} else {
PlayingMode = PlayingModeE.DesignMode;
}
} else {
PlayingMode = PlayingModeE.DesignMode;
m_lostPieceBlack.BoardDesignMode = true;
panelWhiteLostPiece.Visible = false;
m_chessCtl.BoardDesignMode = true;
}
SetCmdState();
}
void m_chessCtl_QueryPiece(object sender, ChessControl.QueryPieceEventArgs e) {
e.Piece = m_lostPieceBlack.SelectedPiece;
}
void m_chessCtl_QueryPawnPromotionType(object sender, ChessControl.QueryPawnPromotionTypeEventArgs e) {
frmQueryPawnPromotionType frm;
using(frm = new frmQueryPawnPromotionType(e.ValidPawnPromotion)) {
if (frm.ShowDialog(this) == DialogResult.OK) {
e.PawnPromotionType = frm.PromotionType;
} else {
e.PawnPromotionType = ChessBoard.MoveTypeE.Normal;
}
}
}
private void hintToolStripMenuItem_Click(object sender, EventArgs e) {
ShowHint();
}
private void redoToolStripMenuItem_Click(object sender, EventArgs e) {
ChessBoard.MoveResultE eMoveResult;
eMoveResult = RedoMove((PlayingMode != PlayingModeE.PlayerAgainstPlayer));
DisplayMessage(eMoveResult, MessageModeE.Verbose);
}
private void automaticPlayToolStripMenuItem_Click(object sender, EventArgs e) {
PlayComputerAgainstComputer(true);
}
private void loadGameToolStripMenuItem_Click(object sender, EventArgs e) {
if (m_chessCtl.LoadFromFile()) {
Invalidate();
CheckIfComputerMustPlay();
}
}
private void cancelAutomaticPlayToolStripMenuItem_Click(object sender, EventArgs e) {
if (PlayingMode == PlayingModeE.ComputerAgainstComputer) {
PlayingMode = PlayingModeE.PlayerAgainstPlayer;
} else {
m_chessCtl.CancelSearch();
}
}
private void playerAgainstPlayerToolStripMenuItem_Click(object sender, EventArgs e) {
if (playerAgainstPlayerToolStripMenuItem.Checked) {
PlayingMode = PlayingModeE.PlayerAgainstComputer;
if (frmGameParameter.AskGameParameter(this)) {
CheckIfComputerMustPlay();
}
} else {
PlayingMode = PlayingModeE.PlayerAgainstPlayer;
}
}
private void revertBoardToolStripMenuItem_Click(object sender, EventArgs e) {
m_eComputerPlayingColor = (m_eComputerPlayingColor == ChessBoard.PlayerColorE.Black) ? ChessBoard.PlayerColorE.White :
ChessBoard.PlayerColorE.Black;
CheckIfComputerMustPlay();
}
private void createABookToolStripMenuItem_Click(object sender, EventArgs e) {
m_chessCtl.CreateBookFromFiles();
}
private void filterAPGNFileToolStripMenuItem_Click(object sender, EventArgs e) {
PgnUtil pgnUtil;
pgnUtil = new PgnUtil();
pgnUtil.CreatePGNSubset();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
frmAbout frm;
using(frm = new frmAbout()) {
frm.ShowDialog(this);
}
}
private void searchModeToolStripMenuItem_Click(object sender, EventArgs e) {
frmSearchMode frm;
using(frm = new frmSearchMode(m_searchMode, m_boardEvalUtil)) {
if (frm.ShowDialog(this) == DialogResult.OK) {
frm.UpdateSearchMode();
ShowSearchMode();
}
}
}
private void testBoardEvaluationToolStripMenuItem_Click(object sender, EventArgs e) {
frmTestBoardEval frm;
SearchEngine.SearchMode searchMode;
int iGameCount;
using(frm = new frmTestBoardEval(m_boardEvalUtil, m_searchMode)) {
if (frm.ShowDialog(this) == DialogResult.OK) {
searchMode = frm.SearchMode;
iGameCount = frm.GameCount;
TestComputerAgainstComputer(iGameCount, searchMode);
}
}
}
void timer1_Tick(object sender, EventArgs e) {
GameTimer gameTimer;
gameTimer = m_chessCtl.GameTimer;
toolStripLabelTimeWhite.Text = GameTimer.GetHumanElapse(gameTimer.WhitePlayTime);
toolStripLabelTimeBlack.Text = GameTimer.GetHumanElapse(gameTimer.BlackPlayTime);
}
private void toolStripButtonLoad_Click(object sender, EventArgs e) {
loadGameToolStripMenuItem_Click(sender, e);
}
private void toolStripButtonSave_Click(object sender, EventArgs e) {
saveGameToolStripMenuItem1_Click(sender, e);
}
private void toolStripButtonHint_Click(object sender, EventArgs e) {
hintToolStripMenuItem_Click(sender, e);
}
private void toolStripButtonUndo_Click(object sender, EventArgs e) {
undoToolStripMenuItem_Click(sender, e);
}
private void toolStripButtonRedo_Click(object sender, EventArgs e) {
redoToolStripMenuItem_Click(sender, e);
}
private void toolStripButtonStop_Click(object sender, EventArgs e) {
cancelAutomaticPlayToolStripMenuItem_Click(sender, e);
}
private void pgnNotationToolStripMenuItem_Click(object sender, EventArgs e) {
SrcChess.Properties.Settings settings;
m_moveViewer.DisplayMode = (pgnNotationToolStripMenuItem.Checked) ? MoveViewer.DisplayModeE.PGN : MoveViewer.DisplayModeE.MovePos;
settings = SrcChess.Properties.Settings.Default;
settings.MoveNotation = (m_moveViewer.DisplayMode == MoveViewer.DisplayModeE.MovePos) ? 0 : 1;
settings.Save();
}
private void flashPieceToolStripMenuItem_Click(object sender, EventArgs e) {
SrcChess.Properties.Settings settings;
settings = SrcChess.Properties.Settings.Default;
settings.FlashPiece = flashPieceToolStripMenuItem.Checked;
settings.Save();
}
private void chooseColorsToolStripMenuItem_Click(object sender, EventArgs e) {
frmPickupColor frm;
Properties.Settings settings;
ChessControl.ChessControlColorInfo colorInfo;
using(frm = new frmPickupColor(m_chessCtl.ColorInfo)) {
if (frm.ShowDialog() == DialogResult.OK) {
colorInfo = frm.ColorInfo;
m_chessCtl.ColorInfo = colorInfo;
settings = Properties.Settings.Default;
settings.WhitePieceColor = colorInfo.m_colWhitePiece.Name;
settings.BlackPieceColor = colorInfo.m_colBlackPiece.Name;
settings.LiteCaseColor = colorInfo.m_colLiteCase.Name;
settings.DarkCaseColor = colorInfo.m_colDarkCase.Name;
settings.Save();
}
}
}
private void saveGameToPGNToolStripMenuItem_Click(object sender, EventArgs e) {
m_chessCtl.SavePGNToFile();
}
private void frmChessBoard_Load(object sender, EventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
namespace ranjeet_chess_game {
public partial class ChessBoard : Form, ChessControl.IMoveListUI {
private class LocalChessControl : ChessControl {
private ChessBoard m_frmFather;
public LocalChessControl() : base() {
}
public ChessBoard Father {
get {
return(m_frmFather);
}
set {
m_frmFather = value;
}
}
public override bool LoadGame(BinaryReader reader) {
bool bRetVal;
string strVersion;
PlayingModeE ePlayingMode;
strVersion = reader.ReadString();
if (strVersion == "SRCCHESS095") {
bRetVal = base.LoadGame(reader);
if (bRetVal) {
ePlayingMode = (PlayingModeE)reader.ReadInt32();
m_frmFather.PlayingMode = ePlayingMode;
} else {
bRetVal = false;
}
} else {
bRetVal = false;
}
return(bRetVal);
}
public override void SaveGame(BinaryWriter writer) {
writer.Write("SRCCHESS095");
base.SaveGame(writer);
writer.Write((int)m_frmFather.m_ePlayingMode);
}
public override void CreateGameFromMove(ChessBoard chessBoardStarting,
List<ChessBoard.MovePosS> listMove,
ChessBoard.PlayerColorE eNextMoveColor,
string strWhitePlayerName,
string strBlackPlayerName,
PgnParser.PlayerTypeE eWhitePlayerType,
PgnParser.PlayerTypeE eBlackPlayerType,
TimeSpan spanPlayerWhite,
TimeSpan spanPlayerBlack) {
base.CreateGameFromMove(chessBoardStarting,
listMove,
eNextMoveColor,
strWhitePlayerName,
strBlackPlayerName,
eWhitePlayerType,
eBlackPlayerType,
spanPlayerWhite,
spanPlayerBlack);
if (eWhitePlayerType == PgnParser.PlayerTypeE.Program) {
if (eBlackPlayerType == PgnParser.PlayerTypeE.Program) {
Father.PlayingMode = PlayingModeE.ComputerAgainstComputer;
} else {
Father.PlayingMode = PlayingModeE.PlayerAgainstComputer;
Father.m_eComputerPlayingColor = ChessBoard.PlayerColorE.White;
}
} else if (eBlackPlayerType == PgnParser.PlayerTypeE.Program) {
Father.PlayingMode = PlayingModeE.PlayerAgainstComputer;
Father.m_eComputerPlayingColor = ChessBoard.PlayerColorE.Black;
} else {
Father.PlayingMode = PlayingModeE.PlayerAgainstPlayer;
}
Father.SetCmdState();
}
}
private class ComputerPlayingStat {
public ComputerPlayingStat() { m_timeSpanMethod1 = TimeSpan.Zero; m_timeSpanMethod2 = TimeSpan.Zero; m_eResult = ChessBoard.MoveResultE.NoRepeat; m_iMethod1MoveCount = 0; m_iMethod2MoveCount = 0; m_bUserCancel = false; }
public TimeSpan m_timeSpanMethod1;
public TimeSpan m_timeSpanMethod2;
public ChessBoard.MoveResultE m_eResult;
public int m_iMethod1MoveCount;
public int m_iMethod2MoveCount;
public bool m_bUserCancel;
};
public enum MessageModeE {
Silent = 0,
CallEndGame = 1,
Verbose = 2
};
public enum PlayingModeE {
PlayerAgainstPlayer,
PlayerAgainstComputer,
ComputerAgainstComputer,
DesignMode
};
private LocalChessControl m_chessCtl;
private LostPiecesControl m_lostPieceBlack;
private LostPiecesControl m_lostPieceWhite;
private MoveViewer m_moveViewer;
private PlayingModeE m_ePlayingMode;
public ChessBoard.PlayerColorE m_eComputerPlayingColor;
private bool m_bSecondThreadBusy;
private SearchEngine.SearchMode m_searchMode;
private BoardEvaluationUtil m_boardEvalUtil;
public ChessBoard() {
SrcChess.Properties.Settings settings;
SearchEngine.SearchMode.OptionE eOption;
SearchEngine.SearchMode.ThreadingModeE eThreadingMode;
int iTransTableSize;
IBoardEvaluation boardEvalWhite;
IBoardEvaluation boardEvalBlack;
ChessControl.ChessControlColorInfo colorInfo;
InitializeComponent();
settings = SrcChess.Properties.Settings.Default;
colorInfo.m_colBlackPiece = NameToColor(settings.BlackPieceColor);
colorInfo.m_colWhitePiece = NameToColor(settings.WhitePieceColor);
colorInfo.m_colLiteCase = NameToColor(settings.LiteCaseColor);
colorInfo.m_colDarkCase = NameToColor(settings.DarkCaseColor);
iTransTableSize = (settings.TransTableSize < 5 || settings.TransTableSize > 256) ? 32 : settings.TransTableSize;
TransTable.TranslationTableSize = iTransTableSize / 32 * 1000000;
eOption = settings.UseAlphaBeta ? SearchEngine.SearchMode.OptionE.UseAlphaBeta : SearchEngine.SearchMode.OptionE.UseMinMax;
if (settings.UseBook) {
eOption |= SearchEngine.SearchMode.OptionE.UseBook;
}
if (settings.UseTransTable) {
eOption |= SearchEngine.SearchMode.OptionE.UseTransTable;
}
if (settings.UsePlyCountIterative) {
eOption |= SearchEngine.SearchMode.OptionE.UseIterativeDepthSearch;
}
switch(settings.UseThread) {
case 0:
eThreadingMode = SearchEngine.SearchMode.ThreadingModeE.Off;
break;
case 1:
eThreadingMode = SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch;
break;
default:
eThreadingMode = SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch;
break;
}
m_boardEvalUtil = new BoardEvaluationUtil();
boardEvalWhite = m_boardEvalUtil.FindBoardEvaluator(settings.WhiteBoardEval);
if (boardEvalWhite == null) {
boardEvalWhite = m_boardEvalUtil.BoardEvaluators[0];
}
boardEvalBlack = m_boardEvalUtil.FindBoardEvaluator(settings.BlackBoardEval);
if (boardEvalBlack == null) {
boardEvalBlack = m_boardEvalUtil.BoardEvaluators[0];
}
m_searchMode = new SearchEngine.SearchMode(boardEvalWhite,
boardEvalBlack,
eOption,
eThreadingMode,
settings.UsePlyCount | settings.UsePlyCountIterative ? ((settings.PlyCount > 1 && settings.PlyCount < 9) ? settings.PlyCount : 6) : 0, // Maximum depth
settings.UsePlyCount | settings.UsePlyCountIterative ? 0 : (settings.AverageTime > 0 && settings.AverageTime < 1000) ? settings.AverageTime : 15,
(settings.RandomMode >= 0 && settings.RandomMode <= 2) ? (SearchEngine.SearchMode.RandomModeE)settings.RandomMode : SearchEngine.SearchMode.RandomModeE.On); // Average time
m_chessCtl = new LocalChessControl();
m_chessCtl.Location = new Point(0, 0);
m_chessCtl.Size = new Size(60 * 9, 60 * 9);
m_chessCtl.UpdateCmdState += new EventHandler(m_chessCtl_UpdateCmdState);
m_chessCtl.Father = this;
m_chessCtl.ColorInfo = colorInfo;
panelBoard.Controls.Add(m_chessCtl);
PlayingMode = PlayingModeE.PlayerAgainstComputer;
m_eComputerPlayingColor = ChessBoard.PlayerColorE.Black;
m_lostPieceBlack = new LostPiecesControl();
m_lostPieceBlack.Location = new Point(0,0);
m_lostPieceBlack.Size = new Size(200, 200);
m_lostPieceBlack.Dock = DockStyle.Fill;
m_lostPieceBlack.ChessControl = m_chessCtl;
m_lostPieceBlack.Color = true;
panelBlackLostPiece.Controls.Add(m_lostPieceBlack);
m_lostPieceWhite = new LostPiecesControl();
m_lostPieceWhite.Location = new Point(0,0);
m_lostPieceWhite.Size = new Size(200, 200);
m_lostPieceWhite.Dock = DockStyle.Fill;
m_lostPieceWhite.ChessControl = m_chessCtl;
m_lostPieceWhite.Color = false;
panelWhiteLostPiece.Controls.Add(m_lostPieceWhite);
m_moveViewer = new MoveViewer();
m_moveViewer.Location = new Point(0,0);
m_moveViewer.Size = panelMoveList.Size;
m_moveViewer.Dock = DockStyle.Fill;
m_moveViewer.NewMoveSelected += new MoveViewer.NewMoveSelectedHandler(m_moveViewer_NewMoveSelected);
m_moveViewer.DisplayMode = (settings.MoveNotation == 0) ? MoveViewer.DisplayModeE.MovePos : MoveViewer.DisplayModeE.PGN;
panelMoveList.Controls.Add(m_moveViewer);
m_chessCtl.MoveListUI = this;
m_chessCtl.MoveSelected += new ChessControl.MoveSelectedEventHandler(m_chessCtl_MoveSelected);
m_chessCtl.QueryPiece += new ChessControl.QueryPieceEventHandler(m_chessCtl_QueryPiece);
m_chessCtl.QueryPawnPromotionType += new ChessControl.QueryPawnPromotionTypeEventHandler(m_chessCtl_QueryPawnPromotionType);
m_bSecondThreadBusy = false;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
ResizeChessCtl();
SetCmdState();
ShowSearchMode();
flashPieceToolStripMenuItem.Checked = settings.FlashPiece;
pgnNotationToolStripMenuItem.Checked = (m_moveViewer.DisplayMode == MoveViewer.DisplayModeE.PGN);
}
private Color NameToColor(string strName) {
Color colRetVal;
int iVal;
if (strName.Length == 8 && (Char.IsLower(strName[0]) || Char.IsDigit(strName[0])) &&
Int32.TryParse(strName, System.Globalization.NumberStyles.HexNumber, null, out iVal)) {
colRetVal = Color.FromArgb((iVal >> 24) & 255, (iVal >> 16) & 255, (iVal >> 8) & 255, iVal & 255);
} else {
colRetVal = Color.FromName(strName);
}
return(colRetVal);
}
private void ResizeChessCtl() {
Rectangle rect;
int iCtlSize;
int iXPos;
int iYPos;
if (m_chessCtl != null) {
rect = panelBoard.ClientRectangle;
iCtlSize = (rect.Width < rect.Height) ? rect.Width : rect.Height;
iXPos = (rect.Width - iCtlSize) / 2;
iYPos = (rect.Height - iCtlSize) / 2;
m_chessCtl.Location = new Point(iXPos, iYPos);
m_chessCtl.Size = new Size(iCtlSize, iCtlSize);
}
}
private void panelBoard_Resize(object sender, EventArgs e) {
ResizeChessCtl();
}
public PlayingModeE PlayingMode {
get {
return(m_ePlayingMode);
}
set {
m_ePlayingMode = value;
playerAgainstPlayerToolStripMenuItem.Checked = (m_ePlayingMode == PlayingModeE.PlayerAgainstPlayer);
switch(value) {
case PlayingModeE.PlayerAgainstPlayer:
m_chessCtl.WhitePlayerType = PgnParser.PlayerTypeE.Human;
m_chessCtl.BlackPlayerType = PgnParser.PlayerTypeE.Human;
break;
case PlayingModeE.PlayerAgainstComputer:
if (m_eComputerPlayingColor == ChessBoard.PlayerColorE.White) {
m_chessCtl.WhitePlayerType = PgnParser.PlayerTypeE.Program;
m_chessCtl.BlackPlayerType = PgnParser.PlayerTypeE.Human;
} else {
m_chessCtl.WhitePlayerType = PgnParser.PlayerTypeE.Human;
m_chessCtl.BlackPlayerType = PgnParser.PlayerTypeE.Program;
}
break;
default:
m_chessCtl.WhitePlayerType = PgnParser.PlayerTypeE.Program;
m_chessCtl.BlackPlayerType = PgnParser.PlayerTypeE.Program;
break;
}
}
}
private void SetPlayingMode(PlayingModeE ePlayingMode) {
PlayingMode = ePlayingMode;
}
private void StartAsyncComputing() {
bool bDifferentThreadForUI;
if (m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch) {
bDifferentThreadForUI = true;
} else if (m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch) {
bDifferentThreadForUI = true;
} else {
bDifferentThreadForUI = false;
}
if (bDifferentThreadForUI) {
m_bSecondThreadBusy = true;
SetCmdState();
}
toolStripStatusLabelMove.Text = "Finding Best Move...";
toolStripStatusLabelPerm.Text = "";
Cursor = Cursors.WaitCursor;
}
private void ShowMoveInStatusBar(ChessBoard.PlayerColorE ePlayerColor, ChessBoard.MovePosS move, int iPermCount, int iDepth, int iCacheHit) {
string strPermCount;
System.Globalization.CultureInfo ci;
ci = new System.Globalization.CultureInfo("en-US");
switch(iPermCount) {
case -1:
strPermCount = "Found in Book.";
break;
case 0:
strPermCount = "---";
break;
default:
strPermCount = iPermCount.ToString("C0", ci).Replace("$", "") + " permutations evaluated. " + iCacheHit.ToString("C0", ci).Replace("$","") + " found in cache.";
break;
}
if (iDepth != -1) {
strPermCount += " " + iDepth.ToString() + " ply.";
}
strPermCount += " " + m_chessCtl.LastFindBestMoveTimeSpan.TotalSeconds.ToString() + " sec(s).";
toolStripStatusLabelMove.Text = ((ePlayerColor == ChessBoard.PlayerColorE.Black) ? "Black " : "White ") + ChessBoard.GetHumanPos(move);
toolStripStatusLabelPerm.Text = strPermCount;
}
private void ShowSearchMode() {
string str;
if ((m_searchMode.m_eOption & SearchEngine.SearchMode.OptionE.UseAlphaBeta) == SearchEngine.SearchMode.OptionE.UseAlphaBeta) {
str = "Alpha-Beta ";
} else {
str = "Min-Max ";
}
if (m_searchMode.m_iSearchDepth == 0) {
str += "(Iterative " + m_searchMode.m_iTimeOutInSec.ToString() + " secs) ";
} else if ((m_searchMode.m_eOption & SearchEngine.SearchMode.OptionE.UseIterativeDepthSearch) == SearchEngine.SearchMode.OptionE.UseIterativeDepthSearch) {
str += "(Iterative " + m_searchMode.m_iSearchDepth.ToString() + " ply) ";
} else {
str += "(" + m_searchMode.m_iSearchDepth.ToString() + " ply) ";
}
if (m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch) {
str += "using " + Environment.ProcessorCount.ToString() + " processor";
if (Environment.ProcessorCount > 1) {
str += "s";
}
str += ". ";
} else {
str += "using 1 processor. ";
}
toolStripStatusLabelSearchMode.Text = str;
}
void ChessControl.IMoveListUI.Reset(ChessBoard chessBoard) {
m_moveViewer.Reset(chessBoard);
}
void ChessControl.IMoveListUI.NewMoveDone(int iPermCount, int iDepth, int iCacheHit) {
ChessBoard.MovePosS movePos;
ChessBoard.PlayerColorE eMoveColor;
m_moveViewer.NewMoveDone(iPermCount, iDepth, iCacheHit);
movePos = m_chessCtl.ChessBoard.MovePosStack.CurrentMove;
eMoveColor = m_chessCtl.ChessBoard.CurrentMoveColor;
ShowMoveInStatusBar(eMoveColor, movePos, iPermCount, iDepth, iCacheHit);
}
private bool DisplayMessage(ChessBoard.MoveResultE eMoveResult, MessageModeE eMessageMode) {
bool bRetVal;
string strOpponent;
if (m_ePlayingMode == PlayingModeE.PlayerAgainstComputer) {
if (m_chessCtl.ChessBoard.NextMoveColor == m_eComputerPlayingColor) {
strOpponent = "Computer is ";
} else {
strOpponent = "You are ";
}
} else {
strOpponent = (m_chessCtl.ChessBoard.NextMoveColor == ChessBoard.PlayerColorE.White) ? "White player is " : "Black player is ";
}
switch(eMoveResult) {
case ChessBoard.MoveResultE.NoRepeat:
bRetVal = false;
break;
case ChessBoard.MoveResultE.TieNoMove:
if (eMessageMode != MessageModeE.Silent) {
MessageBox.Show("Draw. " + strOpponent + "unable to move.");
}
bRetVal = true;
break;
case ChessBoard.MoveResultE.TieNoMatePossible:
if (eMessageMode != MessageModeE.Silent) {
MessageBox.Show("Draw. Not enough pieces to make a checkmate.");
}
bRetVal = true;
break;
case ChessBoard.MoveResultE.ThreeFoldRepeat:
if (eMessageMode != MessageModeE.Silent) {
MessageBox.Show("Draw. 3 times the same board.");
}
bRetVal = true;
break;
case ChessBoard.MoveResultE.FiftyRuleRepeat:
if (eMessageMode != MessageModeE.Silent) {
MessageBox.Show("Draw. 50 moves without moving a pawn or eating a piece.");
}
bRetVal = true;
break;
case ChessBoard.MoveResultE.Check:
if (eMessageMode == MessageModeE.Verbose) {
MessageBox.Show(strOpponent + "in check.");
}
bRetVal = false;
break;
case ChessBoard.MoveResultE.Mate:
if (eMessageMode != MessageModeE.Silent) {
MessageBox.Show(strOpponent + "checkmate.");
}
bRetVal = true;
break;
default:
bRetVal = false;
break;
}
return(bRetVal);
}
void m_moveViewer_NewMoveSelected(object sender, MoveViewer.NewMoveSelectedEventArg e) {
ChessBoard.MoveResultE eResult;
bool bSucceed;
if (PlayingMode == PlayingModeE.PlayerAgainstPlayer) {
eResult = m_chessCtl.SelectMove(e.NewIndex, out bSucceed);
DisplayMessage(eResult, MessageModeE.Verbose);
e.Cancel = !bSucceed;
} else {
e.Cancel = true;
}
}
void m_chessCtl_UpdateCmdState(object sender, EventArgs e) {
m_lostPieceBlack.Invalidate();
m_lostPieceWhite.Invalidate();
SetCmdState();
}
private void SetCmdState() {
bool bDesignMode;
bDesignMode = (PlayingMode == PlayingModeE.DesignMode);
m_chessCtl.AutoSelection = !m_bSecondThreadBusy;
newGameToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
createAGameFromPGNToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
loadGameToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
toolStripButtonLoad.Enabled = !(m_bSecondThreadBusy || bDesignMode);
saveGameToolStripMenuItem1.Enabled = !(m_bSecondThreadBusy || bDesignMode);
toolStripButtonSave.Enabled = !(m_bSecondThreadBusy || bDesignMode);
quitToolStripMenuItem.Enabled = !m_bSecondThreadBusy;
hintToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
toolStripButtonHint.Enabled = !(m_bSecondThreadBusy || bDesignMode);
undoToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode || m_chessCtl.UndoCount < ((m_ePlayingMode == PlayingModeE.PlayerAgainstPlayer) ? 1 : 2));
toolStripButtonUndo.Enabled = !(m_bSecondThreadBusy || bDesignMode || m_chessCtl.UndoCount < ((m_ePlayingMode == PlayingModeE.PlayerAgainstPlayer) ? 1 : 2));
redoToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode || m_chessCtl.RedoCount == 0);
toolStripButtonRedo.Enabled = !(m_bSecondThreadBusy || bDesignMode || m_chessCtl.RedoCount == 0);
playerAgainstPlayerToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
searchModeToolStripMenuItem.Enabled = !m_bSecondThreadBusy;
flashPieceToolStripMenuItem.Enabled = !m_bSecondThreadBusy;
automaticPlayToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
fastAutomaticPlayToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
designModeToolStripMenuItem.Enabled = !m_bSecondThreadBusy;
revertBoardToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
createABookToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
filterAPGNFileToolStripMenuItem.Enabled = !(m_bSecondThreadBusy || bDesignMode);
cancelPlayToolStripMenuItem.Enabled = m_bSecondThreadBusy;
toolStripButtonStop.Enabled = m_bSecondThreadBusy;
}
private void UnlockBoard() {
m_bSecondThreadBusy = false;
Cursor = Cursors.Arrow;
SetCmdState();
}
private bool PlayComputerMove(bool bFlash, MessageModeE eMessageMode, ChessBoard.MovePosS move, int iPermCount, int iDepth, int iCacheHit, ComputerPlayingStat stat) {
bool bRetVal;
ChessBoard.MoveResultE eResult;
ChessBoard.PlayerColorE eColorToPlay;
eColorToPlay = m_chessCtl.NextMoveColor;
eResult = m_chessCtl.DoMove(move,
bFlash,
iPermCount,
iDepth,
iCacheHit);
if (stat != null) {
stat.m_eResult = eResult;
}
bRetVal = DisplayMessage(eResult, eMessageMode);
return(bRetVal);
}
private void PlayComputerAsync(bool bFlash, SearchEngine.SearchMode searchMode) {
Func<bool> delPlayComputerMove;
Action delUnlockBoard;
Action delSetPlayingMode;
ChessBoard chessBoard;
ChessBoard.MovePosS move;
int iPermCount;
int iCacheHit;
int iMaxDepth;
bool bEndOfGame;
bool bMoveFound;
bool bMultipleThread;
chessBoard = m_chessCtl.ChessBoard.Clone();
bMultipleThread = (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
bMoveFound = m_chessCtl.FindBestMove(searchMode,
chessBoard,
out move,
out iPermCount,
out iCacheHit,
out iMaxDepth);
if (bMoveFound) {
if (bMultipleThread) {
delPlayComputerMove = () => PlayComputerMove(bFlash, MessageModeE.CallEndGame, move, iPermCount, iMaxDepth, iCacheHit, null);
bEndOfGame = (bool)Invoke(delPlayComputerMove);
} else {
bEndOfGame = PlayComputerMove(bFlash, MessageModeE.CallEndGame, move, iPermCount, iMaxDepth, iCacheHit, null);
}
} else {
bEndOfGame = DisplayMessage(m_chessCtl.ChessBoard.CheckNextMove(), MessageModeE.CallEndGame);
}
if (bMultipleThread) {
delUnlockBoard = () => UnlockBoard();
Invoke(delUnlockBoard);
if (bEndOfGame) {
delSetPlayingMode = () => SetPlayingMode(PlayingModeE.PlayerAgainstPlayer);
Invoke(delSetPlayingMode);
}
} else {
UnlockBoard();
if (bEndOfGame) {
SetPlayingMode(PlayingModeE.PlayerAgainstPlayer);
}
}
}
private void PlayComputer(bool bFlash, bool bSilent) {
bool bMultipleThread;
bMultipleThread = (m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
StartAsyncComputing();
if (bMultipleThread) {
Task.Factory.StartNew(() => PlayComputerAsync(bFlash, m_searchMode));
} else {
PlayComputerAsync(bFlash, m_searchMode);
}
}
private void PlayComputerAgainstComputerAsync(bool bFlash, SearchEngine.SearchMode searchMode) {
bool bEndOfGame;
int iCount;
Func<ChessBoard.MovePosS,int,int,int,bool> delPlayComputerMove = null;
Action delUnlockBoard;
Action delSetPlayingMode;
ChessBoard chessBoard;
ChessBoard.MovePosS move;
int iPermCount;
int iCacheHit;
int iMaxDepth;
bool bMoveFound;
bool bMultipleThread;
bMultipleThread = (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
if (bMultipleThread) {
delPlayComputerMove = (a1,a2,a3,a4) => { return(PlayComputerMove(bFlash, MessageModeE.CallEndGame, a1, a2, a3, a4, null)); };
}
iCount = 0;
do {
chessBoard = m_chessCtl.ChessBoard.Clone();
bMoveFound = m_chessCtl.FindBestMove(searchMode,
chessBoard,
out move,
out iPermCount,
out iCacheHit,
out iMaxDepth);
if (bMoveFound) {
if (bMultipleThread) {
bEndOfGame = (bool)Invoke(delPlayComputerMove, new object[] { move, iPermCount, iMaxDepth, iCacheHit });
} else {
bEndOfGame = PlayComputerMove(bFlash, MessageModeE.CallEndGame, move, iPermCount, iMaxDepth, iCacheHit, null);
}
} else {
bEndOfGame = DisplayMessage(m_chessCtl.ChessBoard.CheckNextMove(), MessageModeE.Verbose);
}
iCount++;
} while (!bEndOfGame && PlayingMode == PlayingModeE.ComputerAgainstComputer && iCount < 500);
if (PlayingMode != PlayingModeE.ComputerAgainstComputer) {
MessageBox.Show("Automatic play canceled");
} else {
if (bMultipleThread) {
delSetPlayingMode = () => SetPlayingMode(PlayingModeE.PlayerAgainstPlayer);
Invoke(delSetPlayingMode);
} else {
SetPlayingMode(PlayingModeE.PlayerAgainstPlayer);
}
if (iCount >= 500) {
MessageBox.Show("Tie!");
}
}
if (bMultipleThread) {
delUnlockBoard = () => UnlockBoard();
Invoke(delUnlockBoard);
} else {
UnlockBoard();
}
}
private void PlayComputerAgainstComputer(bool bFlash) {
bool bMultipleThread;
bMultipleThread = (m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
PlayingMode = PlayingModeE.ComputerAgainstComputer;
StartAsyncComputing();
if (bMultipleThread) {
Task.Factory.StartNew(() => PlayComputerAgainstComputerAsync(bFlash, m_searchMode));
} else {
PlayComputerAgainstComputerAsync(bFlash, m_searchMode);
}
}
private void TestShowResult(int iGameCount, SearchEngine.SearchMode searchMode, ComputerPlayingStat stat, int iMethod1Win, int iMethod2Win) {
string strMsg;
string strMethod1;
string strMethod2;
int iTimeMethod1;
int iTimeMethod2;
strMethod1 = searchMode.m_boardEvaluationWhite.Name;
strMethod2 = searchMode.m_boardEvaluationBlack.Name;
iTimeMethod1 = (stat.m_iMethod1MoveCount == 0) ? 0 : stat.m_timeSpanMethod1.Milliseconds / stat.m_iMethod1MoveCount;
iTimeMethod2 = (stat.m_iMethod2MoveCount == 0) ? 0 : stat.m_timeSpanMethod2.Milliseconds / stat.m_iMethod2MoveCount;
strMsg = iGameCount.ToString() + " game(s) played.\r\n" +
iMethod1Win.ToString() + " win(s) for method #1 (" + strMethod1 + "). Average time = " + iMethod1Win.ToString() + " ms per move.\r\n" +
iMethod2Win.ToString() + " win(s) for method #2 (" + strMethod2 + "). Average time = " + iMethod2Win.ToString() + " ms per move.\r\n" +
(iGameCount - iMethod1Win - iMethod2Win).ToString() + " draw(s).";
MessageBox.Show(strMsg);
}
private void TestComputerAgainstComputerAsync(int iGameCount, SearchEngine.SearchMode searchMode) {
int iCount;
Func<ChessBoard.MovePosS,int,int,int,ComputerPlayingStat,bool> delPlayComputerMove = null;
Action delResetBoard = null;
Action delUnlockBoard;
Action delSetPlayingMode;
Action delShowResultDel;
ChessBoard chessBoard;
ChessBoard.MovePosS move;
int iPermCount;
int iCacheHit;
int iMaxDepth;
int iGameIndex;
int iMethod1Win = 0;
int iMethod2Win = 0;
DateTime dateTime;
bool bMoveFound;
bool bMultipleThread;
bool bEven;
bool bEndOfGame;
IBoardEvaluation boardEvaluation1;
IBoardEvaluation boardEvaluation2;
ComputerPlayingStat stat;
stat = new ComputerPlayingStat();
bMultipleThread = (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
boardEvaluation1 = searchMode.m_boardEvaluationWhite;
boardEvaluation2 = searchMode.m_boardEvaluationBlack;
if (bMultipleThread) {
delPlayComputerMove = (a1, a2, a3, a4, a5) => PlayComputerMove(false, MessageModeE.Silent, a1, a2, a3, a4, a5);
delResetBoard = () => ResetBoard();
}
iGameIndex = 0;
while (iGameIndex < iGameCount && !stat.m_bUserCancel) {
bEven = ((iGameIndex & 1) == 0);
searchMode.m_boardEvaluationWhite = bEven ? boardEvaluation1 : boardEvaluation2;
searchMode.m_boardEvaluationBlack = bEven ? boardEvaluation2 : boardEvaluation1;
if (bMultipleThread) {
Invoke(delResetBoard);
} else {
ResetBoard();
}
iCount = 0;
do {
chessBoard = m_chessCtl.ChessBoard.Clone();
dateTime = DateTime.Now;
bMoveFound = m_chessCtl.FindBestMove(searchMode,
chessBoard,
out move,
out iPermCount,
out iCacheHit,
out iMaxDepth);
if (bMoveFound) {
if ((m_chessCtl.NextMoveColor == ChessBoard.PlayerColorE.White && bEven) ||
(m_chessCtl.NextMoveColor == ChessBoard.PlayerColorE.Black && !bEven)) {
stat.m_timeSpanMethod1 += DateTime.Now - dateTime;
stat.m_iMethod1MoveCount++;
} else {
stat.m_timeSpanMethod2 += DateTime.Now - dateTime;
stat.m_iMethod2MoveCount++;
}
if (bMultipleThread) {
bEndOfGame = (bool)Invoke(delPlayComputerMove, new object[] { move, iPermCount, iMaxDepth, iCacheHit, stat } );
} else {
bEndOfGame = PlayComputerMove(false, MessageModeE.Silent, move, iPermCount, iMaxDepth, iCacheHit, stat);
}
} else {
bEndOfGame = true;
}
iCount++;
} while (!bEndOfGame && PlayingMode == PlayingModeE.ComputerAgainstComputer && iCount < 250);
if (PlayingMode != PlayingModeE.ComputerAgainstComputer) {
stat.m_bUserCancel = true;
} else if (iCount < 250) {
if (stat.m_eResult == ChessBoard.MoveResultE.Mate) {
if ((m_chessCtl.NextMoveColor == ChessBoard.PlayerColorE.Black && bEven) ||
(m_chessCtl.NextMoveColor == ChessBoard.PlayerColorE.White && !bEven)) {
iMethod1Win++;
} else {
iMethod2Win++;
}
}
}
iGameIndex++;
}
searchMode.m_boardEvaluationWhite = boardEvaluation1;
searchMode.m_boardEvaluationBlack = boardEvaluation2;
if (bMultipleThread) {
delSetPlayingMode = () => SetPlayingMode(PlayingModeE.PlayerAgainstPlayer);
delUnlockBoard = () => UnlockBoard();
delShowResultDel = () => TestShowResult(iGameIndex, searchMode, stat, iMethod1Win, iMethod2Win);
Invoke(delShowResultDel);
Invoke(delSetPlayingMode);
Invoke(delUnlockBoard);
} else {
TestShowResult(iGameIndex, searchMode, stat, iMethod1Win, iMethod2Win);
SetPlayingMode(PlayingModeE.PlayerAgainstPlayer);
UnlockBoard();
}
}
private void TestComputerAgainstComputer(int iGameCount, SearchEngine.SearchMode searchMode) {
bool bMultipleThread;
bMultipleThread = (m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
PlayingMode = PlayingModeE.ComputerAgainstComputer;
StartAsyncComputing();
if (bMultipleThread) {
Task.Factory.StartNew(() => TestComputerAgainstComputerAsync(iGameCount, searchMode));
} else {
TestComputerAgainstComputerAsync(iGameCount, searchMode);
}
}
private void ShowHintMove(ChessBoard.MovePosS move, int iPermCount, int iDepth, int iCacheHit) {
ShowMoveInStatusBar(m_chessCtl.NextMoveColor, move, iPermCount, iDepth, iCacheHit);
m_chessCtl.ShowHintMove(move);
}
private void ShowHintAsync(SearchEngine.SearchMode searchMode) {
Action delShowHintMove;
Action delUnlockBoard;
ChessBoard chessBoard;
ChessBoard.MovePosS move;
int iPermCount;
int iCacheHit;
int iMaxDepth;
bool bMoveFound;
bool bMultipleThread;
chessBoard = m_chessCtl.ChessBoard.Clone();
bMultipleThread = (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
bMoveFound = m_chessCtl.FindBestMove(searchMode,
chessBoard,
out move,
out iPermCount,
out iCacheHit,
out iMaxDepth);
if (bMultipleThread) {
if (bMoveFound) {
delShowHintMove = () => ShowHintMove(move, iPermCount, iMaxDepth, iCacheHit);
Invoke(delShowHintMove);
}
delUnlockBoard = () => UnlockBoard();
Invoke(delUnlockBoard);
} else {
if (bMoveFound) {
ShowHintMove(move, iPermCount, iMaxDepth, iCacheHit);
}
UnlockBoard();
}
}
private void ShowHint() {
bool bMultipleThread;
bMultipleThread = (m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch ||
m_searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch);
StartAsyncComputing();
if (bMultipleThread) {
Task.Factory.StartNew(() => ShowHintAsync(m_searchMode));
} else {
ShowHintAsync(m_searchMode);
}
}
private void Undo(bool bPlayerAgainstComputer) {
bool bFlash;
bFlash = flashPieceToolStripMenuItem.Checked;
if (bPlayerAgainstComputer) {
if (m_chessCtl.UndoCount > 1) {
m_chessCtl.UndoMove(bFlash); // Computer
m_chessCtl.UndoMove(bFlash); // User
}
} else {
if (m_chessCtl.UndoCount != 0) {
m_chessCtl.UndoMove(bFlash);
}
}
}
private ChessBoard.MoveResultE RedoMove(bool bPlayerAgainstComputer) {
ChessBoard.MoveResultE eRetVal = ChessBoard.MoveResultE.NoRepeat;
bool bFlash;
bFlash = flashPieceToolStripMenuItem.Checked;
if (m_chessCtl.RedoCount != 0) {
eRetVal = m_chessCtl.RedoMove(bFlash); // Computer
}
if (bPlayerAgainstComputer && m_chessCtl.RedoCount != 0) {
eRetVal = m_chessCtl.RedoMove(bFlash); // User
}
return(eRetVal);
}
private void CheckIfComputerMustPlay() {
switch(PlayingMode) {
case PlayingModeE.ComputerAgainstComputer:
Update();
PlayComputerAgainstComputer(true);
break;
case PlayingModeE.PlayerAgainstComputer:
if (m_chessCtl.NextMoveColor == m_eComputerPlayingColor) {
Update();
PlayComputer(flashPieceToolStripMenuItem.Checked, false);
}
break;
default:
break;
}
}
void m_chessCtl_MoveSelected(object sender, ChessControl.MoveSelectedEventArgs e) {
bool bFlash;
bool bEndOfGame;
ChessBoard.MoveResultE eMoveResult;
bFlash = flashPieceToolStripMenuItem.Checked;
eMoveResult = m_chessCtl.DoMove(e.Move, bFlash, -1, 0, 0);
bEndOfGame = DisplayMessage(eMoveResult, MessageModeE.Verbose);
if (!bEndOfGame) {
CheckIfComputerMustPlay();
}
}
private void ToggleDesignMode() {
if (PlayingMode == PlayingModeE.DesignMode) {
PlayingMode = PlayingModeE.PlayerAgainstPlayer;
if (frmGameParameter.AskGameParameter(this)) {
m_chessCtl.BoardDesignMode = false;
if (m_chessCtl.BoardDesignMode) {
PlayingMode = PlayingModeE.DesignMode;
MessageBox.Show("Invalid board configuration. Correct or reset.");
} else {
m_lostPieceBlack.BoardDesignMode = false;
panelWhiteLostPiece.Visible = true;
CheckIfComputerMustPlay();
}
} else {
PlayingMode = PlayingModeE.DesignMode;
}
} else {
PlayingMode = PlayingModeE.DesignMode;
m_lostPieceBlack.BoardDesignMode = true;
panelWhiteLostPiece.Visible = false;
m_chessCtl.BoardDesignMode = true;
}
SetCmdState();
}
void m_chessCtl_QueryPiece(object sender, ChessControl.QueryPieceEventArgs e) {
e.Piece = m_lostPieceBlack.SelectedPiece;
}
void m_chessCtl_QueryPawnPromotionType(object sender, ChessControl.QueryPawnPromotionTypeEventArgs e) {
frmQueryPawnPromotionType frm;
using(frm = new frmQueryPawnPromotionType(e.ValidPawnPromotion)) {
if (frm.ShowDialog(this) == DialogResult.OK) {
e.PawnPromotionType = frm.PromotionType;
} else {
e.PawnPromotionType = ChessBoard.MoveTypeE.Normal;
}
}
}
private void hintToolStripMenuItem_Click(object sender, EventArgs e) {
ShowHint();
}
private void redoToolStripMenuItem_Click(object sender, EventArgs e) {
ChessBoard.MoveResultE eMoveResult;
eMoveResult = RedoMove((PlayingMode != PlayingModeE.PlayerAgainstPlayer));
DisplayMessage(eMoveResult, MessageModeE.Verbose);
}
private void automaticPlayToolStripMenuItem_Click(object sender, EventArgs e) {
PlayComputerAgainstComputer(true);
}
private void loadGameToolStripMenuItem_Click(object sender, EventArgs e) {
if (m_chessCtl.LoadFromFile()) {
Invalidate();
CheckIfComputerMustPlay();
}
}
private void cancelAutomaticPlayToolStripMenuItem_Click(object sender, EventArgs e) {
if (PlayingMode == PlayingModeE.ComputerAgainstComputer) {
PlayingMode = PlayingModeE.PlayerAgainstPlayer;
} else {
m_chessCtl.CancelSearch();
}
}
private void playerAgainstPlayerToolStripMenuItem_Click(object sender, EventArgs e) {
if (playerAgainstPlayerToolStripMenuItem.Checked) {
PlayingMode = PlayingModeE.PlayerAgainstComputer;
if (frmGameParameter.AskGameParameter(this)) {
CheckIfComputerMustPlay();
}
} else {
PlayingMode = PlayingModeE.PlayerAgainstPlayer;
}
}
private void revertBoardToolStripMenuItem_Click(object sender, EventArgs e) {
m_eComputerPlayingColor = (m_eComputerPlayingColor == ChessBoard.PlayerColorE.Black) ? ChessBoard.PlayerColorE.White :
ChessBoard.PlayerColorE.Black;
CheckIfComputerMustPlay();
}
private void createABookToolStripMenuItem_Click(object sender, EventArgs e) {
m_chessCtl.CreateBookFromFiles();
}
private void filterAPGNFileToolStripMenuItem_Click(object sender, EventArgs e) {
PgnUtil pgnUtil;
pgnUtil = new PgnUtil();
pgnUtil.CreatePGNSubset();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
frmAbout frm;
using(frm = new frmAbout()) {
frm.ShowDialog(this);
}
}
private void searchModeToolStripMenuItem_Click(object sender, EventArgs e) {
frmSearchMode frm;
using(frm = new frmSearchMode(m_searchMode, m_boardEvalUtil)) {
if (frm.ShowDialog(this) == DialogResult.OK) {
frm.UpdateSearchMode();
ShowSearchMode();
}
}
}
private void testBoardEvaluationToolStripMenuItem_Click(object sender, EventArgs e) {
frmTestBoardEval frm;
SearchEngine.SearchMode searchMode;
int iGameCount;
using(frm = new frmTestBoardEval(m_boardEvalUtil, m_searchMode)) {
if (frm.ShowDialog(this) == DialogResult.OK) {
searchMode = frm.SearchMode;
iGameCount = frm.GameCount;
TestComputerAgainstComputer(iGameCount, searchMode);
}
}
}
void timer1_Tick(object sender, EventArgs e) {
GameTimer gameTimer;
gameTimer = m_chessCtl.GameTimer;
toolStripLabelTimeWhite.Text = GameTimer.GetHumanElapse(gameTimer.WhitePlayTime);
toolStripLabelTimeBlack.Text = GameTimer.GetHumanElapse(gameTimer.BlackPlayTime);
}
private void toolStripButtonLoad_Click(object sender, EventArgs e) {
loadGameToolStripMenuItem_Click(sender, e);
}
private void toolStripButtonSave_Click(object sender, EventArgs e) {
saveGameToolStripMenuItem1_Click(sender, e);
}
private void toolStripButtonHint_Click(object sender, EventArgs e) {
hintToolStripMenuItem_Click(sender, e);
}
private void toolStripButtonUndo_Click(object sender, EventArgs e) {
undoToolStripMenuItem_Click(sender, e);
}
private void toolStripButtonRedo_Click(object sender, EventArgs e) {
redoToolStripMenuItem_Click(sender, e);
}
private void toolStripButtonStop_Click(object sender, EventArgs e) {
cancelAutomaticPlayToolStripMenuItem_Click(sender, e);
}
private void pgnNotationToolStripMenuItem_Click(object sender, EventArgs e) {
SrcChess.Properties.Settings settings;
m_moveViewer.DisplayMode = (pgnNotationToolStripMenuItem.Checked) ? MoveViewer.DisplayModeE.PGN : MoveViewer.DisplayModeE.MovePos;
settings = SrcChess.Properties.Settings.Default;
settings.MoveNotation = (m_moveViewer.DisplayMode == MoveViewer.DisplayModeE.MovePos) ? 0 : 1;
settings.Save();
}
private void flashPieceToolStripMenuItem_Click(object sender, EventArgs e) {
SrcChess.Properties.Settings settings;
settings = SrcChess.Properties.Settings.Default;
settings.FlashPiece = flashPieceToolStripMenuItem.Checked;
settings.Save();
}
private void chooseColorsToolStripMenuItem_Click(object sender, EventArgs e) {
frmPickupColor frm;
Properties.Settings settings;
ChessControl.ChessControlColorInfo colorInfo;
using(frm = new frmPickupColor(m_chessCtl.ColorInfo)) {
if (frm.ShowDialog() == DialogResult.OK) {
colorInfo = frm.ColorInfo;
m_chessCtl.ColorInfo = colorInfo;
settings = Properties.Settings.Default;
settings.WhitePieceColor = colorInfo.m_colWhitePiece.Name;
settings.BlackPieceColor = colorInfo.m_colBlackPiece.Name;
settings.LiteCaseColor = colorInfo.m_colLiteCase.Name;
settings.DarkCaseColor = colorInfo.m_colDarkCase.Name;
settings.Save();
}
}
}
private void saveGameToPGNToolStripMenuItem_Click(object sender, EventArgs e) {
m_chessCtl.SavePGNToFile();
}
private void frmChessBoard_Load(object sender, EventArgs e)
{
}
}
}
File ToolStripMenuItem_Click Form1...(create png image)......
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ranjeet_chess_game {
public partial class frmCreatePGNGame : Form {
private List<ChessBoard.MovePosS> m_arrMoveList;
private ChessBoard.PlayerColorE m_eStartingColor;
private ChessBoard m_chessBoardStarting;
private string m_strWhitePlayerName;
private string m_strBlackPlayerName;
private PgnParser.PlayerTypeE m_eWhiteType;
private PgnParser.PlayerTypeE m_eBlackType;
private TimeSpan m_spanWhitePlayer;
private TimeSpan m_spanBlackPlayer;
private void butOk_Click(object sender, EventArgs e) {
string strGame;
PgnParser parser;
List<ChessBoard.MovePosS> listGame;
int iSkip;
int iTruncated;
strGame = textBox1.Text;
if (String.IsNullOrEmpty(strGame)) {
MessageBox.Show("No PGN text has been pasted.");
} else {
listGame = new List<ChessBoard.MovePosS>(256);
parser = new PgnParser(false);
if (!parser.ParseSingle(strGame,
false,
listGame,
out iSkip,
out iTruncated,
out m_chessBoardStarting,
out m_eStartingColor,
out m_strWhitePlayerName,
out m_strBlackPlayerName,
out m_eWhiteType,
out m_eBlackType,
out m_spanWhitePlayer,
out m_spanBlackPlayer)) {
MessageBox.Show("The specified board is invalid.");
} else if (iSkip != 0) {
MessageBox.Show("The game is incomplete. Paste another game.");
} else if (iTruncated != 0) {
MessageBox.Show("The selected game includes an unsupported pawn promotion (only pawn promotion to queen is supported).");
} else if (listGame.Count == 0 && m_chessBoardStarting == null) {
MessageBox.Show("Game is empty.");
} else {
m_arrMoveList = listGame;
DialogResult = DialogResult.OK;
Close();
}
}
}
public List<ChessBoard.MovePosS> MoveList {
get {
return(m_arrMoveList);
}
}
public ChessBoard.PlayerColorE StartingColor {
get {
return(m_eStartingColor);
}
}
public string WhitePlayerName {
get {
return(m_strWhitePlayerName);
}
}
public string BlackPlayerName {
get {
return(m_strBlackPlayerName);
}
}
public PgnParser.PlayerTypeE WhitePlayerType {
get {
return(m_eWhiteType);
}
}
public TimeSpan BlackTimer {
get {
return(m_spanBlackPlayer);
}
}
private void frmCreatePGNGame_Load(object sender, EventArgs e)
{
}
}
}
File ToolStripMenuItem_Click Form2....(create parameters).....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ranjeet_chess_game {
public partial class frmGameParameter : Form {
private frmChessBoard m_frmFather;
public frmGameParameter() {
InitializeComponent();
}
private frmGameParameter(frmChessBoard frmFather) : this() {
m_frmFather = frmFather;
switch(m_frmFather.PlayingMode) {
case frmChessBoard.PlayingModeE.DesignMode:
throw new System.ApplicationException("Must not be called in design mode.");
case frmChessBoard.PlayingModeE.PlayerAgainstComputer:
radioButtonPlayerAgainstComputer.Checked = true;
break;
case frmChessBoard.PlayingModeE.PlayerAgainstPlayer:
radioButtonPlayerAgainstPlayer.Checked = true;
break;
case frmChessBoard.PlayingModeE.ComputerAgainstComputer:
radioButtonComputerAgainstComputer.Checked = true;
break;
}
switch(m_frmFather.m_eComputerPlayingColor) {
case ChessBoard.PlayerColorE.Black:
radioButtonComputerPlayBlack.Checked = true;
break;
case ChessBoard.PlayerColorE.White:
radioButtonComputerPlayWhite.Checked = true;
break;
}
CheckState();
}
private void CheckState() {
groupBoxComputerPlay.Enabled = radioButtonPlayerAgainstComputer.Checked;
}
private void butOk_Click(object sender, EventArgs e) {
if (radioButtonPlayerAgainstComputer.Checked) {
m_frmFather.PlayingMode = frmChessBoard.PlayingModeE.PlayerAgainstComputer;
} else if (radioButtonPlayerAgainstPlayer.Checked) {
m_frmFather.PlayingMode = frmChessBoard.PlayingModeE.PlayerAgainstPlayer;
} else if (radioButtonComputerAgainstComputer.Checked) {
m_frmFather.PlayingMode = frmChessBoard.PlayingModeE.ComputerAgainstComputer;
}
m_frmFather.m_eComputerPlayingColor = (radioButtonComputerPlayBlack.Checked) ? ChessBoard.PlayerColorE.Black : ChessBoard.PlayerColorE.White;
}
private void radioButtonPlayerAgainstComputer_CheckedChanged(object sender, EventArgs e) {
CheckState();
}
private void radioButtonPlayerAgainstPlayer_CheckedChanged(object sender, EventArgs e) {
CheckState();
}
private void radioButtonComputerAgainstComputer_CheckedChanged(object sender, EventArgs e) {
CheckState();
}
public static bool AskGameParameter(frmChessBoard frmFather) {
bool bRetVal;
frmGameParameter frm;
frm = new frmGameParameter(frmFather);
using(frm) {
bRetVal = (frm.ShowDialog() == DialogResult.OK);
}
return(bRetVal);
}
private void frmGameParameter_Load(object sender, EventArgs e)
{
}
}
}
File ToolStripMenuItem_Click Form3....(png image filter).....
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ranjeet_chess_game {
/// <summary>Form use to filter the content of a PGN file</summary>
public partial class frmPGNFilter : Form {
///ry <summa>Represent an ELO range in the checked list control</summary>
private class RangeItem {
public int m_iRange;
public RangeItem(int iRange) { m_iRange = iRange; }
public override string ToString() {
return("Range " + m_iRange.ToString() + " - " + (m_iRange + 99).ToString());
}
}
public PgnUtil.FilterClause m_filterClause;
/// <summary>PGN utility class</summary>
private PgnUtil m_pgnUtil;
private Stream m_streamInp;
public frmPGNFilter() {
InitializeComponent();
m_filterClause = new PgnUtil.FilterClause();
}
public frmPGNFilter(PgnUtil pgnUtil, Stream streamInp, int iMinELO, int iMaxELO, string[] arrPlayers, string strInpFileName, int iGameCount) : this() {
m_pgnUtil = pgnUtil;
m_streamInp = streamInp;
iMinELO = iMinELO / 100 * 100;
checkedListBoxRange.Items.Clear();
for (int iIndex = iMinELO; iIndex < iMaxELO; iIndex += 100) {
checkedListBoxRange.Items.Add(new RangeItem(iIndex), true);
}
checkedListBoxPlayers.Items.Clear();
foreach (string strPlayer in arrPlayers) {
checkedListBoxPlayers.Items.Add(strPlayer, true);
}
checkedListBoxEnding.Items.Clear();
checkedListBoxEnding.Items.Add("White win", true);
checkedListBoxEnding.Items.Add("Black win", true);
checkedListBoxEnding.Items.Add("Draws", true);
checkBoxAllRanges.Checked = true;
checkBoxAllPlayer.Checked = true;
checkBoxAllEndGame.Checked = true;
checkedListBoxPlayers.Enabled = false;
checkedListBoxRange.Enabled = false;
checkedListBoxEnding.Enabled = false;
labelDesc.Text = iGameCount.ToString() + " games found in the file '" + strInpFileName + "'";
}
private void CheckAllItems(CheckedListBox checkedListBox, bool bChecked) {
int iCount;
checkedListBox.SuspendLayout();
iCount = checkedListBox.Items.Count;
for (int iIndex = 0; iIndex < iCount; iIndex++) {
checkedListBox.SetItemChecked(iIndex, bChecked);
}
checkedListBox.ResumeLayout();
}
private void butSelectAllRange_Click(object sender, EventArgs e) {
CheckAllItems(checkedListBoxRange, true);
}
private void buttonClearAllRange_Click(object sender, EventArgs e) {
CheckAllItems(checkedListBoxRange, false);
}
private void buttonSelectAllPlayers_Click(object sender, EventArgs e) {
CheckAllItems(checkedListBoxPlayers, true);
}
private void buttonClearAllPlayers_Click(object sender, EventArgs e) {
CheckAllItems(checkedListBoxPlayers, false);
}
private void buttonSelectAllEndGame_Click(object sender, EventArgs e) {
CheckAllItems(checkedListBoxEnding, true);
}
private void buttonClearAllEndGame_Click(object sender, EventArgs e) {
CheckAllItems(checkedListBoxEnding, false);
}
private void checkBoxAllRanges_CheckedChanged(object sender, EventArgs e) {
checkedListBoxRange.Enabled = !checkBoxAllRanges.Checked;
buttonClearAllRange.Enabled = !checkBoxAllRanges.Checked;
butSelectAllRange.Enabled = !checkBoxAllRanges.Checked;
}
private void checkBoxAllPlayer_CheckedChanged(object sender, EventArgs e) {
checkedListBoxPlayers.Enabled = !checkBoxAllPlayer.Checked;
buttonClearAllPlayers.Enabled = !checkBoxAllPlayer.Checked;
buttonSelectAllPlayers.Enabled = !checkBoxAllPlayer.Checked;
}
private void checkBoxAllEndGame_CheckedChanged(object sender, EventArgs e) {
checkedListBoxEnding.Enabled = !checkBoxAllEndGame.Checked;
buttonClearAllEndGame.Enabled = !checkBoxAllEndGame.Checked;
buttonSelectAllEndGame.Enabled = !checkBoxAllEndGame.Checked;
}
private bool SyncInfo() {
bool bRetVal = true;
if (checkBoxAllRanges.Checked || checkedListBoxRange.CheckedItems.Count == checkedListBoxRange.Items.Count) {
m_filterClause.m_bAllRanges = true;
m_filterClause.m_hashRanges = null;
} else {
m_filterClause.m_bAllEnding = false;
if (checkedListBoxRange.CheckedItems.Count == 0 && !checkBoxIncludeUnrated.Checked) {
MessageBox.Show("At least one range must be selected.");
bRetVal = false;
} else {
m_filterClause.m_hashRanges = new Dictionary<int,int>(checkedListBoxRange.CheckedItems.Count);
foreach (RangeItem rangeItem in checkedListBoxRange.CheckedItems) {
m_filterClause.m_hashRanges.Add(rangeItem.m_iRange, 0);
}
}
}
m_filterClause.m_bIncludesUnrated = checkBoxIncludeUnrated.Checked;
if (checkBoxAllPlayer.Checked || checkedListBoxPlayers.CheckedItems.Count == checkedListBoxPlayers.Items.Count) {
m_filterClause.m_bAllPlayers = true;
m_filterClause.m_hashPlayerList = null;
} else {
m_filterClause.m_bAllPlayers = false;
if (checkedListBoxPlayers.CheckedItems.Count == 0) {
MessageBox.Show("At least one player must be selected.");
bRetVal = false;
} else {
m_filterClause.m_hashPlayerList = new Dictionary<string,string>(checkedListBoxPlayers.CheckedItems.Count);
foreach (string strPlayer in checkedListBoxPlayers.CheckedItems) {
m_filterClause.m_hashPlayerList.Add(strPlayer, null);
}
}
}
if (checkBoxAllEndGame.Checked || checkedListBoxEnding.CheckedItems.Count == checkedListBoxEnding.Items.Count) {
m_filterClause.m_bAllEnding = true;
m_filterClause.m_bEndingWhiteWinning = true;
m_filterClause.m_bEndingBlackWinning = true;
m_filterClause.m_bEndingDraws = true;
} else {
m_filterClause.m_bAllEnding = false;
if (checkedListBoxEnding.CheckedItems.Count == 0) {
MessageBox.Show("At least one ending must be selected.");
bRetVal = false;
} else {
m_filterClause.m_bEndingWhiteWinning = checkedListBoxEnding.GetItemChecked(0);
m_filterClause.m_bEndingBlackWinning = checkedListBoxEnding.GetItemChecked(1);
m_filterClause.m_bEndingDraws = checkedListBoxEnding.GetItemChecked(2);
}
}
if (m_filterClause.m_bAllRanges &&
m_filterClause.m_bAllPlayers &&
m_filterClause.m_bAllEnding &&
m_filterClause.m_bIncludesUnrated) {
MessageBox.Show("At least one filtering option must be selected.");
bRetVal = false;
}
return(bRetVal);
}
public PgnUtil.FilterClause FilteringClause {
get {
return(m_filterClause);
}
}
private void butOk_Click(object sender, EventArgs e) {
if (SyncInfo()) {
DialogResult = DialogResult.OK;
Close();
}
}
private void butTest_Click(object sender, EventArgs e) {
int iCount;
if (SyncInfo()) {
iCount = m_pgnUtil.FilterPGN(m_streamInp, null, FilteringClause);
MessageBox.Show("The specified filter will result in " + iCount.ToString() + " game(s) selected.");
}
}
private void frmPGNFilter_Load(object sender, EventArgs e)
{
}
}
}
File ToolStripMenuItem_Click Form4....(png picker).....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ranjeet_chess_game {
public partial class frmPGNGamePicker : Form {
private PgnUtil m_pgnUtil;
private Stream m_streamInp;
private List<ChessBoard.MovePosS> m_arrMoveList;
private string m_strSelectedGame;
private ChessBoard.PlayerColorE m_eStartingColor;
private ChessBoard m_chessBoardStarting;
private string m_strWhitePlayerName;
private string m_strBlackPlayerName;
private PgnParser.PlayerTypeE m_eWhiteType;
private PgnParser.PlayerTypeE m_eBlackType;
private TimeSpan m_spanWhitePlayer;
private TimeSpan m_spanBlackPlayer;
public frmPGNGamePicker() {
InitializeComponent();
m_pgnUtil = new PgnUtil();
m_strSelectedGame = null;
m_eStartingColor = ChessBoard.PlayerColorE.White;
m_chessBoardStarting = null;
}
private string GetSelectedGame() {
string strRetVal;
PgnUtil.PGNGameDescItem itemDesc;
int iSelectedIndex;
iSelectedIndex = listBoxGames.SelectedIndex;
if (iSelectedIndex != -1) {
itemDesc = listBoxGames.SelectedItem as PgnUtil.PGNGameDescItem;
strRetVal = m_pgnUtil.GetNGame(m_streamInp, itemDesc.Index);
} else {
strRetVal = null;
}
return(strRetVal);
}
private void RefreshGameDisplay() {
m_strSelectedGame = GetSelectedGame();
textBoxGame.Text = (m_strSelectedGame == null) ? "" : m_strSelectedGame;
}
public bool InitForm(string strFileName) {
bool bRetVal;
m_streamInp = m_pgnUtil.OpenInpFile(strFileName);
if (m_streamInp == null) {
bRetVal = false;
} else {
if (m_pgnUtil.FillListBoxWithDesc(m_streamInp, listBoxGames) < 1) {
MessageBox.Show("No games found in the PGN File '" + strFileName + "'");
bRetVal = false;
} else {
listBoxGames.SelectedIndex = 0;
bRetVal = true;
}
}
return(bRetVal);
}
private void butOk_Click(object sender, EventArgs e) {
string strGame;
PgnParser parser;
List<ChessBoard.MovePosS> listGame;
int iSkip;
int iTruncated;
strGame = GetSelectedGame();
if (strGame != null) {
listGame = new List<ChessBoard.MovePosS>(256);
parser = new PgnParser(false);
if (!parser.ParseSingle(strGame,
false,
listGame,
out iSkip,
out iTruncated,
out m_chessBoardStarting,
out m_eStartingColor,
out m_strWhitePlayerName,
out m_strBlackPlayerName,
out m_eWhiteType,
out m_eBlackType,
out m_spanWhitePlayer,
out m_spanBlackPlayer)) {
MessageBox.Show("The specified board is invalid.");
} else if (iSkip != 0) {
MessageBox.Show("The game is incomplete. Select another game.");
} else if (iTruncated != 0) {
MessageBox.Show("The selected game includes an unsupported pawn promotion (only pawn promotion to queen is supported).");
} else if (listGame.Count == 0 && m_chessBoardStarting == null) {
MessageBox.Show("Game is empty.");
} else {
m_arrMoveList = listGame;
DialogResult = DialogResult.OK;
Close();
}
}
}
private void listBoxGames_SelectedIndexChanged(object sender, EventArgs e) {
RefreshGameDisplay();
}
public List<ChessBoard.MovePosS> MoveList {
get {
return(m_arrMoveList);
}
}
public string SelectedGame {
get {
return(m_strSelectedGame);
}
}
public ChessBoard StartingChessBoard {
get {
return(m_chessBoardStarting);
}
}
public ChessBoard.PlayerColorE StartingColor {
get {
return(m_eStartingColor);
}
}
public string WhitePlayerName {
get {
return(m_strWhitePlayerName);
}
}
public string BlackPlayerName {
get {
return(m_strBlackPlayerName);
}
}
public PgnParser.PlayerTypeE WhitePlayerType {
get {
return(m_eWhiteType);
}
}
public PgnParser.PlayerTypeE BlackPlayerType {
get {
return(m_eBlackType);
}
}
public TimeSpan WhiteTimer {
get {
return(m_spanWhitePlayer);
}
}
public TimeSpan BlackTimer {
get {
return(m_spanBlackPlayer);
}
}
private void frmPGNGamePicker_Load(object sender, EventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ranjeet_chess_game {
public partial class frmPGNGamePicker : Form {
private PgnUtil m_pgnUtil;
private Stream m_streamInp;
private List<ChessBoard.MovePosS> m_arrMoveList;
private string m_strSelectedGame;
private ChessBoard.PlayerColorE m_eStartingColor;
private ChessBoard m_chessBoardStarting;
private string m_strWhitePlayerName;
private string m_strBlackPlayerName;
private PgnParser.PlayerTypeE m_eWhiteType;
private PgnParser.PlayerTypeE m_eBlackType;
private TimeSpan m_spanWhitePlayer;
private TimeSpan m_spanBlackPlayer;
public frmPGNGamePicker() {
InitializeComponent();
m_pgnUtil = new PgnUtil();
m_strSelectedGame = null;
m_eStartingColor = ChessBoard.PlayerColorE.White;
m_chessBoardStarting = null;
}
private string GetSelectedGame() {
string strRetVal;
PgnUtil.PGNGameDescItem itemDesc;
int iSelectedIndex;
iSelectedIndex = listBoxGames.SelectedIndex;
if (iSelectedIndex != -1) {
itemDesc = listBoxGames.SelectedItem as PgnUtil.PGNGameDescItem;
strRetVal = m_pgnUtil.GetNGame(m_streamInp, itemDesc.Index);
} else {
strRetVal = null;
}
return(strRetVal);
}
private void RefreshGameDisplay() {
m_strSelectedGame = GetSelectedGame();
textBoxGame.Text = (m_strSelectedGame == null) ? "" : m_strSelectedGame;
}
public bool InitForm(string strFileName) {
bool bRetVal;
m_streamInp = m_pgnUtil.OpenInpFile(strFileName);
if (m_streamInp == null) {
bRetVal = false;
} else {
if (m_pgnUtil.FillListBoxWithDesc(m_streamInp, listBoxGames) < 1) {
MessageBox.Show("No games found in the PGN File '" + strFileName + "'");
bRetVal = false;
} else {
listBoxGames.SelectedIndex = 0;
bRetVal = true;
}
}
return(bRetVal);
}
private void butOk_Click(object sender, EventArgs e) {
string strGame;
PgnParser parser;
List<ChessBoard.MovePosS> listGame;
int iSkip;
int iTruncated;
strGame = GetSelectedGame();
if (strGame != null) {
listGame = new List<ChessBoard.MovePosS>(256);
parser = new PgnParser(false);
if (!parser.ParseSingle(strGame,
false,
listGame,
out iSkip,
out iTruncated,
out m_chessBoardStarting,
out m_eStartingColor,
out m_strWhitePlayerName,
out m_strBlackPlayerName,
out m_eWhiteType,
out m_eBlackType,
out m_spanWhitePlayer,
out m_spanBlackPlayer)) {
MessageBox.Show("The specified board is invalid.");
} else if (iSkip != 0) {
MessageBox.Show("The game is incomplete. Select another game.");
} else if (iTruncated != 0) {
MessageBox.Show("The selected game includes an unsupported pawn promotion (only pawn promotion to queen is supported).");
} else if (listGame.Count == 0 && m_chessBoardStarting == null) {
MessageBox.Show("Game is empty.");
} else {
m_arrMoveList = listGame;
DialogResult = DialogResult.OK;
Close();
}
}
}
private void listBoxGames_SelectedIndexChanged(object sender, EventArgs e) {
RefreshGameDisplay();
}
public List<ChessBoard.MovePosS> MoveList {
get {
return(m_arrMoveList);
}
}
public string SelectedGame {
get {
return(m_strSelectedGame);
}
}
public ChessBoard StartingChessBoard {
get {
return(m_chessBoardStarting);
}
}
public ChessBoard.PlayerColorE StartingColor {
get {
return(m_eStartingColor);
}
}
public string WhitePlayerName {
get {
return(m_strWhitePlayerName);
}
}
public string BlackPlayerName {
get {
return(m_strBlackPlayerName);
}
}
public PgnParser.PlayerTypeE WhitePlayerType {
get {
return(m_eWhiteType);
}
}
public PgnParser.PlayerTypeE BlackPlayerType {
get {
return(m_eBlackType);
}
}
public TimeSpan WhiteTimer {
get {
return(m_spanWhitePlayer);
}
}
public TimeSpan BlackTimer {
get {
return(m_spanBlackPlayer);
}
}
private void frmPGNGamePicker_Load(object sender, EventArgs e)
{
}
}
}
File ToolStripMenuItem_Click Form5....( pickup color).....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ranjeet_chess_game {
/// <summary>Pickup the colors use to draw the chess control</summary>
public partial class frmPickupColor : Form {
private ChessControl m_chessCtl;
private ChessControl.ChessControlColorInfo m_colorInfo;
public frmPickupColor() {
InitializeComponent();
m_chessCtl = new ChessControl();
m_chessCtl.Location = new Point(0, 0);
m_chessCtl.Size = panel1.Size;
panel1.Controls.Add(m_chessCtl);
}
public frmPickupColor(ChessControl.ChessControlColorInfo colorInfo) {
InitializeComponent();
m_chessCtl = new ChessControl();
m_chessCtl.Location = new Point(0, 0);
m_chessCtl.Size = panel1.Size;
m_chessCtl.ColorInfo = colorInfo;
m_colorInfo = colorInfo;
panel1.Controls.Add(m_chessCtl);
}
public ChessControl.ChessControlColorInfo ColorInfo {
get {
return(m_colorInfo);
}
set {
m_colorInfo = value;
}
}
private void frmPickupColor_Load(object sender, EventArgs e) {
butWhitePieceColor.BackColor = m_colorInfo.m_colWhitePiece;
butBlackPieceColor.BackColor = m_colorInfo.m_colBlackPiece;
butLiteSquareColor.BackColor = m_colorInfo.m_colLiteCase;
butDarkSquareColor.BackColor = m_colorInfo.m_colDarkCase;
}
private void butWhitePieceColor_Click(object sender, EventArgs e) {
colorDialog1.Color = m_colorInfo.m_colWhitePiece;
if (colorDialog1.ShowDialog(this) == DialogResult.OK) {
m_colorInfo.m_colWhitePiece = colorDialog1.Color;
butWhitePieceColor.BackColor = colorDialog1.Color;
m_chessCtl.ColorInfo = m_colorInfo;
}
}
private void butBlackPieceColor_Click(object sender, EventArgs e) {
colorDialog1.Color = m_colorInfo.m_colBlackPiece;
if (colorDialog1.ShowDialog(this) == DialogResult.OK) {
m_colorInfo.m_colBlackPiece = colorDialog1.Color;
butBlackPieceColor.BackColor = colorDialog1.Color;
m_chessCtl.ColorInfo = m_colorInfo;
}
}
private void butLiteSquareColor_Click(object sender, EventArgs e) {
colorDialog1.Color = m_colorInfo.m_colLiteCase;
if (colorDialog1.ShowDialog(this) == DialogResult.OK) {
m_colorInfo.m_colLiteCase = colorDialog1.Color;
butLiteSquareColor.BackColor = colorDialog1.Color;
m_chessCtl.ColorInfo = m_colorInfo;
}
}
private void butDarkSquareColor_Click(object sender, EventArgs e) {
colorDialog1.Color = m_colorInfo.m_colDarkCase;
if (colorDialog1.ShowDialog(this) == DialogResult.OK) {
m_colorInfo.m_colDarkCase = colorDialog1.Color;
butDarkSquareColor.BackColor = colorDialog1.Color;
m_chessCtl.ColorInfo = m_colorInfo;
}
}
private void butResetToDefault_Click(object sender, EventArgs e) {
m_colorInfo.m_colLiteCase = Color.DarkGray;
butLiteSquareColor.BackColor = Color.DarkGray;
m_colorInfo.m_colDarkCase = Color.DarkRed;
butDarkSquareColor.BackColor = Color.DarkRed;
m_colorInfo.m_colBlackPiece = Color.Black;
butBlackPieceColor.BackColor = Color.Black;
m_colorInfo.m_colWhitePiece = Color.FromArgb(235, 235, 235);
butWhitePieceColor.BackColor = Color.FromArgb(235, 235, 235);
m_chessCtl.ColorInfo = m_colorInfo;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ranjeet_chess_game {
/// <summary>Pickup the colors use to draw the chess control</summary>
public partial class frmPickupColor : Form {
private ChessControl m_chessCtl;
private ChessControl.ChessControlColorInfo m_colorInfo;
public frmPickupColor() {
InitializeComponent();
m_chessCtl = new ChessControl();
m_chessCtl.Location = new Point(0, 0);
m_chessCtl.Size = panel1.Size;
panel1.Controls.Add(m_chessCtl);
}
public frmPickupColor(ChessControl.ChessControlColorInfo colorInfo) {
InitializeComponent();
m_chessCtl = new ChessControl();
m_chessCtl.Location = new Point(0, 0);
m_chessCtl.Size = panel1.Size;
m_chessCtl.ColorInfo = colorInfo;
m_colorInfo = colorInfo;
panel1.Controls.Add(m_chessCtl);
}
public ChessControl.ChessControlColorInfo ColorInfo {
get {
return(m_colorInfo);
}
set {
m_colorInfo = value;
}
}
private void frmPickupColor_Load(object sender, EventArgs e) {
butWhitePieceColor.BackColor = m_colorInfo.m_colWhitePiece;
butBlackPieceColor.BackColor = m_colorInfo.m_colBlackPiece;
butLiteSquareColor.BackColor = m_colorInfo.m_colLiteCase;
butDarkSquareColor.BackColor = m_colorInfo.m_colDarkCase;
}
private void butWhitePieceColor_Click(object sender, EventArgs e) {
colorDialog1.Color = m_colorInfo.m_colWhitePiece;
if (colorDialog1.ShowDialog(this) == DialogResult.OK) {
m_colorInfo.m_colWhitePiece = colorDialog1.Color;
butWhitePieceColor.BackColor = colorDialog1.Color;
m_chessCtl.ColorInfo = m_colorInfo;
}
}
private void butBlackPieceColor_Click(object sender, EventArgs e) {
colorDialog1.Color = m_colorInfo.m_colBlackPiece;
if (colorDialog1.ShowDialog(this) == DialogResult.OK) {
m_colorInfo.m_colBlackPiece = colorDialog1.Color;
butBlackPieceColor.BackColor = colorDialog1.Color;
m_chessCtl.ColorInfo = m_colorInfo;
}
}
private void butLiteSquareColor_Click(object sender, EventArgs e) {
colorDialog1.Color = m_colorInfo.m_colLiteCase;
if (colorDialog1.ShowDialog(this) == DialogResult.OK) {
m_colorInfo.m_colLiteCase = colorDialog1.Color;
butLiteSquareColor.BackColor = colorDialog1.Color;
m_chessCtl.ColorInfo = m_colorInfo;
}
}
private void butDarkSquareColor_Click(object sender, EventArgs e) {
colorDialog1.Color = m_colorInfo.m_colDarkCase;
if (colorDialog1.ShowDialog(this) == DialogResult.OK) {
m_colorInfo.m_colDarkCase = colorDialog1.Color;
butDarkSquareColor.BackColor = colorDialog1.Color;
m_chessCtl.ColorInfo = m_colorInfo;
}
}
private void butResetToDefault_Click(object sender, EventArgs e) {
m_colorInfo.m_colLiteCase = Color.DarkGray;
butLiteSquareColor.BackColor = Color.DarkGray;
m_colorInfo.m_colDarkCase = Color.DarkRed;
butDarkSquareColor.BackColor = Color.DarkRed;
m_colorInfo.m_colBlackPiece = Color.Black;
butBlackPieceColor.BackColor = Color.Black;
m_colorInfo.m_colWhitePiece = Color.FromArgb(235, 235, 235);
butWhitePieceColor.BackColor = Color.FromArgb(235, 235, 235);
m_chessCtl.ColorInfo = m_colorInfo;
}
}
}
File ToolStripMenuItem_Click Form6....( Queryfrm).....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ranjeet_chess_game {
/// <summary>Query the promotion type of a pawn</summary>
public partial class frmQueryPawnPromotionType : Form {
private ChessBoard.ValidPawnPromotionE m_eValidPawnPromotion;
public frmQueryPawnPromotionType() {
InitializeComponent();
}
public frmQueryPawnPromotionType(ChessBoard.ValidPawnPromotionE eValidPawnPromotion) : this() {
m_eValidPawnPromotion = eValidPawnPromotion;
}
private void frmQueryPawnPromotionType_Load(object sender, EventArgs e) {
radioButtonQueen.Enabled = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Queen) != ChessBoard.ValidPawnPromotionE.None);
radioButtonRook.Enabled = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Rook) != ChessBoard.ValidPawnPromotionE.None);
radioButtonBishop.Enabled = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Bishop) != ChessBoard.ValidPawnPromotionE.None);
radioButtonKnight.Enabled = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Knight) != ChessBoard.ValidPawnPromotionE.None);
radioButtonPawn.Enabled = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Pawn) != ChessBoard.ValidPawnPromotionE.None);
if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Queen) != ChessBoard.ValidPawnPromotionE.None) {
radioButtonQueen.Checked = true;
} else if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Rook) != ChessBoard.ValidPawnPromotionE.None) {
radioButtonRook.Checked = true;
} else if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Bishop) != ChessBoard.ValidPawnPromotionE.None) {
radioButtonBishop.Checked = true;
} else if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Knight) != ChessBoard.ValidPawnPromotionE.None) {
radioButtonKnight.Checked = true;
} else if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Pawn) != ChessBoard.ValidPawnPromotionE.None) {
radioButtonPawn.Checked = true;
}
}
public ChessBoard.MoveTypeE PromotionType {
get {
ChessBoard.MoveTypeE eRetVal;
if (radioButtonRook.Checked) {
eRetVal = ChessBoard.MoveTypeE.PawnPromotionToRook;
} else if (radioButtonBishop.Checked) {
eRetVal = ChessBoard.MoveTypeE.PawnPromotionToBishop;
} else if (radioButtonKnight.Checked) {
eRetVal = ChessBoard.MoveTypeE.PawnPromotionToKnight;
} else if (radioButtonPawn.Checked) {
eRetVal = ChessBoard.MoveTypeE.PawnPromotionToPawn;
} else {
eRetVal = ChessBoard.MoveTypeE.PawnPromotionToQueen;
}
return(eRetVal);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ranjeet_chess_game {
/// <summary>Query the promotion type of a pawn</summary>
public partial class frmQueryPawnPromotionType : Form {
private ChessBoard.ValidPawnPromotionE m_eValidPawnPromotion;
public frmQueryPawnPromotionType() {
InitializeComponent();
}
public frmQueryPawnPromotionType(ChessBoard.ValidPawnPromotionE eValidPawnPromotion) : this() {
m_eValidPawnPromotion = eValidPawnPromotion;
}
private void frmQueryPawnPromotionType_Load(object sender, EventArgs e) {
radioButtonQueen.Enabled = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Queen) != ChessBoard.ValidPawnPromotionE.None);
radioButtonRook.Enabled = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Rook) != ChessBoard.ValidPawnPromotionE.None);
radioButtonBishop.Enabled = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Bishop) != ChessBoard.ValidPawnPromotionE.None);
radioButtonKnight.Enabled = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Knight) != ChessBoard.ValidPawnPromotionE.None);
radioButtonPawn.Enabled = ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Pawn) != ChessBoard.ValidPawnPromotionE.None);
if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Queen) != ChessBoard.ValidPawnPromotionE.None) {
radioButtonQueen.Checked = true;
} else if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Rook) != ChessBoard.ValidPawnPromotionE.None) {
radioButtonRook.Checked = true;
} else if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Bishop) != ChessBoard.ValidPawnPromotionE.None) {
radioButtonBishop.Checked = true;
} else if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Knight) != ChessBoard.ValidPawnPromotionE.None) {
radioButtonKnight.Checked = true;
} else if ((m_eValidPawnPromotion & ChessBoard.ValidPawnPromotionE.Pawn) != ChessBoard.ValidPawnPromotionE.None) {
radioButtonPawn.Checked = true;
}
}
public ChessBoard.MoveTypeE PromotionType {
get {
ChessBoard.MoveTypeE eRetVal;
if (radioButtonRook.Checked) {
eRetVal = ChessBoard.MoveTypeE.PawnPromotionToRook;
} else if (radioButtonBishop.Checked) {
eRetVal = ChessBoard.MoveTypeE.PawnPromotionToBishop;
} else if (radioButtonKnight.Checked) {
eRetVal = ChessBoard.MoveTypeE.PawnPromotionToKnight;
} else if (radioButtonPawn.Checked) {
eRetVal = ChessBoard.MoveTypeE.PawnPromotionToPawn;
} else {
eRetVal = ChessBoard.MoveTypeE.PawnPromotionToQueen;
}
return(eRetVal);
}
}
}
}
File ToolStripMenuItem_Click Form7...( search_frm).....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ranjeet_chess_game {
/// <summary>
/// Enter the search mode
/// </summary>
public partial class frmSearchMode : Form {
/// <summary>Source search mode object</summary>
private SearchEngine.SearchMode m_searchMode;
/// <summary>Board evaluation utility class</summary>
private BoardEvaluationUtil m_boardEvalUtil;
public frmSearchMode() {
InitializeComponent();
}
internal frmSearchMode(SearchEngine.SearchMode searchMode, BoardEvaluationUtil boardEvalUtil) : this() {
int iPos;
m_searchMode = searchMode;
m_boardEvalUtil = boardEvalUtil;
foreach (IBoardEvaluation boardEval in m_boardEvalUtil.BoardEvaluators) {
iPos = comboBoxWhiteBEval.Items.Add(boardEval.Name);
if (searchMode.m_boardEvaluationWhite == boardEval) {
comboBoxWhiteBEval.SelectedIndex = iPos;
}
iPos = comboBoxBlackBEval.Items.Add(boardEval.Name);
if (searchMode.m_boardEvaluationBlack == boardEval) {
comboBoxBlackBEval.SelectedIndex = iPos;
}
}
checkBoxTransTable.Checked = ((searchMode.m_eOption & SearchEngine.SearchMode.OptionE.UseTransTable) != 0);
if (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch) {
radioButtonOnePerProc.Checked = true;
} else if (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch) {
radioButtonOneForUI.Checked = true;
} else {
radioButtonNoThread.Checked = true;
}
checkBoxBookOpening.Checked = ((searchMode.m_eOption & SearchEngine.SearchMode.OptionE.UseBook) != 0);
if ((searchMode.m_eOption & SearchEngine.SearchMode.OptionE.UseAlphaBeta) != 0) {
radioButtonAlphaBeta.Checked = true;
} else {
radioButtonMinMax.Checked = true;
checkBoxTransTable.Enabled = false;
}
if (searchMode.m_iSearchDepth == 0) {
radioButtonAvgTime.Checked = true;
textBoxTimeInSec.Text = searchMode.m_iTimeOutInSec.ToString();
plyCount.Value = 6;
} else {
if ((searchMode.m_eOption & SearchEngine.SearchMode.OptionE.UseIterativeDepthSearch) == SearchEngine.SearchMode.OptionE.UseIterativeDepthSearch) {
radioButtonFixDepthIterative.Checked = true;
} else {
radioButtonFixDepth.Checked = true;
}
plyCount.Value = searchMode.m_iSearchDepth;
textBoxTimeInSec.Text = "15";
}
switch(searchMode.m_eRandomMode) {
case SearchEngine.SearchMode.RandomModeE.Off:
radioButtonRndOff.Checked = true;
break;
case SearchEngine.SearchMode.RandomModeE.OnRepetitive:
radioButtonRndOnRep.Checked = true;
break;
default:
radioButtonRndOn.Checked = true;
break;
}
textBoxTransSize.Text = (TransTable.TranslationTableSize / 1000000 * 32).ToString(); // Roughly 32 bytes / entry
}
private void radioButtonAlphaBeta_CheckedChanged(object sender, EventArgs e) {
checkBoxTransTable.Enabled = radioButtonAlphaBeta.Checked;
}
private void SetPlyAvgTimeState() {
if (radioButtonAvgTime.Checked) {
plyCount.Enabled = false;
labelNumberOfPly.Enabled = false;
textBoxTimeInSec.Enabled = true;
labelAvgTime.Enabled = true;
} else {
plyCount.Enabled = true;
labelNumberOfPly.Enabled = true;
textBoxTimeInSec.Enabled = false;
labelAvgTime.Enabled = false;
}
}
private void radioButtonFixDepth_CheckedChanged(object sender, EventArgs e) {
SetPlyAvgTimeState();
}
private void radioButtonFixDepthIterative_CheckedChanged(object sender, EventArgs e) {
SetPlyAvgTimeState();
}
private void textBoxTimeInSec_TextChanged(object sender, EventArgs e) {
int iVal;
butOk.Enabled = (Int32.TryParse(textBoxTimeInSec.Text, out iVal) &&
iVal > 0 &&
iVal < 999);
}
private void textBoxTransSize_TextChanged(object sender, EventArgs e) {
int iVal;
butOk.Enabled = (Int32.TryParse(textBoxTransSize.Text, out iVal) &&
iVal > 4 &&
iVal < 256);
}
public void UpdateSearchMode() {
Properties.Settings settings = Properties.Settings.Default;
int iTransTableSize;
IBoardEvaluation boardEval;
settings.UseAlphaBeta = radioButtonAlphaBeta.Checked;
settings.UseBook = checkBoxBookOpening.Checked;
settings.UseTransTable = checkBoxTransTable.Checked;
settings.PlyCount = (int)plyCount.Value;
settings.AverageTime = Int32.Parse(textBoxTimeInSec.Text);
settings.UsePlyCount = radioButtonFixDepth.Checked;
settings.UsePlyCountIterative = radioButtonFixDepthIterative.Checked;
m_searchMode.m_eOption = (radioButtonAlphaBeta.Checked) ? SearchEngine.SearchMode.OptionE.UseAlphaBeta :
SearchEngine.SearchMode.OptionE.UseMinMax;
if (checkBoxBookOpening.Checked) {
m_searchMode.m_eOption |= SearchEngine.SearchMode.OptionE.UseBook;
}
if (checkBoxTransTable.Checked) {
m_searchMode.m_eOption |= SearchEngine.SearchMode.OptionE.UseTransTable;
}
if (radioButtonOnePerProc.Checked) {
m_searchMode.m_eThreadingMode = SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch;
settings.UseThread = 2;
} else if (radioButtonOneForUI.Checked) {
m_searchMode.m_eThreadingMode = SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch;
settings.UseThread = 1;
} else {
m_searchMode.m_eThreadingMode = SearchEngine.SearchMode.ThreadingModeE.Off;
settings.UseThread = 0;
}
if (radioButtonAvgTime.Checked) {
m_searchMode.m_iSearchDepth = 0;
m_searchMode.m_iTimeOutInSec = Int32.Parse(textBoxTimeInSec.Text);
} else {
m_searchMode.m_iSearchDepth = (int)plyCount.Value;
m_searchMode.m_iTimeOutInSec = 0;
if (radioButtonFixDepthIterative.Checked) {
m_searchMode.m_eOption |= SearchEngine.SearchMode.OptionE.UseIterativeDepthSearch;
}
}
if (radioButtonRndOff.Checked) {
m_searchMode.m_eRandomMode = SearchEngine.SearchMode.RandomModeE.Off;
} else if (radioButtonRndOnRep.Checked) {
m_searchMode.m_eRandomMode = SearchEngine.SearchMode.RandomModeE.OnRepetitive;
} else {
m_searchMode.m_eRandomMode = SearchEngine.SearchMode.RandomModeE.On;
}
settings.RandomMode = (int)m_searchMode.m_eRandomMode;
iTransTableSize = Int32.Parse(textBoxTransSize.Text);
settings.TransTableSize = iTransTableSize;
TransTable.TranslationTableSize = iTransTableSize / 32 * 1000000;
boardEval = m_boardEvalUtil.FindBoardEvaluator(comboBoxWhiteBEval.SelectedItem.ToString());
if (boardEval == null) {
boardEval = m_boardEvalUtil.BoardEvaluators[0];
}
m_searchMode.m_boardEvaluationWhite = boardEval;
settings.WhiteBoardEval = boardEval.Name;
boardEval = m_boardEvalUtil.FindBoardEvaluator(comboBoxBlackBEval.SelectedItem.ToString());
if (boardEval == null) {
boardEval = m_boardEvalUtil.BoardEvaluators[0];
}
m_searchMode.m_boardEvaluationBlack = boardEval;
settings.BlackBoardEval = boardEval.Name;
settings.Save();
}
private void frmSearchMode_Load(object sender, EventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ranjeet_chess_game {
/// <summary>
/// Enter the search mode
/// </summary>
public partial class frmSearchMode : Form {
/// <summary>Source search mode object</summary>
private SearchEngine.SearchMode m_searchMode;
/// <summary>Board evaluation utility class</summary>
private BoardEvaluationUtil m_boardEvalUtil;
public frmSearchMode() {
InitializeComponent();
}
internal frmSearchMode(SearchEngine.SearchMode searchMode, BoardEvaluationUtil boardEvalUtil) : this() {
int iPos;
m_searchMode = searchMode;
m_boardEvalUtil = boardEvalUtil;
foreach (IBoardEvaluation boardEval in m_boardEvalUtil.BoardEvaluators) {
iPos = comboBoxWhiteBEval.Items.Add(boardEval.Name);
if (searchMode.m_boardEvaluationWhite == boardEval) {
comboBoxWhiteBEval.SelectedIndex = iPos;
}
iPos = comboBoxBlackBEval.Items.Add(boardEval.Name);
if (searchMode.m_boardEvaluationBlack == boardEval) {
comboBoxBlackBEval.SelectedIndex = iPos;
}
}
checkBoxTransTable.Checked = ((searchMode.m_eOption & SearchEngine.SearchMode.OptionE.UseTransTable) != 0);
if (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch) {
radioButtonOnePerProc.Checked = true;
} else if (searchMode.m_eThreadingMode == SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch) {
radioButtonOneForUI.Checked = true;
} else {
radioButtonNoThread.Checked = true;
}
checkBoxBookOpening.Checked = ((searchMode.m_eOption & SearchEngine.SearchMode.OptionE.UseBook) != 0);
if ((searchMode.m_eOption & SearchEngine.SearchMode.OptionE.UseAlphaBeta) != 0) {
radioButtonAlphaBeta.Checked = true;
} else {
radioButtonMinMax.Checked = true;
checkBoxTransTable.Enabled = false;
}
if (searchMode.m_iSearchDepth == 0) {
radioButtonAvgTime.Checked = true;
textBoxTimeInSec.Text = searchMode.m_iTimeOutInSec.ToString();
plyCount.Value = 6;
} else {
if ((searchMode.m_eOption & SearchEngine.SearchMode.OptionE.UseIterativeDepthSearch) == SearchEngine.SearchMode.OptionE.UseIterativeDepthSearch) {
radioButtonFixDepthIterative.Checked = true;
} else {
radioButtonFixDepth.Checked = true;
}
plyCount.Value = searchMode.m_iSearchDepth;
textBoxTimeInSec.Text = "15";
}
switch(searchMode.m_eRandomMode) {
case SearchEngine.SearchMode.RandomModeE.Off:
radioButtonRndOff.Checked = true;
break;
case SearchEngine.SearchMode.RandomModeE.OnRepetitive:
radioButtonRndOnRep.Checked = true;
break;
default:
radioButtonRndOn.Checked = true;
break;
}
textBoxTransSize.Text = (TransTable.TranslationTableSize / 1000000 * 32).ToString(); // Roughly 32 bytes / entry
}
private void radioButtonAlphaBeta_CheckedChanged(object sender, EventArgs e) {
checkBoxTransTable.Enabled = radioButtonAlphaBeta.Checked;
}
private void SetPlyAvgTimeState() {
if (radioButtonAvgTime.Checked) {
plyCount.Enabled = false;
labelNumberOfPly.Enabled = false;
textBoxTimeInSec.Enabled = true;
labelAvgTime.Enabled = true;
} else {
plyCount.Enabled = true;
labelNumberOfPly.Enabled = true;
textBoxTimeInSec.Enabled = false;
labelAvgTime.Enabled = false;
}
}
private void radioButtonFixDepth_CheckedChanged(object sender, EventArgs e) {
SetPlyAvgTimeState();
}
private void radioButtonFixDepthIterative_CheckedChanged(object sender, EventArgs e) {
SetPlyAvgTimeState();
}
private void textBoxTimeInSec_TextChanged(object sender, EventArgs e) {
int iVal;
butOk.Enabled = (Int32.TryParse(textBoxTimeInSec.Text, out iVal) &&
iVal > 0 &&
iVal < 999);
}
private void textBoxTransSize_TextChanged(object sender, EventArgs e) {
int iVal;
butOk.Enabled = (Int32.TryParse(textBoxTransSize.Text, out iVal) &&
iVal > 4 &&
iVal < 256);
}
public void UpdateSearchMode() {
Properties.Settings settings = Properties.Settings.Default;
int iTransTableSize;
IBoardEvaluation boardEval;
settings.UseAlphaBeta = radioButtonAlphaBeta.Checked;
settings.UseBook = checkBoxBookOpening.Checked;
settings.UseTransTable = checkBoxTransTable.Checked;
settings.PlyCount = (int)plyCount.Value;
settings.AverageTime = Int32.Parse(textBoxTimeInSec.Text);
settings.UsePlyCount = radioButtonFixDepth.Checked;
settings.UsePlyCountIterative = radioButtonFixDepthIterative.Checked;
m_searchMode.m_eOption = (radioButtonAlphaBeta.Checked) ? SearchEngine.SearchMode.OptionE.UseAlphaBeta :
SearchEngine.SearchMode.OptionE.UseMinMax;
if (checkBoxBookOpening.Checked) {
m_searchMode.m_eOption |= SearchEngine.SearchMode.OptionE.UseBook;
}
if (checkBoxTransTable.Checked) {
m_searchMode.m_eOption |= SearchEngine.SearchMode.OptionE.UseTransTable;
}
if (radioButtonOnePerProc.Checked) {
m_searchMode.m_eThreadingMode = SearchEngine.SearchMode.ThreadingModeE.OnePerProcessorForSearch;
settings.UseThread = 2;
} else if (radioButtonOneForUI.Checked) {
m_searchMode.m_eThreadingMode = SearchEngine.SearchMode.ThreadingModeE.DifferentThreadForSearch;
settings.UseThread = 1;
} else {
m_searchMode.m_eThreadingMode = SearchEngine.SearchMode.ThreadingModeE.Off;
settings.UseThread = 0;
}
if (radioButtonAvgTime.Checked) {
m_searchMode.m_iSearchDepth = 0;
m_searchMode.m_iTimeOutInSec = Int32.Parse(textBoxTimeInSec.Text);
} else {
m_searchMode.m_iSearchDepth = (int)plyCount.Value;
m_searchMode.m_iTimeOutInSec = 0;
if (radioButtonFixDepthIterative.Checked) {
m_searchMode.m_eOption |= SearchEngine.SearchMode.OptionE.UseIterativeDepthSearch;
}
}
if (radioButtonRndOff.Checked) {
m_searchMode.m_eRandomMode = SearchEngine.SearchMode.RandomModeE.Off;
} else if (radioButtonRndOnRep.Checked) {
m_searchMode.m_eRandomMode = SearchEngine.SearchMode.RandomModeE.OnRepetitive;
} else {
m_searchMode.m_eRandomMode = SearchEngine.SearchMode.RandomModeE.On;
}
settings.RandomMode = (int)m_searchMode.m_eRandomMode;
iTransTableSize = Int32.Parse(textBoxTransSize.Text);
settings.TransTableSize = iTransTableSize;
TransTable.TranslationTableSize = iTransTableSize / 32 * 1000000;
boardEval = m_boardEvalUtil.FindBoardEvaluator(comboBoxWhiteBEval.SelectedItem.ToString());
if (boardEval == null) {
boardEval = m_boardEvalUtil.BoardEvaluators[0];
}
m_searchMode.m_boardEvaluationWhite = boardEval;
settings.WhiteBoardEval = boardEval.Name;
boardEval = m_boardEvalUtil.FindBoardEvaluator(comboBoxBlackBEval.SelectedItem.ToString());
if (boardEval == null) {
boardEval = m_boardEvalUtil.BoardEvaluators[0];
}
m_searchMode.m_boardEvaluationBlack = boardEval;
settings.BlackBoardEval = boardEval.Name;
settings.Save();
}
private void frmSearchMode_Load(object sender, EventArgs e)
{
}
}
}
File ToolStripMenuItem_Click Form8...( testfrm).....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ranjeet_chess_game {
/// <summary>Enter parameters for testing the board evaluation functions</summary>
public partial class frmTestBoardEval : Form {
/// <summary>Board evaluation utility</summary>
BoardEvaluationUtil m_boardEvalUtil;
/// <summary>Resulting search mode</summary>
SearchEngine.SearchMode m_searchMode;
public frmTestBoardEval() {
InitializeComponent();
}
public frmTestBoardEval(BoardEvaluationUtil boardEvalUtil, SearchEngine.SearchMode searchModeTemplate) : this() {
m_searchMode = new SearchEngine.SearchMode(boardEvalUtil.BoardEvaluators[0],
boardEvalUtil.BoardEvaluators[0],
SearchEngine.SearchMode.OptionE.UseAlphaBeta,
searchModeTemplate.m_eThreadingMode,
4,
0,
searchModeTemplate.m_eRandomMode);
foreach (IBoardEvaluation boardEval in boardEvalUtil.BoardEvaluators) {
comboBoxWhiteBEval.Items.Add(boardEval.Name);
comboBoxBlackBEval.Items.Add(boardEval.Name);
}
comboBoxWhiteBEval.SelectedIndex = 0;
comboBoxBlackBEval.SelectedIndex = (comboBoxBlackBEval.Items.Count == 0) ? 0 : 1;
m_boardEvalUtil = boardEvalUtil;
SetButtonState();
}
public SearchEngine.SearchMode SearchMode {
get {
IBoardEvaluation boardEval;
boardEval = m_boardEvalUtil.FindBoardEvaluator(comboBoxWhiteBEval.SelectedItem.ToString());
if (boardEval == null) {
boardEval = m_boardEvalUtil.BoardEvaluators[0];
}
m_searchMode.m_boardEvaluationWhite = boardEval;
boardEval = m_boardEvalUtil.FindBoardEvaluator(comboBoxBlackBEval.SelectedItem.ToString());
if (boardEval == null) {
boardEval = m_boardEvalUtil.BoardEvaluators[0];
}
m_searchMode.m_boardEvaluationBlack = boardEval;
m_searchMode.m_iSearchDepth = (int)plyCount.Value;
return(m_searchMode);
}
}
public int GameCount {
get {
return((int)gameCount.Value);
}
}
private void SetButtonState() {
SearchEngine.SearchMode searchMode;
if (m_boardEvalUtil != null) {
searchMode = SearchMode;
butOk.Enabled = true ; //(searchMode.m_boardEvaluationWhite != m_searchMode.m_boardEvaluationBlack);
}
}
private void comboBoxWhiteBEval_SelectedIndexChanged(object sender, EventArgs e) {
SetButtonState();
}
private void comboBoxBlackBEval_SelectedIndexChanged(object sender, EventArgs e) {
SetButtonState();
}
private void frmTestBoardEval_Load(object sender, EventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ranjeet_chess_game {
/// <summary>Enter parameters for testing the board evaluation functions</summary>
public partial class frmTestBoardEval : Form {
/// <summary>Board evaluation utility</summary>
BoardEvaluationUtil m_boardEvalUtil;
/// <summary>Resulting search mode</summary>
SearchEngine.SearchMode m_searchMode;
public frmTestBoardEval() {
InitializeComponent();
}
public frmTestBoardEval(BoardEvaluationUtil boardEvalUtil, SearchEngine.SearchMode searchModeTemplate) : this() {
m_searchMode = new SearchEngine.SearchMode(boardEvalUtil.BoardEvaluators[0],
boardEvalUtil.BoardEvaluators[0],
SearchEngine.SearchMode.OptionE.UseAlphaBeta,
searchModeTemplate.m_eThreadingMode,
4,
0,
searchModeTemplate.m_eRandomMode);
foreach (IBoardEvaluation boardEval in boardEvalUtil.BoardEvaluators) {
comboBoxWhiteBEval.Items.Add(boardEval.Name);
comboBoxBlackBEval.Items.Add(boardEval.Name);
}
comboBoxWhiteBEval.SelectedIndex = 0;
comboBoxBlackBEval.SelectedIndex = (comboBoxBlackBEval.Items.Count == 0) ? 0 : 1;
m_boardEvalUtil = boardEvalUtil;
SetButtonState();
}
public SearchEngine.SearchMode SearchMode {
get {
IBoardEvaluation boardEval;
boardEval = m_boardEvalUtil.FindBoardEvaluator(comboBoxWhiteBEval.SelectedItem.ToString());
if (boardEval == null) {
boardEval = m_boardEvalUtil.BoardEvaluators[0];
}
m_searchMode.m_boardEvaluationWhite = boardEval;
boardEval = m_boardEvalUtil.FindBoardEvaluator(comboBoxBlackBEval.SelectedItem.ToString());
if (boardEval == null) {
boardEval = m_boardEvalUtil.BoardEvaluators[0];
}
m_searchMode.m_boardEvaluationBlack = boardEval;
m_searchMode.m_iSearchDepth = (int)plyCount.Value;
return(m_searchMode);
}
}
public int GameCount {
get {
return((int)gameCount.Value);
}
}
private void SetButtonState() {
SearchEngine.SearchMode searchMode;
if (m_boardEvalUtil != null) {
searchMode = SearchMode;
butOk.Enabled = true ; //(searchMode.m_boardEvaluationWhite != m_searchMode.m_boardEvaluationBlack);
}
}
private void comboBoxWhiteBEval_SelectedIndexChanged(object sender, EventArgs e) {
SetButtonState();
}
private void comboBoxBlackBEval_SelectedIndexChanged(object sender, EventArgs e) {
SetButtonState();
}
private void frmTestBoardEval_Load(object sender, EventArgs e)
{
}
}
}
No comments:
Post a Comment