Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tic Tac Toe
- TL, TM, TR, ML, MM, MR, BL, BM, BR = 'TL', 'TM', 'TR', 'ML', 'MM', 'MR', 'BL', 'BM', 'BR'
- ALL_SPACES = (TL, TM, TR, ML, MM, MR, BL, BM, BR)
- X, O, BLANK = 'X', 'O', ' '
- def main():
- print('Welcome to Tic Tac Toe!')
- mainBoard = TicTacToeBoard()
- turn = X
- nextTurn = O
- while True:
- print('It is ' + turn + '\'s turn.')
- print(mainBoard)
- move = mainBoard.getPlayerMove()
- mainBoard[move] = turn
- if mainBoard.isWinner(turn):
- print(mainBoard)
- print(turn + ' has won the game!')
- break
- elif len(mainBoard) == 9:
- print(mainBoard)
- print('The game is a tie!')
- break
- print()
- turn, nextTurn = nextTurn, turn
- class TicTacToeBoard:
- def __init__(self, startingMarks=None):
- # Create a new, blank tic tac toe board.
- if startingMarks is None:
- startingMarks = {}
- self._spaces = {}
- for space in ALL_SPACES:
- self._spaces.setdefault(space, startingMarks.get(space, BLANK))
- self._i = 0
- def __len__(self):
- spacesUsed = 0
- for space in ALL_SPACES:
- if self[space] != BLANK:
- spacesUsed += 1
- return spacesUsed
- def __str__(self):
- # Display a text-representation of the board.
- s = [f'{self[TL]}|{self[TM]}|{self[TR]}',
- '-----',
- f'{self[ML]}|{self[MM]}|{self[MR]}',
- '-----',
- f'{self[BL]}|{self[BM]}|{self[BR]}']
- return '\n'.join(s)
- def __iter__(self):
- return self
- def __next__(self):
- if self._i == 9:
- self._i = 0
- raise StopIteration
- self._i += 1
- space = ALL_SPACES[self._i - 1]
- return (space, self[space])
- def __repr__(self):
- className = type(self).__name__
- return f'{className}({dict(self)})'
- def isWinner(self, mark):
- le = mark # Syntactic sugar to make the following code shorter.
- return ((self[TL] == m and self[TM] == m and self[TR] == m) or # across the top
- (self[ML] == m and self[MM] == m and self[MR] == m) or # across the middle
- (self[BL] == m and self[BM] == m and self[BR] == m) or # across the selfttom
- (self[TL] == m and self[ML] == m and self[BL] == m) or # down the left side
- (self[TM] == m and self[MM] == m and self[BM] == m) or # down the middle
- (self[TR] == m and self[MR] == m and self[BR] == m) or # down the right side
- (self[TL] == m and self[MM] == m and self[BR] == m) or # diagonal
- (self[TR] == m and self[MM] == m and self[BL] == m)) # diagonal
- def getPlayerMove(self):
- # Let the player type in their move.
- move = None
- while move not in ALL_SPACES or not self[move] == BLANK:
- print('What is your move? (TL TM TR ML MM MR BL BM BR)')
- move = input().upper()
- return move
- #def isFull(self):
- # # Return True if every space on the board has been taken. Otherwise return False.
- # return len(self) == 9
- def __getitem__(self, key):
- if key not in ALL_SPACES:
- raise KeyError(f'index must be one of {ALL_SPACES}')
- return self._spaces[key]
- def __setitem__(self, key, value):
- # Sets the space on the board to mark, if it is valid.
- if key not in ALL_SPACES:
- raise KeyError(f'index must be one of {ALL_SPACES}')
- if value not in (X, O, BLANK):
- raise ValueError('invalid mark for space')
- self._spaces[key] = value
- def __delitem__(self, key):
- if key not in ALL_SPACES:
- raise KeyError(f'index must be one of {ALL_SPACES}')
- self._spaces[key] = BLANK
- if __name__ == '__main__':
- pass#main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement