summaryrefslogtreecommitdiff
path: root/TicTacToe.py
diff options
context:
space:
mode:
authorLogan Hunt <loganthebean222@gmail.com>2017-07-11 21:14:14 -0700
committerGitHub <noreply@github.com>2017-07-11 21:14:14 -0700
commit2d108095a2cec042375dbb935b5261e2339e4fcc (patch)
tree969f4e7f3eba70e5e7e9699750bc3db7c4af880f /TicTacToe.py
parentcd928ffaae6ff879df180600b337edeb04e2ad62 (diff)
downloadold-src-backup-2d108095a2cec042375dbb935b5261e2339e4fcc.tar.gz
old-src-backup-2d108095a2cec042375dbb935b5261e2339e4fcc.zip
Add files via upload
Diffstat (limited to 'TicTacToe.py')
-rw-r--r--TicTacToe.py58
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()