Advertisement
asweigart

tictactoe.py

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