Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.81 KB | None | 0 0
  1. from time import sleep
  2. validlocations1 =['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','b1','b2','b3','b4','b5','b6','b7','b8','b9','b10','c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','d1','d2','d3','d4','d5','d6','d7','d8','d9','d10','e1','e2','e3','e4','e5','e6','e7','e8','e9','e10','f1','f2','f3','f4','f5','f6','f7','f8','f9','f10','g1','g2','g3','g4','g5','g6','g7','g8','g9','g10','h1','h2','h3','h4','h5','h6','h7','h8','h9','h10','i1','i2','i3','i4','i5','i6','i7','i8','i9','i10','j1','j2','j3','j4','j5','j6','j7','j8','j9','j10']
  3. validlocations2 =['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','b1','b2','b3','b4','b5','b6','b7','b8','b9','b10','c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','d1','d2','d3','d4','d5','d6','d7','d8','d9','d10','e1','e2','e3','e4','e5','e6','e7','e8','e9','e10','f1','f2','f3','f4','f5','f6','f7','f8','f9','f10','g1','g2','g3','g4','g5','g6','g7','g8','g9','g10','h1','h2','h3','h4','h5','h6','h7','h8','h9','h10','i1','i2','i3','i4','i5','i6','i7','i8','i9','i10','j1','j2','j3','j4','j5','j6','j7','j8','j9','j10']
  4. validlocations3 =['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','b1','b2','b3','b4','b5','b6','b7','b8','b9','b10','c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','d1','d2','d3','d4','d5','d6','d7','d8','d9','d10','e1','e2','e3','e4','e5','e6','e7','e8','e9','e10','f1','f2','f3','f4','f5','f6','f7','f8','f9','f10','g1','g2','g3','g4','g5','g6','g7','g8','g9','g10','h1','h2','h3','h4','h5','h6','h7','h8','h9','h10','i1','i2','i3','i4','i5','i6','i7','i8','i9','i10','j1','j2','j3','j4','j5','j6','j7','j8','j9','j10']
  5. validlocations4 =['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','b1','b2','b3','b4','b5','b6','b7','b8','b9','b10','c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','d1','d2','d3','d4','d5','d6','d7','d8','d9','d10','e1','e2','e3','e4','e5','e6','e7','e8','e9','e10','f1','f2','f3','f4','f5','f6','f7','f8','f9','f10','g1','g2','g3','g4','g5','g6','g7','g8','g9','g10','h1','h2','h3','h4','h5','h6','h7','h8','h9','h10','i1','i2','i3','i4','i5','i6','i7','i8','i9','i10','j1','j2','j3','j4','j5','j6','j7','j8','j9','j10']
  6.  
  7.  
  8. player1=input("Player one, input your name: ")
  9. player2=input("Player two, input your name: ")
  10.  
  11. ships = ["destroyer", "submarine", "cruiser", "battleship", "carrier"]
  12.  
  13. destroyerlocations1=[]
  14. sublocations1=[]
  15. cruiserlocations1=[]
  16. battleshiplocations1=[]
  17. carrierlocations1=[]
  18.  
  19. destroyerlocations2=[]
  20. sublocations2=[]
  21. cruiserlocations2=[]
  22. battleshiplocations2=[]
  23. carrierlocations2=[]
  24.  
  25. def newlocationup(recentlocation):
  26. newlocation = list(recentlocation)
  27. newlocation[-1]=int(newlocation[-1])-1
  28. newlocation[-1]=str(newlocation[-1])
  29. newlocation="".join(newlocation)
  30. return newlocation
  31.  
  32. def newlocationdown(recentlocation):
  33. newlocation = list(recentlocation)
  34. newlocation[-1]=int(newlocation[-1])+1
  35. newlocation[-1]=str(newlocation[-1])
  36. newlocation="".join(newlocation)
  37. return newlocation
  38.  
  39. def newlocationright(recentlocation):
  40. newlocation=list(recentlocation)
  41. newlocation[0]= chr(ord(newlocation[0])+1)
  42. newlocation="".join(newlocation)
  43. return newlocation
  44.  
  45. def newlocationleft(recentlocation):
  46. newlocation=list(recentlocation)
  47. newlocation[0]= chr(ord(newlocation[0])-1)
  48. newlocation="".join(newlocation)
  49. return newlocation
  50.  
  51.  
  52. def asklocation(player, ship, validlocations):
  53. print(player, "where would you like your", ship + "?")
  54. shiplocation = input()
  55. while shiplocation not in validlocations:
  56. print('invalid location')
  57. shiplocation = input()
  58. return shiplocation
  59.  
  60. def askdirection(player, ship, validlocations):
  61. print("Which direction should your", ship, "face?")
  62. shipdirection = input()
  63. while shipdirection != 'up' and shipdirection != 'down' and shipdirection != 'right' and shipdirection != 'left':
  64. print("Invalid direction.")
  65. shipdirection = input()
  66. return shipdirection
  67.  
  68. def placingships(player,ship,length,locationslist,validlocations):
  69. goodlocation=False
  70. while not goodlocation:
  71. shiplocation = asklocation(player, ship, validlocations)
  72. shipdirection = askdirection(player, ship, validlocations)
  73. shiplocations = [shiplocation]
  74. if shipdirection == 'up':
  75. for i in range(1, length):
  76. shiplocations.append(newlocationup(shiplocations[-1]))
  77. elif shipdirection == 'down':
  78. for i in range(1, length):
  79. shiplocations.append(newlocationdown(shiplocations[-1]))
  80. elif shipdirection == 'left':
  81. for i in range(1, length):
  82. shiplocations.append(newlocationleft(shiplocations[-1]))
  83. elif shipdirection == 'right':
  84. for i in range(1, length):
  85. shiplocations.append(newlocationright(shiplocations[-1]))
  86. goodlocation=True
  87. for location in shiplocations:
  88. if location not in validlocations:
  89. goodlocation=False
  90. print(location, "is not valid.")
  91. for location in shiplocations:
  92. locationslist.append(location)
  93. validlocations.remove(location)
  94. return locationslist
  95.  
  96. placingships(player1,ships[0],2,destroyerlocations1,validlocations1)
  97. print(destroyerlocations1)
  98. placingships(player1,ships[1],3,sublocations1,validlocations1)
  99. print(sublocations1)
  100. placingships(player1,ships[2],3,cruiserlocations1,validlocations1)
  101. print(cruiserlocations1)
  102. placingships(player1,ships[3],4,battleshiplocations1,validlocations1)
  103. print(battleshiplocations1)
  104. placingships(player1,ships[4],5,carrierlocations1,validlocations1)
  105. print(carrierlocations1)
  106. sleep(1)
  107. print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
  108. placingships(player2,ships[0],2,destroyerlocations2,validlocations2)
  109. print(destroyerlocations2)
  110. placingships(player2,ships[1],3,sublocations2,validlocations2)
  111. print(sublocations2)
  112. placingships(player2,ships[2],3,cruiserlocations2,validlocations2)
  113. print(cruiserlocations2)
  114. placingships(player2,ships[3],4,battleshiplocations2,validlocations2)
  115. print(battleshiplocations2)
  116. placingships(player2,ships[4],5,carrierlocations2,validlocations2)
  117. print(carrierlocations2)
  118. print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
  119.  
  120. d1=0
  121. s1=0
  122. cr1=0
  123. b1=0
  124. ca1=0
  125. d2=0
  126. s2=0
  127. cr2=0
  128. b2=0
  129. ca2=0
  130. shots = []
  131. shots2 = []
  132.  
  133. def hitormiss(list1, shot, ship):
  134. if shot in list1:
  135. list1.remove(shot)
  136. print("Hit " + ship + "!")
  137.  
  138. def sinkingships(ship, list1):
  139. print(ship.title(), 'sunk')
  140. list1.append('donttypethis')
  141.  
  142. while True:
  143. print("\n\nWhere would you like to shoot", player2)
  144. shot = input()
  145. while shot in shots or not shot in validlocations3:
  146. if shot == 'shotlist':
  147. print(shots.sort())
  148. print("You have already shot there, or that is an invalid location.")
  149. shot = input()
  150. shots.append(shot)
  151. validlocations3.remove(shot)
  152. hitormiss(destroyerlocations1, shot, 'destroyer')
  153. hitormiss(sublocations1, shot, 'submarine')
  154. hitormiss(cruiserlocations1, shot, 'cruiser')
  155. hitormiss(battleshiplocations1, shot, 'battleship')
  156. hitormiss(carrierlocations1, shot, 'carrier')
  157. if len(destroyerlocations1)==0:
  158. sinkingships('destroyer',destroyerlocations1)
  159. d1 = 1
  160. if len(sublocations1)==0:
  161. sinkingships('submarine',sublocations1)
  162. s1 = 1
  163. if len(cruiserlocations1)==0:
  164. sinkingships('cruiser',cruiserlocations1)
  165. cr1 = 1
  166. if len(battleshiplocations1)==0:
  167. sinkingships('battleship',battleshiplocations1)
  168. b1 = 1
  169. if len(carrierlocations1)==0:
  170. sinkingships('carrier',carrierlocations1)
  171. ca1 = 1
  172. shipsdestroyed=d1+s1+cr1+b1+ca1
  173. print(player2 + ', you have sunk', shipsdestroyed, 'ships.')
  174. if shipsdestroyed == 5:
  175. break
  176.  
  177. print("\n\nWhere would you like to shoot", player1)
  178. shot = input()
  179. while shot in shots2 or not shot in validlocations4:
  180. if shot == 'shotlist':
  181. print(shots2.sort())
  182. print("You have already shot there, or that is an invalid location.")
  183. shot = input()
  184. shots2.append(shot)
  185. validlocations4.remove(shot)
  186. hitormiss(destroyerlocations2,shot,'destroyer')
  187. hitormiss(sublocations2,shot,'submarine')
  188. hitormiss(cruiserlocations2, shot, 'cruiser')
  189. hitormiss(battleshiplocations2,shot,'battleship')
  190. hitormiss(carrierlocations2, shot, 'carrier')
  191. if len(destroyerlocations2)==0:
  192. sinkingships('destroyer',destroyerlocations2)
  193. d2 = 1
  194. if len(sublocations2)==0:
  195. sinkingships('submarine',sublocations2)
  196. s2 = 1
  197. if len(cruiserlocations2)==0:
  198. sinkingships('cruiser',cruiserlocations2)
  199. cr2 = 1
  200. if len(battleshiplocations2)==0:
  201. sinkingships('battleship',battleshiplocations2)
  202. b2 = 1
  203. if len(carrierlocations2)==0:
  204. sinkingships('carrier',carrierlocations2)
  205. ca2 = 1
  206.  
  207. shipsdestroyed2 = d2+s2+cr2+b2+ca2
  208. print(player1 + ', you have sunk', shipsdestroyed2, 'ships.')
  209. if shipsdestroyed2 == 5:
  210. break
  211.  
  212. if shipsdestroyed2 == 5:
  213. print('\n\n'player1, "has sunk all of", player2 + "'s ships!")
  214. print(player1, "is the winner!")
  215. if shipsdestroyed == 5:
  216. print(player2, "has sunk all of", player1 + "'s ships!")
  217. print(player2, "is the winner!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement