Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #begin
  2. from random import randint
  3. import numpy as np
  4.  
  5. class BattleShipGame():
  6. #create 3d matrix
  7. userArr = np.zeros((8,8))
  8. aiArr = np.zeros((8,8))
  9. ship_lengths = [1,2,3,4]
  10.  
  11.  
  12.  
  13. #function to place AI ships
  14. def placeShips(self):
  15. for i in ship_lengths:
  16. for j in range(0,i):
  17. previous_point = []
  18. while(True):
  19. a = [randint(0,len(aiArr)-1), randint(0,len(aiArr)-1)]
  20. if j==0:
  21. if aiArr[a[0],a[1]] == 0:
  22. aiArr[a[0],a[1]] = 1
  23. break;
  24. elif aiArr[a[0],a[1]] == 0 and isNextTo(aiArr[a[0],a[1]], previous_point
  25. aiArr[a[0],a[1]] == 1
  26. previous_point = [a[0],a[1]]
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. #check if said point is next to another, if it is, place
  34. def isNextTo(self,point1,point2):
  35. if point1[0]+1==point2[0] or point1[0]-1==point2[0]:
  36. return True
  37. elif point1[1]+1==point2[1] or point1[1]-1==point2[1]:
  38. return True
  39. else:
  40. return False
  41.  
  42.  
  43. #function to let user choose coordinates of ship to hit
  44.  
  45.  
  46.  
  47. #function to let user place ships
  48.  
  49.  
  50.  
  51. #function to see if the user hits the ships
  52.  
  53.  
  54.  
  55. #data structure to store ships and hits
  56.  
  57.  
  58.  
  59. #function to see if ship is sunk
  60.  
  61.  
  62.  
  63. #function to start game
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement