Advertisement
ALEXANDAR_GEORGIEV

Workshop

Jun 1st, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. # Това е от днес, направи една игра , нещо като тетрикс
  2. # Workshop
  3. from collections import deque
  4.  
  5.  
  6. def place_circle():
  7. row = 0
  8. while row != ROWS and board[row][player_col] == "0":
  9. row += 1
  10.  
  11. board[row - 1][player_col] = player_symbol
  12.  
  13. return row - 1
  14.  
  15.  
  16. def check_for_win(row, col):
  17. for x, y in directions:
  18. count = 1
  19. for i in range(1, 4):
  20. new_row = row + x * i
  21. new_col = col + y * i
  22.  
  23. if not (0 <= new_row < ROWS and 0 <= new_col < COLS):
  24. break
  25.  
  26. if board[new_row][new_col] != player_symbol:
  27. break
  28.  
  29. count += 1
  30.  
  31. for i in range(1, 4):
  32. new_row = row - x * i
  33. new_col = col - y * i
  34.  
  35. if not (0 <= new_row < ROWS and 0 <= new_col < COLS):
  36. break
  37.  
  38. if board[new_row][new_col] != player_symbol:
  39. break
  40.  
  41. count += 1
  42.  
  43. if count >= 4:
  44. return True
  45.  
  46. return False
  47.  
  48.  
  49. ROWS, COLS = 6, 7
  50.  
  51. board = [["0"] * COLS for row in range(ROWS)]
  52. # print(*board, sep="\n")
  53.  
  54. player_one_symbol = "1"
  55. player_two_symbol = "2"
  56. turns = deque([[1, player_one_symbol], [2, player_two_symbol]])
  57.  
  58. win = False
  59.  
  60. directions = (
  61. (1, 0),
  62. (-1, 0),
  63. (0, 1),
  64. (0, -1),
  65. (-1, -1),
  66. (1, 1),
  67. (-1, 1),
  68. (1, -1)
  69. )
  70.  
  71. while not win:
  72. [print(f"[ {', '.join(row)} ]") for row in board]
  73.  
  74. player_number, player_symbol = turns[0]
  75. try:
  76. player_col = int(input(f"Player {player_number}, please chose a column: ")) - 1
  77. except ValueError:
  78. print(f"Select a valid number in the range (1-{COLS})")
  79. continue
  80.  
  81. if not 0 <= player_col < COLS:
  82. print(f"Select a valid number in the range (1-{COLS})")
  83. continue
  84.  
  85. if board[0][player_col] != "0":
  86. print("No empty spaces on that column, choose another one!")
  87. continue
  88.  
  89. row = place_circle()
  90. win = check_for_win(row, player_col)
  91.  
  92. turns.rotate()
  93.  
  94. print(f"Player {turns[1][0]} wins!")
  95.  
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement