import random board = [[],[]] HUMAN = 0 COMPUTER = 1 EMPTY = 0 SHIP = 1 HIT = 2 MISS = 3 shipsNumber = 1 ships = [shipsNumber*2, shipsNumber*2] # BEGIN: Initialize gameboard for players for who in [HUMAN, COMPUTER]: safety = 100 shipsToSet = shipsNumber for r in range(10): row = [] for c in range(10): row.append(EMPTY) board[who].append(row) # Or the same with list comprehension # which is more Pythonic approach but less universal (portable) ''' board[who] = [[EMPTY for _ in range(10)] for _ in range(10)] ''' while True and safety > 0: orientation = random.randint(0, 1) if orientation == 0: # Vertical orientation r = random.randint(0, 9-1) c = random.randint(0, 9) if board[who][r][c] == EMPTY and board[who][r+1][c] == EMPTY: board[who][r][c] = SHIP board[who][r+1][c] = SHIP shipsToSet -= 1 else: # Horizontal orientation r = random.randint(0, 9) c = random.randint(0, 9-1) if board[who][r][c] == EMPTY and board[who][r][c+1] == EMPTY: board[who][r][c] = SHIP board[who][r][c+1] = SHIP shipsToSet -= 1 safety -= 1 if shipsToSet == 0: break if safety == 0: print("PROBLEM Z USTAWIENIEM STATKÓW") print('\n=================\n') print(' 0123456789') for r in range(0,10): print(f'{r} ', end = '') for c in range(0,10): if board[who][r][c] == EMPTY: print('.', end = '') elif board[who][r][c] == SHIP: print('#', end = '') print() # END: Initialize gameboard for players # BEGIN: Main game loop who = HUMAN while True: whoReadable = "Gracza" if who == HUMAN else "Komputera" print(f"Teraz kolej {whoReadable}") # BEGIN: Oddanie strzału if who == HUMAN: r = int(input('Podaj wiersz')) c = int(input('Podaj kolumnę')) else: r = random.randint(0, 9) c = random.randint(0, 9) opponent = HUMAN if who == COMPUTER else COMPUTER if r>=0 and r<=9 and c>=0 and c<= 9: if board[opponent][r][c] == SHIP: board[opponent][r][c] = HIT ships[opponent] -= 1 elif board[opponent][r][c] == EMPTY: board[opponent][r][c] = MISS else: print("Niepoprawne współrzędne, tracisz swój ruch.") else: print("Współrzędne ze złego zakresu.") # END: Oddanie strzału # BEGIN: Wyświetlenie planszy opponentReadable = "Komputera" if who == HUMAN else "Gracza" print(f'\n=== stan planszy {opponentReadable} ===\n') print(' 0123456789') for r in range(0,10): print(f'{r} ', end = '') for c in range(0,10): if board[opponent][r][c] == EMPTY: print('.', end = '') elif board[opponent][r][c] == SHIP: print('#', end = '') elif board[opponent][r][c] == HIT: print('X', end = '') elif board[opponent][r][c] == MISS: print('*', end = '') print() # END: Wyświetlenie planszy # BEGIN: Podjęcie decyzji czy jest zwycięzca if ships[opponent] == 0: player = "Wygrałeś!" if who == HUMAN else "Komputer wygrał!" print(f"Brawo! {player}") break # END: Podjęcie decyzji czy jest zwycięzca who = HUMAN if who == COMPUTER else COMPUTER # END: Main game loop