/// <summary> /// Create an alternate version of the board where a certain move was made. /// </summary> /// <param name="boardColor">The color of the player making the move.</param> /// <param name="move">The move.</param> /// <returns>The board as of the new move.</returns> /// <remarks>Useful for previewing and AI algorithms.</remarks> public Board CheckMove( BoardColors boardColor, Move move ) { // make sure it's a valid move if( !IsValidMove( boardColor, move ) ) { return null; } // create a copy of the existing board Board board = new Board( boardSize ); board.blackPieceCount = blackPieceCount; board.whitePieceCount = whitePieceCount; board.currentColor = currentColor; // array is of value type, so shallow is fine board.spaces = spaces.Clone() as BoardColors[ , ]; board.ApplyMove( boardColor, move ); return board; }
/// <summary> /// Apply a move to the board. /// </summary> /// <param name="boardColor">The color of the player making the move.</param> /// <param name="move">The move.</param> /// <remarks> /// The move is assumed to be valid before entering this function. /// </remarks> public void ApplyMove( BoardColors boardColor, Move move ) { // safety-check the player if( boardColor != currentColor ) { throw new InvalidOperationException( "Move made out of turn." ); } // set the new piece lastMove = move; this[ move.Row, move.Column ] = currentColor; // flip lines in all directions BoardColors otherColor = BoardColors.Black; switch( boardColor ) { case BoardColors.Black: blackPassed = false; otherColor = BoardColors.White; break; case BoardColors.White: whitePassed = false; otherColor = BoardColors.Black; break; } for( int directionRow = -1; directionRow <= 1; directionRow++ ) { for( int directionColumn = -1; directionColumn <= 1; directionColumn++ ) { // use the spaces array to check as it includes the illegal boundary int currentRow = move.Row + 1 + directionRow; int currentColumn = move.Column + 1 + directionColumn; while( spaces[ currentRow, currentColumn ] == otherColor ) { if( spaces[ currentRow + directionRow, currentColumn + directionColumn ] == currentColor ) { while( spaces[ currentRow, currentColumn ] == otherColor ) { this[ currentRow - 1, currentColumn - 1 ] = currentColor; currentRow -= directionRow; currentColumn -= directionColumn; } break; } else if( spaces[ currentRow + directionRow, currentColumn + directionColumn ] == otherColor ) { currentRow += directionRow; currentColumn += directionColumn; } else { break; } } } } // it's now the other player's turn currentColor = otherColor; }
/// <summary> /// Checks if a move is valid. /// </summary> /// <param name="boardColor">The color of the player making the move.</param> /// <param name="move">The move.</param> /// <returns>If true, the move is valid.</returns> public bool IsValidMove( BoardColors boardColor, Move move ) { // if it's not the right turn, it's not a valid move if( boardColor != currentColor ) { return false; } // if it's not empty, then it's never a possible move if( this[ move.Row, move.Column ] != BoardColors.Empty ) { return false; } // check for possible lines BoardColors otherColor = ( currentColor == BoardColors.White ) ? BoardColors.Black : BoardColors.White; for( int directionRow = -1; directionRow <= 1; directionRow++ ) { for( int directionColumn = -1; directionColumn <= 1; directionColumn++ ) { int currentRow = move.Row + 1 + directionRow; int currentColumn = move.Column + 1 + directionColumn; while( spaces[ currentRow, currentColumn ] == otherColor ) { if( spaces[ currentRow + directionRow, currentColumn + directionColumn ] == currentColor ) { return true; } else if( spaces[ currentRow + directionRow, currentColumn + directionColumn ] == otherColor ) { currentRow += directionRow; currentColumn += directionColumn; } else { break; } } } } return false; }
public override void Update( Board board ) { // safety check the parameter if( board == null ) { throw new ArgumentNullException( "board" ); } // quit now if it's not my turn if( board.CurrentColor != BoardColor ) { return; } // check if we have a valid move if( checkValidMove ) { if( board.HasValidMove( BoardColor ) ) { checkValidMove = false; // already checked, waiting for selection } else { board.Pass( BoardColor ); } } // if we don't have any input, don't bother if( inputState == null ) { return; } // apply selection-movement input and generate a desired movement Point cursorMovement = new Point( 0, 0 ); float degrees = (int)SimpleArcCamera.Rotation % 360 - 180; if( Math.Abs( degrees ) < 45 ) { if( inputState.IsPieceSelectionUp( playerIndex ) ) { cursorMovement.Y--; } if( inputState.IsPieceSelectionDown( playerIndex ) ) { cursorMovement.Y++; } if( inputState.IsPieceSelectionLeft( playerIndex ) ) { cursorMovement.X--; } if( inputState.IsPieceSelectionRight( playerIndex ) ) { cursorMovement.X++; } } else if( ( degrees >= 45 ) && ( degrees < 135 ) ) { if( inputState.IsPieceSelectionUp( playerIndex ) ) { cursorMovement.X++; } if( inputState.IsPieceSelectionDown( playerIndex ) ) { cursorMovement.X--; } if( inputState.IsPieceSelectionLeft( playerIndex ) ) { cursorMovement.Y--; } if( inputState.IsPieceSelectionRight( playerIndex ) ) { cursorMovement.Y++; } } else if( Math.Abs( degrees ) >= 135 ) { if( inputState.IsPieceSelectionUp( playerIndex ) ) { cursorMovement.Y++; } if( inputState.IsPieceSelectionDown( playerIndex ) ) { cursorMovement.Y--; } if( inputState.IsPieceSelectionLeft( playerIndex ) ) { cursorMovement.X++; } if( inputState.IsPieceSelectionRight( playerIndex ) ) { cursorMovement.X--; } } else if( ( degrees > -135 ) && ( degrees <= -45 ) ) { if( inputState.IsPieceSelectionUp( playerIndex ) ) { cursorMovement.X--; } if( inputState.IsPieceSelectionDown( playerIndex ) ) { cursorMovement.X++; } if( inputState.IsPieceSelectionLeft( playerIndex ) ) { cursorMovement.Y++; } if( inputState.IsPieceSelectionRight( playerIndex ) ) { cursorMovement.Y--; } } // check for valid move and apply if( board.IsValidSpace( cursorPosition.X + cursorMovement.X, cursorPosition.Y + cursorMovement.Y ) ) { cursorPosition.X += cursorMovement.X; cursorPosition.Y += cursorMovement.Y; } // apply play-piece input if( inputState.IsPlayPiece( playerIndex ) ) { Move move = new Move( cursorPosition.X, cursorPosition.Y ); if( board.IsValidMove( BoardColor, move ) ) { board.ApplyMove( BoardColor, move ); checkValidMove = true; } else { AudioManager.PlayCue( "Drop_Illegal" ); } } }