Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. #Battleships!
  2. import random
  3. import time
  4. #Asigns Playing to True
  5. playing = True
  6. hits = []
  7. comp_hits = []
  8. already = []
  9. values = []
  10. def checkpos(computer_values, values):
  11. values = computer_values
  12. if len(computer_values) == 6:
  13. for i in range(6):
  14. if int(ord(computer_values[i][0]))-65 > 5:
  15. computer_values = input('Invalid - Enter 6 positions')
  16. computer_values = computer_values.split()
  17. checkpos(computer_values, values)
  18. elif int(computer_values[i][1]) > 5:
  19. computer_values = input('Invalid - Enter 6 positions')
  20. computer_values = computer_values.split()
  21. checkpos(computer_values, values)
  22. else:
  23. computer_values = input('Invalid - Enter 6 positions')
  24. computer_values = computer_values.split()
  25. checkpos(computer_values, values)
  26. return
  27. def compvals(comp_hits, values):
  28. comp_hits = values
  29. comptest(comp_hits, values)
  30. return
  31. def comptest(comp_hits, values):
  32. for i in range(len(comp_hits)):
  33. if comp_hits.count(comp_hits[i]) > 1:
  34. comp_hits.clear()
  35. pick(comp_hits)
  36. return
  37. def comp_shot(grid2, comp_hits):
  38. first = random.randint(65, 69)
  39. first = chr(first)
  40. second = random.randint(1,5)
  41. second = str(second)
  42. pos = first+second
  43. if int(pos[1]) > 5:
  44. pos = input('Guess a Position: ').upper()
  45. shotpos(pos, grid2)
  46. row = int(pos[1])
  47. column = int(ord(pos[0]))-65
  48. if pos in comp_hits:
  49. for i in range(len(comp_hits)):
  50. if pos == comp_hits[i]:
  51. break
  52. grid2[column][row] = 'X'
  53. comp_hits.remove(pos)
  54. elif pos not in comp_hits:
  55. grid2[column][row] = 'O'
  56. for num in range(6):
  57. for num2 in range(6):
  58. print(' ', grid[num][num2], end='')
  59. print('t ', end='')
  60. for num2 in range(6):
  61. print(' ', grid2[num][num2], end='')
  62. print()
  63. return
  64. #Determines Row number and Position from coordinates
  65. def shotpos(pos, grid, hits):
  66. try:
  67. int(pos[1])
  68. except:
  69. pos = input('Invalid - Guess a Position: ').upper()
  70. shotpos(pos, grid, hits)
  71. else:
  72. if int(pos[1]) > 5:
  73. pos = input('Invalid - Guess a Position: ').upper()
  74. shotpos(pos, grid, hits)
  75. row = int(pos[1])
  76. column = int(ord(pos[0]))-65
  77. if column > 5:
  78. pos = input('Invalid - Guess a Position: ').upper()
  79. shotpos(pos, grid, hits)
  80. if pos in hits:
  81. for i in range(len(hits)):
  82. if pos == hits[i]:
  83. break
  84. grid[column][row] = 'X'
  85. hits.remove(pos)
  86. elif pos not in hits:
  87. grid[column][row] = 'O'
  88. for num in range(6):
  89. for num2 in range(6):
  90. print(' ', grid[num][num2], end='')
  91. print('t ', end='')
  92. for num2 in range(6):
  93. print(' ', grid2[num][num2], end='')
  94. print()
  95. return
  96. def test(hits):
  97. for i in range(len(hits)):
  98. if hits.count(hits[i]) > 1:
  99. hits.clear()
  100. pick(hits)
  101. def pick(hits):
  102. for i in range(6):
  103. first = random.randint(65, 69)
  104. first = chr(first)
  105. second = random.randint(1,5)
  106. second = str(second)
  107. both = first+second
  108. hits.append(both)
  109. test(hits)
  110. def compshot(grid2, comp_hits, already):
  111. row = random.randint(1,5)
  112. column = chr(random.randint(65,69))
  113. pos = column+str(row)
  114. column = ord(column)-65
  115. if pos in already:
  116. compshot(grid2, comp_hits, already)
  117. elif pos in comp_hits:
  118. for i in range(len(comp_hits)):
  119. if pos == comp_hits[i]:
  120. break
  121. grid2[column][row] = 'X'
  122. comp_hits.remove(pos)
  123. already.append(pos)
  124. elif pos not in comp_hits:
  125. grid2[column][row] = 'O'
  126. already.append(pos)
  127. for num in range(6):
  128. for num2 in range(6):
  129. print(' ', grid[num][num2], end='')
  130. print('t ', end='')
  131. for num2 in range(6):
  132. print(' ', grid2[num][num2], end='')
  133. print()
  134. return
  135. #Takes User name and Capitalize it.
  136. user_name = (input('Please Enter Your Name: ')+'.').capitalize()
  137. #Outputs the user_name and asks if wanting to play.
  138. print('Hello,', user_name, 'Are You ready to play Battleships?')
  139. #Gets User answer and Checks if want to play
  140. if (input(' '))[0].lower() == 'n':
  141. #If the answer is no, Quit the Game
  142. print('Ok, That's Fine')
  143. SystemExit()
  144. #Grid of Asterisks That makes up the Playing Field
  145. grid = [['A ','*','*','*','*','*'],
  146. ['B ','*','*','*','*','*'],
  147. ['C ','*','*','*','*','*'],
  148. ['D ','*','*','*','*','*'],
  149. ['E ','*','*','*','*','*'],
  150. [' ','1','2','3','4','5']]
  151. grid2 = [['A ','*','*','*','*','*'],
  152. ['B ','*','*','*','*','*'],
  153. ['C ','*','*','*','*','*'],
  154. ['D ','*','*','*','*','*'],
  155. ['E ','*','*','*','*','*'],
  156. [' ','1','2','3','4','5']]
  157. computer_values = input('Enter 6 positions')
  158. computer_values = computer_values.split()
  159. values = computer_values
  160. checkpos(computer_values, values)
  161. compvals(comp_hits, values)
  162. #Prints out Grid
  163. for num in range(6):
  164. for num2 in range(6):
  165. print(' ', grid[num][num2], end='')
  166. print('t ', end='')
  167. for num2 in range(6):
  168. print(' ', grid2[num][num2], end='')
  169. print()
  170. pick(hits)
  171. while hits:
  172. pos = input('Guess a position: ').upper()
  173. shotpos(pos, grid, hits)
  174. print('Computer is thinking...')
  175. time.sleep(1)
  176. compshot(grid2, comp_hits, already)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement