I'm doing my first python game it is Battleship without using random


#1
def initialise_board(): #cria tabuleiro 10 por 10
    game_board = [\*]
    opponent_board = [\*]
    letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
    for x in range(len(letters)):
        game_board.append([\*])
        opponent_board.append([])
        for y in range(1, 11):
            game_board[ ].append(str(letters[ ])+str(y))
            opponent_board[ ].append(str(letters[ ])+str(y))

    choose_ships(game_board, opponent_board) #funçao para chamar barcos

I have tried to set the board but it appears as text and the slots are filled with the coordinates and I would like the game to know the coordinates but not show them


#2

Can’t run that. It’s got syntax errors in it and there’s also nothing calling the function or printing out any kind of result.

Rather than storing coordinates on the board, perhaps put water in each location? Coordinates are how you would look up a particular location, they are not the content.


#3
def initialise_board(): #create a 10x10 board   
    game_board=([' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                           [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                           [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                           [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                           [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                           [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                           [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                           [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                           [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                           [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '])
     opponent_board=([' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                                  [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                                  [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                                  [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                                  [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                                  [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                                  [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                                  [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                                  [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
                                  [' ',' ',' ',' ',' ',' ',' ',' ',' ',' '])
    
choose_ships(game_board, opponent_board) #call function to choose ships

I have changed my code but it isn’t holding the board setup it is appearing as a list and not a square


#4

What’s a square?
Perhaps you’d want to print the elements in the list in a pattern that has a square shape.

If you want something written out in a particular way, you gotta write code that writes those characters to output


#5

Yes I want a square shape. But I don’t know how to write the code in the way you say.


#6

Have you thought about what the output should be? You’d probably want to print one row, then a newline, then the next row, and so on, until the whole board has been printed.

You could either create that string and print it out all at once, or print out small parts at a time, depending on what you find more convenient.

Your program only knows what you put in it, right? So printing a square means pushing characters around into whatever configuration you want. And I believe you know all about how to do that!


#7
def choose_ships(game_board, opponent_board):
    Ships = {'PortaAvioes': 5, '4Canhoes': 4, '3Canhoes': 3, 'Submarino': 1, '2Canhoes': 2} #size of each ship
    P1_Ships = [['PortaAvioes', 1], ['4Canhoes', 1], ['3Canhoes', 2], ['Submarino', 4], ['2Canhoes', 3]] #number of ships to place
    P2_Ships = [['PortaAvioes', 1], ['4Canhoes', 1], ['3Canhoes', 2], ['Submarino', 4], ['2Canhoes', 3]]

    for x in P1_Ships: #place ships
        r = 0

        while x[1] > 0: #check there's ships available
        r += 1
        type = x[0]
        ship_size = Ships[x[0]]
        position = input("Jogador 1, inserir coordenada de {0}: ".format(x[0])) #choose position (i.e, A1)
        check = place_ship(game_board, ship_size, position, type)
        if check is True:
            x[1] -= 1
        else:
            print("Localizaçao Invalida.")

I’ve now set the ships but I’ve printed i’s names which takes up too much space instead of printing a symbol for example an X. I don’t know how to change it


#8

Maybe your board shouldn’t contain names, or maybe you shouldn’t print what’s in the board and instead look at what is in the board and deciding based on that what to print. Either works and really just comes down to that you should make a decision on what should happen and then make it so. You decide, not python, there’s no special knowledge needed here. Or maybe you’d just write out larger cells so that they fit. Pick a solution and go for it.