Advertisement
moomoomoo309

TicTacToe

Dec 9th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. from abc import ABCMeta, abstractmethod, abstractproperty
  2. from enum import Enum
  3.  
  4.  
  5. class TicTacToeBoardBase:
  6.     """A base class used for implementing the GUI part of a Tic-Tac-Toe board."""
  7.     __metaclass__ = ABCMeta
  8.     Empty = 0
  9.     X = 1
  10.     O = 2
  11.     defaultBoardColor = (0, 0, 0)
  12.     defaultBoardWidth = 100
  13.     defaultXColor = (1, 0, 0)
  14.     defaultOColor = (0, 1, 0)
  15.  
  16.     class winCase(Enum):
  17.         vertLeft=1
  18.         vertMiddle=2
  19.         vertRight=3
  20.         horizTop=4
  21.         horizMiddle=5
  22.         horizBottom=6
  23.         diagonalTopLeft=7
  24.         diagonalTopRight=8
  25.  
  26.     @abstractproperty
  27.     def board(self):
  28.         """The board, stored as a 1D array of length 9. Can only store Board.X, Board.Y, and Board.Empty. See above."""
  29.  
  30.     @abstractproperty
  31.     def boardPosition(self):
  32.         """The position of the board, stored as a tuple."""
  33.  
  34.     @abstractproperty
  35.     def boardColor(self):
  36.         """The color of the board, stored as a 3 or 4 item tuple containing floats between 0 and 1."""
  37.  
  38.     @abstractproperty
  39.     def boardWidth(self):
  40.         """The width of the lines of the board, in pixels."""
  41.  
  42.     @abstractproperty
  43.     def xColor(self):
  44.         """The color of the X, stored as a 3 or 4 item tuple containing floats between 0 and 1."""
  45.  
  46.     @abstractproperty
  47.     def oColor(self):
  48.         """The color of the O, stored as a 3 or 4 item tuple containing floats between 0 and 1."""
  49.  
  50.     @abstractproperty
  51.     def boardSize(self):
  52.         """The length of the (square) board."""
  53.  
  54.     @abstractmethod
  55.     def __init__(self, x, y, boardColor, xColor, oColor, boardWidth):
  56.         """Create the canvas, and anything else needed to start the program initially."""
  57.         _,__,___,____,_____,_____=x,y,boardColor,xColor,oColor,boardWidth # Removes PyCharm warning
  58.  
  59.     @abstractmethod
  60.     def drawBoard(self):
  61.         """Draw the board at the given coordinates, with the given width and height.
  62.           Assume (0,0) is in the top left, and the positive direction is right in the X axis, and down in the Y axis.
  63.  
  64.           The minimum size of the board is 5px by 5px."""
  65.  
  66.     @abstractmethod
  67.     def redrawBoard(self):
  68.         """Redraw the board after a value change."""
  69.  
  70.     @abstractmethod
  71.     def drawX(self, xIndex, yIndex):
  72.         """Draw an X at the given x index or y index, treating it as a 3x3, zero-indexed array.
  73.           Raise an exception when an  X is drawn over an existing X or existing O."""
  74.  
  75.     @abstractmethod
  76.     def drawO(self, xIndex, yIndex):
  77.         """Draw an X at the given x index or y index, treating it as a 3x3, zero-indexed array.
  78.           Raise an exception when an X is drawn over an existing X or existing O."""
  79.  
  80.     @abstractmethod
  81.     def drawLine(self, winCase):
  82.         """Draw the victory line for the given winCase."""
  83.  
  84.     @abstractmethod
  85.     def onClick(self, x, y, button=1):
  86.         """Run when the mouse is pressed."""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement