Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- # Convert compass direction to row & column increments
- delta = {
- 'S': (1, 0),
- 'N': (-1, 0),
- 'E': (0, 1),
- 'W': (0, -1),
- }
- num_rows = 4
- num_cols = 6
- def make_board():
- return [['0'] * num_cols for _ in range(num_rows)]
- def print_board(board):
- print('\n'.join([' '.join(row) for row in board]), end='\n\n')
- def place_twoship(board):
- ship_placed = False
- x = random.randrange(num_rows)
- y = random.randrange(num_cols)
- while not ship_placed:
- direction = random.choice("NSEW")
- dx, dy = delta[direction]
- nx, ny = x + dx, y + dy
- print(x, y, direction, nx, ny)
- if 0 <= nx < num_rows and 0 <= ny < num_cols:
- board[x][y] = "2"
- board[nx][ny] = "2"
- twoshipcord.extend([(x, y), (nx, ny)])
- flippedbattleshipstwo.extend([(y, x), (ny, nx)])
- ship_placed = True
- board = make_board()
- twoshipcord = []
- flippedbattleshipstwo = []
- place_twoship(board)
- print_board(board)
- print(twoshipcord, flippedbattleshipstwo)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement