VitalyD

Untitled

Jun 19th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.64 KB | None | 0 0
  1. import random
  2. import os
  3.  
  4. # Configs:
  5. size = 10
  6. monitoring = False
  7.  
  8. # Style:
  9. s_buffer = "^"
  10. s_ship = "A"
  11. s_space = "."
  12. s_hit = "x"
  13. s_destroyed = "W"
  14. s_miss = "*"
  15.  
  16. # Available ships (quantity, size):
  17. ships_list = [[1, 4], [2, 3], [3, 2], [4, 1]]
  18.  
  19.  
  20. class Board(object):
  21.  
  22.     def __init__(self):
  23.         self.board = []
  24.         # Here will be containing spawned ships information:
  25.         self.spawned = []
  26.  
  27.     def create(self):
  28.         for row in range(size):
  29.             self.board.append([s_space] * size)
  30.  
  31.     def random(self):
  32.  
  33.         for ship in ships_list:
  34.             for unit in range(ship[0]):
  35.  
  36.                 spawning = True
  37.                 while spawning:
  38.  
  39.                     # Define the refer of ship (0 - x, 1 - y):
  40.                     global refer
  41.                     refer = random.randrange(2)
  42.                     if refer == 0:
  43.                         location_y = random.randrange(size)
  44.                         location_x = random.randrange(size - (ship[1] - 1))
  45.                     else:
  46.                         location_y = random.randrange(size - (ship[1] - 1))
  47.                         location_x = random.randrange(size)
  48.  
  49.                     # Testing if ship has own space on the board:
  50.                     offset = 0
  51.                     for testing in range(ship[1]):
  52.                         if refer == 0 and self.board[location_y][location_x + offset] != s_space:
  53.                             continue
  54.                         elif refer == 1 and self.board[location_y + offset][location_x] != s_space:
  55.                             continue
  56.                         offset += 1
  57.                         if offset == ship[1]:
  58.                             spawning = False
  59.  
  60.                 # Creating ship body:
  61.                 offset = 0
  62.                 current_ship = []
  63.                 for marker in range(ship[1]):
  64.                     if refer == 0:
  65.                         self.board[location_y][location_x + offset] = s_ship
  66.                         current_ship.append([location_y, location_x + offset])
  67.                     else:
  68.                         self.board[location_y + offset][location_x] = s_ship
  69.                         current_ship.append([location_y + offset, location_x])
  70.                     offset += 1
  71.                 self.spawned.append(current_ship)
  72.  
  73.                 # Creating buffer zone for unit:
  74.                 for unit_point in current_ship:
  75.                     for buffer_point in ([0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [1, -1], [-1, 1], [-1, -1]):
  76.                         b_point_y = unit_point[0] + buffer_point[0]
  77.                         b_point_x = unit_point[1] + buffer_point[1]
  78.                         if b_point_y in range(size) and b_point_x in range(size):
  79.                             if self.board[b_point_y][b_point_x] == s_space:
  80.                                 self.board[b_point_y][b_point_x] = s_buffer
  81.  
  82.     def updating(self, ship):
  83.         # Creating buffer zone for unit:
  84.         for unit in ship:
  85.             for buffer_point in ([0, 0], [0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [1, -1], [-1, 1], [-1, -1]):
  86.                 b_point_y = unit[0] + buffer_point[0]
  87.                 b_point_x = unit[1] + buffer_point[1]
  88.                 if b_point_y in range(size) and b_point_x in range(size):
  89.                     if self.board[b_point_y][b_point_x] == s_buffer:
  90.                         self.board[b_point_y][b_point_x] = s_miss
  91.                     elif self.board[b_point_y][b_point_x] == s_hit:
  92.                         self.board[b_point_y][b_point_x] = s_destroyed
  93.  
  94.  
  95. def print_boards():
  96.     # Printing top of the scale:
  97.     print("\n    Your board" + (" " * (size + 5)) + "Enemy board")
  98.     print("    " + (" ".join(str(i) for i in list(range(size)))), end=(" " * 2))
  99.     print("    " + (" ".join(str(i) for i in list(range(size)))))
  100.     print("   " + (" |" * size), end=(" " * 2))
  101.     print("   " + (" |" * size))
  102.     # Printing left part of scale and board:
  103.     n = 0
  104.     for i in range(size):
  105.         if monitoring:
  106.             print(str(n) + " - " + " ".join(str(i) for i in player.board[n]), end=(" " * 2))
  107.             print(str(n) + " - " + " ".join(str(i) for i in ai.board[n]))
  108.         else:
  109.             print(str(n) + " - " + " ".join(str(i) for i in player.board[n]).replace(s_buffer, s_space), end=(" " * 2))
  110.             print(str(n) + " - " + " ".join(str(i) for i in ai.board[n]).replace(s_ship, s_space).replace(s_buffer, s_space))
  111.         n += 1
  112.  
  113.  
  114. def press_ent():
  115.     input("Press the Enter bottom to continue.\n")
  116.  
  117.  
  118. def state_of_ships(enemy):
  119.     global destroy
  120.     destroy = False
  121.     # Checking state of all ships:
  122.     for d_ship in enemy.spawned:
  123.         damage = 0
  124.         for d_unit in d_ship:
  125.             if enemy.board[d_unit[0]][d_unit[1]] == s_hit:
  126.                 damage += 1
  127.         # If the current ship was destroyed:
  128.         if damage == len(d_ship):
  129.             enemy.updating(d_ship)
  130.             enemy.spawned.remove(d_ship)
  131.             destroy = True
  132.  
  133.  
  134. def ai_pass():
  135.     ai_guessing = True
  136.     while ai_guessing:
  137.  
  138.         # Probability of AI intuition (zero is enable):
  139.         ai_intuition = random.randrange(size * 5)
  140.  
  141.         # AI intuition (dishonest searching for enemy valid ship):
  142.         if ai_intuition == 0:
  143.             ai_int_ship = random.randrange(len(player.spawned))  # Choosing for ship.
  144.             ai_int_unit = random.randrange(len(player.spawned[ai_int_ship]))  # Choosing for unit.
  145.             ai_guess_y = player.spawned[ai_int_ship][ai_int_unit][0]
  146.             ai_guess_x = player.spawned[ai_int_ship][ai_int_unit][1]
  147.  
  148.         # AI random guessing:
  149.         else:
  150.             ai_guess_y = random.randrange(size)
  151.             ai_guess_x = random.randrange(size)
  152.  
  153.         # Checking. If hit:
  154.         if player.board[ai_guess_y][ai_guess_x] == s_ship:
  155.             player.board[ai_guess_y][ai_guess_x] = s_hit
  156.             state_of_ships(player)
  157.             if destroy:
  158.                 print("\nAI has destroyed your ship (X: %s, Y: %s)." % (ai_guess_x, ai_guess_y))
  159.             else:
  160.                 print("\nAI has damaged your ship (X: %s, Y: %s)." % (ai_guess_x, ai_guess_y))
  161.             break
  162.  
  163.         # Checking. If miss:
  164.         elif player.board[ai_guess_y][ai_guess_x] == s_space or player.board[ai_guess_y][ai_guess_x] == s_buffer:
  165.             player.board[ai_guess_y][ai_guess_x] = s_miss
  166.             print("\nAI has miss (X: %s, Y: %s)." % (ai_guess_x, ai_guess_y))
  167.             break
  168.  
  169.         # Checking. Otherwise restart guessing:
  170.         else:
  171.             continue
  172.  
  173.  
  174. def clear():
  175.     # Clearing of previous board and text:
  176.     os.system('cls' if os.name == 'nt' else 'clear')
  177.  
  178. # Welcome:
  179. print("Welcome to \"Sea Battle\" BETA version by Aunmag!\n")
  180. press_ent()
  181.  
  182. # Board AI creating:
  183. ai = Board()
  184. ai.create()
  185. ai.random()
  186.  
  187. # Board player creating:
  188. while True:
  189.     clear()
  190.     player = Board()
  191.     player.create()
  192.     player.random()
  193.     print("\nHIT: Here will be printing information about enemy.")
  194.     print_boards()
  195.     print("\nLook at your board (by left side).")
  196.     print("Press the Enter button to change spawn of your ships again (random).")
  197.     regenerate = input("Or if you're ready enter \"c\" letter here to continue: ")
  198.     if str(regenerate.lower()) != "c":
  199.         continue
  200.     else:
  201.         break
  202.  
  203. # Board player creating:
  204. print("\nOk! AI is going to start first. Get ready!\n")
  205. press_ent()
  206.  
  207.  
  208. game = True
  209. while game:
  210.  
  211.     clear()
  212.     ai_pass()
  213.     print_boards()
  214.  
  215.     guessing = True
  216.     while guessing:
  217.  
  218.         # Printing information about lost enemy's ships:
  219.         print("\nEnemy ships remain: " + str(len(ai.spawned)) + ". ", end="")
  220.         print("Your ships remain: " + str(len(player.spawned)) + ".")
  221.  
  222.         # Inputting the coordinates to hit:
  223.         guess_x = input("Choose X (column) position to strike: ")
  224.         guess_y = input("Choose Y (line) position to strike: ")
  225.  
  226.         # Checking. If guessing can't be integer:
  227.         if not guess_x.isdigit() or not guess_y.isdigit():
  228.             print("\nError! You've entered wrong coordinates! Change your choose.")
  229.             continue
  230.  
  231.         # Converting guessing to integer form string:
  232.         guess_x = int(guess_x)
  233.         guess_y = int(guess_y)
  234.  
  235.         # Checking. If guessing isn't in the board:
  236.         if not (guess_x in range(size)) or not (guess_y in range(size)):
  237.             print("\nError! This location is too far to hit it! Change your choose.")
  238.             continue
  239.  
  240.         # Checking. If hit:
  241.         elif ai.board[guess_y][guess_x] == s_ship:
  242.             ai.board[guess_y][guess_x] = s_hit
  243.             state_of_ships(ai)
  244.             if destroy:
  245.                 print("\nYou've destroyed enemy ship!", end=" ")
  246.             else:
  247.                 print("\nYou've damaged enemy ship!", end=" ")
  248.             press_ent()
  249.             break
  250.  
  251.         # Checking. If miss:
  252.         elif ai.board[guess_y][guess_x] == s_space or ai.board[guess_y][guess_x] == s_buffer:
  253.             ai.board[guess_y][guess_x] = s_miss
  254.             print("\nYou've missed.", end=" ")
  255.             press_ent()
  256.  
  257.         # Checking. If hit the same palace again:
  258.         else:
  259.             print("\nYou've already hit to this location, change your choose.")
  260.             continue
  261.  
  262.         # Checking. End:
  263.         break
  264.  
  265.     if len(ai.spawned) == 0:
  266.         input("You are WINNER! You've destroyed all enemy units! Press the Enter to end this game.")
  267.         break
  268.  
  269.     if len(player.spawned) == 0:
  270.         print("You are LOSER! All your units were destroyed.")
  271.         input("Total enemy ships remain: " + str(len(ai.spawned)) + ".")
  272.         break
Advertisement
Add Comment
Please, Sign In to add comment