Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function IsBlocked(currentCell As Range, destCell As Range) As Boolean
- 'Armazena as coordenadas de início e fim
- Dim startRow As Integer
- Dim startCol As Integer
- Dim endRow As Integer
- Dim endCol As Integer
- 'Define as coordenadas de início e fim
- If currentCell.Row < destCell.Row Then
- startRow = currentCell.Row + 1
- endRow = destCell.Row - 1
- Else
- startRow = destCell.Row + 1
- endRow = currentCell.Row - 1
- End If
- If currentCell.Column < destCell.Column Then
- startCol = currentCell.Column + 1
- endCol = destCell.Column - 1
- Else
- startCol = destCell.Column + 1
- endCol = currentCell.Column - 1
- End If
- 'Verifica se há peças no caminho
- For i = startRow To endRow
- For j = startCol To endCol
- If Cells(i, j).Value <> "" Then
- IsBlocked = True
- Exit Function
- End If
- Next j
- Next i
- End Function
- Sub MovePiece()
- 'Define as variáveis
- Dim currentCell As Range
- Dim destCell As Range
- 'Define a célula atual selecionada
- Set currentCell = Selection
- 'Pergunta ao usuário para selecionar a célula de destino
- Set destCell = Application.InputBox( _
- prompt:="Selecione a célula de destino:", _
- Type:=8)
- 'Verifica qual peça está sendo movida
- Select Case currentCell.Value
- Case ChrW(&H2657) 'Bispo branco
- 'Verifica se o bispo está se movendo na diagonal
- If Abs(destCell.Row - currentCell.Row) = Abs(destCell.Column - currentCell.Column) Then
- If Not IsBlocked(currentCell, destCell) Then
- 'Verifica se a célula de destino está vazia ou contém uma peça inimiga
- If destCell.Value = "" Or destCell.Value = ChrW(&H265C) Or destCell.Value = ChrW(&H265E) Or destCell.Value = ChrW(&H265D) Or destCell.Value = ChrW(&H265F) Or destCell.Value = ChrW(&H265A) Then
- 'Move o bispo
- destCell.Value = ChrW(&H2657)
- currentCell.Value = ""
- Else
- MsgBox "Movimento inválido"
- End If
- Else
- MsgBox "Movimento inválido"
- End If
- Case ChrW(&H2655) 'Rainha branca
- 'Verifica se a rainha está se movendo na diagonal, linha ou coluna
- If (currentCell.Row = destCell.Row And currentCell.Column <> destCell.Column) Or _
- (currentCell.Column = destCell.Column And currentCell.Row <> destCell.Row) Or _
- ((Math.Abs(rowDiff) = Math.Abs(colDiff)) And (currentCell.Row <> destCell.Row) And (currentCell.Column <> destCell.Column)) Then
- If Not IsBlocked(currentCell, destCell) Then
- If destCell.Value = "" Or destCell.Value = ChrW(&H265B) Or destCell.Value = ChrW(&H265D) Or destCell.Value = ChrW(&H265C) Or destCell.Value = ChrW(&H265E) Or destCell.Value = ChrW(&H265A) Then
- 'Move a rainha
- destCell.Value = ChrW(&H2655)
- currentCell.Value = ""
- 'remover a peça capturada da tabela
- ' ou adicionar ao seu contador de capturas
- Else
- MsgBox "Movimento inválido"
- End If
- Else
- MsgBox "Movimento inválido"
- End If
- Case ChrW(&H265D) 'Bispo preto
- 'Verifica se o bispo está se movendo na diagonal
- If Abs(destCell.Row - currentCell.Row) = Abs(destCell.Column - currentCell.Column) Then
- If Not IsBlocked(currentCell, destCell) Then
- 'Verifica se a célula de destino está vazia ou contém uma peça inimiga
- If destCell.Value = "" Or destCell.Value = ChrW(&H2657) Or destCell.Value = ChrW(&H2656) Or destCell.Value = ChrW(&H2658) Or destCell.Value = ChrW(&H2659) Or destCell.Value = ChrW(&H2654) Then
- 'Move o bispo
- destCell.Value = ChrW(&H265D)
- currentCell.Value = ""
- Else
- MsgBox "Movimento inválido"
- End If
- Else
- MsgBox "Movimento inválido"
- End If
- Case ChrW(&H265B) 'Rainha preta
- 'Verifica se a rainha está se movendo na diagonal, linha ou coluna
- If (currentCell.Row = destCell.Row And currentCell.Column <> destCell.Column) Or _
- (currentCell.Column = destCell.Column And currentCell.Row <> destCell.Row) Or _
- ((Math.Abs(rowDiff) = Math.Abs(colDiff)) And (currentCell.Row <> destCell.Row) And (currentCell.Column <> destCell.Column)) Then
- If Not IsBlocked(currentCell, destCell) Then
- 'Verifica se a célula de destino está vazia ou se tem uma peça do time oponente
- If destCell.Value = "" Or destCell.Value = ChrW(&H2654) Or destCell.Value = ChrW(&H2656) Or destCell.Value = ChrW(&H2655) Or destCell.Value = ChrW(&H2657) Or destCell.Value = ChrW(&H2659) Then
- 'Move a rainha
- destCell.Value = ChrW(&H265B)
- currentCell.Value = ""
- 'remover a peça capturada da tabela
- ' ou adicionar ao seu contador de capturas
- Else
- MsgBox "Movimento inválido"
- End If
- Else
- MsgBox "Movimento inválido"
- End If
- Case Else
- MsgBox "Peça inválida"
- Exit Sub
- End Select
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment