Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. from random import randint
  2.  
  3. board = []
  4.  
  5.  
  6. def boardmaker(board):
  7. rows = int(input("How many rows? "))
  8. cols = int(input("How many columns? "))
  9.  
  10. for x in range(rows):
  11. board.append("O" * cols)
  12.  
  13. return board
  14.  
  15. def print_board(board):
  16. for x in range(len(board)):
  17. print(" ".join(board[x]))
  18.  
  19. #shipgetter will take user inputs to create a number of ships for the game
  20. def shipgetter(board):
  21. #Here we caluculate the area of the game board to check if there's enough room for the ships
  22. area = len(board) * len(board[0])
  23. carriers = 0
  24. battleships = 0
  25. submarines = 0
  26. destroyers = 0
  27. patrols = 0
  28. print(area)
  29.  
  30. counter = 0
  31. while counter == 0:
  32. if len(board) >= 5 or len(board[0]) >= 5:
  33. carriers = int(input("How many aircraft carriers (5 length)? "))
  34. if len(board) >= 4 or len(board[0]) >= 4:
  35. battleships = int(input("How many battleships (4 length)? "))
  36. if len(board) >= 3 or len(board[0]) >= 3:
  37. submarines = int(input("How many submarines (3 length)? "))
  38. if len(board) >= 2 or len(board[0]) >= 2:
  39. destroyers = int(input("How many destroyers (2 length)? "))
  40. if len(board) >= 1 or len(board[0]) >= 1:
  41. patrols = int(input("How many patrol boats (1 length)? "))
  42.  
  43.  
  44. if ((carriers * 5) + (battleships * 4) + (submarines * 3) + (destroyers * 2) + patrols) <= area:
  45. counter = 1
  46.  
  47. else:
  48. print("Sorry, but there is not enough room for all those ships!")
  49. again = str.lower(input("Would you like to play again? y/n: "))
  50.  
  51. if again == "no" or again == "n":
  52. break
  53.  
  54.  
  55. print(str(carriers) + " carrier(s).")
  56. print(str(battleships) + " battleship(s).")
  57. print(str(submarines) + " submarine(s).")
  58. print(str(destroyers) + " destroyer(s).")
  59. print(str(patrols) + " patrol boat(s).")
  60.  
  61. return(board, carriers, battleships, submarines, destroyers, patrols)
  62.  
  63. def boardchecker(answerboard, shiplen, vert, randrows, randcols):
  64.  
  65.  
  66. row = random.choice(randrows)
  67. col = random.choice(randcols)
  68. counter = 0
  69.  
  70. if vert == 0:
  71. while counter < 0:
  72. if answerboard[row[col]] == "O":
  73. answerboard[row[col]] = "X"
  74. col += 1
  75. counter += 1
  76. else:
  77. return 0
  78. if vert == 1:
  79. while counter < 0:
  80. if answerboard[row[col]] == "O":
  81. answerboard[row[col]] + "X"
  82. row += 1
  83. counter += 1
  84. else:
  85. return 0
  86.  
  87.  
  88. return answerboard
  89.  
  90. def shipmaker(board, crr, btl, sub, dst, ptr):
  91. randrows = []
  92. randcols = []
  93. shiplist = [[crr, 5], [btl, 4], [sub, 3], [dst, 2], [ptr, 1]]
  94. print(shiplist)
  95. answerboard = board
  96. #shiptotal is the total number of all ships on the board
  97. shiptotal = (crr + btl + sub + dst + ptr)
  98. #When vert = 0, ships will be made horizontally. If vert = 1, ships are made vertically
  99. vert = 0
  100. for x in range(len(board)):
  101. randrows.append(x)
  102.  
  103. for x in range(len(board[0])):
  104. randcols.append(x)
  105.  
  106. print(randrows)
  107. print(randcols)
  108.  
  109. while shiptotal > 0:
  110.  
  111. for x in range(len(shiplist)):
  112. count = 0
  113. while shiplist[x[0]]> 0:
  114. while count == 0:
  115. if boardchecker(answerboard, shiplist[x[1]], randint(0, 1), randrows, randcols) != 0:
  116. count += 1
  117. shiplist[x[0]] -= 1
  118. shiptotal -= 1
  119.  
  120. print(answerboard)
  121.  
  122.  
  123. boardmaker(board)
  124. print_board(board)
  125. board, crr, btl, sub, dst, ptr = shipgetter(board)
  126.  
  127. print(str(crr) + " carrier(s).")
  128. print(str(btl) + " battleship(s).")
  129. print(str(sub) + " submarine(s).")
  130. print(str(dst) + " destroyer(s).")
  131. print(str(ptr) + " patrol boat(s).")
  132.  
  133. shipmaker(board, crr, btl, sub, dst, ptr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement