vojta249

small_console_game

Nov 2nd, 2023 (edited)
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.84 KB | None | 0 0
  1. def print_plan(plan):
  2.     for rows in range(len(plan)):
  3.         for collumn in range(len(plan[rows])):
  4.             if plan[rows][collumn] == 1:
  5.                 print("X", end="")
  6.             elif plan[rows][collumn] == 2:
  7.                 print("O", end="")
  8.             elif plan[rows][collumn] == 3:
  9.                 print("*", end="")
  10.             else:
  11.                 print(".", end="")
  12.         print()
  13.  
  14.  
  15. def make_empty_plan(width, height):
  16.     plan = []
  17.     for rows in range(height):
  18.         plan.append([])
  19.     for rows in range(len(plan)):
  20.         for _ in range(width):
  21.             plan[rows].append(0)
  22.     return plan
  23.  
  24.  
  25. def strategy_random(plan, free_spaces):
  26.     position = random.randint(1, free_spaces)
  27.     for rows in range(len(plan)):
  28.         for collumn in range(len(plan[rows])):
  29.             if plan[rows][collumn] == 0:
  30.                 position -= 1
  31.             if position == 0:
  32.                 return [rows, collumn]
  33.  
  34.  
  35. def strategy_maxblock(plan, played, return_amount=False):
  36.     maximal_damage = 0
  37.     point_plan = make_empty_plan(len(plan[0]), len(plan))
  38.     for k in range(len(plan)):
  39.         for m in range(len(plan[k])):
  40.             if plan[k][m] == 0:
  41.                 for i in range(-1, 2):
  42.                     for j in range(-1, 2):
  43.                         if 0 <= k - i <= len(plan)-1 and\
  44.                            0 <= m - j <= len(plan[k])-1 and\
  45.                            plan[k-i][m-j] == 0:
  46.                             point_plan[k][m] += 1
  47.     for i in range(len(point_plan)):
  48.         for j in range(len(point_plan[i])):
  49.             if point_plan[i][j] > maximal_damage:
  50.                 maximal_damage = point_plan[i][j]
  51.                 played = [i, j]
  52.     if return_amount:
  53.         return played, maximal_damage
  54.     return played
  55.  
  56. def strategy_lastplace(plan, free_spaces, played):
  57.     played, damage = strategy_maxblock(plan, played, True)
  58.     if free_spaces <= 9 and free_spaces == damage:
  59.         return played
  60.     played = strategy_random(plan, free_spaces)
  61.     return played
  62.  
  63. def replacer(plan, played, player):
  64.     removed_spaces = 0
  65.     for i in range(-1, 2):
  66.         for j in range(-1, 2):
  67.             if 0 <= played[0] - i <= len(plan)-1 and\
  68.                0 <= played[1] - j <= len(plan[0])-1 and\
  69.                plan[played[0]-i][played[1]-j] == 0:
  70.                 plan[played[0]-i][played[1]-j] = 3
  71.                 removed_spaces += 1
  72.     plan[played[0]][played[1]] = player
  73.     return removed_spaces
  74.  
  75.  
  76. def one_strategy(plan, free_spaces, strategy, player):
  77.     played = [0, 0]
  78.     if strategy == 1 and player == 2:
  79.         played = strategy_random(plan, free_spaces)
  80.     elif strategy == 2 and player == 2:
  81.         played = strategy_maxblock(plan, played)
  82.     elif strategy == 3 and player == 2:
  83.         played = strategy_lastplace(plan, free_spaces, played)
  84.     else:
  85.         played[1] = int(input("Please input coordinate of X axis "))-1
  86.         played[0] = int(input("Please input coordinate of Y axis "))-1
  87.     free_spaces -= (replacer(plan, played, player))
  88.     print("Moved:", (played[1]+1), (played[0]+1))
  89.     return free_spaces
  90.  
  91. def which_strategy(plan, free_spaces, strategy, played):
  92.     if strategy == 1:
  93.         played = strategy_random(plan, free_spaces)
  94.     elif strategy == 2:
  95.         played = strategy_maxblock(plan, played)
  96.     else:
  97.         played = strategy_lastplace(plan, free_spaces, played)
  98.     return played
  99.  
  100.  
  101. def two_strategies(plan, free_spaces, strategy_1, strategy_2, player):
  102.     played = [0, 0]
  103.     if player == 1:
  104.         played = which_strategy(plan, free_spaces, strategy_1, played)
  105.     else:
  106.         played = which_strategy(plan, free_spaces, strategy_2, played)
  107.     print("Player", player, "moves")
  108.     print("Moved:", (played[0]+1), (played[1]+1))
  109.     free_spaces -= replacer(plan, played, player)
  110.     return free_spaces
  111.  
  112. def two_players_game(plan, free_spaces, player):
  113.     played = [0, 0]
  114.     print("Player", player, "moves")
  115.     played[1] = int(input("Pleaase input coordinate of X axes"))-1
  116.     played[0] = int(input("Pleaase input coordinate of Y axes"))-1
  117.     print("Moved:", (played[0]+1), (played[1]+1))
  118.     free_spaces -= replacer(plan, played, player)
  119.     return free_spaces
  120.  
  121. def play_game(width, height, strategy_1=None, strategy_2=None):
  122.     free_spaces = width * height
  123.     player = 2
  124.     plan = make_empty_plan(width, height)
  125.     while free_spaces > 0:
  126.         player = 3 - player
  127.         if strategy_1 is None and strategy_2 is None:
  128.             free_spaces = two_players_game(plan, free_spaces, player)
  129.         elif strategy_2 is None:
  130.             free_spaces = one_strategy(plan, free_spaces, strategy_1, player)
  131.         else:
  132.             free_spaces = two_strategies(plan, free_spaces, strategy_1,
  133.                                          strategy_2, player)
  134.         print_plan(plan)
  135.     print("Player", player, "wins")
Advertisement
Add Comment
Please, Sign In to add comment