Advertisement
Mori007

newgametic

Feb 15th, 2021
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. from guizero import App, Box, PushButton, Text
  2.  
  3. # Functions -------------
  4. def clear_board():
  5.     new_board = [[None, None, None, None], [None, None, None, None], [None, None, None, None], [None, None, None, None]]
  6.     for x in range(4):
  7.         for y in range(4):
  8.             button = PushButton(board, text="", grid=[x, y], width=3, command=choose_square, args=[x,y])
  9.             new_board[x][y] = button
  10.     return new_board
  11.  
  12. def choose_square(x, y):
  13.     board_squares[x][y].text = turn
  14.     board_squares[x][y].disable()
  15.     toggle_player()
  16.     check_win()
  17.  
  18. def toggle_player():
  19.     global turn
  20.     if turn == "X":
  21.         turn = "O"
  22.     else:
  23.         turn = "X"
  24.     message.value = "Next... It is your turn, " + turn
  25.  
  26. def check_win():
  27.     winner = None
  28.  
  29.     # Vertical lines
  30.     if (
  31.         board_squares[0][0].text == board_squares[0][1].text == board_squares[0][2].text
  32.     ) and board_squares[0][2].text in ["X", "O"]:
  33.         winner = board_squares[0][0]
  34.     elif (
  35.         board_squares[1][0].text == board_squares[1][1].text == board_squares[1][2].text
  36.     ) and board_squares[1][2].text in ["X", "O"]:
  37.         winner = board_squares[1][0]
  38.     elif (
  39.         board_squares[2][0].text == board_squares[2][1].text == board_squares[2][2].text
  40.     ) and board_squares[2][2].text in ["X", "O"]:
  41.         winner = board_squares[2][0]
  42.  
  43.     # Horizontal lines
  44.     elif (
  45.         board_squares[0][0].text == board_squares[1][0].text == board_squares[2][0].text
  46.     ) and board_squares[2][0].text in ["X", "O"]:
  47.         winner = board_squares[0][0]
  48.     elif (
  49.         board_squares[0][1].text == board_squares[1][1].text == board_squares[2][1].text
  50.     ) and board_squares[2][1].text in ["X", "O"]:
  51.         winner = board_squares[0][1]
  52.     elif (
  53.         board_squares[0][2].text == board_squares[1][2].text == board_squares[2][2].text
  54.     ) and board_squares[2][2].text in ["X", "O"]:
  55.         winner = board_squares[0][2]
  56.  
  57.     # Diagonals
  58.     elif (
  59.         board_squares[0][0].text == board_squares[1][1].text == board_squares[2][2].text
  60.     ) and board_squares[2][2].text in ["X", "O"]:
  61.         winner = board_squares[0][0]
  62.     elif (
  63.         board_squares[2][0].text == board_squares[1][1].text == board_squares[0][2].text
  64.     ) and board_squares[0][2].text in ["X", "O"]:
  65.         winner = board_squares[0][2]
  66.        
  67.     if winner is not None:
  68.         message.value = winner.text + " is winner"
  69.     elif moves_taken() == 9:
  70.         message.value = "It's a draw"
  71.  
  72. def moves_taken():
  73.     moves = 0
  74.     for row in board_squares:
  75.         for col in row:
  76.             if col.text == "X" or col.text == "O":
  77.                 moves = moves + 1
  78.     return moves
  79.  
  80.        
  81. # Variables -------------
  82. turn = "X"
  83.  
  84. # App -------------------
  85. app = App("MY Tic tac toe")
  86.  
  87. board = Box(app, layout="grid")
  88. board_squares = clear_board()
  89. message = Text(app, text="It is your turn, " + turn)
  90.  
  91. app.display()
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement