gra w sapera w vba
- User
gra w sapera w vba
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
gra w sapera w vba
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Oto prosty kod do gry w Saper (Minesweeper) napisany w VBA dla Excela. Możesz go umieścić w module VBA w edytorze Visual Basic (Alt+F11).
' Moduł główny gry Saper
Option Explicit
' Zmienne globalne
Dim Pole() As Integer ' 0 - puste, -1 - bomba, >0 - liczba bomb wokół
Dim Odkryte() As Boolean
Dim Flagi() As Boolean
Dim Szerokosc As Integer, Wysokosc As Integer
Dim LiczbaBomb As Integer
Dim CzasStart As Double
Dim CzasTrwania As Double
Dim GraTrwa As Boolean
' Inicjalizacja gry
Sub InicjujGre()
Szerokosc = 10
Wysokosc = 10
LiczbaBomb = 15
GraTrwa = True
ReDim Pole(Szerokosc, Wysokosc)
ReDim Odkryte(Szerokosc, Wysokosc)
ReDim Flagi(Szerokosc, Wysokosc)
' Czyszczenie planszy
For Dim x As Integer = 1 To Szerokosc
For Dim y As Integer = 1 To Wysokosc
Pole(x, y) = 0
Odkryte(x, y) = False
Flagi(x, y) = False
Next y
Next x
' Umieszczanie bomb
Dim Bomby As Integer
Bomby = 0
While Bomby < LiczbaBomb
Dim x As Integer, y As Integer
x = Int((Szerokosc) * Rnd + 1)
y = Int((Wysokosc) * Rnd + 1)
If Pole(x, y) <> -1 Then
Pole(x, y) = -1
Bomby = Bomby + 1
End If
Wend
' Obliczanie liczb wokół bomb
For Dim x As Integer = 1 To Szerokosc
For Dim y As Integer = 1 To Wysokosc
If Pole(x, y) = -1 Then
For Dim dx As Integer = -1 To 1
For Dim dy As Integer = -1 To 1
If dx = 0 And dy = 0 Then GoTo Skip
Dim nx As Integer, ny As Integer
nx = x + dx
ny = y + dy
If nx >= 1 And nx <= Szerokosc And ny >= 1 And ny <= Wysokosc Then
If Pole(nx, ny) <> -1 Then
Pole(nx, ny) = Pole(nx, ny) + 1
End If
End If
Skip:
Next dy
Next dx
End If
Next y
Next x
' Rysowanie planszy
RysujPlansze
' Uruchomienie timera
CzasStart = Timer
Application.OnTime Now + TimeValue("00:00:01"), "AktualizujCzas"
End Sub
' Rysowanie planszy
Sub RysujPlansze()
Dim arkusz As Worksheet
Set arkusz = ActiveSheet
' Czyszczenie poprzedniej planszy
arkusz.Cells.Clear
' Rysowanie nagłówka
arkusz.Cells(1, 1).Value = "SAPER"
arkusz.Cells(1, 1).Font.Bold = True
arkusz.Cells(1, 1).Font.Size = 16
' Rysowanie planszy
For Dim x As Integer = 1 To Szerokosc
For Dim y As Integer = 1 To Wysokosc
Dim komorka As Range
Set komorka = arkusz.Cells(y + 2, x + 1)
' Formatowanie komórki
With komorka
.Borders.Weight = xlThin
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Font.Bold = True
.ColumnWidth = 4
.RowHeight = 20
' Kolorowanie komórki
If Odkryte(x, y) Then
If Pole(x, y) = -1 Then
.Interior.Color = RGB(255, 0, 0) ' Czerwony dla bomby
.Value = "B"
ElseIf Pole(x, y) > 0 Then
.Value = Pole(x, y)
Select Case Pole(x, y)
Case 1: .Font.Color = RGB(0, 0, 255) ' Niebieski
Case 2: .Font.Color = RGB(0, 128, 0) ' Zielony
Case 3: .Font.Color = RGB(255, 0, 0) ' Czerwony
Case 4: .Font.Color = RGB(0, 0, 128) ' Ciemnoniebieski
Case 5: .Font.Color = RGB(128, 0, 0) ' Ciemnoczerwony
Case 6: .Font.Color = RGB(0, 128, 128) ' Turkusowy
Case 7: .Font.Color = RGB(0, 0, 0) ' Czarny
Case 8: .Font.Color = RGB(128, 128, 128) ' Szary
End Select
Else
.Interior.Color = RGB(200, 200, 200) ' Szary dla pustych pól
End If
Else
If Flagi(x, y) Then
.Value = "F"
.Font.Color = RGB(255, 0, 0) ' Czerwony dla flagi
Else
.Value = ""
.Interior.Color = RGB(150, 150, 150) ' Jasnoszary dla nieodkrytych
End If
End If
End With
Next y
Next x
' Wyświetlenie informacji o grze
arkusz.Cells(Wysokosc + 3, 1).Value = "Czas: " & Format(CzasTrwania, "0.0") & " s"
arkusz.Cells(Wysokosc + 4, 1).Value = "Pozostałe bomby: " & LiczbaBomb - IleFlag()
End Sub
' Obsługa kliknięcia na komórkę
Sub KliknijKomorke(x As Integer, y As Integer, przycisk As Integer)
If Not GraTrwa Then Exit Sub
' Lewy przycisk - odkrycie pola
If przycisk = 1 Then
If Flagi(x, y) Then Exit Sub ' Nie można odkryć pola z flagą
If Pole(x, y) = -1 Then
' Przegrana
Odkryte(x, y) = True
RysujPlansze
MsgBox "Przegrałeś! Spróbuj ponownie.", vbExclamation
GraTrwa = False
Else
OdkryjPole x, y
If SprawdzWygrana() Then
' Wygrana
RysujPlansze
MsgBox "Gratulacje! Wygrywasz!", vbInformation
GraTrwa = False
End If
End If
' Prawy przycisk - postawienie flagi
ElseIf przycisk = 2 Then
If Not Odkryte(x, y) Then
Flagi(x, y) = Not Flagi(x, y)
End If
End If
RysujPlansze
End Sub
' Odkrywanie pola i sąsiednich pól
Sub OdkryjPole(x As Integer, y As Integer)
If x < 1 Or x > Szerokosc Or y < 1 Or y > Wysokosc Then Exit Sub
If Odkryte(x, y) Then Exit Sub
Odkryte(x, y) = True
' Jeśli pole jest puste, odkryj sąsiednie pola
If Pole(x, y) = 0 Then
For Dim dx As Integer = -1 To 1
For Dim dy As Integer = -1 To 1
If dx = 0 And dy = 0 Then GoTo Skip2
OdkryjPole x + dx, y + dy
Skip2:
Next dy
Next dx
End If
End Sub
' Sprawdzanie warunku zwycięstwa
Function SprawdzWygrana() As Boolean
For Dim x As Integer = 1 To Szerokosc
For Dim y As Integer = 1 To Wysokosc
If Pole(x, y) <> -1 And Not Odkryte(x, y) Then
SprawdzWygrana = False
Exit Function
End If
Next y
Next x
SprawdzWygrana = True
End Function
' Liczenie postawionych flag
Function IleFlag() As Integer
Dim licznik As Integer
licznik = 0
For Dim x As Integer = 1 To Szerokosc
For Dim y As Integer = 1 To Wysokosc
If Flagi(x, y) Then licznik = licznik + 1
Next y
Next x
IleFlag = licznik
End Function
' Aktualizacja czasu
Sub AktualizujCzas()
If GraTrwa Then
CzasTrwania = Timer - CzasStart
RysujPlansze
Application.OnTime Now + TimeValue("00:00:01"), "AktualizujCzas"
End If
End Sub
' Makro do uruchomienia gry
Sub UruchomGre()
InicjujGre
End Sub
Możesz dostosować rozmiar planszy i liczbę bomb, zmieniając wartości zmiennych Szerokosc, Wysokosc i LiczbaBomb w procedurze InicjujGre.
Gra zawiera podstawowe funkcje Sapera: odkrywanie pól, flagowanie bomb, licznik czasu i sprawdzanie warunku zwycięstwa.