Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def place_twoship(board, playershiplist):
- twoship = 1 # how many "pegs" the ship has.
- it = 0
- while twoship == 1: # Start checking if surrounding areas are clear for the ship to expand one space
- # Checks for direction, then make sure index isn't out of bound.
- while it < 4:
- direction = random.choice("NSEW") # Code by Kevin below from Stackoverflow, thank you.
- print(direction)
- x = random_row(board)
- y = random_col(board)
- if direction == "N" and x - 1 >= 0 and y + 1 < 5:
- if board[x][y] not in {'1', '2'}: # check if the spot is empty for a ship
- board[x][y] = "2"
- playershiplist.append([x, y])
- if board[x-1][y] != "2":
- board[x-1][y] = "2"
- twoship = 2
- playershiplist.append([x, y - 1])
- break
- elif direction == "S" and y + 1 < 5:
- if board[x][y] not in {'1', '2'}: # check if the spot is empty for a ship
- board[y][x] = "2"
- playershiplist.append([x, y])
- if board[y + 1][ x] != "2":
- board[y + 1][x] = "2"
- twoship = 2
- playershiplist.append([x, y + 1])
- break
- elif direction == "E" and x < 5 and y + 1 < 5:
- if board[x][y] not in {'1', '2'}: # check if the spot is empty for a ship
- board[y][x] = "2"
- playershiplist.append([x, y])
- if board[y][x + 1] != "2":
- board[y][x + 1] = "2"
- twoship = 2
- playershiplist.append([x, y + 1])
- break
- elif direction == "W" and x - 1 >= 0:
- if board[x][y] not in {'1', '2'}: # check if the spot is empty for a ship
- board[y][x] = "2"
- playershiplist.append([y, x])
- if board[y][x - 1] != "2":
- board[y][x - 1] = "2"
- twoship = 2
- playershiplist.append([y, x -1 ])
- break
- else:
- it += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement