edumeister_logo

class TicTacToe: def __init__(self): # "showing page 1-2 out of 2

class TicTacToe:
def __init__(self):
# "board" is a list of 10 strings representing the board (ignore
index 0)
self.board = [" "]*10
self.board[0]="#"
def drawBoard(self):
# This method prints out the board with current plays adjacent to a
board with index.
print(' ' + self.board[7] + ' | ' + self.board[8] + ' | ' +
self.board[9],' 7 | 8 | 9 ', sep='\t')
print('-----------','-----------', sep='\t')
print(' ' + self.board[4] + ' | ' + self.board[5] + ' | ' +
self.board[6],' 4 | 5 | 6 ', sep='\t')
print('-----------','-----------', sep='\t')
print(' ' + self.board[1] + ' | ' + self.board[2] + ' | ' +
self.board[3],' 1 | 2 | 3 ', sep='\t')
def boardFull(self):
# This method checks if the board is already full and returns True.
Returns false otherwise
if " " in self.board:
return False
else:
return True
def cellIsEmpty(self, cell):
if cell==0 or cell>9:
return False
else:
return self.board[cell]==" "
def assignMove(self, cell,ch):
# assigns the cell of the board to the character ch
if cell<10 and cell>0:
self.board[cell]=ch
def whoWon(self):
# returns the symbol of the player who won if there is a winner,
otherwise it returns an empty string.
if self.isWinner("x"):
return "x"
elif self.isWinner("o"):
return "o"
else:
return ""
def isWinner(self, ch):
# Given a player's letter, this method returns True if that player
has won.
return ((self.board[7] == ch and self.board[8] == ch and
self.board[9] == ch) or # across the top
(self.board[4] == ch and self.board[5] == ch and
self.board[6] == ch) or # across the middle
(self.board[1] == ch and self.board[2] == ch and
self.board[3] == ch) or # across the bottom
(self.board[7] == ch and self.board[4] == ch and
self.board[1] == ch) or # down the left side
(self.board[8] == ch and self.board[5] == ch and
self.board[2] == ch) or # down the middle
(self.board[9] == ch and self.board[6] == ch and
self.board[3] == ch) or # down the right side
(self.board[7] == ch and self.board[5] == ch and
self.board[3] == ch) or # diagonal
(self.board[9] == ch and self.board[5] == ch and
self.board[1] == ch)) # diagonal
print ("Welcome to Tic Tac Toe Series")
myGameLoop=True
while myGameLoop:
myBoard=TicTacToe()
gameIsOn=True
turn="x"
while gameIsOn:
myBoard.drawBoard()
print ("It is the turn for", turn,". ",end="")
move="0"
while not myBoard.cellIsEmpty(int(move)):
move="0"
while move not in "1 2 3 4 5 6 7 8 9".split():
move=input("what is your move?")
if not myBoard.cellIsEmpty(int(move)):
print (move,"is not available.
",end='')
move="0"
myBoard.assignMove(int(move),turn)
winner=myBoard.whoWon()
if winner!='':
myBoard.drawBoard()
print (turn,"wins. Congrats!")
input("Press Enter to continue")
gameIsOn=False
elif myBoard.boardFull():
myBoard.drawBoard()
print ("It's a tie.")
input("Press Enter to continue")
gameIsOn=False
elif turn=="x":
turn="o"
else:
turn="x"
answer='x'
while answer.upper() not in "YN":
answer=input("Do you want to play another game? (Y/N)")
if answer.upper() == "N":
myGameLoop=False