/// <summary> /// check whether the two king can face to face /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void checkKingFaceToFace(object sender, EventArgs e) { bool hasChessBetweenKing = false; if (jiang.GridY == shuai.GridY) { foreach (Control curCtr in this.panel1.Controls) { if (curCtr is BaseChess) { BaseChess tempChess = (BaseChess)curCtr; if (tempChess.GridY == jiang.GridY && tempChess != jiang && tempChess != shuai) { hasChessBetweenKing = true; break; } } } if (!hasChessBetweenKing) { showDangerInfo(); } } }
/// <summary> /// 根据棋子物体获取棋子对象实例 /// </summary> /// <param name="chess">棋子物体</param> /// <returns></returns> public static BaseChess GetChessInstance(GameObject chess) { Component[] components = chess.GetComponents <Component>(); Type type = components[2].GetType(); //默认第3个组件都是继承同一父类BaseChess的脚本 //Type baseType = type.BaseType; //其实这样也可以的,这个是参考反射的 BaseChess bc = chess.GetComponent(type) as BaseChess; //关键是这一句 return(bc); }
/// <summary> /// it's common from select chess piece, or attack another chess piece /// </summary> /// <param name="theChessPiece"></param> private void chessItemSelected(BaseChess theChessPiece) { if (_selectChess == null) { //第一次选择棋子 if (theChessPiece.Type == _currentActionType) { _selectChess = theChessPiece; //_previousChess = _selectChess; } else { //选择错了棋子 theChessPiece.IsChecked = false; } } else { //已经选择了一个棋子,再次选择时是对方棋子 if (theChessPiece.Type != _currentActionType) { BaseChess beAttackChess = theChessPiece; if (_selectChess.move(beAttackChess.Location)) { chessArray[_selectChess.GridY, _selectChess.GridX] = (byte)_selectChess.PieceType; chessArray[beAttackChess.GridY, beAttackChess.GridX] = 0; _previousOppositeChess = beAttackChess.Clone(); beAttackChess.remove(); string move = ChessUtils.getMoveString(_selectChess.GridX, _selectChess.GridY, _selectChess.PreviousGridX, _selectChess.PreviousGridY); _updateMovesStep(move, true); _updateFenStr(); doSomeAfterMove(); } else { //不遵循着法 beAttackChess.IsChecked = false; } } else { //如果再次选择时还是己方棋子,更改check状态 _selectChess.IsChecked = false; //更换当前引用 _selectChess = theChessPiece; } } }
/// <summary> /// check if the king can be defeat by other chess piece /// </summary> /// <param name="theKing"></param> /// <returns></returns> private bool isKingDangerFromOtherChess(King theKing) { foreach (Control curCtr in this.panel1.Controls) { if (curCtr is BaseChess) { BaseChess tempChess = (BaseChess)curCtr; ChessType oppositeType = _getOppositeType(theKing.Type); if (tempChess.Type == oppositeType) { if (tempChess.obeyTheLimit(theKing.GridX, theKing.GridY)) { showDangerInfo(); return(true); } } } } return(false); }
private void chessItem_MouseClick(object sender, EventArgs e) { BaseChess _tempChess = (BaseChess)sender; //if (_selectChess != null && _tempChess != _selectChess) //{ // _selectChess.IsChecked = false; //} if (_tempChess.Type == m_EngineType) { return; } chessItemSelected(_tempChess); //else //{ // _selectChess.IsChecked = false; // // _tempChess.IsChecked = false; //} }
private void UndoButton_Click(object sender, EventArgs e) { try { _changeType(); if (_previousChess != null) { _previousChess.GridX = _previousChess.PreviousGridX; _previousChess.GridY = _previousChess.PreviousGridY; _previousChess.InitChess(); } if (_previousOppositeChess != null) { //Type theType = _previousOppositeChess.GetType(); //object theChess = Activator.CreateInstance(theType); //BaseChess newChess = _previousOppositeChess.Clone(); //int index = this.panel1.Controls.GetChildIndex(_previousOppositeChess, false); //Pawn theBing = new Pawn(); //theBing.GridX = 6; //theBing.GridY = 4; //theBing.Type = ChessType.Black; //theBing.Text = "卒"; _previousOppositeChess.MouseClick += new MouseEventHandler(chessItem_MouseClick); this.panel1.Controls.Add(_previousOppositeChess); _previousOppositeChess = null; } if (dangerLabel.Visible) { dangerLabel.Visible = false; } UndoButton.Enabled = false; } catch (System.Exception ex) { MessageBox.Show(ex.ToString()); } }
/// <summary> /// get the best move from engine /// </summary> private void _executeBestMove() { int currentGridX, currentGridY, nextGridX, nextGridY; BaseChess _attackedChess = null; if (ChessUtils.getGridXY(bestMove, out currentGridX, out currentGridY, out nextGridX, out nextGridY)) { _selectChess = BaseChess.getChessOnPoint(panel1, currentGridX, currentGridY); _attackedChess = BaseChess.getChessOnPoint(panel1, nextGridX, nextGridY); if (_selectChess != null) { chessItemSelected(_selectChess); if (_attackedChess != null) { chessItemSelected(_attackedChess); } else { chessMove(nextGridX, nextGridY); } } } }
/// <summary> /// do something after chess move or attack /// </summary> private void doSomeAfterMove() { if (_currentActionType == m_PlayerType) { _sendPositionCommand(); _theEngineClient.SendGoCommand(5); _theEngineClient.BestMoveReceived += new BestMoveReceivedEventHandler(_theEngineClient_BestMoveReceived); } _updateFenStr(); //the type should not change before sending the position _changeType(); //两种将的方式都会显示将 checkKingFaceToFace(null, null); if (!isKingDangerFromOtherChess(jiang) && !isKingDangerFromOtherChess(shuai)) { dangerLabel.Visible = false; } string move = ""; UndoButton.Enabled = true; _previousChess = _selectChess; _selectChess.IsChecked = false; _selectChess = null; }