I've been trying to create the game battleship with python3 as my first project. I've mainly been having trouble with the method for the CPU to generate it's own ships on the board. I explain the problem a lot better in the comments in my script, it is below.
I'd appreciate it if I could get some help with the method I've been making.
from random import randint from string import ascii_uppercase #This is a small script for the game battleship. THe game is one user vs the CPU. The user sets their ships while the CPU also sets- #their own ships. Then, both players try to guess each others ships. The CPU uses the random library to guess coordinates on a 10x10 grid. #Python 3.7 on Windows #Mainly need help on this shipmaker method. This method is where the CPU sets their own ships. It takes two arguments: 'map' which is- #the grid (10x10) and numtimes which is how many ships should be placed. The max number of ships would be 4 since I wouldn't want the- #ship size being more than 5 units. The default ship size is 2 and increased by 1 every time a ship is made. The graph starts off as- #a 10x10 2d array filled with "O" and each ship letter is functional to the alphabet. For ex. ship #1 is 'A', ship #2 is 'B', #3 is C,- #and so on. Also, this method makes sure the ships do not cross each other. It also randomly decides if the ship will be vertical- #or horizontal. When I'm testing this, I print the board that the CPU makes to make sure it's working properly. The problem is it only- #graphs one letter no matter how big 'numtimes' is. I'd appreciate some help on my method. This code is pretty much based on my limited- #knowledge on python as I've been learning for a short period, but making the game battleship on your cmd shouldn't be too hard. #BTW, my first attempted method ended up in a gridlock as the code wasn't returning anything and my computer's CPU was being heavily over used def shipmaker(map, numtimes): #only works if map is a square & max numtimes is 4 asci = ascii_uppercase #alphabet themap = len(map)-1 #array/board size truth = False #Used for first while loop below for i in range(numtimes): while truth == False: size = 2+i #size increases along with numtimes. Ship number one is two units long direction = randint(0,1) #This depicts whether the ship will be vertical or horizontal while True: #For the first coordinate row = randint(0,themap-size) #row of first coordinate col = randint(0,themap-size) #column of first coordinate if map[row][col] == "O": #If the coordinate was "O", it hasn't been used yet. The ships cannot overlap each other map[row][col]=asci[size-2] break for x in range(size-1): if direction == 1: #vertical if map[row+x][col]!="O": #further continues breaking the loop if coordinate was already used break else: #if coordinate wasn't used row+=x map[row][col] = asci[size-2] #Makes ship equal to letter corresponding to the alphabet else: #horizontal; Same thing below except the column coordinates are changed since this is horizontal if map[row][col+x]!="O": break else: col+=x map[row][col] = asci[size - 2] if map[row][col] == asci[size-2]: truth = True #breaks the while loop if everything was good and goes on to the next ship in the superior for loop def print_board(board): #prints the board without any commas for row in board: print(" ".join(row)) board,preboard,userboard = ,, #The 'board' will be what the CPU will be guessing on, the 'preboard' is the board where the CPU places it's ships, and 'userboard'- #is where the user will be able to see what coordinates they've missed/hit. for x in range(0,10): #creates the boards and fills them with a 10x10 array filled with "O" board.append(["O"] * 10) preboard.append(["O"] * 10) userboard.append(["O"] * 10) shipmaker(preboard,3) print_board(preboard) #don't worry about whatever that is below this trueorfalse = False print("Welcome to Battleship. You will be playing against a CPU on a 10x10 board") print("There are four ships with the sizes: 2,3,4,and 5 units. To play, enter a longitude and latitude 1-10 as well as it's direction-") for i in range(1,5): while trueorfalse == False: tworow = int(input("Will ship No."+str(i)+" be vertical (1)or horizontal (2)? Enter 1 or 2: ")) long = int(input("Enter a longitude 1-10: ")) #row lat = int(input("Enter a latitude 1-10: ")) #col if tworow == 1 or 2: if 1 <= long <= 10 and 1<=lat<=10: trueorfalse = True else: print("Make sure your numbers are in the parameters. Retry ") trueorfalse = False #Code is not finished yet. The above only makes sure the user's inputs were in the parameter.