napisz mi prostą gre saper
- User
napisz mi prostą gre saper
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
napisz mi prostą gre saper
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Oto prosty kod gry Saper w języku Python, który działa w terminalu:
import random
class Saper:
def __init__(self, width=10, height=10, mines=10):
self.width = width
self.height = height
self.mines = mines
self.board = [[' ' for _ in range(width)] for _ in range(height)]
self.revealed = [[False for _ in range(width)] for _ in range(height)]
self.flags = [[False for _ in range(width)] for _ in range(height)]
self.game_over = False
self.place_mines()
self.calculate_numbers()
def place_mines(self):
mines_placed = 0
while mines_placed < self.mines:
x = random.randint(0, self.width - 1)
y = random.randint(0, self.height - 1)
if self.board[y][x] != '*':
self.board[y][x] = '*'
mines_placed += 1
def calculate_numbers(self):
for y in range(self.height):
for x in range(self.width):
if self.board[y][x] == '*':
continue
count = 0
for dy in [-1, 0, 1]:
for dx in [-1, 0, 1]:
if dx == 0 and dy == 0:
continue
nx, ny = x + dx, y + dy
if 0 <= nx < self.width and 0 <= ny < self.height:
if self.board[ny][nx] == '*':
count += 1
if count > 0:
self.board[y][x] = str(count)
def reveal(self, x, y):
if self.game_over or self.revealed[y][x] or self.flags[y][x]:
return
if self.board[y][x] == '*':
self.game_over = True
return
self.revealed[y][x] = True
if self.board[y][x] == ' ':
for dy in [-1, 0, 1]:
for dx in [-1, 0, 1]:
if dx == 0 and dy == 0:
continue
nx, ny = x + dx, y + dy
if 0 <= nx < self.width and 0 <= ny < self.height:
self.reveal(nx, ny)
def toggle_flag(self, x, y):
if self.game_over or self.revealed[y][x]:
return
self.flags[y][x] = not self.flags[y][x]
def check_win(self):
for y in range(self.height):
for x in range(self.width):
if not self.revealed[y][x] and self.board[y][x] != '*':
return False
return True
def display(self):
print(" " + " ".join(str(i).rjust(2) for i in range(self.width)))
print(" +" + "-" * (self.width * 2 + 1) + "+")
for y in range(self.height):
print(str(y).rjust(2) + "|", end="")
for x in range(self.width):
if self.revealed[y][x]:
print(self.board[y][x].rjust(2), end="")
elif self.flags[y][x]:
print("F".rjust(2), end="")
else:
print(".".rjust(2), end="")
print("|")
print(" +" + "-" * (self.width * 2 + 1) + "+")
def main():
print("Witaj w grze Saper!")
print("Podaj współrzędne w formacie 'x y' (np. '3 5')")
print("Aby postawić flagę, dodaj 'f' na końcu (np. '3 5 f')")
game = Saper()
while not game.game_over:
game.display()
if game.check_win():
print("Gratulacje! Wygrałeś!")
break
try:
command = input("Twój ruch: ").strip().split()
if len(command) < 2:
print("Nieprawidłowe polecenie. Spróbuj ponownie.")
continue
x = int(command[0])
y = int(command[1])
if x < 0 or x >= game.width or y < 0 or y >= game.height:
print("Współrzędne poza planszą. Spróbuj ponownie.")
continue
if len(command) == 3 and command[2].lower() == 'f':
game.toggle_flag(x, y)
else:
game.reveal(x, y)
except ValueError:
print("Nieprawidłowe współrzędne. Spróbuj ponownie.")
if game.game_over:
game.display()
print("Koniec gry! Trafiłeś na minę!")
if __name__ == "__main__":
main()
Możesz dostosować rozmiar planszy i liczbę min, zmieniając parametry w konstruktorze Saper(width, height, mines).