Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. def place_twoship(board, playershiplist):
  2. twoship = 1 # how many "pegs" the ship has.
  3.  
  4. it = 0
  5.  
  6. while twoship == 1: # Start checking if surrounding areas are clear for the ship to expand one space
  7. # Checks for direction, then make sure index isn't out of bound.
  8. while it < 4:
  9. direction = random.choice("NSEW") # Code by Kevin below from Stackoverflow, thank you.
  10. print(direction)
  11.  
  12. x = random_row(board)
  13. y = random_col(board)
  14.  
  15. if direction == "N" and x - 1 >= 0 and y + 1 < 5:
  16. if board[x][y] not in {'1', '2'}: # check if the spot is empty for a ship
  17. board[x][y] = "2"
  18. playershiplist.append([x, y])
  19. if board[x-1][y] != "2":
  20. board[x-1][y] = "2"
  21. twoship = 2
  22. playershiplist.append([x, y - 1])
  23. break
  24. elif direction == "S" and y + 1 < 5:
  25. if board[x][y] not in {'1', '2'}: # check if the spot is empty for a ship
  26. board[y][x] = "2"
  27. playershiplist.append([x, y])
  28. if board[y + 1][ x] != "2":
  29. board[y + 1][x] = "2"
  30. twoship = 2
  31. playershiplist.append([x, y + 1])
  32. break
  33. elif direction == "E" and x < 5 and y + 1 < 5:
  34. if board[x][y] not in {'1', '2'}: # check if the spot is empty for a ship
  35. board[y][x] = "2"
  36. playershiplist.append([x, y])
  37. if board[y][x + 1] != "2":
  38. board[y][x + 1] = "2"
  39. twoship = 2
  40. playershiplist.append([x, y + 1])
  41. break
  42. elif direction == "W" and x - 1 >= 0:
  43. if board[x][y] not in {'1', '2'}: # check if the spot is empty for a ship
  44. board[y][x] = "2"
  45. playershiplist.append([y, x])
  46. if board[y][x - 1] != "2":
  47. board[y][x - 1] = "2"
  48. twoship = 2
  49. playershiplist.append([y, x -1 ])
  50. break
  51. else:
  52. it += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement