I'm making in-console Battleship game in Python and I have a code that checks if the ship can fit into the list. It does not look very appealing and I'd like to simplify it into a more appealing version.
if (direction is "N" and row - size < 0) or \
(direction is "E" and column + size > 10) or \
(direction is "S" and row + size > 10) or \
(direction is "W" and column - size < 0):
print("It does not fit")
I was thinking of using the dictionary, but couldn't think of a way to use variables and maths operator (-+ <>) inside
if any([direction is "N" and row - size < 0, direction is "E" and column + size > 10, direction is "S" and row + size > 10, direction is "W" and column - size < 0]):
print("It does not fit")
Sure there is a lot of improvement with your code to but this is a start
Other improvements
if any([direction == "N" and size > row, direction == "E" and column + size > 10, direction == "S" and row + size > 10, direction == "W" and size > column]):
A better way, more clear;
dir_classifier = {'N': size > row,
'E': (column + size) > 10,
'W': size > column,
'S': (row + size > 10)}
if dir_classifier[direction]:
print("It does not fit")