I've tried changing the values, and lengths, but I cant seem to get it right. Any help would be appreciated.
""" This program will play a game of Tic-Tac-Toe. It will play at least one
game and then let the user decide whether to play additional games.
Version 1.0 has just the top-level logic.
Version 2.0 introduces the TTTgame class and shows some basic techniques
for defining objects and using them.
Version 3.0 starts adding attributes to the game and fleshes out some basic
functions.
Version 4.0 concentrates on the getMove method and the draw method. We will
figure out winning in the next version.
Version 5.0 figures out if someone has won the game by getting 3 in a line.
"""
def clearScreen():
for i in range(80):
print
def displayBoard(L):
# clearScreen()
print ' '+str(L[0]) + ' | ' + str(L[1]) + ' | ' + str(L[2])
print '---+---+---'
print ' '+str(L[3]) + ' | ' + str(L[4]) + ' | ' + str(L[5])
print '---+---+---'
print ' '+str(L[6]) + ' | ' + str(L[7]) + ' | ' + str(L[8])
print
def contains(L1,L2):
for i in L2:
if i not in L1:
return False
return True
PlayAgain = True
class TTTgame:
def __init__(self):
self.board = range(1,10)
self.player = 'X'
self.free = range(1,10)
self.moves = {'X' : [ ], 'O' : [ ] }
def won(self,p):
winners = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7],
[2, 5, 8], [3, 6, 9], [1, 5, 9], [3, 5, 7] ]
mvs = self.moves[p]
for w in winners:
if contains(mvs,w):
return True
return False
def draw(self):
return len(self.free) == 0
def getMove(self,p):
legalMove = False
while not legalMove:
mv = raw_input("Please enter a legal move for "+p+'. ')
if (len(mv) == 1) and (mv in '123456789') and int(mv) in self.free:
legalMove = True
mv = int(mv)
self.moves[p].append(mv)
self.free.remove(mv)
self.board[mv-1] = p
def play(self):
gameOver = False
while not gameOver:
displayBoard(self.board)
if self.won('X'):
print "X won!"
gameOver = True
elif self.won('O'):
print "O won!"
gameOver = True
elif self.draw():
print "The game is a draw!"
gameOver = True
elif self.player == 'X':
self.getMove('X')
self.player = 'O'
else:
self.getMove('O')
self.player = 'X'
while PlayAgain:
TTT = TTTgame()
TTT.play()
YN = raw_input("Do you want to play again (Y/n)? ").lower()
if 'y' not in YN:
PlayAgain = False
print "Game Over"
This post has been edited by Simown: 14 February 2013 - 11:05 AM
Reason for edit:: Fixed code tags

New Topic/Question
Reply



MultiQuote



|