Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- class s_map:
- def __init__(self, ship_locations):
- self.s_loc = ship_locations
- self.hit_coord = []
- self.missed_coord = []
- self.zone = dict.fromkeys((chr(i) + str(k) for i in range(65, 75) for k in range(1,11)), None)
- self.zone.update(ship_locations)
- def __str__(self):
- return str(self.zone)
- def beautiful_output(self):
- global ship_list
- output = ''
- list_keys = sorted(list(sorted(self.zone.keys())), key = lambda x: int(x[1:]))
- for i in list_keys:
- coord = str(self.zone.get(i))
- if coord == 'None':
- output += ' ~ '
- else:
- _ship = ship_list[int(coord[-1])]
- if _ship.get_c(i) == False:
- output += ' @ '
- else:
- output += ' X '
- if i[0] == 'J':
- output += ' '
- output += '\n'
- return str(output)
- def beautiful_output_bot(self):
- output = ''
- list_keys = sorted(list(sorted(self.zone.keys())), key = lambda x: int(x[1:]))
- for i in list_keys:
- if i in self.hit_coord:
- output += ' X '
- elif i in self.missed_coord:
- output += ' * '
- else:
- output += ' ~ '
- if i[0] == 'J':
- output += ' '
- output += '\n'
- return str(output)
- def strike_p(self, coord):
- global ship_list
- if coord in self.hit_coord:
- self.strike_p(bot_hit_point())
- coord_value = self.zone.get(coord)
- if coord_value == None:
- self.missed_coord.append(coord)
- return False
- else:
- self.hit_coord.append(coord)
- hit_ship = ship_list[int(coord_value[-1])]
- hit_ship.hp.update({coord : True})
- if hit_ship.is_dead() == True:
- return 'dead'
- else:
- return 'still alive'
- def strike_b(self, coord):
- global ship_list_bot
- if coord in self.hit_coord:
- self.strike_b(input("You've already hit this point, enter another: "))
- coord_value = self.zone.get(coord)
- if coord_value == None:
- self.missed_coord.append(coord)
- return False
- else:
- self.hit_coord.append(coord)
- hit_ship = ship_list_bot[int(coord_value[-1])]
- hit_ship.hp.update({coord : True})
- if hit_ship.is_dead() == True:
- return 'dead'
- else:
- return 'still alive'
- def still_playing(self):
- global ship_list
- for i in ship_list:
- for j in i.hp.values():
- if j == False:
- return True
- return False
- def still_playing_bot(self):
- global ship_list_bot
- for i in ship_list_bot:
- for j in i.hp.values():
- if j == False:
- return True
- return False
- class ship:
- def __init__(self, param_list):
- self.size = param_list[0]
- self.hp = dict.fromkeys(param_list[1], False)
- def __str__(self):
- return str(self.hp)
- def get_c(self, coord):
- return self.hp.get(coord)
- def is_dead(self):
- if False in list(self.hp.values()):
- return False
- else:
- return True
- index = 0
- index_bot = 0
- ship_locations = {}
- ship_locations_bot = {}
- ship_list = []
- ship_list_bot = []
- used_coords = set()
- used_coords_bot = set()
- def bot_vert(size):
- coord_letter = random.choice(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'])
- coord_row = random.randint(1, (11-size))
- output = str(size)
- for i in range(size):
- output += ' '
- output += str(coord_letter)
- output += str(coord_row + i)
- return output
- def bot_hor(size):
- coord_row = random.randint(1, 10)
- coord_letter = random.randint(65, 74 - size)
- output = str(size)
- for i in range(size):
- output += ' '
- output += chr(coord_letter + i)
- output += str(coord_row)
- return output
- def bot_add_ship(size):
- global ship_list
- decision = random.choice(['vert', 'hor'])
- if decision == 'vert':
- string = bot_vert(size)
- while ship_add_bot(string) != None:
- string = bot_vert(size)
- else:
- string = bot_hor(size)
- while ship_add_bot(string) != None:
- string = bot_hor(size)
- def is_used(string):
- global used_coords
- for i in string.split()[1:]:
- if i in used_coords:
- return True
- else:
- used_coords.add(i)
- return False
- def is_used_bot(string):
- global used_coords_bot
- for i in string.split()[1:]:
- if i in used_coords_bot:
- return True
- else:
- used_coords_bot.add(i)
- return False
- def ship_to_dict(ship):
- global ship_locations, index
- ship_locations.update(dict.fromkeys(list(ship.hp.keys()), 'ship' + str(index)))
- index += 1
- return ship_locations
- def ship_to_dict_bot(ship):
- global ship_locations_bot, index_bot
- ship_locations_bot.update(dict.fromkeys(list(ship.hp.keys()), 'ship' + str(index_bot)))
- index_bot += 1
- return ship_locations_bot
- def Hello_msg():
- print('Hello! Welcome to SeaBattle! Start adding your ships, following this pattern: size "coord" "coord" "coord" "coord" ')
- 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.")
- def ship_add(string):
- global ship_list
- if is_used(string) == True:
- print('There is a ship with this coordinates, try another coordinates:')
- ship_add(input())
- string_param = (int(string.split()[0]) , string.split()[1:])
- ship_list.append(ship(string_param))
- ship_to_dict(ship(string_param))
- return None
- def ship_add_bot(string):
- global ship_list_bot
- if is_used_bot(string) == True:
- return 'Error'
- string_param = (int(string.split()[0]) , string.split()[1:])
- ship_list_bot.append(ship(string_param))
- ship_to_dict_bot(ship(string_param))
- return None
- def bot_start():
- ship_size = [1, 1, 1, 1, 2, 2, 2, 3, 3, 4]
- for i in ship_size:
- bot_add_ship(i)
- def bot_hit_point():
- coord_letter = random.choice(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'])
- coord_row = random.randint(1, 10)
- hit_point = str(coord_letter) + str(coord_row)
- return hit_point
- def starting_game():
- global ship_locations
- global ship_locations_bot
- Hello_msg()
- bot_start()
- bot_map = s_map(ship_locations_bot)
- for i in range(10):
- ship_add(input())
- player_map = s_map(ship_locations)
- print(ship_locations_bot)
- print("Okay, let's start!")
- print(player_map.beautiful_output())
- print("It's your turn, enter coordinate to shoot: ")
- while player_map.still_playing() == True and bot_map.still_playing_bot() == True:
- hit_point_player = input('Enter coordiate: ')
- player_turn = bot_map.strike_b(hit_point_player)
- while player_turn != False:
- print('You hit the ship, it is ' + player_turn)
- print('Bot map: ' + '\n' + bot_map.beautiful_output_bot())
- hit_point_player = input('You hit the target, enter one more coordinate: ')
- player_turn = bot_map.strike_b(hit_point_player)
- print('You missed' + '\n')
- print('Bot map: ' + '\n' + bot_map.beautiful_output_bot())
- hit_point_bot = bot_hit_point()
- bot_turn = player_map.strike_p(hit_point_bot)
- while bot_turn != False:
- print('Bot hit the ship, it is ' + bot_turn)
- print('Your map: ' + '\n' + player_map.beautiful_output_bot())
- hit_point_bot = bot_hit_point()
- bot_turn = player_map.strike_p(hit_point_bot)
- print("Bot missed, it's your turn now")
- print('Your map: ' + '\n' + player_map.beautiful_output())
- print('The game is over')
- if player_map.still_playing() == True:
- print('You lost')
- else:
- print('You won')
- return 0
- starting_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement