0

Im making the game of battleships in python, currently 2 players are able to place 5 ships of different lengths on two seperate grids, then the players (should) be able to take turns placing bombs in order to destroy the opponents ships, im not worried about a win condition currently.

my problem is that i cannot figure out how to alternate between players without resetting the grid each time, or being stuck on a single player.

here is my code:

def ships_place(player): #the ship function
    ship_grid = 10
    x = []
    for i in range(ship_grid): #height of grid
        row = []
        for q in range(ship_grid): #width of grid
            row.append(" ") 
        x.append(row)   #creates the grid (all of the above lines)

    ship_positions = []          #creates another grid to store the ship positions
    for i in range(ship_grid):
      row = []
      for q in range(ship_grid):
        row.append(0)
      ship_positions.append(row)

    ship_lengths = [2, 2, 3, 4, 5]
    m = 0
    
    while m < 5:
      try:
        print("placing ship of length", ship_lengths[m])
        print("place your ships player",player, "(eg 1,2):")
        move = input(": ") #input of where to place the ship, eg 1,2 or 4,4
        rowlen, col = move.split(",")  #this splits the input into 2 so you can place the ship on a y then x axis
        col = int(col)-1  #y axis
        rowlen = int(rowlen)-1  #x axis
      
        if col + ship_lengths[m] > ship_grid and rowlen <= ship_grid: #if the columnn you placed the ship in + the length of ship is greater than the size of the grid: it prints a message
          print("too far down or to the right")
          continue
    
        for i in range(ship_lengths[m]): #iterates x times depending on how long the ship is
          if x[rowlen][col] == " " and ship_positions[rowlen][col] == 0: #if the place where u wanna place a ship is empty it will put an "x" there (but ship_positions must = 0 there) 
            x[rowlen][col] = "x"
            ship_positions[rowlen][col] = 1  #if you place a ship somewhere it will make the value of that square 1, so no other ship can be placed on that square
            col = col+1 #this is so you can place the entire ship, col = col+1 will be the value when the "ship_lenghs[m]" iterates again
          else:
              print("cant place a ship on another ship!")  #if the co-ordanate is filled (with an x and 1) it will print this message
              m -=1
              break
        m += 1 #if this value goes over 5 the while loop will end. this value represents the amount of ships placed
        for i in range(len(x)): #prints the main grid which shows where all the ships are
          print(x[i])
          print('')
      except(ValueError):
        continue

    return x
  

player1_ships = ships_place(1) #runs the ship function for player 1
player2_ships = ships_place(2) #runs the ship function for player 2



player1_hits = []
for i in range(10):
  row = [" "] * 10
  player1_hits.append(row)
player2_hits = player1_hits.copy()







def bomber(players, ships, opponent_ships, opponent_hits): #the bomber function
  bomb_grid = 10
  x = [] #the main grid (the one that shows where all the ships are)
  for i in range(bomb_grid): #height of grid
      row = []
      for q in range(bomb_grid): #width of grid
          row.append(" ") 
      x.append(row) 

  bomb_positions = [] #the other grid which stores the bomb positions
  for i in range (bomb_grid):
      row = []
      for q in range(bomb_grid):
         row.append(0)
      bomb_positions.append(row)
  
  n = 0
  while n < 1: #n is the value that checks which players turn it is basically, if it goes above 1 the code will end, making it player 2's turn
    print("place bomb")
    print("place your bomb player",players, "(eg 1,2):")
    place = input()
    rowlen, col = place.split(",")  #this splits the input into 2 so you can place the ship on a y then x axis
    col = int(col)-1  #y axis
    rowlen = int(rowlen)-1  #x axis
    
    if col < 0 or col >= bomb_grid or rowlen < 0 or rowlen >= bomb_grid: #if the column where you placed the bomb or the row where you placed the bomb is not on the grid it will print the message below
      print("too far down or to the right")
      continue

    if bomb_positions[rowlen][col] == 1: #if you place the bomb somewhere where its already been placed it will run the message below (not placed = 0, placed = 1)
      print("bomb already placed here")
      continue

    bomb_positions[rowlen][col] = 1 #makes the place where you put the bomb = to 1 so you cant place it there again
    n +=1 #makes n = 1 so that it will not iterate the while loop again, and instead move onto player 2's turn

    if opponent_ships[rowlen][col] == "x":
      x[rowlen][col] = "X"
      opponent_hits[rowlen][col] = "X" #if the place where you placed the bomb is == a place where theres a ship on the opponents board it will print a hit
      print("hit")
    else:
      print("miss") #if the place where the bomb was placed doesnt equal a place where theres a ship it will run the line below
      x[rowlen][col] = "O"
      opponent_hits[rowlen][col] = "O"

    for i in range(len(x)):
      print(x[i])
      print('')

  if players == 1:
    players = 2
  else:
    players = 1

    
      
bomber(1, player1_ships, player2_ships, player2_hits) #these 2 lines run the functions for both players
bomber(2, player2_ships, player1_ships, player1_hits)`

ive tried adding a loop when it executes the function, (found out quickly that it didnt work, just reset the grid every time)

ive also tried adding a counter for players to place bombs, didnt work because it just let them place multiple but not on a turn by turn basis,

ive tried using this: if players == 1: players = 2 else: players = 1 to switch between players but it doesnt work.

The result im expecting is for player 1 to place a bomb, then for player 2 to do the same, then for it to switch back to player 1 for them to place another bomb on the same grid they origionally placed on. And for this to continue for however long (a while loop most likely)

Max
  • 1
  • 2

0 Answers0