The whole game of tic-tac-toe occurs in the console. I do not know how to implement the output image several times. That is, the player walks with a cross, the output of the updated image. I went to the computer nolikom-> output pictures. In the setting function, the game field is set up and output. And then in the function display_board should be a drawing of a cross or a zero, depending on the move, but it displays nothing in this function. Here is the complete program code.

 import matplotlib.pyplot as plt from matplotlib.lines import Line2D class Game: X = "X" O = "O" EMPTY = " " NUM_SQUARES = 9 TIE = "TIE" xCircle = [0.5,1.5,2.5,0.5,1.5,2.5,0.5,1.5,2.5] yCircle = [2.5,2.5,2.5,1.5,1.5,1.5,0.5,0.5,0.5] radius = 0.5 xLine = [[0,1],[1,2],[2,3],[0,1],[1,2],[2,3],[0,1],[1,2],[2,3]] yLine = [[2,3],[2,3],[2,3],[1,2],[1,2],[1,2],[0,1],[0,1],[0,1]] fig = plt.figure() ax = fig.add_subplot(111,aspect='equal') color = 'k' fill =False def setting(self): self.ax = self.fig.add_subplot(111,aspect='equal') self.ax.set_xticks([1,2,3]) self.ax.set_yticks([1,2,3]) self.ax.tick_params(axis='both', left='off', top='off', right='off', bottom='off', labelleft='off', labeltop='off', labelright='off', labelbottom='off') self.ax.grid() plt.draw() plt.pause(0.1) def ask_yes_no(self,question): """Π—Π°Π΄Π°Ρ‘Ρ‚ вопрос с ΠΎΡ‚Π²Π΅Ρ‚ΠΎΠΌ 'Π”Π°' ΠΈΠ»ΠΈ 'НСт'.""" response = None while response not in ("y", "n"): response = input(question).lower() return response def ask_number(self,question, low, high): """ΠŸΡ€ΠΎΡΠΈΡ‚ ввСсти число ΠΈΠ· Π΄ΠΈΠ°ΠΏΠ°Π·ΠΎΠ½Π°""" response = None while response not in range(low, high): response = int(input(question)) return response def pieces(self): """ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚ ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ½ΠΎΡΡ‚ΡŒ ΠΏΠ΅Ρ€ΠΎΠ²ΠΎΠ³ΠΎ Ρ…ΠΎΠ΄Π°.""" go_first = self.ask_yes_no("Π˜Π³Ρ€Π°Ρ‚ΡŒ крСстиками? (y, n): ") if go_first == "y": print("\n Π’Ρ‹ ΠΈΠ³Ρ€Π°Π΅Ρ‚Π΅ ΠΊΡ€Π΅ΠΊΡ‚ΠΈΠΊΠ°ΠΌΠΈ.") human = self.X computer = self.O else: print("\n Π’Ρ‹ ΠΈΠ³Ρ€Π°Π΅Ρ‚Π΅ Π½ΠΎΠ»ΠΈΠΊΠ°ΠΌΠΈ") computer = self.X human = self.O return computer, human def new_board(self): """Π‘ΠΎΠ·Π΄Π°Ρ‘Ρ‚ Π½ΠΎΠ²ΡƒΡŽ ΠΈΠ³Ρ€ΠΎΠ²ΡƒΡŽ доску.""" board = [] for square in range(self.NUM_SQUARES): board.append(self.EMPTY) return board def display_board(self,board): """ΠžΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ°Π΅Ρ‚ ΠΈΠ³Ρ€ΠΎΠ²ΡƒΡŽ доску Π½Π° экранС.""" for i in range(self.NUM_SQUARES): if (board[i]!=self.EMPTY): if (board[i]==self.X): line = Line2D(self.xLine[i], self.yLine[i],color='k') self.ax.add_line(line) plt.draw() plt.pause(0.1) if (board[i]==self.O): self.ax.add_artist(plt.Circle((self.xCircle[i], self.yCircle[i]), self.radius, color=self.color, fill=self.fill)) plt.draw() plt.pause(0.1) def legal_moves(self,board): """Π‘ΠΎΠ·Π΄Π°Ρ‘Ρ‚ список доступных Ρ…ΠΎΠ΄ΠΎΠ².""" moves = [] for square in range(self.NUM_SQUARES): if board[square] == self.EMPTY: moves.append(square) return moves def winner(self,board): """ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚ побСдитСля Π² ΠΈΠ³Ρ€Π΅.""" WAYS_TO_WIN = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6)) for row in WAYS_TO_WIN: if board[row[0]] == board[row[1]] == board[row[2]] != self.EMPTY: winner = board[row[0]] return winner if self.EMPTY not in board: return self.TIE return None def human_move(self,board, human): """ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ Ρ…ΠΎΠ΄ Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠ°""" legal = self.legal_moves(board) move = None while move not in legal: move = self.ask_number("Π’Π²ΠΎΠΉ Ρ…ΠΎΠ΄. Π’Ρ‹Π±Π΅Ρ€ΠΈ ΠΎΠ΄Π½ΠΎ ΠΈΠ· ΠΏΠΎΠ»Π΅ΠΉ (0 - 8):", 0, self.NUM_SQUARES) if move not in legal: print("\nΠ­Ρ‚ΠΎ ΠΏΠΎΠ»Π΅ ΡƒΠΆΠ΅ занято. Π’Ρ‹Π±Π΅Ρ€ΠΈ Π΄Ρ€ΡƒΠ³ΠΎΠ΅.\n") return move def computer_move(self,board, computer, human): """Π”Π΅Π»Π°Π΅Ρ‚ Ρ…ΠΎΠ΄ Π·Π° ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΎΡ‚ΠΈΠ²Π½ΠΈΠΊΠ°.""" # создадим Ρ€Π°Π±ΠΎΡ‡ΡƒΡŽ копию доски, ΠΏΠΎΡ‚ΠΎΠΌΡƒ Ρ‡Ρ‚ΠΎ функция Π±ΡƒΠ΄Π΅Ρ‚ ΠΌΠ΅Π½ΡΡ‚ΡŒ Π½Π΅ΠΊΠΎΡ‚ΠΎΡ€ΠΎΡ‹Π΅ элСмСнты Π² спискС board = board[:] # Ρ…ΠΎΠ΄Ρ‹, ΠΎΡ‚ Π»ΡƒΡ‡ΡˆΠ΅Π³ΠΎ ΠΊ Ρ…ΡƒΠ΄ΡˆΠ΅ΠΌΡƒ BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7) print("Π― Π²Ρ‹Π±Π΅Ρ€Ρƒ ΠΏΠΎΠ»Π΅ Π½ΠΎΠΌΠ΅Ρ€", end = " ") # Ссли ΡΠ΄Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΌ Ρ…ΠΎΠ΄ΠΎΠΌ ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΠΎΠ±Π΅Π΄ΠΈΡ‚ΡŒ ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€, Π²Ρ‹Π±Π΅Ρ€Π΅ΠΌ этот Ρ…ΠΎΠ΄ for move in self.legal_moves(board): board[move] = computer if self.winner(board) == computer: print(move) return move # Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ² ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΡƒ этого Ρ…ΠΎΠ΄Π°, ΠΎΡ‚ΠΌΠ΅Π½ΠΈΠΌ Π΅Π³ΠΎ (Π² локальной ΠΊΠΎΠΏΠΈΠΈ ΠΈΠ³Ρ€ΠΎΠ²ΠΎΠΉ доски) board[move] = self.EMPTY # Ссли ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΌ Ρ…ΠΎΠ΄ΠΎΠΌ ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΠΎΠ±Π΅Π΄ΠΈΡ‚ΡŒ Ρ‡Π΅Π³Π»ΠΎΠ²Π΅ΠΊ, Π±Π»ΠΎΠΊΠΈΡ€ΡƒΠ΅ΠΌ этот Ρ…ΠΎΠ΄ for moves in self.legal_moves(board): board[move] = human if self.winner(board) == human: print(move) return move # Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ² ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΡƒ этого Ρ…ΠΎΠ΄Π°, ΠΎΡ‚ΠΌΠ΅Π½ΠΈΠΌ Π΅Π³ΠΎ (Π² локальной ΠΊΠΎΠΏΠΈΠΈ ΠΈΠ³Ρ€ΠΎΠ²ΠΎΠΉ доски) board[move] = self.EMPTY # ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΌ Ρ…ΠΎΠ΄ΠΎΠΌ Π½ΠΈ ΠΎΠ΄Π½Π° ΠΈΠ· сторон Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΠΎΠ±Π΅Π΄ΠΈΡ‚ΡŒ, # Π²Ρ‹Π±Π΅Ρ€Π΅ΠΌ Π»ΡƒΡ‡ΡˆΠ΅Π΅ ΠΈΠ· доступных ΠΏΠΎΠ»Π΅ΠΉ for move in BEST_MOVES: if move in self.legal_moves(board): print(move) return move def next_turn(self,turn): """ΠžΡΡƒΡ‰Π΅ΡΡ‚Π²Π»ΡΠ΅Ρ‚ ΠΏΠ΅Ρ€Π΅Ρ…ΠΎΠ΄ Ρ…ΠΎΠ΄Π°.""" if turn == self.X: return self.O else: return self.X def congrat_winner(self,the_winner, computer, human): """ΠŸΠΎΠ·Π΄Ρ€Π°Π²Π»ΡΠ΅Ρ‚ побСдитСля ΠΈΠ³Ρ€Ρ‹.""" if the_winner == computer: print("Π’Ρ‹ ΠΏΡ€ΠΎΠΈΠ³Ρ€Π°Π»ΠΈ!") elif the_winner == human: print("Π’Ρ‹ Π²Ρ‹ΠΈΠ³Ρ€Π°Π»ΠΈ!") elif the_winner == self.TIE: print("ΠΠΈΡ‡ΡŒΡ!") def main(self): computer, human = self.pieces() self.setting() turn = self.X board = self.new_board() while not self.winner(board): if turn == human: move = self.human_move(board, human) board[move] = human else: move = self.computer_move(board, computer, human) board[move] = computer self.display_board(board) turn = self.next_turn(turn) the_winner =self.winner(board) self.congrat_winner(the_winner, computer, human) plt.ion() # запуск ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹ clas=Game() clas.main() #input("НаТмитС Enter, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π²Ρ‹ΠΉΡ‚ΠΈ.") 
  • Comments are not intended for extended discussion; conversation moved to chat . - YurySPb ♦

1 answer 1

In my case, Anaconda with Spider was used. And so the plt.draw() function at the second draw produced the following <Figure size 432x288 with 0 Axes> . Solution: installed the matplotlib module in IDLE 3.7. Installation line - we write in the pip install matplotlib . After running the program in IDLE 3.7 and re-drawing earned.