Advertisement
Guest User

SeaBattle

a guest
Oct 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.36 KB | None | 0 0
  1. import random
  2. class s_map:
  3.     def __init__(self, ship_locations):
  4.         self.s_loc = ship_locations
  5.         self.hit_coord = []
  6.         self.missed_coord = []
  7.         self.zone = dict.fromkeys((chr(i) + str(k) for i in range(65, 75) for k in range(1,11)), None)
  8.         self.zone.update(ship_locations)
  9.  
  10.     def __str__(self):
  11.         return str(self.zone)
  12.  
  13.     def beautiful_output(self):
  14.         global ship_list
  15.         output = ''
  16.         list_keys = sorted(list(sorted(self.zone.keys())), key = lambda x: int(x[1:]))
  17.         for i in list_keys:
  18.             coord = str(self.zone.get(i))
  19.             if coord ==  'None':
  20.                 output += ' ~ '
  21.             else:
  22.                 _ship = ship_list[int(coord[-1])]
  23.                 if _ship.get_c(i) == False:
  24.                     output += ' @ '
  25.                 else:
  26.                     output += ' X '
  27.             if i[0] == 'J':
  28.                 output += ' '
  29.                 output += '\n'
  30.         return str(output)
  31.  
  32.     def beautiful_output_bot(self):
  33.         output = ''
  34.         list_keys = sorted(list(sorted(self.zone.keys())), key = lambda x: int(x[1:]))
  35.         for i in list_keys:
  36.             if i in self.hit_coord:
  37.                 output += ' X '
  38.             elif i in self.missed_coord:
  39.                 output += ' * '
  40.             else:
  41.                 output += ' ~ '
  42.             if i[0] == 'J':
  43.                 output += ' '
  44.                 output += '\n'
  45.  
  46.         return str(output)
  47.  
  48.  
  49.  
  50.     def strike_p(self, coord):
  51.         global ship_list
  52.         if coord in self.hit_coord:
  53.             self.strike_p(bot_hit_point())
  54.         coord_value = self.zone.get(coord)
  55.         if coord_value == None:
  56.             self.missed_coord.append(coord)
  57.             return False
  58.         else:
  59.             self.hit_coord.append(coord)
  60.             hit_ship = ship_list[int(coord_value[-1])]
  61.             hit_ship.hp.update({coord : True})
  62.             if hit_ship.is_dead() == True:
  63.                 return 'dead'
  64.             else:
  65.                 return 'still alive'
  66.  
  67.  
  68.     def strike_b(self, coord):
  69.         global ship_list_bot
  70.         if coord in self.hit_coord:
  71.             self.strike_b(input("You've already hit this point, enter another: "))
  72.         coord_value = self.zone.get(coord)
  73.         if coord_value == None:
  74.             self.missed_coord.append(coord)
  75.             return False
  76.         else:
  77.             self.hit_coord.append(coord)
  78.             hit_ship = ship_list_bot[int(coord_value[-1])]
  79.             hit_ship.hp.update({coord : True})
  80.             if hit_ship.is_dead() == True:
  81.                 return 'dead'
  82.             else:
  83.                 return 'still alive'
  84.  
  85.  
  86.  
  87.     def still_playing(self):
  88.         global ship_list
  89.         for i in ship_list:
  90.             for j in i.hp.values():
  91.                 if j == False:
  92.                     return True
  93.         return False
  94.  
  95.     def still_playing_bot(self):
  96.         global ship_list_bot
  97.         for i in ship_list_bot:
  98.             for j in i.hp.values():
  99.                 if j == False:
  100.                     return True
  101.         return False
  102.  
  103.  
  104.  
  105. class ship:
  106.     def __init__(self, param_list):
  107.         self.size = param_list[0]
  108.         self.hp = dict.fromkeys(param_list[1], False)
  109.  
  110.     def __str__(self):
  111.         return str(self.hp)
  112.  
  113.     def get_c(self, coord):
  114.         return self.hp.get(coord)
  115.  
  116.  
  117.     def is_dead(self):
  118.         if False in list(self.hp.values()):
  119.             return False
  120.         else:
  121.             return True
  122.  
  123. index = 0
  124. index_bot = 0
  125. ship_locations = {}
  126. ship_locations_bot = {}
  127. ship_list = []
  128. ship_list_bot = []
  129. used_coords = set()
  130. used_coords_bot = set()
  131.  
  132. def bot_vert(size):
  133.     coord_letter = random.choice(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'])
  134.     coord_row = random.randint(1, (11-size))
  135.     output = str(size)
  136.     for i in range(size):
  137.         output += ' '
  138.         output += str(coord_letter)
  139.         output += str(coord_row + i)
  140.  
  141.  
  142.     return output
  143.  
  144. def bot_hor(size):
  145.     coord_row = random.randint(1, 10)
  146.     coord_letter = random.randint(65, 74 - size)
  147.     output = str(size)
  148.     for i in range(size):
  149.         output += ' '
  150.         output += chr(coord_letter + i)
  151.         output += str(coord_row)
  152.  
  153.  
  154.     return output
  155.  
  156. def bot_add_ship(size):
  157.     global ship_list
  158.     decision = random.choice(['vert', 'hor'])
  159.     if decision == 'vert':
  160.         string = bot_vert(size)
  161.         while ship_add_bot(string) != None:
  162.             string = bot_vert(size)
  163.  
  164.     else:
  165.         string = bot_hor(size)
  166.         while ship_add_bot(string) != None:
  167.             string = bot_hor(size)
  168.  
  169. def is_used(string):
  170.     global used_coords
  171.     for i in string.split()[1:]:
  172.         if i in used_coords:
  173.             return True
  174.         else:
  175.             used_coords.add(i)
  176.     return False
  177.  
  178. def is_used_bot(string):
  179.     global used_coords_bot
  180.     for i in string.split()[1:]:
  181.         if i in used_coords_bot:
  182.             return True
  183.         else:
  184.             used_coords_bot.add(i)
  185.     return False
  186.  
  187. def ship_to_dict(ship):
  188.     global ship_locations, index
  189.     ship_locations.update(dict.fromkeys(list(ship.hp.keys()), 'ship' + str(index)))
  190.     index += 1
  191.     return ship_locations
  192.  
  193. def ship_to_dict_bot(ship):
  194.     global ship_locations_bot, index_bot
  195.     ship_locations_bot.update(dict.fromkeys(list(ship.hp.keys()), 'ship' + str(index_bot)))
  196.     index_bot += 1
  197.     return ship_locations_bot
  198.  
  199. def Hello_msg():
  200.     print('Hello! Welcome to SeaBattle! Start adding your ships, following this pattern: size "coord" "coord" "coord" "coord" ')
  201.     print("Remeber the rules: you can only put your ship in a vertical or horizontal way." + '\n' + "There're 10 ships you can built: 1 four-deck, 2 three-deck, 3 two-deck and 4 one-deck.")
  202.  
  203. def ship_add(string):
  204.     global ship_list
  205.     if is_used(string) == True:
  206.         print('There is a ship with this coordinates, try another coordinates:')
  207.         ship_add(input())
  208.     string_param = (int(string.split()[0]) , string.split()[1:])
  209.     ship_list.append(ship(string_param))
  210.     ship_to_dict(ship(string_param))
  211.     return None
  212.  
  213. def ship_add_bot(string):
  214.     global ship_list_bot
  215.     if is_used_bot(string) == True:
  216.         return 'Error'
  217.     string_param = (int(string.split()[0]) , string.split()[1:])
  218.     ship_list_bot.append(ship(string_param))
  219.     ship_to_dict_bot(ship(string_param))
  220.     return None
  221.  
  222. def bot_start():
  223.     ship_size = [1, 1, 1, 1, 2, 2, 2, 3, 3, 4]
  224.     for i in ship_size:
  225.         bot_add_ship(i)
  226.  
  227. def bot_hit_point():
  228.     coord_letter = random.choice(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'])
  229.     coord_row = random.randint(1, 10)
  230.     hit_point = str(coord_letter) + str(coord_row)
  231.     return hit_point
  232.  
  233. def starting_game():
  234.     global ship_locations
  235.     global ship_locations_bot
  236.     Hello_msg()
  237.     bot_start()
  238.     bot_map = s_map(ship_locations_bot)
  239.     for i in range(10):
  240.         ship_add(input())
  241.     player_map = s_map(ship_locations)
  242.     print(ship_locations_bot)
  243.     print("Okay, let's start!")
  244.     print(player_map.beautiful_output())
  245.     print("It's your turn, enter coordinate to shoot: ")
  246.     while player_map.still_playing() == True and bot_map.still_playing_bot() == True:
  247.         hit_point_player = input('Enter coordiate: ')
  248.         player_turn = bot_map.strike_b(hit_point_player)
  249.         while player_turn != False:
  250.             print('You hit the ship, it is ' + player_turn)
  251.             print('Bot map: ' + '\n' + bot_map.beautiful_output_bot())
  252.             hit_point_player = input('You hit the target, enter one more coordinate: ')
  253.             player_turn = bot_map.strike_b(hit_point_player)
  254.         print('You missed' + '\n')
  255.         print('Bot map: ' + '\n' + bot_map.beautiful_output_bot())
  256.         hit_point_bot = bot_hit_point()
  257.         bot_turn = player_map.strike_p(hit_point_bot)
  258.         while bot_turn != False:
  259.             print('Bot hit the ship, it is ' + bot_turn)
  260.             print('Your map: ' + '\n' + player_map.beautiful_output_bot())
  261.             hit_point_bot = bot_hit_point()
  262.             bot_turn = player_map.strike_p(hit_point_bot)
  263.         print("Bot missed, it's your turn now")
  264.         print('Your map: ' + '\n' + player_map.beautiful_output())
  265.  
  266.     print('The game is over')
  267.     if player_map.still_playing() == True:
  268.         print('You lost')
  269.     else:
  270.         print('You won')
  271.  
  272.  
  273.     return 0
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280. starting_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement