Advertisement
Guest User

SeaBattle

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