Advertisement
asweigart

tictactoe_oop.py

Dec 8th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.18 KB | None | 0 0
  1. # Tic Tac Toe
  2.  
  3. TL, TM, TR, ML, MM, MR, BL, BM, BR = 'TL', 'TM', 'TR', 'ML', 'MM', 'MR', 'BL', 'BM', 'BR'
  4. ALL_SPACES = (TL, TM, TR, ML, MM, MR, BL, BM, BR)
  5. X, O, BLANK = 'X', 'O', ' '
  6.  
  7. def main():
  8.     game = TicTacToeGame()
  9.     game.run()
  10.  
  11. class TicTacToeGame:
  12.     def run(self):
  13.         print('Welcome to Tic Tac Toe!')
  14.         mainBoard = TicTacToeBoard()
  15.         turn = X
  16.         nextTurn = O
  17.  
  18.         while True:
  19.             print('It is ' + turn + '\'s turn.')
  20.             mainBoard.drawBoard()
  21.             move = mainBoard.getPlayerMove()
  22.             mainBoard.setSpace(move, turn)
  23.  
  24.             if mainBoard.isWinner(turn):
  25.                 mainBoard.drawBoard()
  26.                 print(turn + ' has won the game!')
  27.                 break
  28.             elif mainBoard.isFull():
  29.                 mainBoard.draw()
  30.                 print('The game is a tie!')
  31.                 break
  32.             print()
  33.  
  34.             turn, nextTurn = nextTurn, turn
  35.  
  36. class TicTacToeBoard:
  37.     def __init__(self, startingMarks=None):
  38.         # Create a new, blank tic tac toe board.
  39.         if startingMarks is None:
  40.             startingMarks = {}
  41.         self.spaces = {}
  42.         for space in ALL_SPACES:
  43.             self.spaces.setdefault(space, startingMarks.get(space, BLANK))
  44.  
  45.     def drawBoard(self):
  46.         # Display a text-representation of the board.
  47.         print(f'{self.spaces[TL]}|{self.spaces[TM]}|{self.spaces[TR]}')
  48.         print('-----')
  49.         print(f'{self.spaces[ML]}|{self.spaces[MM]}|{self.spaces[MR]}')
  50.         print('-----')
  51.         print(f'{self.spaces[BL]}|{self.spaces[BM]}|{self.spaces[BR]}')
  52.  
  53.     def isWinner(self, mark):
  54.         bo, m = self.spaces, mark # These shorter names make the following code shorter.
  55.         return ((bo[TL] == m and bo[TM] == m and bo[TR] == m) or # across the top
  56.                 (bo[ML] == m and bo[MM] == m and bo[MR] == m) or # across the middle
  57.                 (bo[BL] == m and bo[BM] == m and bo[BR] == m) or # across the bottom
  58.                 (bo[TL] == m and bo[ML] == m and bo[BL] == m) or # down the left side
  59.                 (bo[TM] == m and bo[MM] == m and bo[BM] == m) or # down the middle
  60.                 (bo[TR] == m and bo[MR] == m and bo[BR] == m) or # down the right side
  61.                 (bo[TL] == m and bo[MM] == m and bo[BR] == m) or # diagonal
  62.                 (bo[TR] == m and bo[MM] == m and bo[BL] == m)) # diagonal
  63.  
  64.     def getPlayerMove(self):
  65.         # Let the player type in their move.
  66.         move = None
  67.         while move not in ALL_SPACES or not self.spaces[move] == BLANK:
  68.             print('What is your move? (TL TM TR ML MM MR BL BM BR)')
  69.             move = input().upper()
  70.         return move
  71.  
  72.     def isFull(self):
  73.         # Return True if every space on the board has been taken. Otherwise return False.
  74.         for space in ALL_SPACES:
  75.             if self.spaces[space] == BLANK:
  76.                 return False
  77.         return True
  78.  
  79.     def setSpace(self, space, mark):
  80.         # Sets the space on the board to mark, if it is valid.
  81.         if mark not in (X, O, BLANK):
  82.             raise ValueError('invalid mark for space')
  83.         self.spaces[space] = mark
  84.  
  85. if __name__ == '__main__':
  86.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement