Advertisement
timber101

Untitled

Mar 26th, 2021
956
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None
  1. from guizero import App, Box, PushButton,  Text, Drawing, Combo, Slider
  2.  
  3. #imports
  4. #from guizero import App
  5.  
  6. #function
  7. def clear_board():
  8.     new_board = [[None, None, None],[None, None, None],[None, None, None]]
  9.     for x in range(3):
  10.         for y in range(3):
  11.             button = PushButton(board, text = "", grid = [x,y], width=3, command = chose_square, args=[x,y])
  12.             new_board[x][y] = button
  13.     return new_board
  14.    
  15. def chose_square(x,y):
  16.     board_squares[x][y].text=turn
  17.     board_squares[x][y].disable()
  18.     toggle_player()
  19.     check_win()
  20.    
  21. ##  i moved this from below    
  22. def toggle_player():
  23.     global turn
  24.     if turn == "X":
  25.         turn = "O"
  26.     else:
  27.         turn = "X"
  28.     message.value = "And now its your turn, " + turn
  29.  
  30. def check_win():
  31.     winner = None
  32.  
  33.     # Verticle lines ## i changed these to elif's
  34.     if(board_squares[0][0].text == board_squares[0][1]
  35. .text == board_squares[0][2].text    ) and board_squares[0][2].text in ["X", "O"]:
  36.         winner = board_squares[0][0]
  37.     elif(board_squares[1][0].text == board_squares[1][1]
  38. .text == board_squares[1][2].text    ) and board_squares[1][1].text in ["X", "O"]:
  39.         winner = board_squares[1][2]
  40.     elif(board_squares[2][0].text == board_squares[2][1]
  41. .text == board_squares[2][2].text    ) and board_squares[2][2].text in ["X", "O"]:
  42.         winner = board_squares[2][2]
  43.     elif(board_squares[0][0].text == board_squares[1][1]
  44. .text == board_squares[2][2].text    ) and board_squares[2][2].text in ["X", "O"]:
  45.         winner = board_squares[2][2]
  46.     elif(board_squares[2][0].text == board_squares[1][1]
  47. .text == board_squares[0][2].text    ) and board_squares[0][2].text in ["X", "O"]:
  48.         winner = board_squares[0][2]
  49.  
  50. #horizontal lines
  51.     elif (board_squares[0][0].text == board_squares[1][0].text == board_squares[2][0].text)and board_squares [2][0].text in ["X", "O"]:
  52.         winner = board_squares[2][0]
  53.     elif (board_squares[0][1].text == board_squares[1][1].text == board_squares[2][1].text)and board_squares [2][1].text in ["X", "O"]:
  54.         winner = board_squares[2][1]
  55.     elif (board_squares[0][2].text == board_squares[1][2].text == board_squares[2][2].text)and board_squares [2][2].text in ["X", "O"]:
  56.         winner = board_squares[2][2]
  57.    
  58.     if winner is not None:
  59.         message.value = winner.text + " wins!"
  60.     elif moves_taken() == 9:
  61.         message.value = "It's a draw"
  62.        
  63. def moves_taken():
  64.     moves = 0
  65.     for row in board_squares:
  66.         for col in row:
  67.             if col.text == "X" or col.text == "O":
  68.                 moves = moves + 1
  69.     return moves
  70.  
  71. #variables
  72. turn = "X"
  73.  
  74. #App
  75. app = App("Tic Tac Toe")
  76. board = Box(app, layout="grid")
  77. board_squares = clear_board()
  78. message =Text(app, text ="Its your turn now, " + turn )
  79.  
  80. #display
  81.  
  82. app.display()
Advertisement
RAW Paste Data Copied
Advertisement