// applying move, returning whether a capture move happend private eAction applyMove(Move? i_Move) { eAction nextAction = eAction.Continue; m_LastMovingPiece = getPieceByPosition(m_CurrPlayerTurn, i_Move.Value.Source); Position capturedPiecePosition; // check if move is a capture move, output parameter get the captured piece position if (captureMove(m_LastMovingPiece, i_Move.Value.Destination, out capturedPiecePosition)) { PlayerInfo enemyPlayer = this.enemyPlayer(m_CurrPlayerTurn); removePieceFromPosition(enemyPlayer, capturedPiecePosition); nextAction = eAction.Capture; } // updating movement m_LastMovingPiece.MovePiece(i_Move.Value.Destination, m_CheckersBoard); return nextAction; }
public void PlayRound(Move i_MoveToApply) { if (isMoveValid(i_MoveToApply)) { const bool v_OnlyCaptureMoves = true; eAction lastAction = applyMove(i_MoveToApply); r_CurrentlyLegalMoves.Clear(); m_LastMovingPiece.AddLegalMovesToList(r_CurrentlyLegalMoves, m_CheckersBoard, v_OnlyCaptureMoves); if (lastAction == eAction.Capture && r_CurrentlyLegalMoves.Any()) { // the player keeping the turn, but we update that it will not be the first move, as in series of capture moves m_FirstMoveThisTurn = false; } else { // turn goes to the next player m_CurrPlayerTurn = enemyPlayer(m_CurrPlayerTurn); m_FirstMoveThisTurn = true; if (!m_CurrPlayerTurn.Human) { makeMachineMove(); } } if(gameOver()) { handleGameOver(); } } else { OnInvalidMoveGiven(new EventArgs()); // show prompt dialog "Move is not Valid" } }
private bool isMoveValid(Move i_MoveToCheck) { const bool v_OnlyCaptureMoves = true; if(!m_FirstMoveThisTurn) { // in middle of a series of captures moves r_CurrentlyLegalMoves.Clear(); m_LastMovingPiece.AddLegalMovesToList(r_CurrentlyLegalMoves, m_CheckersBoard, v_OnlyCaptureMoves); } else { fillLegalMovesList(m_CurrPlayerTurn, v_OnlyCaptureMoves); // Try to find only capture moves if (!r_CurrentlyLegalMoves.Any()) { fillLegalMovesList(m_CurrPlayerTurn, !v_OnlyCaptureMoves); // get all possible moves } } return r_CurrentlyLegalMoves.Contains(i_MoveToCheck); }
// Goes over possible moves of a piece and adds it into the list if it's a valid move for this part of the round public void AddLegalMovesToList( LinkedList<Move> i_LegalMovesList, CheckersGameBoard i_CheckersGameBoard, bool i_OnlyCaptureMoves) { int maxIndexOfPossibleMove = m_King ? k_KingNumberOfPossibleMoves : k_ManNumberOfPossibleMoves; Position newPossiblePosition; bool captureMove; for (int i = 0; i < maxIndexOfPossibleMove; ++i) { newPossiblePosition = CurrPosition.AddPosition(r_PossibleMoves[i]); if (isMoveValid(newPossiblePosition, i_CheckersGameBoard, out captureMove) && ((i_OnlyCaptureMoves && captureMove) || !i_OnlyCaptureMoves)) { // Create a linkedList node and add it to the legal moves list - Source: currPosition, Dst: newLegalPosition Move tempMove = new Move(m_CurrPosition, newPossiblePosition); LinkedListNode<Move> tempNode = new LinkedListNode<Move>(tempMove); // If we only need capture moves and it's a capture move; or if we don't nessecarily need a capture move - add to list: i_LegalMovesList.AddLast(tempNode); } } }