Create a class TicTacToe that will enable you to write a complete program to play the game of tic-tac-toe. The class contains a 3-by-3 double-subscripted list of letters. The constructor should initialize the empty board to all zeros. Allow two human players. Wherever the first player moves, place an "X" in the specified square; place an "O" wherever the second player moves. Each move must be to

an empty square. After each move, determine whether the game has been won and whether the game is a draw. [Note: If you feel ambitious, modify your program so that the computer makes the moves for one of the players automatically. Also, allow the player to choose whether to go first or second.]

What will be an ideal response?


```
# Class TicTacToe.

class TicTacToe:
"""Defines a TicTacToe board and enables game moves"""

emptyMark = "?"
xMark = "X"
oMark = "O"

def __init__( self ):
"""Creates a 3x3 empty TicTacToe board"""

self.__board = [ [ TicTacToe.emptyMark ] * 3,
[ TicTacToe.emptyMark ] * 3,
[ TicTacToe.emptyMark ] * 3 ]

def isFull( self ):
"""Returns 1 if the board is full, 0 otherwise"""

for x in range( 0, 3 ):

for y in range( 0, 3 ):

if self.__board[ x ][ y ] == TicTacToe.emptyMark:
return 0

return 1

def isWon( self ):
"""Returns value of player game has been won, 0 otherwise"""

# horizontal
for row in self.__board:

if TicTacToe.emptyMark != row[ 0 ] == row[ 1 ] == row[ 2 ]:
return row[ 0 ]

# vertical
for top in range( 3 ):

if TicTacToe.emptyMark != self.__board[ 0 ][ top ] == \
self.__board[ 1 ][ top ] == self.__board[ 2 ][ top ]:

return self.__board[ 0 ][ top ]

# diagonal
if TicTacToe.emptyMark != self.__board[ 0 ][ 0 ] == \
self.__board[ 1 ][ 1 ] == self.__board[ 2 ][ 2 ]:

return self.__board[ 0 ][ 0 ]

if TicTacToe.emptyMark != self.__board[ 0 ][ 2 ] == \
self.__board[ 1 ][ 1 ] == self.__board[ 2 ][ 0 ]:

return self.__board[ 0 ][ 2 ]

return 0 # no win if this point reached

def displayBoard( self ):
"""Displays the board"""

for row in range( 3 ):

for column in range( 3 ):
print self.__board[ column ][ row ],

print

def move( self, coordinates, mark ):
"""Places mark at indicated coordinates"""

self.__testCoordinates( coordinates )
self.__board[ coordinates[ 0 ] ][ coordinates[ 1 ] ] = mark

def isMarked( self, coordinates ):
"""Returns player value if the space has mark, 0 otherwise"""

self.__testCoordinates( coordinates )

return self.__board[ coordinates[ 0 ] ][
coordinates[ 1 ] ] != TicTacToe.emptyMark

def __testCoordinates( self, coordinates ):
"""Utility method to test validity of coordinates"""

x, y = coordinates

if ( x not in range( 3 ) ) or ( y not in range( 3 ) ) \
or self.__board[ x ][ y ] != TicTacToe.emptyMark:

raise ValueError, "Invalid move"
```
```
# Uses class TicTacToe to implement a game.

from TicTacToe import TicTacToe

gameBoard = TicTacToe() # create TicTacToe object
players = ( TicTacToe.xMark, TicTacToe.oMark )
player = 0 # player 1 starts

print """This program allows two players to play TicTacToe.
Please enter your move as x- and y-coordinates
of the form "x,y" when prompted. The upper
left-hand corner of the board is ( 0, 0 )
and the lower right-hand corner is ( 2, 2 )."""

while 1:

print "\nWhere do you want to place your mark, player %d?" \
% ( player + 1 )

# coordinates are in form "%d,%d"
coordinates = raw_input( "Enter coordinates: " )
coordinates = ( int( coordinates[ 0 ] ),
int( coordinates[ 2 ] ) )

if gameBoard.isMarked( coordinates ): # test for valid move
print "Coordinates already marked. Try again"
continue # repeat prompt

gameBoard.move( coordinates , players[ player ] ) # make move
gameBoard.displayBoard() # display board

if gameBoard.isWon(): # game ends if someone wins
print "Player %d won!" % ( player + 1 )
break

if gameBoard.isFull(): # game ends if the board is full
print "The game ends with a tie."
break

player ^= 1 # next player's turn
```
This program allows two players to play TicTacToe.
Please enter your move as x- and y-coordinates
of the form "x,y" when prompted. The upper
left-hand corner of the board is ( 0, 0 )
and the lower right-hand corner is ( 2, 2 ).
Where do you want to place your mark, player 1?
Enter coordinates: 1,1
? ? ?
? X ?
? ? ?
Where do you want to place your mark, player 2?
Enter coordinates: 0,0
O ? ?
? X ?
? ? ?
Where do you want to place your mark, player 1?
Enter coordinates: 1,0
O X ?
? X ?
? ? ?
Where do you want to place your mark, player 2?
Enter coordinates: 0,1
O X ?
O X ?
? ? ?
Where do you want to place your mark, player 1?
Enter coordinates: 1,2
O X ?
O X ?
? X ?
Player 1 won!
This program allows two players to play TicTacToe.
Please enter your move as x- and y-coordinates
of the form "x,y" when prompted. The upper
left-hand corner of the board is ( 0, 0 )
and the lower right-hand corner is ( 2, 2 ).
Where do you want to place your mark, player 1?
Enter coordinates: 0,0
X ? ?
? ? ?
? ? ?
Where do you want to place your mark, player 2?
Enter coordinates: 1,1
X ? ?
? O ?
? ? ?
Where do you want to place your mark, player 1?
Enter coordinates: 2,2
X ? ?
? O ?
? ? X
Where do you want to place your mark, player 2?
Enter coordinates: 0,1
X ? ?
O O ?
? ? X
Where do you want to place your mark, player 1?
Enter coordinates: 1,0
X X ?
O O ?
? ? X
Where do you want to place your mark, player 2?
Enter coordinates: 2,1
X X ?
O O O
? ? X
Player 2 won!
This program allows two players to play TicTacToe.
Please enter your move as x- and y-coordinates
of the form "x,y" when prompted. The upper
left-hand corner of the board is ( 0, 0 )
and the lower right-hand corner is ( 2, 2 ).
Where do you want to place your mark, player 1?
Enter coordinates: 1,1
? ? ?
? X ?
? ? ?
Where do you want to place your mark, player 2?
Enter coordinates: 0,0
O ? ?
? X ?
? ? ?
Where do you want to place your mark, player 1?
Enter coordinates: 2,0
O ? X
? X ?
? ? ?
Where do you want to place your mark, player 2?
Enter coordinates: 2,2
O ? X
? X ?
? ? O
Where do you want to place your mark, player 1?
Enter coordinates: 0,2
O ? X
? X ?
X ? O
Player 1 won!

Computer Science & Information Technology

You might also like to view...

A process is identified by

A. the name of the program being run B. an integer

Computer Science & Information Technology

Certificates are issued by trusted companies that are known as certification ________

Fill in the blank(s) with correct word

Computer Science & Information Technology

A group of items that come one after another in succession is called

A) a series. B) a data marker. C) tabs. D) a legend.

Computer Science & Information Technology

How do you control the size of the bullet?

a. Use CSS b. Use HTML c. Use digital imaging software d. Use list-style-type property

Computer Science & Information Technology