I am making a battleship project from scratch. What would be a way to identify a specific point on the map, and let you choose it, and then print out the chosen grid, with the "#" replaced with something else like a "X"?
Here's the code:
turns = 10
alphabet = ["A","B","C","D","E","F","G","H","I","J","a","b","c","d","e","f","g","h","i","j"]
def StartScreen():
print("Welcome to Battleship!")
print('''
1. Start
2. Exit
''')
choice = int(input("Enter your number: "))
if choice == 1:
GameScreen()
elif choice == 2:
exit()
else:
print("Please choose a valid number.")
StartScreen()
def GameScreen():
print("\n")
print(" A B C D E F G H I J")
row = [" # ", "# ", "# ", "# ", "# ", "# ", "# ", "# ", "# ", "# "]
i = -1
while i != 9:
print(i+1, row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9])
i = i + 1
print("\n")
rowx = input("Choose a letter(A-J): ")
rowy = int(input("Enter a number(0-9): "))
if rowx in alphabet and rowy in range(0,10):
print(f'you chose {rowx.upper()}{rowy}')
else:
print("Please choose a valid point(A-J/0-9)\n")
StartScreen()
It would be better for you to set your board in a different way. So far you have a unique list,
row, which you print ten times to simulate a board. This will make it uncomfortable to keep track of changes, as you whish to do. Use a 2d list instead:Of course, you can generate this board automatically:
Then you can ask for two integers as input, call them
iandj, and setboard[i][j] = "X". This will produce the desired result.