diff options
Diffstat (limited to 'TicTacToe.py')
-rw-r--r-- | TicTacToe.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/TicTacToe.py b/TicTacToe.py new file mode 100644 index 0000000..4bfc0ae --- /dev/null +++ b/TicTacToe.py @@ -0,0 +1,58 @@ +class TicTacToe: + def __init__(self): + self.board = {'a3':' ','b3': ' ','c3': ' ','a2' : ' ','b2':' ','c2' : ' ', 'a1': ' ', 'b1' : ' ','c1' : ' '} + self.turn = 'X' + self.turns = 0 + def flipTurn(self,turn): + return {'X':'O','O':'X'}[turn] + def printBoard(self): + print('\n'*200, + ''' + {} | {} | {} 3 + ---------- + {} | {} | {} 2 + ---------- + {} | {} | {} 1 + a b c + '''.format(self.board['a3'],self.board['b3'],self.board['c3'],self.board['a2'],self.board['b2'],self.board['c2'],self.board['a1'],self.board['b1'],self.board['c1'])) + def getWin (self,space1,space2,space3): + if(self.board[space1] == 'X' and self.board[space2] == 'X' and self.board[space3] == 'X' or self.board[space1] == 'O' and self.board[space2] == 'O' and self.board[space3] == 'O'): + input('{} won! Ctrl+C to quit, enter to start a new game'.format(self.flipTurn(self.turn))) + win = True + import TicTacToe + quit() + def testers(self): + self.getWin('a3','b3','c3') + self.getWin('a2','b2','c2') + self.getWin('a1','b1','c1') + self.getWin('a3','a2','a1') + self.getWin('b2','b3','b1') + self.getWin('c1','c2','c3') + self.getWin('a1','b2','c3') + self.getWin('a3','b2','c1') + def gameLoop(self): + self.win = False + while True: + if self.turns == 9: + input('Enter to play again, CTRL + C to quit') + import TicTacToe + quit() + self.printBoard() + self.testers() + self.turns += 1 + print('It is ',self.turn,"'s turn. Put in move: ") + invalid = True + while(invalid): + place = input() + if(place in self.board.keys()): + if(self.board[place] == ' '): + invalid = False + if not self.win: + if self.turns == 9: + print('Cat') + else: + print('That move is invalid. Try again!') + self.board[place] = self.turn + self.turn = self.flipTurn(self.turn) +board = TicTacToe() +board.gameLoop() |