void moveNextMove(bool _moveOneSide = false) { m_Missed = false; m_WhiteTurn = !m_WhiteTurn; m_Board.move(m_CurLine.moveList[m_CurMoveIdx++]); UpdateInfos(); if (m_Board.isChecked(m_Board.whiteTurnToMove)) { AudioSource.PlayClipAtPoint(Check_Sound, Vector3.zero, 1.0f); } else { AudioSource.PlayClipAtPoint(MovePiece_Sound, Vector3.zero, 1.0f); } if (checkEndLine()) { return; } Invoke("moveComputerMove", 1.5f); }
public void OnPieceMove(cgSimpleMove _move) { isChanged = true; Btn_Save.enabled = true; if (curMove < moveList.Count) { moveList.RemoveRange(curMove, moveList.Count - curMove); } moveList.Add(_move); board.move(_move); if (board.isChecked(board.whiteTurnToMove)) { AudioSource.PlayClipAtPoint(Check_Sound, Vector3.zero, 1.0f); } else { AudioSource.PlayClipAtPoint(MovePiece_Sound, Vector3.zero, 1.0f); } curMove++; UpdateInfos(); Wnd_Board.UpdateBoard(); }
/// <summary> /// Check if the game is over, should be performed after every move. /// </summary> private void _checkGameOver() { bool isChecked = _abstractBoard.isChecked(whiteTurnToMove); List <cgSimpleMove> responses = _abstractBoard.findLegalMoves(_abstractBoard.whiteTurnToMove); for (int i = responses.Count; i > 0; i--) { if (!_abstractBoard.verifyLegality(responses[i - 1])) { responses.RemoveAt(i - 1); continue; } } if (responses.Count == 0 && isChecked) { _gameOver(!whiteTurnToMove, whiteTurnToMove); if ((_humanPlayerIsBlack && whiteTurnToMove) || (_humanPlayerIsWhite && !whiteTurnToMove)) { playSound(winSound); } else { playSound(loseSound); } } else if (isChecked) { playSound(checkSound); } else if (responses.Count == 0 && !isChecked) { //Draw by stalemate - no legal moves available to player whose turn it is to move. _gameOver(false, false); playSound(loseSound); } }
/// <summary> /// Check if the game is over, should be called after every move. /// </summary> private void _checkGameOver() { bool isChecked = _abstractBoard.isChecked(whiteTurnToMove); List <cgSimpleMove> responses = _abstractBoard.findLegalMoves(_abstractBoard.whiteTurnToMove); for (int i = responses.Count; i > 0; i--) { if (!_abstractBoard.verifyLegality(responses[i - 1])) { responses.RemoveAt(i - 1); } } if (responses.Count == 0 && isChecked) { //Checkmate. _gameOver(!whiteTurnToMove, whiteTurnToMove); if ((_humanPlayerIsBlack && whiteTurnToMove) || (_humanPlayerIsWhite && !whiteTurnToMove)) { playSound(winSound); } else { playSound(loseSound); } } else if (isChecked) { //Checked but not checkmate. playSound(checkSound); } else if (responses.Count == 0 && !isChecked) { //Draw by stalemate - no legal moves available to player whose turn it is to move. _gameOver(false, false); playSound(loseSound); } else { //Test Draw by material insuffience //Collect living non-king pieces, to test draw by material insuffience bool blackBishopsEvenSquares = false; bool blackBishopsUnevenSquares = false; bool blackKnights = false; bool whiteBishopsEvenSquares = false; bool whiteBishopsUnevenSquares = false; bool whiteKnights = false; bool drawByMaterial = false; bool hasEnoughOtherMaterial = false; for (byte i = 0; i < _abstractBoard.squares.Count; i++) { sbyte sb = _abstractBoard.squares[i]; if (sb != 0 && Math.Abs(sb) != 6)//Count everything but empty squares and king pieces. { if (sb == 3) { whiteKnights = true; } else if (sb == 4) { if (i / 2 == 0) { whiteBishopsEvenSquares = true; } else { whiteBishopsUnevenSquares = true; } } else if (sb == -3) { blackKnights = true; } else if (sb == 4) { if (i / 2 == 0) { blackBishopsEvenSquares = true; } else { blackBishopsUnevenSquares = true; } } else //pawn, queen or rook is alive, no need to test further material insuffience { hasEnoughOtherMaterial = true; break; } } } if (!hasEnoughOtherMaterial) { //White bishop on even square only piece left and possibly other black bishops on even square(which doesnt matter.) if (whiteBishopsEvenSquares && !whiteBishopsUnevenSquares && !blackBishopsUnevenSquares && !blackKnights && !whiteKnights) { drawByMaterial = true; } //White bishop on uneven square left and possibly other black bishops on uneven square(which doesnt matter.) else if (whiteBishopsUnevenSquares && !whiteBishopsEvenSquares && !blackBishopsEvenSquares && !blackKnights && !whiteKnights) { drawByMaterial = true; } //black bishop on even square only piece left else if (blackBishopsEvenSquares && !whiteBishopsEvenSquares && !whiteBishopsUnevenSquares && !blackBishopsUnevenSquares && !blackKnights && !whiteKnights) { drawByMaterial = true; } //black bishop on uneven square only piece left. else if (blackBishopsUnevenSquares && !whiteBishopsEvenSquares && !whiteBishopsUnevenSquares && !blackBishopsEvenSquares && !blackKnights && !whiteKnights) { drawByMaterial = true; } //black knight only piece left. else if (blackKnights && !whiteBishopsEvenSquares && !whiteBishopsUnevenSquares && !blackBishopsEvenSquares && !blackBishopsUnevenSquares && !whiteKnights) { drawByMaterial = true; } //white knight only piece left. else if (whiteKnights && !blackKnights && !whiteBishopsUnevenSquares && !blackBishopsEvenSquares && !blackBishopsUnevenSquares && !blackKnights) { drawByMaterial = true; } } if (drawByMaterial) { _gameOver(false, false); playSound(loseSound); } } }