Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. from random import random, randint
  2.  
  3. hit_board = []
  4. enemy_ship_board = []
  5.  
  6. for x in range(10):
  7. hit_board.append(["0"] * 10)
  8. enemy_ship_board.append(["0"] * 10)
  9.  
  10. def new_screen():
  11. print("Battleship!")
  12. print_board(hit_board)
  13.  
  14. def print_board(board):
  15. for row in board:
  16. print(" ".join(row))
  17. print("\n")
  18.  
  19. def random_position(board, xBound, yBound):
  20. xCoordinate = randint(0, (len(board) - 1) - xBound)
  21. yCoordinate = randint(0, (len(board[0]) - 1) - yBound)
  22. return (xCoordinate, yCoordinate)
  23.  
  24. class Ship:
  25. def __init__(self, name, size):
  26. self.size = size
  27. self.name = name
  28. self.direction = (int)(random() * 2) * 90
  29. self.positions = []
  30. self.damage = 0
  31.  
  32. empty_space = False
  33. row = 0
  34. col = 0
  35. while not empty_space:
  36. empty_space = True
  37. if self.direction == 0:
  38. (row, col) = random_position(hit_board, self.size, 0)
  39. for i in range(self.size):
  40. if enemy_ship_board[row + i][col] == 1:
  41. empty_space = False
  42. break
  43. elif self.direction == 90:
  44. (row, col) = random_position(hit_board, 0, self.size)
  45. for i in range(self.size):
  46. if enemy_ship_board[row][col + i] == 1:
  47. empty_space = False
  48. break
  49.  
  50. if self.direction == 0:
  51. for i in range(self.size):
  52. self.positions.append((row + i, col))
  53. enemy_ship_board[row + i][col] = 1
  54. elif self.direction == 90:
  55. for i in range(self.size):
  56. self.positions.append((row, col + i))
  57. enemy_ship_board[row][col + i] = 1
  58.  
  59. ships = []
  60. ships.append(Ship("submarine", 1))
  61. ships.append(Ship("corsair", 2))
  62. ships.append(Ship("cruiser", 3))
  63. ships.append(Ship("battleship", 4))
  64. ships.append(Ship("carrier", 5))
  65.  
  66. #for debugging
  67. for ship in ships:
  68. print(ship.name + " positions: ")
  69. for position in ship.positions:
  70. print(str(position[0]) + "," + str(position[1]))
  71.  
  72. def main():
  73. while(len(ships) > 0):
  74. new_screen()
  75. guess_row = int(input("Enter Row: "))
  76. guess_col = int(input("Enter Col: "))
  77. if (guess_row < 0 or guess_row > (len(hit_board) - 1) or guess_row == "") or (guess_col <0 or guess_col > (len(hit_board[0]) - 1) or guess_col == ""):
  78. print("That Position is not on the board")
  79. elif(hit_board[guess_row][guess_col] != "0"):
  80. print("You already fired at that spot")
  81. else:
  82. hit = False
  83. for ship in ships:
  84. for position in ship.positions:
  85. if guess_row == position[0] and guess_col == position[1]:
  86. hit = True
  87. ship.damage += 1
  88. if ship.damage == ship.size:
  89. print("You sunk my " + ship.name)
  90. for section in ship.positions:
  91. hit_board[section[0]][section[1]] = "D"
  92. ships.remove(ship)
  93. else:
  94. print("You hit my " + ship.name)
  95. hit_board[guess_row][guess_col] = "H"
  96. break
  97. if hit == True:
  98. break
  99. if hit == False:
  100. print("Ha you missed")
  101. hit_board[guess_row][guess_col] = "X"
  102. print("you sunk all my ships")
  103.  
  104. if __name__ == "__main__":
  105. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement