public static int GetPositionValue(Point pos, eChessSide eSide, bool IsEndGame, eChessSide ownSide) { if (IsEndGame == false) { if (eSide == ownSide) { return(KingTable[pos.X, pos.Y]); } else { return(KingTable[9 - pos.X, 9 - pos.Y]); } } else { if (eSide == ownSide) { return(KingTableEndGame[pos.X, pos.Y]); } else { return(KingTableEndGame[9 - pos.X, 9 - pos.Y]); } } }
public static ArrayList FindAllImposibleMoves(ChessState[,] arrState, Point pos) { ArrayList arrMove = new ArrayList(); eChessSide side = arrState[pos.X, pos.Y].Side; Point[] How_To_Move = new Point[] { new Point(1, 1), new Point(1, -1), new Point(-1, 1), new Point(-1, -1), new Point(1, 0), new Point(0, 1), new Point(-1, 0), new Point(0, -1) }; for (int i = 0; i < How_To_Move.Length; i++) { Point pos_temp = new Point(pos.X + How_To_Move[i].X, pos.Y + How_To_Move[i].Y); while (arrState[pos_temp.X, pos_temp.Y].Type == eChessPieceType.Null) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos_temp.X, pos_temp.Y), eMove.Moving, arrState[pos.X, pos.Y].Moves)); pos_temp.X += How_To_Move[i].X; pos_temp.Y += How_To_Move[i].Y; } //Eating if (arrState[pos_temp.X, pos_temp.Y].Side == side && arrState[pos_temp.X, pos_temp.Y].Type > 0) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos_temp.X, pos_temp.Y), eMove.Eating, new Point(pos_temp.X, pos_temp.Y), arrState[pos_temp.X, pos_temp.Y].Type, arrState[pos_temp.X, pos_temp.Y].Moves, arrState[pos.X, pos.Y].Moves)); } } return(arrMove); }
public Uc_ChessCell(Point Position, Bitmap BackImage, eChessSide Side, eChessBoardStyle Style) { InitializeComponent(); this._position.X = Position.X; this._position.Y = Position.Y; this._side = Side; this._boardStyle = Style; this._backImage = BackImage; //Update backimage }
public static int GetPositionValue(Point pos, eChessSide eSide, eChessSide ownSide) { if (eSide == ownSide) { return(QueenTable[pos.X, pos.Y]); } else { return(QueenTable[9 - pos.X, 9 - pos.Y]); } }
public Uc_ChessCell(Point Position, eChessSide Side, eChessBoardStyle BoardStyle) { InitializeComponent(); this._position.X = Position.X; this._position.Y = Position.Y; this._side = Side; this._boardStyle = BoardStyle; this._backImage = Read_Image_From_Resources.GetChessBoardBitMap(this._side, this._boardStyle); this.BackImage = _backImage; //Update backimage }
/// <summary> /// Make a random move /// </summary> /// <param name="arrState"></param> /// <param name="side"></param> /// <returns></returns> public static ChessMove RandomMove(ChessState[,] arrState, eChessSide side, eChessSide ownside) { ArrayList arrCanMoves = FindAllPosibleMoves(arrState, side, ownside); if (arrCanMoves.Count == 0) { return(null); } Random rd = new Random(arrCanMoves.Count); return((ChessMove)arrCanMoves[rd.Next(arrCanMoves.Count)]); }
public static ArrayList FindWhoCheckAndKingPosition(ChessState[,] arrState, eChessSide side, eChessSide ownside) { ArrayList WhoCheck = new ArrayList(); ArrayList cantMoves = new ArrayList(); int i, j; for (i = 1; i < 9; i++) { for (j = 1; j < 9; j++) { if (arrState[i, j].Side != side) { if (arrState[i, j].Type == eChessPieceType.Bishop) { cantMoves = ChessPiece_Bishop.FindAllPosibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.King) { cantMoves = ChessPiece_King.FindAllPosibleMoves(arrState, new Point(i, j), ownside); } else if (arrState[i, j].Type == eChessPieceType.Knight) { cantMoves = ChessPiece_Knight.FindAllPosibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.Pawn) { cantMoves = ChessPiece_Pawn.FindAllPosibleMoves(arrState, new Point(i, j), ownside); } else if (arrState[i, j].Type == eChessPieceType.Queen) { cantMoves = ChessPiece_Queen.FindAllPosibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.Rook) { cantMoves = ChessPiece_Rook.FindAllPosibleMoves(arrState, new Point(i, j)); } foreach (ChessMove p in cantMoves) { if (arrState[p.MoveTo.X, p.MoveTo.Y].Side == side && arrState[p.MoveTo.X, p.MoveTo.Y].Type == eChessPieceType.King) { WhoCheck.Add(p); break; } } } } } return(WhoCheck); }
public Uc_ChessPiece(eChessSide side, eChessPieceType type, eChessPieceStyle style, int cellSize, int piecesize) { InitializeComponent(); this._side = side; this._type = type; this._style = style; _cellSize = cellSize; _pieceSize = piecesize; _image = Read_Image_From_Resources.GetChessPieceBitMap(_side, _type, _style); //UserControl Size this.Size = new Size(this._pieceSize, this._pieceSize); }
public static ChessMove MakeAComputerMove(ChessState[,] arrState, eChessSide side, Dictionary <string, int> arrPosition, eGameDifficulty difficult, eChessSide ownside) { _myBestMove = null; _myBestScore = _lowestScore; if (difficult == eGameDifficulty.Hard) { _maxDepth = 5; } else if (difficult == eGameDifficulty.Normal) { _maxDepth = 4; } else { _maxDepth = 3; } _mySide = side; _opSide = eChessSide.Black; if (_mySide == eChessSide.Black) { _opSide = eChessSide.White; } _ownSide = ownside; if (FindAllPosibleMoves(arrState, _mySide, _ownSide).Count + FindAllPosibleMoves(arrState, _opSide, _ownSide).Count < 30) { _maxDepth = 5; } ChessState[,] NewState = new ChessState[10, 10]; for (int i = 0; i <= 9; i++) { for (int j = 0; j <= 9; j++) { NewState[i, j] = new ChessState(arrState[i, j].Type, arrState[i, j].Side, arrState[i, j].Moves); } } AlphaBeta(NewState, arrPosition, _maxDepth, _mySide, _lowestScore, -_lowestScore); /* * PredictMove(NewState, _myBestMove); * if (Checked(NewState, _mySide) == false) * return _myBestMove; * * return RandomMove(State,side);*/ return(_myBestMove); }
/// <summary> /// Check Is that one side is checked? /// </summary> /// <param name="arrState"></param> /// <param name="side"></param> /// <returns></returns> public static bool Checked(ChessState[,] arrState, eChessSide side, eChessSide ownside) { int i, j; ArrayList cantMoves = new ArrayList(); for (i = 1; i < 9; i++) { for (j = 1; j < 9; j++) { if (arrState[i, j].Side != side) { if (arrState[i, j].Type == eChessPieceType.Bishop) { cantMoves = ChessPiece_Bishop.FindAllPosibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.King) { cantMoves = ChessPiece_King.FindAllPosibleMoves(arrState, new Point(i, j), ownside); } else if (arrState[i, j].Type == eChessPieceType.Knight) { cantMoves = ChessPiece_Knight.FindAllPosibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.Pawn) { cantMoves = ChessPiece_Pawn.FindAllPosibleMoves(arrState, new Point(i, j), ownside); } else if (arrState[i, j].Type == eChessPieceType.Queen) { cantMoves = ChessPiece_Queen.FindAllPosibleMoves(arrState, new Point(i, j)); } else if (arrState[i, j].Type == eChessPieceType.Rook) { cantMoves = ChessPiece_Rook.FindAllPosibleMoves(arrState, new Point(i, j)); } foreach (ChessMove p in cantMoves) { if (arrState[p.MoveTo.X, p.MoveTo.Y].Side == side && arrState[p.MoveTo.X, p.MoveTo.Y].Type == eChessPieceType.King) { return(true); } } } } } return(false); }
/* * Speelman considers that endgames are positions in which each player has 13 * or fewer points in material (not counting the king)- wikipedia * * Khi giá trị quân cờ của mỗi đối thủ nhỏ hơn hoặc bằng 13(khoảng 1350 điểm)=>EndGame */ /// <summary> /// Check that the game is gonna be ended /// </summary> /// <param name="arrState"></param> /// <param name="side"></param> /// <returns></returns> private static bool IsEndGame(ChessState[,] arrState, eChessSide side) { int myScore = 0; int opScore = 0; int i, j; for (i = 0; i <= 8; i++) { for (j = 0; j <= 8; j++) { if (arrState[i, j].Type > 0) { if (arrState[i, j].Side == side) { switch (arrState[i, j].Type) { case eChessPieceType.Bishop: myScore += (int)PieceValue.Bishop; break; case eChessPieceType.Knight: myScore += (int)PieceValue.Knight; break; case eChessPieceType.Pawn: myScore += (int)PieceValue.Pawn; break; case eChessPieceType.Queen: myScore += (int)PieceValue.Queen; break; case eChessPieceType.Rook: myScore += (int)PieceValue.Rook; break; } } else { switch (arrState[i, j].Type) { case eChessPieceType.Bishop: opScore += (int)PieceValue.Bishop; break; case eChessPieceType.Knight: opScore += (int)PieceValue.Knight; break; case eChessPieceType.Pawn: opScore += (int)PieceValue.Pawn; break; case eChessPieceType.Queen: opScore += (int)PieceValue.Queen; break; case eChessPieceType.Rook: opScore += (int)PieceValue.Rook; break; } } } } } return(myScore <= 1350 && opScore <= 1350); }
/* * Hàm này trả về tên của hình ảnh quân cờ chứa trong resource * VD: Muon lay quan Mã màu Đen Style Classic * Bitmap img=GetChessPieceBitMap(ChessPieceSide.Black,eChessPieceType.Knight,eChessPieceStyle.Classic); * Hàm này lấy hình ảnh trong Resource có tên là "Black_K_1"; */ public static Bitmap GetChessPieceBitMap(eChessSide Side, eChessPieceType Type, eChessPieceStyle Style) { string strImg = ""; switch (Side) { case eChessSide.Black: strImg += "Black_"; break; case eChessSide.White: strImg += "White_"; break; } switch (Type) { case eChessPieceType.Pawn: strImg += "P_"; break; case eChessPieceType.Bishop: strImg += "B_"; break; case eChessPieceType.Knight: strImg += "N_"; break; case eChessPieceType.Rook: strImg += "R_"; break; case eChessPieceType.Queen: strImg += "Q_"; break; case eChessPieceType.King: strImg += "K_"; break; } strImg += (int)Style; Bitmap img = (Bitmap)Properties.Resources.ResourceManager.GetObject(strImg); return(img); }
public Form_Promotion(eChessSide side, eChessPieceStyle style, System.Globalization.CultureInfo language) { InitializeComponent(); //Initalize pictureBox1.Image = Read_Image_From_Resources.GetChessPieceBitMap(side, eChessPieceType.Queen, style); pictureBox2.Image = Read_Image_From_Resources.GetChessPieceBitMap(side, eChessPieceType.Rook, style); pictureBox3.Image = Read_Image_From_Resources.GetChessPieceBitMap(side, eChessPieceType.Knight, style); pictureBox4.Image = Read_Image_From_Resources.GetChessPieceBitMap(side, eChessPieceType.Bishop, style); //Change Language Assembly a = Assembly.Load("Chess Library"); ResourceManager rm = new ResourceManager("Chess_Library.Resources.Lang.Langres", a); this.Text = rm.GetString("Form_Promotion", language); labelX_form_promotion.Text = rm.GetString("labelX_form_promotion", language); }
public static ArrayList FindAllImposibleMoves(ChessState[,] arrState, Point pos, eChessSide ownSide) { ArrayList arrMove = new ArrayList(); eChessSide side = arrState[pos.X, pos.Y].Side; if (side == ownSide) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X - 1, pos.Y - 1), eMove.Eating, new Point(pos.X - 1, pos.Y - 1), arrState[pos.X - 1, pos.Y - 1].Type, arrState[pos.X - 1, pos.Y - 1].Moves, arrState[pos.X, pos.Y].Moves)); arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X - 1, pos.Y + 1), eMove.Eating, new Point(pos.X - 1, pos.Y + 1), arrState[pos.X - 1, pos.Y + 1].Type, arrState[pos.X - 1, pos.Y + 1].Moves, arrState[pos.X, pos.Y].Moves)); } else { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X + 1, pos.Y - 1), eMove.Eating, new Point(pos.X + 1, pos.Y - 1), arrState[pos.X + 1, pos.Y - 1].Type, arrState[pos.X + 1, pos.Y - 1].Moves, arrState[pos.X, pos.Y].Moves)); arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X + 1, pos.Y + 1), eMove.Eating, new Point(pos.X + 1, pos.Y + 1), arrState[pos.X + 1, pos.Y + 1].Type, arrState[pos.X + 1, pos.Y + 1].Moves, arrState[pos.X, pos.Y].Moves)); } return(arrMove); }
public static Bitmap GetChessBoardBitMap(eChessSide Side, eChessBoardStyle Style) { string strImg = ""; switch (Side) { case eChessSide.Black: strImg += "Black_C_"; break; case eChessSide.White: strImg += "White_C_"; break; } strImg += (int)Style; Bitmap img = (Bitmap)Properties.Resources.ResourceManager.GetObject(strImg); return(img); }
private void btn_Start_Click(object sender, EventArgs e) { if (combobox_Game_Mode.SelectedIndex == 0 || combobox_Game_Mode.SelectedIndex == -1) { _gameMode = eGameMode.VsComputer; } else if (combobox_Game_Mode.SelectedIndex == 1) { _gameMode = eGameMode.VsHuman; } else { _gameMode = eGameMode.ComVsCom; } if (radiobtn_Black.Checked) { _ownSide = eChessSide.Black; } else { _ownSide = eChessSide.White; } if (radiobtn_Easy.Checked) { _gameDifficulty = eGameDifficulty.Easy; } else if (radiobtn_Medium.Checked) { _gameDifficulty = eGameDifficulty.Normal; } else { _gameDifficulty = eGameDifficulty.Hard; } _timeLimit = (short)integerInput_TimeLimit.Value; _timeBonus = (short)integerInput_TimeBonus.Value; }
public static int GetPositionValue(Point pos, eChessSide eSide, eChessSide ownSide) { int value = 0; if (eSide == ownSide) { value = PawnTable[pos.X, pos.Y]; //Tốt cánh xe bị trừ 15% giá trị if (pos.Y == 8 || pos.Y == 1) { value -= 15; } } else { value = PawnTable[9 - pos.X, 9 - pos.Y]; if (pos.Y == 8 || pos.Y == 1) { value -= 15; } } return(value); }
private void btn_Newgame_Click(object sender, EventArgs e) { Form_New_game form_temp = new Form_New_game(_language); if (form_temp.ShowDialog() == DialogResult.OK) { _ownSide = form_temp._ownSide; _gameMode = form_temp._gameMode; _gameDifficulty = form_temp._gameDifficulty; this._timeLimit = form_temp._timeLimit; this._timeBonus = form_temp._timeBonus; if (_gameMode == eGameMode.VsHuman) { CreateChessBoard(_ownSide, _gameMode); } else if (_gameMode == eGameMode.VsComputer) { CreateChessBoard(_ownSide, _gameMode, _gameDifficulty); } } form_temp.Dispose(); }
/// <summary> /// Find all possible Moves of a Chesspiece in position of a State but not check the Chess Rule /// </summary> /// <param name="arrState"></param> /// <param name="side"></param> /// <returns></returns> public static ArrayList FindAllPosibleMovesNotRule(ChessState[,] arrState, eChessSide side, eChessSide ownside) { int i, j; ArrayList arrCanMoves = new ArrayList(); ArrayList arrCanMovesTemp = new ArrayList(); for (i = 1; i <= 8; i++) { for (j = 1; j <= 8; j++) { if (arrState[i, j].Type > 0 && arrState[i, j].Side == side) { arrCanMovesTemp = FindAllPosibleMovesNotRule(arrState, new Point(i, j), ownside); foreach (ChessMove m in arrCanMovesTemp) { arrCanMoves.Add(m); } } } } return(arrCanMoves); }
/// <summary> /// Add A,B,C,D,..., 1 2 3 4 to Chess Board /// </summary> /// <param name="intcellsize"></param> /// <param name="eownside"></param> void AddNotation(int intcellsize, eChessSide eownside) { for (int i = 0; i < 36; i++) { lblNotation[i] = new Label(); lblNotation[i].Height = intcellsize; lblNotation[i].Width = intcellsize; lblNotation[i].Image = Read_Image_From_Resources.GetChessBoardBitMap(eChessSide.Black, eChessBoardStyle.BoardEdge); lblNotation[i].TextAlign = ContentAlignment.MiddleCenter; lblNotation[i].Font = new Font(FontFamily.GenericSansSerif, 14.0f); lblNotation[i].ImageAlign = ContentAlignment.MiddleCenter; lblNotation[i].ForeColor = Color.White; } lblNotation[0].Height = _notationSize; lblNotation[0].Width = _notationSize; lblNotation[0].Location = new Point(); lblNotation[0].BackColor = Color.Blue; panel1.Controls.Add(lblNotation[0]); for (int i = 1; i <= 8; i++) { lblNotation[i].Height = _notationSize; if (eownside == eChessSide.White) { lblNotation[i].Text = Convert.ToChar(64 + i).ToString(); } else { lblNotation[i].Text = Convert.ToChar(73 - i).ToString(); } lblNotation[i].Location = new Point(intcellsize * (i - 1) + _notationSize, 0); panel1.Controls.Add(lblNotation[i]); } lblNotation[9].Height = _notationSize; lblNotation[9].Width = _notationSize; lblNotation[9].BackColor = Color.Blue; lblNotation[9].Location = new Point(0, _notationSize + intcellsize * 8); panel1.Controls.Add(lblNotation[9]); for (int i = 1; i <= 8; i++) { lblNotation[9 + i].Height = _notationSize; if (eownside == eChessSide.White) { lblNotation[9 + i].Text = Convert.ToChar(64 + i).ToString(); } else { lblNotation[9 + i].Text = Convert.ToChar(73 - i).ToString(); } lblNotation[9 + i].Location = new Point(intcellsize * (i - 1) + _notationSize, _notationSize + intcellsize * 8); panel1.Controls.Add(lblNotation[9 + i]); } lblNotation[18].Height = _notationSize; lblNotation[18].Width = _notationSize; lblNotation[18].BackColor = Color.Blue; lblNotation[18].Location = new Point(_notationSize + intcellsize * 8, 0); panel1.Controls.Add(lblNotation[18]); for (int i = 1; i <= 8; i++) { lblNotation[18 + i].Width = _notationSize; if (eownside == eChessSide.White) { lblNotation[18 + i].Text = Convert.ToString(9 - i); } else { lblNotation[18 + i].Text = Convert.ToString(i); } lblNotation[18 + i].Location = new Point(0, intcellsize * (i - 1) + _notationSize); panel1.Controls.Add(lblNotation[18 + i]); } lblNotation[27].Height = _notationSize; lblNotation[27].Width = _notationSize; lblNotation[27].BackColor = Color.Blue; lblNotation[27].Location = new Point(_notationSize + intcellsize * 8, _notationSize + intcellsize * 8); panel1.Controls.Add(lblNotation[27]); for (int i = 1; i <= 8; i++) { lblNotation[27 + i].Width = _notationSize; if (eownside == eChessSide.White) { lblNotation[27 + i].Text = Convert.ToString(9 - i); } else { lblNotation[27 + i].Text = Convert.ToString(i); } lblNotation[27 + i].Location = new Point(_notationSize + intcellsize * 8, intcellsize * (i - 1) + _notationSize); panel1.Controls.Add(lblNotation[27 + i]); } }
/// <summary> /// Play with computer given State /// </summary> /// <param name="ownside"></param> /// <param name="gamemode"></param> /// <param name="gamedifficulty"></param> /// <param name="arrState"></param> private void CreateChessBoard(eChessSide ownside, eGameMode gamemode, eGameDifficulty gamedifficulty, ChessState[,] arrState) { if (board != null) { board.CancelThinking(); board.Dispose(); ucCountDownTimer1.StopTimer(); ucCountDownTimer2.StopTimer(); ucCountDownTimer1.Min = _timeLimit; ucCountDownTimer2.Min = _timeLimit; ucCountDownTimer1.Sec = 0; ucCountDownTimer2.Sec = 0; } else { ucChessPieceEated1 = new Uc_ChessPieceEated(ownside); ucChessPieceEated2 = new Uc_ChessPieceEated(ownside); ucCountDownTimer1 = new Uc_CountDownTimer(); ucCountDownTimer2 = new Uc_CountDownTimer(); ucCountDownTimer1.StopTimer(); ucCountDownTimer2.StopTimer(); ucCountDownTimer1.Min = _timeLimit; ucCountDownTimer2.Min = _timeLimit; ucCountDownTimer1.Sec = 1; ucCountDownTimer2.Sec = 1; ucCountDownTimer1.TimeOut += new Uc_CountDownTimer.TimeOutHandler(UcCountDownTimer_TimeOut); ucCountDownTimer2.TimeOut += new Uc_CountDownTimer.TimeOutHandler(UcCountDownTimer_TimeOut); } this.SuspendLayout(); btn_Undo.Enabled = true; btn_Redo.Enabled = true; btn_Savegame.Enabled = true; btn_Savemoved.Enabled = true; panel1.Visible = true; panel1.Controls.Clear(); pnlTimerLeft.Controls.Clear(); pnlTimerRight.Controls.Clear(); pnlTimerRight.Visible = true; pnlTimerLeft.Visible = true; pnlTimerLeft.Controls.Add(this.ucCountDownTimer1); pnlTimerRight.Controls.Add(this.ucCountDownTimer2); ucCountDownTimer1.Size = new Size(110, 35); ucCountDownTimer2.Size = new Size(110, 35); ucCountDownTimer1.Location = new Point(8, 8); ucCountDownTimer2.Location = new Point(8, 8); pnlTimerLeft.Size = new Size(ucCountDownTimer1.Width + 16, ucCountDownTimer1.Height + 16); pnlTimerRight.Size = pnlTimerLeft.Size; Options obj = new Options(); board = new Uc_ChessBoard(obj.BoardStyle, obj.PieceStyle, ownside, gamemode, gamedifficulty, obj.CellSize, obj.PieceSize, obj.PlaySound, _language, arrState); board.MoveMaked += new Uc_ChessBoard.MoveMakedHandler(MoveMaked); if (board.GameMode == eGameMode.VsComputer) { btn_Hint.Enabled = true; } else { btn_Hint.Enabled = false; } Bitmap bmpBackImage = new Bitmap(board.Width, board.Height); board.DrawToBitmap(bmpBackImage, board.Bounds); board.BackgroundImage = bmpBackImage; board.BoardBitmap = bmpBackImage; _notationSize = (int)((obj.CellSize * 38) / 100); this._gameMode = gamemode; this._ownSide = ownside; AddNotation(obj.CellSize, ownside); board.Location = new Point(_notationSize, _notationSize); this.panel1.ClientSize = new Size(obj.CellSize * 8 + _notationSize * 2, obj.CellSize * 8 + _notationSize * 2); this.panel1.Controls.Add(board); this.MinimumSize = new Size(1000, 230 + obj.CellSize * 8); pnlTimerLeft.Location = new Point(8, 161); panel1.Location = new Point(pnlTimerLeft.Location.X + 2 + pnlTimerLeft.Width, 161); pnlTimerRight.Location = new Point(panel1.Location.X + 2 + panel1.Width, panel1.Location.Y + panel1.Height - pnlTimerRight.Height); //ChessPieceEated pnlChessPieceEated1.Controls.Clear(); pnlChessPieceEated2.Controls.Clear(); pnlChessPieceEated1.Visible = true; pnlChessPieceEated2.Visible = true; if (board.OwnSide == eChessSide.White) { ucChessPieceEated1.Side = eChessSide.White; pnlChessPieceEated1.BackColor = Color.White; ucChessPieceEated2.Side = eChessSide.Black; pnlChessPieceEated2.BackColor = Color.DimGray; } else { ucChessPieceEated1.Side = eChessSide.Black; pnlChessPieceEated1.BackColor = Color.DimGray; ucChessPieceEated2.Side = eChessSide.White; pnlChessPieceEated2.BackColor = Color.White; } pnlChessPieceEated1.Size = new Size(pnlTimerLeft.Size.Width - 45, (pnlTimerLeft.Width - 30) * 4); pnlChessPieceEated2.Size = new Size(pnlTimerRight.Size.Width - 45, (pnlTimerRight.Width - 30) * 4); pnlChessPieceEated1.Location = new Point(pnlTimerLeft.Location.X + pnlTimerLeft.Width - pnlChessPieceEated1.Width, pnlTimerLeft.Location.Y + pnlTimerLeft.Height + 10); pnlChessPieceEated2.Location = new Point(pnlTimerRight.Location.X, pnlTimerRight.Location.Y - pnlChessPieceEated2.Height - 10); ucChessPieceEated1.Size = new Size(pnlChessPieceEated1.Size.Width, pnlChessPieceEated1.Size.Height); ucChessPieceEated2.Size = new Size(pnlChessPieceEated2.Size.Width, pnlChessPieceEated2.Size.Height); pnlChessPieceEated1.Controls.Add(ucChessPieceEated1); pnlChessPieceEated2.Controls.Add(ucChessPieceEated2); ucChessPieceEated1.LoadChessPieces(board.stkChessPieceEated); ucChessPieceEated2.LoadChessPieces(board.stkChessPieceEated); if (board.OwnSide == eChessSide.White) { ucCountDownTimer2.StartTimer(); } else { ucCountDownTimer1.StartTimer(); } this.ResumeLayout(); }
public static ArrayList FindAllPosibleMoves(ChessState[,] arrState, Point pos, eChessSide ownSide) { eChessSide side = arrState[pos.X, pos.Y].Side; ArrayList arrMove = new ArrayList(); if (side == ownSide) { //First Move if (arrState[pos.X, pos.Y].Moves == 0) { if (arrState[pos.X - 2, pos.Y].Type == eChessPieceType.Null && arrState[pos.X - 1, pos.Y].Type == eChessPieceType.Null) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X - 2, pos.Y), eMove.Moving, arrState[pos.X, pos.Y].Moves)); } } //Move ahead if (arrState[pos.X - 1, pos.Y].Type == eChessPieceType.Null) { if (pos.X == 2) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X - 1, pos.Y), eMove.Promotion, arrState[pos.X, pos.Y].Moves)); } else { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X - 1, pos.Y), eMove.Moving, arrState[pos.X, pos.Y].Moves)); } } //Eat diagonal if (arrState[pos.X - 1, pos.Y - 1].Side != side && arrState[pos.X - 1, pos.Y - 1].Type > 0) { if (pos.X == 2) { //Both Eating and Promotion arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X - 1, pos.Y - 1), eMove.Promotion, new Point(pos.X - 1, pos.Y - 1), arrState[pos.X - 1, pos.Y - 1].Type, arrState[pos.X - 1, pos.Y - 1].Moves, arrState[pos.X, pos.Y].Moves)); } else { //Just Eating arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X - 1, pos.Y - 1), eMove.Eating, new Point(pos.X - 1, pos.Y - 1), arrState[pos.X - 1, pos.Y - 1].Type, arrState[pos.X - 1, pos.Y - 1].Moves, arrState[pos.X, pos.Y].Moves)); } } if (arrState[pos.X - 1, pos.Y + 1].Side != side && arrState[pos.X - 1, pos.Y + 1].Type > 0) { if (pos.X == 2) { //Both Eating and Promotion arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X - 1, pos.Y + 1), eMove.Promotion, new Point(pos.X - 1, pos.Y + 1), arrState[pos.X - 1, pos.Y + 1].Type, arrState[pos.X - 1, pos.Y + 1].Moves, arrState[pos.X, pos.Y].Moves)); } else { //Just Eating arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X - 1, pos.Y + 1), eMove.Eating, new Point(pos.X - 1, pos.Y + 1), arrState[pos.X - 1, pos.Y + 1].Type, arrState[pos.X - 1, pos.Y + 1].Moves, arrState[pos.X, pos.Y].Moves)); } } //En passant if (pos.X == 4 && Uc_ChessBoard.PositionLastMove.X == 2) { if (arrState[pos.X - 1, pos.Y - 1].Type == eChessPieceType.Null && arrState[pos.X, pos.Y - 1].Side != side && arrState[pos.X, pos.Y - 1].Type == eChessPieceType.Pawn) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X - 1, pos.Y - 1), eMove.EnPassant, new Point(pos.X, pos.Y - 1), arrState[pos.X, pos.Y - 1].Type, arrState[pos.X, pos.Y - 1].Moves, arrState[pos.X, pos.Y].Moves)); } if (arrState[pos.X - 1, pos.Y + 1].Type == eChessPieceType.Null && arrState[pos.X, pos.Y - 1].Side != side && arrState[pos.X, pos.Y + 1].Type == eChessPieceType.Pawn) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X - 1, pos.Y + 1), eMove.EnPassant, new Point(pos.X, pos.Y + 1), arrState[pos.X, pos.Y + 1].Type, arrState[pos.X, pos.Y + 1].Moves, arrState[pos.X, pos.Y].Moves)); } } } else { //First Move if (arrState[pos.X, pos.Y].Moves == 0) { if (arrState[pos.X + 2, pos.Y].Type == eChessPieceType.Null && arrState[pos.X + 1, pos.Y].Type == eChessPieceType.Null) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X + 2, pos.Y), eMove.Moving, arrState[pos.X, pos.Y].Moves)); } } //Move ahead if (arrState[pos.X + 1, pos.Y].Type == eChessPieceType.Null) { if (pos.X == 7) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X + 1, pos.Y), eMove.Promotion, arrState[pos.X, pos.Y].Moves)); } else { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X + 1, pos.Y), eMove.Moving, arrState[pos.X, pos.Y].Moves)); } } //Eat diagonal if (arrState[pos.X + 1, pos.Y - 1].Side != side && arrState[pos.X + 1, pos.Y - 1].Type > 0) { if (pos.X == 7) { //Both Eating and Promotion arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X + 1, pos.Y - 1), eMove.Promotion, new Point(pos.X + 1, pos.Y - 1), arrState[pos.X + 1, pos.Y - 1].Type, arrState[pos.X + 1, pos.Y - 1].Moves, arrState[pos.X, pos.Y].Moves)); } else { //Just Eating arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X + 1, pos.Y - 1), eMove.Eating, new Point(pos.X + 1, pos.Y - 1), arrState[pos.X + 1, pos.Y - 1].Type, arrState[pos.X + 1, pos.Y - 1].Moves, arrState[pos.X, pos.Y].Moves)); } } if (arrState[pos.X + 1, pos.Y + 1].Side != side && arrState[pos.X + 1, pos.Y + 1].Type > 0) { if (pos.X == 7) { //Both Eating and Promotion arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X + 1, pos.Y + 1), eMove.Promotion, new Point(pos.X + 1, pos.Y + 1), arrState[pos.X + 1, pos.Y + 1].Type, arrState[pos.X + 1, pos.Y + 1].Moves, arrState[pos.X, pos.Y].Moves)); } else { //Just Eating arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X + 1, pos.Y + 1), eMove.Eating, new Point(pos.X + 1, pos.Y + 1), arrState[pos.X + 1, pos.Y + 1].Type, arrState[pos.X + 1, pos.Y + 1].Moves, arrState[pos.X, pos.Y].Moves)); } } //En passant if (pos.X == 5 && Uc_ChessBoard.PositionLastMove.X == 7) { if (arrState[pos.X + 1, pos.Y - 1].Type == eChessPieceType.Null && arrState[pos.X, pos.Y - 1].Side != side && arrState[pos.X, pos.Y - 1].Type == eChessPieceType.Pawn) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X + 1, pos.Y - 1), eMove.EnPassant, new Point(pos.X, pos.Y - 1), arrState[pos.X, pos.Y - 1].Type, arrState[pos.X, pos.Y - 1].Moves, arrState[pos.X, pos.Y].Moves)); } if (arrState[pos.X + 1, pos.Y + 1].Type == eChessPieceType.Null && arrState[pos.X, pos.Y + 1].Side != side && arrState[pos.X, pos.Y + 1].Type == eChessPieceType.Pawn) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos.X + 1, pos.Y + 1), eMove.EnPassant, new Point(pos.X, pos.Y + 1), arrState[pos.X, pos.Y + 1].Type, arrState[pos.X, pos.Y + 1].Moves, arrState[pos.X, pos.Y].Moves)); } } } return(arrMove); }
private void btn_Options_Click(object sender, EventArgs e) { if (this._computerMoving == true) { return; } Form_Options form_temp = new Form_Options(this._language); if (form_temp.ShowDialog() == DialogResult.OK && board != null) { //Save this game ChessState[,] SaveThisState = new ChessState[10, 10]; int i, j; for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { SaveThisState[i, j] = new ChessState(board.arrState[i, j].Type, board.arrState[i, j].Side, board.arrState[i, j].Moves); } } eGameStatus gameStatus = board.GameStatus; eGameEndReason gameStatusReason = board.GameStatusReason; eChessSide whoTurn = board.WhoTurn; ArrayList arrWhoCheck = board.arrWhoCheck; Stack <ChessMove> stkUndo = board.stkUndo; Stack <ChessMove> stkRedo = board.stkRedo; Stack <String> stkChessMoveString = board.stkChessMoveString; Stack <Bitmap_Side_ChessPieceEated> stkChessPieceEated = board.stkChessPieceEated; bool clearStackRedo = board.clear_Stack_Redo; Point positionLastMove = new Point(Uc_ChessBoard.PositionLastMove.X, Uc_ChessBoard.PositionLastMove.Y); Point positionChoosen = new Point(board.PositionChoosen.X, board.PositionChoosen.Y); Dictionary <string, int> arrPosition = board.arrPosition; if (this._gameMode == eGameMode.VsComputer) { CreateChessBoard(this._ownSide, this._gameMode, this._gameDifficulty, SaveThisState); } else { CreateChessBoard(this._ownSide, this._gameMode, SaveThisState); } //Unload this game board.GameStatus = gameStatus; board.GameStatusReason = gameStatusReason; board.WhoTurn = whoTurn; board.arrWhoCheck = arrWhoCheck; board.stkUndo = stkUndo; board.stkRedo = stkRedo; board.stkChessMoveString = stkChessMoveString; board.clear_Stack_Redo = clearStackRedo; Uc_ChessBoard.PositionLastMove = new Point(positionLastMove.X, positionLastMove.Y); board.PositionChoosen = new Point(positionChoosen.X, positionChoosen.Y); board.arrPosition = arrPosition; board.stkChessPieceEated = stkChessPieceEated; ucChessPieceEated1.LoadChessPieces(board.stkChessPieceEated); ucChessPieceEated2.LoadChessPieces(board.stkChessPieceEated); } }
public ChessState(eChessPieceType Type, eChessSide side, int moves) { this._type = Type; this._side = side; this.moves = moves; }
public ChessState(eChessPieceType Type, eChessSide side) { this._type = Type; this._side = side; this.moves = 0; }
/// <summary> /// Sort arrCanMoves to make Alpha Beta more faster /// </summary> /// <param name="arrState"></param> /// <param name="arrcanmoves"></param> /// <param name="side"></param> public static ArrayList SortChessMoves(ChessState[,] arrState, ArrayList arrcanmoves, eChessSide side) { int i; ArrayList arrSort = new ArrayList(); if (arrcanmoves.Count == 0) { return(arrSort); } Score[] score = new Score[arrcanmoves.Count]; for (i = 0; i < arrcanmoves.Count; i++) { ChessMove m = (ChessMove)arrcanmoves[i]; score[i].value = (int)m.ChessPieceEated; score[i].position = i; } Array.Sort(score, (x, y) => x.value.CompareTo(y.value)); for (i = 0; i < arrcanmoves.Count; i++) { arrSort.Add((ChessMove)arrcanmoves[score[i].position]); } return(arrSort); }
public Bitmap_Side_ChessPieceEated(eChessSide side, Bitmap image) { this.Side = side; this.Image = image; }
/// <summary> /// Alpha beta fail hard version /// </summary> /// <param name="arrState"></param> /// <param name="arrposition"></param> /// <param name="depth"></param> /// <param name="side"></param> /// <param name="alpha"></param> /// <param name="beta"></param> /// <returns></returns> public static int AlphaBeta(ChessState[,] arrState, Dictionary <string, int> arrposition, int depth, eChessSide side, int alpha, int beta) { int bestScore = EvaluateChessBoard(arrState, side); if (depth == 0) { return(bestScore); } if (bestScore < _lowestScore) { return(bestScore); } bestScore = alpha; ChessMove bestMove = null; //Find all possible moves of this side => sort this to make alpha beta be faster ArrayList arrCanMoves = new ArrayList(); if (depth > 1) { arrCanMoves = SortChessMoves(arrState, FindAllPosibleMovesNotRule(arrState, side, _ownSide), side); } else { arrCanMoves = FindAllPosibleMovesNotRule(arrState, side, _ownSide); } //If no more moves if (arrCanMoves.Count == 0) { //opSide win if (Checked(arrState, _mySide, _ownSide) == true) { if (side == _mySide) { return(_lowestScore); } else { return(-_lowestScore); } } //mySide win if (Checked(arrState, _opSide, _ownSide) == true) { if (side == _mySide) { return(-_lowestScore); } else { return(_lowestScore); } } //Draw return(0); } while (arrCanMoves.Count > 0 && bestScore < beta) { //Pop a ChessMove ChessMove m = (ChessMove)arrCanMoves[arrCanMoves.Count - 1]; arrCanMoves.RemoveAt(arrCanMoves.Count - 1); //Compute new State after move PredictMove(arrState, m); //Add this Position string newPosition = MakePosition(arrState); if (arrposition.ContainsKey(newPosition)) { arrposition[newPosition]++; } else { arrposition.Add(newPosition, 1); } //Another side's move eChessSide newSide = eChessSide.Black; if (newSide == side) { newSide = eChessSide.White; } //Next move genneration int value = 0; if (arrposition[newPosition] < 3) { value = -AlphaBeta(arrState, arrposition, depth - 1, newSide, -beta, -alpha); } UnPredictMove(arrState, m); //Undo this Position if (arrposition[newPosition] > 1) { arrposition[newPosition] -= 1; } else { arrposition.Remove(newPosition); } //Update best if (value > bestScore) { bestScore = value; bestMove = m; if (depth == _maxDepth) { if (_myBestScore < bestScore) { _myBestScore = bestScore; _myBestMove = bestMove; } } if (value > alpha) { alpha = value; } if (value >= beta) { bestScore = beta; beta = bestScore - 1; } } } //_myBestMove = bestMove; return(bestScore); }
public Uc_ChessPieceEated(eChessSide ownside) { InitializeComponent(); this._ownSide = ownside; }
public static ArrayList FindAllPosibleMoves(ChessState[,] arrState, Point pos, eChessSide ownSide) { ArrayList arrMove = new ArrayList(); eChessSide side = arrState[pos.X, pos.Y].Side; Point[] How_To_Move = new Point[] { new Point(1, 1), new Point(1, -1), new Point(-1, 1), new Point(-1, -1), new Point(1, 0), new Point(0, 1), new Point(-1, 0), new Point(0, -1) }; bool[,] can_Moves = Chess_Engine.FindAllKingCanMove(arrState, side, ownSide); for (int i = 0; i < How_To_Move.Length; i++) { Point pos_temp = new Point(pos.X + How_To_Move[i].X, pos.Y + How_To_Move[i].Y); if (can_Moves[pos_temp.X, pos_temp.Y] == true) { if (arrState[pos_temp.X, pos_temp.Y].Type == eChessPieceType.Null) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos_temp.X, pos_temp.Y), eMove.Moving, arrState[pos.X, pos.Y].Moves)); } if (arrState[pos_temp.X, pos_temp.Y].Side != side && arrState[pos_temp.X, pos_temp.Y].Type > 0) { arrMove.Add(new ChessMove(new Point(pos.X, pos.Y), new Point(pos_temp.X, pos_temp.Y), eMove.Eating, new Point(pos_temp.X, pos_temp.Y), arrState[pos_temp.X, pos_temp.Y].Type, arrState[pos_temp.X, pos_temp.Y].Moves, arrState[pos.X, pos.Y].Moves)); } } } if (arrState[pos.X, pos.Y].Moves == 0) { //King Side castling if (arrState[pos.X, 6].Type == eChessPieceType.Null && arrState[pos.X, 7].Type == eChessPieceType.Null && arrState[pos.X, 8].Type == eChessPieceType.Rook && can_Moves[pos.X, 6] && can_Moves[pos.X, 7] && arrState[pos.X, 8].Moves == 0) { arrMove.Add(new ChessMove(new Point(pos.X, 5), new Point(pos.X, 7), eMove.Castling, arrState[pos.X, pos.Y].Moves)); } //Queen Side castling if (arrState[pos.X, 4].Type == eChessPieceType.Null && arrState[pos.X, 3].Type == eChessPieceType.Null && arrState[pos.X, 2].Type == eChessPieceType.Null && arrState[pos.X, 1].Type == eChessPieceType.Rook && can_Moves[pos.X, 4] && can_Moves[pos.X, 3] && arrState[pos.X, 1].Moves == 0) { arrMove.Add(new ChessMove(new Point(pos.X, 5), new Point(pos.X, 3), eMove.Castling, arrState[pos.X, pos.Y].Moves)); } //Promote Side castling if (pos.X == 8 && arrState[7, 5].Type == eChessPieceType.Null && arrState[6, 5].Type == eChessPieceType.Null && arrState[5, 5].Type == eChessPieceType.Null && arrState[4, 5].Type == eChessPieceType.Null && arrState[3, 5].Type == eChessPieceType.Null && arrState[2, 5].Type == eChessPieceType.Null && arrState[1, 5].Type == eChessPieceType.Rook && can_Moves[7, 5] && can_Moves[6, 5] && arrState[1, 5].Moves == 0) { arrMove.Add(new ChessMove(new Point(8, 5), new Point(6, 5), eMove.Castling, arrState[pos.X, pos.Y].Moves)); } if (pos.X == 1 && arrState[7, 5].Type == eChessPieceType.Null && arrState[6, 5].Type == eChessPieceType.Null && arrState[5, 5].Type == eChessPieceType.Null && arrState[4, 5].Type == eChessPieceType.Null && arrState[3, 5].Type == eChessPieceType.Null && arrState[2, 5].Type == eChessPieceType.Null && arrState[8, 5].Type == eChessPieceType.Rook && can_Moves[2, 5] && can_Moves[3, 5] && arrState[8, 5].Moves == 0) { arrMove.Add(new ChessMove(new Point(1, 5), new Point(3, 5), eMove.Castling, arrState[pos.X, pos.Y].Moves)); } } return(arrMove); }