|
| 1 | +from collections import deque |
| 2 | + |
| 3 | + |
| 4 | +class TicTacToeGame: |
| 5 | + |
| 6 | + def __init__(self, matrix: list, board: str, players: dict, move: deque): |
| 7 | + self.matrix = matrix |
| 8 | + self.board = board |
| 9 | + self.players = players |
| 10 | + self.move = move |
| 11 | + |
| 12 | + def check_win(self, player): |
| 13 | + checks = { |
| 14 | + "vertical": ([-1, -1], 0), |
| 15 | + "horizontal": (0, [1, 1]), |
| 16 | + "diagonal": [(-1, -1, 1, 1), (1, -1, -1, 1)] |
| 17 | + } |
| 18 | + |
| 19 | + symbol = self.players[player] |
| 20 | + |
| 21 | + for row in range(3): |
| 22 | + |
| 23 | + for col in range(3): |
| 24 | + |
| 25 | + if self.matrix[row][col] == symbol: |
| 26 | + |
| 27 | + winner = True |
| 28 | + m_row = row |
| 29 | + |
| 30 | + for num in checks["vertical"][0]: |
| 31 | + try: |
| 32 | + |
| 33 | + m_row += num |
| 34 | + |
| 35 | + if self.matrix[m_row][col] != symbol: |
| 36 | + winner = False |
| 37 | + break |
| 38 | + |
| 39 | + except IndexError: |
| 40 | + winner = False |
| 41 | + break |
| 42 | + |
| 43 | + if winner: |
| 44 | + return True |
| 45 | + |
| 46 | + winner = True |
| 47 | + m_col = col |
| 48 | + |
| 49 | + for num in checks["horizontal"][1]: |
| 50 | + try: |
| 51 | + |
| 52 | + m_col += num |
| 53 | + |
| 54 | + if self.matrix[row][m_col] != symbol: |
| 55 | + winner = False |
| 56 | + break |
| 57 | + |
| 58 | + except IndexError: |
| 59 | + winner = False |
| 60 | + break |
| 61 | + |
| 62 | + if winner: |
| 63 | + return True |
| 64 | + |
| 65 | + if self.matrix[0][0] == symbol and self.matrix[1][1] == symbol and self.matrix[2][2] == symbol or \ |
| 66 | + self.matrix[0][-1] == symbol and self.matrix[1][-2] == symbol and self.matrix[2][-3] == symbol: |
| 67 | + return True |
| 68 | + |
| 69 | + return False |
| 70 | + |
| 71 | + def new_position(self): |
| 72 | + player = self.move.popleft() |
| 73 | + |
| 74 | + while True: |
| 75 | + position = input(f"{player} choose a free position [1-9]: ") |
| 76 | + |
| 77 | + if position.isdigit(): |
| 78 | + if 1 <= int(position) <= 3: |
| 79 | + if self.matrix[0][int(position)-1] == 'X' or self.matrix[0][int(position)-1] == 'O': |
| 80 | + print("Position is not free!") |
| 81 | + else: |
| 82 | + self.matrix[0][int(position)-1] = self.players[player] |
| 83 | + break |
| 84 | + |
| 85 | + elif 4 <= int(position) <= 6: |
| 86 | + if self.matrix[1][int(position)-4] == 'X' or self.matrix[1][int(position)-4] == 'O': |
| 87 | + print("Position is not free!") |
| 88 | + else: |
| 89 | + self.matrix[1][int(position)-4] = self.players[player] |
| 90 | + break |
| 91 | + |
| 92 | + elif 7 <= int(position) <= 9: |
| 93 | + if self.matrix[2][int(position)-7] == 'X' or self.matrix[2][int(position)-7] == 'O': |
| 94 | + print("Position is not free!") |
| 95 | + else: |
| 96 | + self.matrix[2][int(position)-7] = self.players[player] |
| 97 | + break |
| 98 | + else: |
| 99 | + print("Choose valid position [1-9]") |
| 100 | + |
| 101 | + else: |
| 102 | + print("Position must be digit between 1-9") |
| 103 | + |
| 104 | + print(self.edit_board()) |
| 105 | + |
| 106 | + self.move.append(player) |
| 107 | + return [self.check_win(player), player] |
| 108 | + |
| 109 | + def draw(self): |
| 110 | + for row in range(3): |
| 111 | + |
| 112 | + for col in range(3): |
| 113 | + if self.matrix[row][col] == ' ': |
| 114 | + return False |
| 115 | + return True |
| 116 | + |
| 117 | + def edit_board(self): |
| 118 | + self.board = f"| {self.matrix[0][0]} | {self.matrix[0][1]} | {self.matrix[0][2]} |\n| {self.matrix[1][0]} | {self.matrix[1][1]} | {self.matrix[1][2]} |\n| {self.matrix[2][0]} | {self.matrix[2][1]} | {self.matrix[2][2]} |" |
| 119 | + return self.board |
| 120 | + |
| 121 | + |
| 122 | +def setup(): |
| 123 | + matrix = [[' '] * 3 for _ in range(3)] |
| 124 | + board = "| | | |\n| | | |\n| | | |" |
| 125 | + |
| 126 | + player_one = input("Player one name: ") |
| 127 | + |
| 128 | + while True: |
| 129 | + |
| 130 | + player_two = input("Player two name: ") |
| 131 | + |
| 132 | + if player_two != player_one: |
| 133 | + break |
| 134 | + |
| 135 | + print("Second player cannot have the same name as the first player!") |
| 136 | + |
| 137 | + player_one_plays_with = input(f"{player_one} would like to play with 'X' or 'O'? ").upper() |
| 138 | + |
| 139 | + if player_one_plays_with == "O": |
| 140 | + players = {player_one: "O", player_two: "X"} |
| 141 | + move = deque([player_two, player_one]) |
| 142 | + |
| 143 | + else: # if player one plays with 'X' or gave invalid character |
| 144 | + players = {player_one: 'X', player_two: 'O'} |
| 145 | + move = deque([player_one, player_two]) |
| 146 | + |
| 147 | + return [matrix, board, players, move] |
| 148 | + |
| 149 | + |
| 150 | +def start_game(items): |
| 151 | + print("This is the numeration of the board:\n| 1 | 2 | 3 |\n| 4 | 5 | 6 |\n| 7 | 8 | 9 |") |
| 152 | + print(f"{items[-1][0]} plays with 'X', he starts first!") |
| 153 | + |
| 154 | + game = TicTacToeGame(*items) |
| 155 | + |
| 156 | + return game |
| 157 | + |
| 158 | + |
| 159 | +def play(): |
| 160 | + game = start_game(setup()) |
| 161 | + |
| 162 | + while True: |
| 163 | + have_winner = game.new_position() |
| 164 | + |
| 165 | + if have_winner[0]: |
| 166 | + print(f"{have_winner[1]} won!") |
| 167 | + restart = input("type \"r\" to restart the game: ").lower() |
| 168 | + |
| 169 | + if restart == "r": |
| 170 | + play() |
| 171 | + |
| 172 | + else: |
| 173 | + break |
| 174 | + |
| 175 | + draw = game.draw() |
| 176 | + |
| 177 | + if draw: |
| 178 | + print("The game is draw!") |
| 179 | + restart = input("type \"r\" to restart the game: ").lower() |
| 180 | + |
| 181 | + if restart == "r": |
| 182 | + play() |
| 183 | + |
| 184 | + else: |
| 185 | + break |
| 186 | + |
| 187 | + |
| 188 | +play() |
0 commit comments