Python Forum
Something the code dont work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Something the code dont work
#1
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
Reply
#2
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
AlexPython likes this post
Reply
#3
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.
AlexPython likes this post
Reply
#4
(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.
Reply
#5
(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.
Reply
#6
(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?
AlexPython likes this post
Reply
#7
(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.
Reply
#8
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
Reply
#9
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.
AlexPython likes this post
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 883 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 740 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Beginner: Code not work when longer list raiviscoding 2 880 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,925 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,534 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 953 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  I dont know why my function won't work? MehHz2526 3 1,255 Nov-28-2022, 09:32 PM
Last Post: deanhystad
  cannot get code to work Led_Zeppelin 10 2,565 Jun-30-2022, 06:28 PM
Last Post: deanhystad
  why I dont get any output from this code William369 2 1,177 Jun-23-2022, 09:18 PM
Last Post: William369
  How does this code work? pd_minh12 3 1,392 Apr-15-2022, 02:50 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020