Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def print_plan(plan):
- for rows in range(len(plan)):
- for collumn in range(len(plan[rows])):
- if plan[rows][collumn] == 1:
- print("X", end="")
- elif plan[rows][collumn] == 2:
- print("O", end="")
- elif plan[rows][collumn] == 3:
- print("*", end="")
- else:
- print(".", end="")
- print()
- def make_empty_plan(width, height):
- plan = []
- for rows in range(height):
- plan.append([])
- for rows in range(len(plan)):
- for _ in range(width):
- plan[rows].append(0)
- return plan
- def strategy_random(plan, free_spaces):
- position = random.randint(1, free_spaces)
- for rows in range(len(plan)):
- for collumn in range(len(plan[rows])):
- if plan[rows][collumn] == 0:
- position -= 1
- if position == 0:
- return [rows, collumn]
- def strategy_maxblock(plan, played, return_amount=False):
- maximal_damage = 0
- point_plan = make_empty_plan(len(plan[0]), len(plan))
- for k in range(len(plan)):
- for m in range(len(plan[k])):
- if plan[k][m] == 0:
- for i in range(-1, 2):
- for j in range(-1, 2):
- if 0 <= k - i <= len(plan)-1 and\
- 0 <= m - j <= len(plan[k])-1 and\
- plan[k-i][m-j] == 0:
- point_plan[k][m] += 1
- for i in range(len(point_plan)):
- for j in range(len(point_plan[i])):
- if point_plan[i][j] > maximal_damage:
- maximal_damage = point_plan[i][j]
- played = [i, j]
- if return_amount:
- return played, maximal_damage
- return played
- def strategy_lastplace(plan, free_spaces, played):
- played, damage = strategy_maxblock(plan, played, True)
- if free_spaces <= 9 and free_spaces == damage:
- return played
- played = strategy_random(plan, free_spaces)
- return played
- def replacer(plan, played, player):
- removed_spaces = 0
- for i in range(-1, 2):
- for j in range(-1, 2):
- if 0 <= played[0] - i <= len(plan)-1 and\
- 0 <= played[1] - j <= len(plan[0])-1 and\
- plan[played[0]-i][played[1]-j] == 0:
- plan[played[0]-i][played[1]-j] = 3
- removed_spaces += 1
- plan[played[0]][played[1]] = player
- return removed_spaces
- def one_strategy(plan, free_spaces, strategy, player):
- played = [0, 0]
- if strategy == 1 and player == 2:
- played = strategy_random(plan, free_spaces)
- elif strategy == 2 and player == 2:
- played = strategy_maxblock(plan, played)
- elif strategy == 3 and player == 2:
- played = strategy_lastplace(plan, free_spaces, played)
- else:
- played[1] = int(input("Please input coordinate of X axis "))-1
- played[0] = int(input("Please input coordinate of Y axis "))-1
- free_spaces -= (replacer(plan, played, player))
- print("Moved:", (played[1]+1), (played[0]+1))
- return free_spaces
- def which_strategy(plan, free_spaces, strategy, played):
- if strategy == 1:
- played = strategy_random(plan, free_spaces)
- elif strategy == 2:
- played = strategy_maxblock(plan, played)
- else:
- played = strategy_lastplace(plan, free_spaces, played)
- return played
- def two_strategies(plan, free_spaces, strategy_1, strategy_2, player):
- played = [0, 0]
- if player == 1:
- played = which_strategy(plan, free_spaces, strategy_1, played)
- else:
- played = which_strategy(plan, free_spaces, strategy_2, played)
- print("Player", player, "moves")
- print("Moved:", (played[0]+1), (played[1]+1))
- free_spaces -= replacer(plan, played, player)
- return free_spaces
- def two_players_game(plan, free_spaces, player):
- played = [0, 0]
- print("Player", player, "moves")
- played[1] = int(input("Pleaase input coordinate of X axes"))-1
- played[0] = int(input("Pleaase input coordinate of Y axes"))-1
- print("Moved:", (played[0]+1), (played[1]+1))
- free_spaces -= replacer(plan, played, player)
- return free_spaces
- def play_game(width, height, strategy_1=None, strategy_2=None):
- free_spaces = width * height
- player = 2
- plan = make_empty_plan(width, height)
- while free_spaces > 0:
- player = 3 - player
- if strategy_1 is None and strategy_2 is None:
- free_spaces = two_players_game(plan, free_spaces, player)
- elif strategy_2 is None:
- free_spaces = one_strategy(plan, free_spaces, strategy_1, player)
- else:
- free_spaces = two_strategies(plan, free_spaces, strategy_1,
- strategy_2, player)
- print_plan(plan)
- print("Player", player, "wins")
Advertisement
Add Comment
Please, Sign In to add comment