Advertisement
ALEXANDAR_GEORGIEV

tik_tok_toe

Jun 2nd, 2023
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. # Морски шах
  2. from collections import deque
  3.  
  4.  
  5. def check_for_win():
  6. player_name, player_symbol = players[0]
  7.  
  8. first_diagonal_win = all([board[i][i] == player_symbol for i in range(SIZE)]) # List [True, True, False] = False ALL -> Прави цикъл
  9. second_diagonal_win = all([board[i][SIZE - i - 1] == player_symbol for i in range(SIZE)]) # List [True, True, False] = False
  10.  
  11. row_win = any([all(player_symbol == pos for pos in row) for row in board]) # List by ROWS [True, False, True] -> Връща 1 Булева променлива
  12. col_win = any([all(board[r][c] == player_symbol for r in range(SIZE)) for c in range(SIZE)]) # List by COLS [True, False, True] -> Връща 1 Булева променлива
  13.  
  14. if any([first_diagonal_win, second_diagonal_win, row_win, col_win]):
  15. print_board()
  16. print(f"{player_name} won!")
  17.  
  18.  
  19. def place_symbol(row, col):
  20. board[row][col] = players[0][1]
  21.  
  22. check_for_win()
  23. print_board()
  24.  
  25. if turns == SIZE * SIZE:
  26. print("Draw!")
  27.  
  28. players.rotate()
  29.  
  30.  
  31. def choose_position():
  32. global turns
  33. while True:
  34. try:
  35. position = int(input(f"{players[0][0]} choose a free position in the range [1-{SIZE + SIZE}]: "))
  36. row, col = (position - 1 ) // SIZE, (position - 1) % SIZE # Индекси на редове и колони
  37. except ValueError:
  38. print(f"{players[0][0]}, please select a valid position!")
  39. continue
  40. if 1 <= position <= SIZE * SIZE and board[row][col] == " ": # TODO: calculate row and col
  41. turns += 1
  42. else:
  43. print(f"{players[0][0]}, please select a valid position!")
  44.  
  45.  
  46. def print_board(begin=False):
  47. if begin:
  48. print("This is the numeration of the board:")
  49. [print(f"| {' | '.join(row)} |") for row in board]
  50.  
  51. for row in range(SIZE):
  52. for col in range(SIZE):
  53. board[row][col] = " "
  54. else:
  55. [print(f"| {' | '.join(row)} |") for row in board]
  56.  
  57.  
  58. def start():
  59. player_one_name = input("Player one, please enter your name: ")
  60. player_two_name = input("Player two, please enter your name: ")
  61.  
  62. while True:
  63. player_one_symbol = input(f"{player_one_name} would you like to play with 'X' or 'O'?").upper()
  64.  
  65. if player_one_symbol not in ["X", "O"]:
  66. print(f"{player_one_name}, please select a valid option!")
  67. else:
  68. break
  69.  
  70. player_two_symbol = "O" if player_one_symbol == "X" else "X"
  71.  
  72. players.append([player_one_name, player_one_symbol])
  73. players.append([player_two_name, player_two_symbol])
  74.  
  75. print_board(begin=True)
  76.  
  77.  
  78.  
  79. SIZE = 3
  80. turns = 0
  81. board = [[str(i), str(i+1), str(i+2)] for i in range(1, SIZE * SIZE + 1, SIZE)]
  82. players = deque()
  83.  
  84. start()
  85.  
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement