Python Forum

Full Version: Something the code dont work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have a little problem with this code, it works sometimes, and something it doesn't. I don't know what is causing the problem. beneath, I will provide the output when it works and when it doesn't.
import random

board = []
for i in range(3):
    board.append([])
    for j in range(10):
        board[i].append([])
        for k in range(10):
            board[i][j].append('-')

ships = []
ships.append(['S', 2])
ships.append(['S', 3])
ships.append(['S', 3])
ships.append(['S', 4])
ships.append(['S', 5])

for ship in ships:
    level = random.randint(0, 2)
    start_space = random.randint(0, 9)
    for i in range(ship[1]):
        board[level][start_space + i][random.randint(0, 9)] = ship[0]

for level in board:
    print('------------------------------')
    for row in level:
        print(row)
Output:
['-', '-', '-', '-', '-', '-', '-', 'S', '-', '-'] ['-', 'S', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', 'S'] ['-', '-', 'S', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ------------------------------ ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', 'S', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', 'S', 'S', '-'] ['-', 'S', '-', '-', '-', '-', '-', '-', '-', 'S'] ['-', '-', '-', '-', '-', '-', '-', 'S', '-', 'S'] ['-', 'S', 'S', 'S', '-', '-', '-', '-', '-', '-'] ['-', 'S', '-', '-', '-', 'S', '-', '-', '-', '-'] ['S', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ------------------------------ ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
Output:
Traceback (most recent call last): File "C:/Users/Alex/Desktop/batt.py", line 22, in <module> board[level][start_space + i][random.randint(0, 9)] = ship[0] IndexError: list index out of range
AlexPython Wrote:I will provide the output when it works and when it doesn't.
can't determine if you are showing what works or what doesn't since your matrix is not labeled.

Please provide:
  • purpose of 'board'
  • Show (clearly identified) a proper board, and dimensions for same.

Thanks
What happens when start_space is 9?

If you are trying to play battleship, your method for placing the ships is all wrong. You can' tear a battleship into 4 pieces and randomly place them all over the board. The 4 spaces used by a battleship have to be next to each other in a row or a column.
(Oct-17-2022, 04:17 AM)deanhystad Wrote: [ -> ]What happens when start_space is 9?

If you are trying to play battleship, your method for placing the ships is all wrong. You can' tear a battleship into 4 pieces and randomly place them all over the board. The 4 spaces used by a battleship have to be next to each other in a row or a column.

Yea is battleship. I just trying to get the board with (5)ships display on the board randomly. Each S is a ship.
(Oct-17-2022, 01:18 AM)Larz60+ Wrote: [ -> ]
AlexPython Wrote:I will provide the output when it works and when it doesn't.
can't determine if you are showing what works or what doesn't since your matrix is not labeled.

Please provide:
  • purpose of 'board'
  • Show (clearly identified) a proper board, and dimensions for same.

Thanks

The board is for the battleship game. I just trying to get the skeleton of the game, a board with 5 ships randomly display on the board. The board is 3x10x10, so 3 boards that are 10x10.
(Oct-17-2022, 02:46 PM)AlexPython Wrote: [ -> ]
(Oct-17-2022, 04:17 AM)deanhystad Wrote: [ -> ]What happens when start_space is 9?

If you are trying to play battleship, your method for placing the ships is all wrong. You can' tear a battleship into 4 pieces and randomly place them all over the board. The 4 spaces used by a battleship have to be next to each other in a row or a column.

Yea is battleship. I just trying to get the board with (5)ships display on the board randomly. Each S is a ship.
This makes sense if it describes the size of the ships
ships.append(['S', 2])  # Destroyer
ships.append(['S', 3])  # Submarine
ships.append(['S', 3])  # Cruiser
ships.append(['S', 4])  # Battleship
ships.append(['S', 5])  # Aircraft carrier
But if those are sizes, this makes no sense.
    for i in range(ship[1]):
        board[level][start_space + i][random.randint(0, 9)] = ship[0]
The 4 parts of a battleship are placed in 4 randomly chosen spots instead of being next to each other. There's also a resonable probability of placing part of your battleship on top of your destroyer, cruiser or sub.
You get the same results by doing this:
for _ in 17:  # 17 PT boats?
    board[level][start_space + i][random.randint(0, 9)] = "S"
[/python]
And what's with the 3 layers? Is this 3 players, or does a play consist of specifying a level, row and column?
(Oct-17-2022, 03:09 PM)deanhystad Wrote: [ -> ]
(Oct-17-2022, 02:46 PM)AlexPython Wrote: [ -> ]Yea is battleship. I just trying to get the board with (5)ships display on the board randomly. Each S is a ship.
This makes sense if it describes the size of the ships
ships.append(['S', 2])  # Destroyer
ships.append(['S', 3])  # Submarine
ships.append(['S', 3])  # Cruiser
ships.append(['S', 4])  # Battleship
ships.append(['S', 5])  # Aircraft carrier
But if those are sizes, this makes no sense.
    for i in range(ship[1]):
        board[level][start_space + i][random.randint(0, 9)] = ship[0]
The 4 parts of a battleship are placed in 4 randomly chosen spots instead of being next to each other. There's also a resonable probability of placing part of your battleship on top of your destroyer, cruiser or sub.
You get the same results by doing this:
for _ in 17:  # 17 PT boats?
    board[level][start_space + i][random.randint(0, 9)] = "S"
[/python]
And what's with the 3 layers? Is this 3 players, or does a play consist of specifying a level, row and column?

Well, yea I was trying to get the ship of 2,3,4,5 pieces together but I wasn't able to get them together so I let them like separate ships. Why 3 layers? I was also trying to get the ships of different sizes in their own boards. Like ships of 2,3,4,5 got their own boards.
Well, anyways thanks for the help. I should start from 0 again and not change the goal of the program mid-way just because I can't find how to do something lol. Thanks again <3
Why are you putting different sized ships on different boards? I don't see the benefit. And if you want different sized ships on different boards, you need 4 boards because you have 4 different sized ships.

Placing the ships is easy. Randomly select a starting point and randomly select the orientation (horizontal or vertical). Place the ship on the board and check for collisions. You have a collision if the ship extends outside the boundary of the board, or if part of the ship collides with a previously placed ship. If you have a collision, randomly choose another starting point. Repeat as necessary. There are few enough ships and a large enough board that the number of retries will be small.
(Oct-17-2022, 04:06 PM)deanhystad Wrote: [ -> ]Why are you putting different sized ships on different boards? I don't see the benefit. And if you want different sized ships on different boards, you need 4 boards because you have 4 different sized ships.

Placing the ships is easy. Randomly select a starting point and randomly select the orientation (horizontal or vertical). Place the ship on the board and check for collisions. You have a collision if the ship extends outside the boundary of the board, or if part of the ship collides with a previously placed ship. If you have a collision, randomly choose another starting point. Repeat as necessary. There are few enough ships and a large enough board that the number of retries will be small.

yea there is no benefit I just wanted to see if I could do that lol. One more question if I want to make that if a ship of a specific size appears on one of the boards but not in the others I need to do an if statement right? For example ship(5) appears on board 1 so it will not appear on board 2,3,4.
Pages: 1 2