Kod
kodzikunknown
python
3 years ago
5.7 kB
12
Indexable
import random
import shelve
import sys
import math
import shelve
import json
beczki = 5
sonarDevices = 20
def getNewBoard():
board = []
for x in range(60):
board.append([])
for y in range(15):
if random.randint(0, 1) == 0:
board[x].append('~')
else:
board[x].append('`')
return board
def drawBoard(board):
tensDigitsLine = ' '
for i in range(1, 6):
tensDigitsLine += (' ' * 9) + str(i)
print(tensDigitsLine)
print(' ' + ('0123456789' * 6))
print()
for row in range(15):
if row < 10:
extraSpace = ' '
else:
extraSpace = ''
boardRow = ''
for column in range(60):
boardRow += board[column][row]
print('%s%s %s %s' % (extraSpace, row, boardRow, row))
print()
print(' ' + ('0123456789' * 6))
print(tensDigitsLine)
def getRandomChests(numChests):
chests = []
while len(chests) < numChests:
newChest = [random.randint(0, 59), random.randint(0, 14)]
if newChest not in chests:
chests.append(newChest)
return chests
def isOnBoard(x, y):
return x >= 0 and x <= 59 and y >= 0 and y <= 14
def makeMove(board, chests, x, y):
smallestDistance = 100
for cx, cy in chests:
distance = math.sqrt((cx - x) * (cx - x) + (cy - y) * (cy - y))
if distance < smallestDistance:
smallestDistance = distance
smallestDistance = round(smallestDistance)
if smallestDistance == 0:
chests.remove([x, y])
return 'Znalazłeś beczkę piwa!'
else:
if smallestDistance < 10:
board[x][y] = str(smallestDistance)
return 'Beczka wykryta %s pól od sonaru.' % (smallestDistance)
else:
board[x][y] = 'X'
return 'Sonar nic nie wykrył.'
def enterPlayerMove(previousMoves):
print('Który obszar chcesz przeszukać? (0-59 0-14) (albo wpisz koniec)')
while True:
move = input()
if move.lower() == 'koniec':
print('Dziękuję za grę!')
sys.exit()
elif move.lower() == "instrukcja":
showInstructions()
move = move.split()
if len(move) == 2 and move[0].isdigit() and move[1].isdigit() and isOnBoard(int(move[0]), int(move[1])):
if [int(move[0]), int(move[1])] in previousMoves:
print('Już tutaj szukałeś')
continue
return [int(move[0]), int(move[1])]
print('Wpisz numer od 0 do 59, spacja, potem numer od 0 do 14.')
def chooseDifficulty():
global beczki
difficulty = int(input("Wybierz poziom trudności: \n1. łatwy\n2. średni\n3. trudny\n"))
if difficulty == 1:
beczki = 3
elif difficulty == 2:
beczki = 5
elif difficulty == 3:
beczki = 8
def showInstructions():
print('''Instrukcja:
Jesteś kapitanem Daru Młodzieży, największego poskiego żaglowca. Ty i grupka studentów wyruszyliście na głębokie morze, aby znaleźć zatopione beczki piwa. Niestety, ponieważ jesteście biednymi studentami, macie do dypozycji jedynie tani sonar, który wskazuje jedynie odległość od celu, a nie kierunek.
Wpisz koordynaty aby przeszukać dany obszar sonarem. Na mapie pojawi się odległość od beczki, lub X jeżeli żadna beczka nie będzie w zasięgu.
Aby wyłowić beczkę pysznego piwa (prawdopodobnie Harnasia) musisz trafić sonarem bezpośrednio w nią. Po wyłowieniu beczki sonar pokaże odległość do innych beczek
Beczki są zakotwiczone w dnie i się nie poruszają. Twój sonar ma zasięg 5. W sumie masz 20 użyć sonara. Powodzenia!
Wciśniej enter aby kontynuować.''')
input()
def runThegame():
global sonarDevices
print('Wyświetlić instrukcję? (tak/nie)')
if input().lower().startswith('t'):
showInstructions()
while True:
chooseDifficulty()
theBoard = getNewBoard() #to do
theChests = getRandomChests(beczki) #to do
drawBoard(theBoard)
previousMoves = []
statlist = [sonarDevices, theChests, theBoard, previousMoves, beczki]
while sonarDevices > 0:
print('Pozostało %s użyć sonaru. %s beczek do znalezienia' % (sonarDevices, len(theChests)))
x, y = enterPlayerMove(previousMoves)
previousMoves.append([x, y])
moveResult = makeMove(theBoard, theChests, x, y)
if moveResult == False:
continue
else:
if moveResult == 'Znalazłeś beczkę piwa!':
for x, y in previousMoves:
makeMove(theBoard, theChests, x, y)
drawBoard(theBoard)
print(moveResult)
if len(theChests) == 0:
print('Znalazłeś wszystkie beczki piwa! Gratulacje i miłej balangi! :)')
break
sonarDevices -= 1
if sonarDevices == 0:
print('Użycia sonaru się wyczerpały! To koniec naszych poszukiwań')
print(' Pozostałe beczki były tutaj: ')
for x, y in theChests:
print(' %s, %s' % (x, y))
print('Chcesz zagrać jescze raz? (tak/nie)')
if not input().lower().startswith('t'):
sys.exit()
if input == "zapisz":
with open("my_file", "w") as stats:
statjson = json.dump(statlist, stats)
elif input == "wczytaj":
with open("my_file", "r") as my_file_read:
statlist = json.load(my_file_read)
def main():
runThegame()Editor is loading...