I have implemented Battleship in Python by using lists. I have used threading to prevent KeyboardInterrupt
, so the player can't cheat by using control-c and stuff.
How can I simplify this code?
import random
import sys
from threading import Thread
import keyring
master_break = keyring.get_password('ship', 'break')
enable = keyring.get_password('ship', 'enable')
activate = keyring.get_password('ship', 'activate')
def no_interupt():
print('This is Battleship.')
print('Hits are marked with Hs and misses are marked with Ms.')
print('If you miss too many times, you lose.')
nuke_enabled = False
while True:
try:
dif = input('What difficulty would you like (1-3): ')
if dif == enable:
nuke_enabled = True
print('Nukes have been enabled.')
if dif != master_break:
dif = int(dif)
elif dif == master_break:
sys.exit()
except ValueError:
if nuke_enabled != True:
print('Invalid format.')
else:
pass
else:
break
lines = 8
board = []
nuke_board = []
for x in range(8):
board.append(['O'] * 8)
nuke_board.append(['N'] * 8)
def split(word):
return [char for char in word]
def print_board(board):
x = 1
print(' A B C D E F G H')
print()
for row in board:
print(x, ' ', ' | '.join(row))
print(' ', ' -'*15)
x += 1
def print_nuke(nuke_board):
x = 1
print(' A B C D E F G H')
print()
for row in nuke_board:
print(x, ' ', ' | '.join(row))
print(' ', ' -'*15)
x += 1
print('Let\'s play Battleship!')
print()
print_board(board)
def random_col(board):
return random.randint(0, 7)
carrier_direct = random.randint(1, 2)
battleship_direct = random.randint(1, 2)
destroyer_direct = random.randint(1, 2)
carrier = 5
battleship = 4
destroyer = 3
ship_len = 12
carrier_sunk = False
battleship_sunk = False
destroyer_sunk = False
turns = 12 - dif
if carrier_direct == 1:
carrier_row = random_col(board)
carrier_col_start = random.randint(0, 2)
carrier_col_end = carrier_col_start + 5
if battleship_direct == 1:
while True:
battleship_row = random_col(board)
battleship_col_start = random.randint(0, 3)
battleship_col_end = battleship_col_start + 4
if battleship_row != carrier_row:
break
if destroyer_direct == 1:
while True:
destroyer_row = random_col(board)
destroyer_col_start = random.randint(0, 4)
destroyer_col_end = destroyer_col_start + 3
if destroyer_row != battleship_row and destroyer_row != carrier_row:
break
else:
while True:
destroyer_col = random_col(board)
destroyer_row_start = random.randint(0, 4)
destroyer_row_end = destroyer_row_start + 3
if carrier_col_start <= destroyer_col and destroyer_col <= carrier_col_end or battleship_col_start <= destroyer_col and destroyer_col <= battleship_col_end:
pass
else:
break
else:
while True:
battleship_col = random_col(board)
battleship_row_start = random.randint(0, 3)
battleship_row_end = battleship_row_start + 4
if carrier_col_start <= battleship_col and battleship_col <= carrier_col_end:
pass
else:
break
if destroyer_direct == 1:
while True:
destroyer_row = random_col(board)
destroyer_col_start = random.randint(0, 4)
destroyer_col_end = destroyer_col_start + 3
if destroyer_row != carrier_row:
if battleship_row_start <= destroyer_row and destroyer_row <= battleship_row_end:
pass
else:
break
else:
while True:
destroyer_col = random_col(board)
destroyer_row_start = random.randint(0, 4)
destroyer_row_end = destroyer_row_start + 3
if destroyer_col != battleship_col:
if destroyer_col <= carrier_col_end and destroyer_col >= carrier_col_start:
pass
else:
break
else:
carrier_col = random_col(board)
carrier_row_start = random.randint(0, 2)
carrier_row_end = carrier_row_start + 5
if battleship_direct == 1:
while True:
battleship_row = random_col(board)
battleship_col_start = random.randint(0, 3)
battleship_col_end = battleship_col_start + 4
if battleship_row <= carrier_row_end and battleship_row >= carrier_row_start:
pass
else:
break
if destroyer_direct == 1:
while True:
destroyer_row = random_col(board)
destroyer_col_start = random.randint(0, 4)
destroyer_col_end = destroyer_col_start + 3
if destroyer_row != battleship_row:
if destroyer_row <= carrier_row_end and destroyer_row >= carrier_row_start:
pass
else:
break
else:
while True:
destroyer_col = random_col(board)
destroyer_row_start = random.randint(0, 4)
destroyer_row_end = destroyer_row_start + 3
if destroyer_col != carrier_col:
if destroyer_col <= battleship_col_end and destroyer_col >= battleship_col_start:
pass
else:
break
else:
while True:
battleship_col = random_col(board)
battleship_row_start = random.randint(0, 3)
battleship_row_end = battleship_row_start + 4
if battleship_col != carrier_col:
break
if destroyer_direct == 1:
while True:
destroyer_row = random_col(board)
destroyer_col_start = random.randint(0, 4)
destroyer_col_end = destroyer_col_start + 3
if destroyer_row <= carrier_row_end and destroyer_row >= carrier_row_start or destroyer_row <= battleship_row_end and destroyer_row >= battleship_row_start:
pass
else:
break
else:
while True:
destroyer_col = random_col(board)
destroyer_row_start = random.randint(0, 4)
destroyer_row_end = destroyer_row_start + 3
if destroyer_col != battleship_col and destroyer_col != carrier_col:
break
while turns > 0:
if ship_len == 0:
print('You win!')
sys.exit()
if carrier == 0 and carrier_sunk != True:
print('You sunk the carrier!')
carrier_sunk = True
elif battleship == 0 and battleship_sunk != True:
print('You sunk the battleship!')
battleship_sunk = True
elif destroyer == 0 and destroyer_sunk != True:
print('You sunk the destroyer!')
destroyer_sunk = True
test_var = False
not_enabled = False
guess = input('Guess coordinate: ').lower()
if guess == master_break:
sys.exit()
if guess == activate:
if nuke_enabled == True:
print('NUKE EM\' ALL!')
print()
print_nuke(nuke_board)
print('You sunk the carrier!')
print('You sunk the battleship!')
print('You sunk the destroyer!')
print('You win! (by using cheats, but WHO CARES!)')
sys.exit()
else:
print('Nukes are not enabled.')
not_enabled = True
try:
guess_col_let = split(guess)[0]
guess_row = int(split(guess)[1])
guess_col = int(ord(guess_col_let) - 96)
except ValueError:
if not_enabled != True:
print('Invalid format.')
else:
pass
except IndexError:
if not_enabled != True:
print('Invalid format.')
else:
pass
else:
test_var = True
if not_enabled != True:
if test_var == True:
try:
if guess_row == carrier_row and carrier_col_start <= guess_col <= carrier_col_end:
print('Hit on the carrier!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
carrier -= 1
ship_len -= 1
elif guess_row == battleship_row and battleship_col_start <= guess_col <= battleship_col_end:
print('Hit on the battleship!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
elif guess_row == destroyer_row and destroyer_col_start <= guess_col <= destroyer_col_end:
print('Hit on the destroyer!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
else:
if guess_row < 1 or guess_row > lines + 1 or guess_col < 1 or guess_col > lines + 1:
print('Oops, that\'s not even in the ocean.')
elif board[guess_row - 1][guess_col - 1] != 'O':
print('You guessed that one already.')
else:
print('You missed!')
board[guess_row - 1][guess_col - 1] = 'M'
turns -= 1
if turns > 1 or turns == 0:
print('You have {} guesses remaining.'.format(turns))
else:
print('You have 1 guess remaining.')
print()
print_board(board)
except NameError:
try:
if guess_row == carrier_row and carrier_col_start <= guess_col <= carrier_col_end:
print('Hit on the carrier!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
carrier -= 1
ship_len -= 1
elif guess_col == battleship_col and battleship_row_start <= guess_row <= battleship_row_end:
print('Hit on the battleship!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
elif guess_row == destroyer_row and destroyer_col_start <= guess_col <= destroyer_col_end:
print('Hit on the destroyer!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
else:
if guess_row < 1 or guess_row > lines + 1 or guess_col < 1 or guess_col > lines + 1:
print('Oops, that\'s not even in the ocean.')
elif board[guess_row - 1][guess_col - 1] != 'O':
print('You guessed that one already.')
else:
print('You missed!')
board[guess_row - 1][guess_col - 1] = 'M'
turns -= 1
if turns > 1 or turns == 0:
print('You have {} guesses remaining.'.format(turns))
else:
print('You have 1 guess remaining.')
print()
print_board(board)
except NameError:
try:
if guess_row == carrier_row and carrier_col_start <= guess_col <= carrier_col_end:
print('Hit on the carrier!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
carrier -= 1
ship_len -= 1
elif guess_col == battleship_col and battleship_row_start <= guess_row <= battleship_row_end:
print('Hit on the battleship!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
elif guess_row == destroyer_row and destroyer_col_start <= guess_col <= destroyer_col_end:
print('Hit on the destroyer!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
else:
if guess_row < 1 or guess_row > lines + 1 or guess_col < 1 or guess_col > lines + 1:
print('Oops, that\'s not even in the ocean.')
elif board[guess_row - 1][guess_col - 1] != 'O':
print('You guessed that one already.')
else:
print('You missed!')
board[guess_row - 1][guess_col - 1] = 'M'
turns -= 1
if turns > 1 or turns == 0:
print('You have {} guesses remaining.'.format(turns))
else:
print('You have 1 guess remaining.')
print()
print_board(board)
except NameError:
try:
if guess_row == carrier_row and carrier_col_start <= guess_col <= carrier_col_end:
print('Hit on the carrier!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
carrier -= 1
ship_len -= 1
elif guess_row == battleship_row and battleship_col_start <= guess_col <= battleship_col_end:
print('Hit on the battleship!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
elif guess_col == destroyer_col and destroyer_row_start <= guess_row <= destroyer_row_end:
print('Hit on the destroyer!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
else:
if guess_row < 1 or guess_row > lines + 1 or guess_col < 1 or guess_col > lines + 1:
print('Oops, that\'s not even in the ocean.')
elif board[guess_row - 1][guess_col - 1] != 'O':
print('You guessed that one already.')
else:
print('You missed!')
board[guess_row - 1][guess_col - 1] = 'M'
turns -= 1
if turns > 1 or turns == 0:
print('You have {} guesses remaining.'.format(turns))
else:
print('You have 1 guess remaining.')
print()
print_board(board)
except NameError:
try:
if guess_col == carrier_col and carrier_row_start <= guess_row <= carrier_row_end:
print('Hit on the carrier!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
carrier -= 1
ship_len -= 1
elif guess_col == battleship_col and battleship_row_start <= guess_row <= battleship_row_end:
print('Hit on the battleship!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
elif guess_col == destroyer_col and destroyer_row_start <= guess_row <= destroyer_row_end:
print('Hit on the destroyer!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
else:
if guess_row < 1 or guess_row > lines + 1 or guess_col < 1 or guess_col > lines + 1:
print('Oops, that\'s not even in the ocean.')
elif board[guess_row - 1][guess_col - 1] != 'O':
print('You guessed that one already.')
else:
print('You missed!')
board[guess_row - 1][guess_col - 1] = 'M'
turns -= 1
if turns > 1 or turns == 0:
print('You have {} guesses remaining.'.format(turns))
else:
print('You have 1 guess remaining.')
print()
print_board(board)
except NameError:
try:
if guess_col == carrier_col and carrier_row_start <= guess_row <= carrier_row_end:
print('Hit on the carrier!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
carrier -= 1
ship_len -= 1
elif guess_col == battleship_col and battleship_row_start <= guess_row <= battleship_row_end:
print('Hit on the battleship!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
elif guess_row == destroyer_row and destroyer_col_start <= guess_col <= destroyer_col_end:
print('Hit on the destroyer!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
else:
if guess_row < 1 or guess_row > lines + 1 or guess_col < 1 or guess_col > lines + 1:
print('Oops, that\'s not even in the ocean.')
elif board[guess_row - 1][guess_col - 1] != 'O':
print('You guessed that one already.')
else:
print('You missed!')
board[guess_row - 1][guess_col - 1] = 'M'
turns -= 1
if turns > 1 or turns == 0:
print('You have {} guesses remaining.'.format(turns))
else:
print('You have 1 guess remaining.')
print()
print_board(board)
except NameError:
try:
if guess_col == carrier_col and carrier_row_start <= guess_row <= carrier_row_end:
print('Hit on the carrier!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
carrier -= 1
ship_len -= 1
elif guess_row == battleship_row and battleship_col_start <= guess_col <= battleship_col_end:
print('Hit on the battleship!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
elif guess_col == destroyer_col and destroyer_row_start <= guess_row <= destroyer_row_end:
print('Hit on the destroyer!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
else:
if guess_row < 1 or guess_row > lines + 1 or guess_col < 1 or guess_col > lines + 1:
print('Oops, that\'s not even in the ocean.')
elif board[guess_row - 1][guess_col - 1] != 'O':
print('You guessed that one already.')
else:
print('You missed!')
board[guess_row - 1][guess_col - 1] = 'M'
turns -= 1
if turns > 1 or turns == 0:
print('You have {} guesses remaining.'.format(turns))
else:
print('You have 1 guess remaining.')
print()
print_board(board)
except NameError:
try:
if guess_col == carrier_col and carrier_row_start <= guess_row <= carrier_row_end:
print('Hit on the carrier!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
carrier -= 1
ship_len -= 1
elif guess_row == battleship_row and battleship_col_start <= guess_col <= battleship_col_end:
print('Hit on the battleship!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
elif guess_row == destroyer_row and destroyer_col_start <= guess_col <= destroyer_col_end:
print('Hit on the destroyer!')
board[guess_row - 1][guess_col - 1] = 'H'
print_board(board)
destroyer -= 1
ship_len -= 1
else:
if guess_row < 1 or guess_row > lines + 1 or guess_col < 1 or guess_col > lines + 1:
print('Oops, that\'s not even in the ocean.')
elif board[guess_row - 1][guess_col - 1] != 'O':
print('You guessed that one already.')
else:
print('You missed!')
board[guess_row - 1][guess_col - 1] = 'M'
turns -= 1
if turns > 1 or turns == 0:
print('You have {} guesses remaining.'.format(turns))
else:
print('You have 1 guess remaining.')
print()
print_board(board)
except NameError:
pass
else:
pass
print('You lose!')
sys.exit()
a = Thread(target=no_interupt)
a.Daemon = True
a.start()
a.join()