fitacola0113

Excel chess game

Feb 11th, 2023
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.59 KB | None | 0 0
  1. Function IsBlocked(currentCell As Range, destCell As Range) As Boolean
  2.     'Armazena as coordenadas de início e fim
  3.     Dim startRow As Integer
  4.     Dim startCol As Integer
  5.     Dim endRow As Integer
  6.     Dim endCol As Integer
  7.  
  8.     'Define as coordenadas de início e fim
  9.     If currentCell.Row < destCell.Row Then
  10.         startRow = currentCell.Row + 1
  11.         endRow = destCell.Row - 1
  12.     Else
  13.         startRow = destCell.Row + 1
  14.         endRow = currentCell.Row - 1
  15.     End If
  16.  
  17.     If currentCell.Column < destCell.Column Then
  18.         startCol = currentCell.Column + 1
  19.         endCol = destCell.Column - 1
  20.     Else
  21.         startCol = destCell.Column + 1
  22.         endCol = currentCell.Column - 1
  23.     End If
  24.  
  25.     'Verifica se há peças no caminho
  26.     For i = startRow To endRow
  27.         For j = startCol To endCol
  28.             If Cells(i, j).Value <> "" Then
  29.                 IsBlocked = True
  30.                 Exit Function
  31.             End If
  32.         Next j
  33.     Next i
  34. End Function
  35.  
  36.  
  37.  
  38. Sub MovePiece()
  39.  
  40. 'Define as variáveis
  41. Dim currentCell As Range
  42. Dim destCell As Range
  43.  
  44. 'Define a célula atual selecionada
  45. Set currentCell = Selection
  46.  
  47. 'Pergunta ao usuário para selecionar a célula de destino
  48. Set destCell = Application.InputBox( _
  49.     prompt:="Selecione a célula de destino:", _
  50.     Type:=8)
  51.  
  52. 'Verifica qual peça está sendo movida
  53. Select Case currentCell.Value
  54.  
  55.  
  56.  
  57. Case ChrW(&H2657) 'Bispo branco
  58. 'Verifica se o bispo está se movendo na diagonal
  59. If Abs(destCell.Row - currentCell.Row) = Abs(destCell.Column - currentCell.Column) Then
  60. If Not IsBlocked(currentCell, destCell) Then
  61.   'Verifica se a célula de destino está vazia ou contém uma peça inimiga
  62.   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
  63.     'Move o bispo
  64.     destCell.Value = ChrW(&H2657)
  65.     currentCell.Value = ""
  66.   Else
  67.     MsgBox "Movimento inválido"
  68.   End If
  69. Else
  70.   MsgBox "Movimento inválido"
  71. End If
  72.  
  73.  
  74. Case ChrW(&H2655) 'Rainha branca
  75.  
  76. 'Verifica se a rainha está se movendo na diagonal, linha ou coluna
  77. If (currentCell.Row = destCell.Row And currentCell.Column <> destCell.Column) Or _
  78. (currentCell.Column = destCell.Column And currentCell.Row <> destCell.Row) Or _
  79. ((Math.Abs(rowDiff) = Math.Abs(colDiff)) And (currentCell.Row <> destCell.Row) And (currentCell.Column <> destCell.Column)) Then
  80.  If Not IsBlocked(currentCell, destCell) Then
  81.  
  82. 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
  83. 'Move a rainha
  84. destCell.Value = ChrW(&H2655)
  85. currentCell.Value = ""
  86. 'remover a peça capturada da tabela
  87. ' ou adicionar ao seu contador de capturas
  88. Else
  89. MsgBox "Movimento inválido"
  90. End If
  91. Else
  92. MsgBox "Movimento inválido"
  93. End If
  94.  
  95.  
  96.  
  97. Case ChrW(&H265D) 'Bispo preto
  98. 'Verifica se o bispo está se movendo na diagonal
  99. If Abs(destCell.Row - currentCell.Row) = Abs(destCell.Column - currentCell.Column) Then
  100. If Not IsBlocked(currentCell, destCell) Then
  101. 'Verifica se a célula de destino está vazia ou contém uma peça inimiga
  102. 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
  103. 'Move o bispo
  104. destCell.Value = ChrW(&H265D)
  105. currentCell.Value = ""
  106. Else
  107. MsgBox "Movimento inválido"
  108. End If
  109. Else
  110. MsgBox "Movimento inválido"
  111. End If
  112.  
  113.  
  114. Case ChrW(&H265B) 'Rainha preta
  115.  
  116. 'Verifica se a rainha está se movendo na diagonal, linha ou coluna
  117. If (currentCell.Row = destCell.Row And currentCell.Column <> destCell.Column) Or _
  118. (currentCell.Column = destCell.Column And currentCell.Row <> destCell.Row) Or _
  119. ((Math.Abs(rowDiff) = Math.Abs(colDiff)) And (currentCell.Row <> destCell.Row) And (currentCell.Column <> destCell.Column)) Then
  120. If Not IsBlocked(currentCell, destCell) Then
  121. 'Verifica se a célula de destino está vazia ou se tem uma peça do time oponente
  122. 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
  123. 'Move a rainha
  124. destCell.Value = ChrW(&H265B)
  125. currentCell.Value = ""
  126. 'remover a peça capturada da tabela
  127. ' ou adicionar ao seu contador de capturas
  128. Else
  129. MsgBox "Movimento inválido"
  130. End If
  131. Else
  132. MsgBox "Movimento inválido"
  133. End If
  134.  
  135.  
  136.  
  137.  
  138.  
  139.    Case Else
  140.         MsgBox "Peça inválida"
  141.         Exit Sub
  142. End Select
  143.  
  144.  
  145. End Sub
  146.  
Advertisement
Add Comment
Please, Sign In to add comment