X = "X"
O = "O"
empty = " "
tie = "Tie"
squares = 9
def display():
print """Welcome to Tic-Tac-Toe. Player will play against the
computer.
\nYou will move by typing in the number to the
corresponding square below:
0 | 1 | 2
-------------
3 | 4 | 5
-------------
6 | 7 | 8 \n"""
def select():
question = ask("Do you want to go first? (y/n)")
if question == "y":
player = X
computer = O
else:
computer = X
player = O
return computer, player
def newBoard():
board = []
for i in range(squares):
board.append(empty)
return board
def displayBoard(board):
print "\n\t", board[0], "|", board[1], "|", board[2]
print "\t", "---------"
print "\t", board[3], "|", board[4], "|", board[5]
print "\t", "---------"
print "\t", board[6], "|", board[7], "|", board[8], "\n"
def boardMoves(board):
moves = []
for i in range(squares):
if board[i] == empty:
moves.append(i)
return moves
def findWinner(board):
win = ((0, 1, 2),
(3, 4, 5),
(6, 7, 8),
(0, 3, 6),
(1, 4, 7),
(2, 5, 8),
(0, 4, 8),
(2, 4, 6))
for i in win:
if board[i[0]] == board[i[1]] == board[i[2]] != empty:
winner = board[i[0]]
return winner
if empty not in board:
return tie
return None
def askMove(question, low, high):
response = None
while response not in range(low, high):
response = int(raw_input(question))
return response
def playerMove(board, player):
legal = boardMoves(board)
move = None
while move not in legal:
move = askMove("Pick a number where you want to move(0-8):",
0, squares)
if move not in legal:
print "\nThat move is taken already. Pick another."
return move
def compMove(board, computer, player):
board = board[:]
strategy = (4, 0, 2, 6, 8, 1, 3, 5, 7)
print "Computer chooses:",
# if computer can win, take that move
for move in boardMoves(board):
board[move] = computer
if findWinner(board) == computer:
print move
return move
board[move] = empty
# if human can win, block that move
for move in boardMoves(board):
board[move] = player
if findWinner(board) == player:
print move
return move
board[move] = empty
# If no one can win pick best open square
for move in strategy:
if move in boardMoves(board):
print move
return move
def nextTurn(turn):
if turn == X:
return 0
else:
return X
def gameWinner(winner, computer, player):
if winner == computer:
print "Computer Wins. Better luck next time"
elif winner == player:
print "You win. Good job!"
elif winner == tie:
print "Tie game. Play again."
def main():
display()
computer, player = select()
turn = X
board = newBoard()
displayBoard(board)
while not findWinner(board):
if turn == player:
move = playerMove(board, player)
board[move] = player
else:
move = compMove(board, computer, player)
board[move] = computer
displayBoard(board)
turn = nextTurn(turn)
winner = findWinner(board)
gameWinner(winner, computer, player)
main()
Here is my problem:
If you hit 'n' at the beginning prompt, the computer does four moves
automatically and wins- you can't input anything. If you hit 'y' at
the beginning prompt, you can play but cannot win. I got three x's in
a row and it didn't let me win. It just keeps letting
you input numbers until the computer wins even if you have three in a
row.
If anyone can help please do asap.
Thank you!
Someone's homework assignment is overdue/due very soon? And, I don't
believe for a second this is your code. In fact, just searching for
(the obvious Java based) function names leads me to believe you've
'butchered' it from Java code (do you not think your teacher/lecturer
can do the same?).
Someone might well help out, but I'd be surprised if you got a "here's
how to fix it response", as from my POV you haven't done any work.
Of course, I'm occasionally wrong,
Jon.
Just lookin for some help man. It is my code. but im here because i
needed help and i dont know whats wrong with it. Figured someone would
offer a hand. Thanks anyway.
<snip original post>
> Someone's homework assignment is overdue/due very soon? And, I don't
> believe for a second this is your code. In fact, just searching for
> (the obvious Java based) function names leads me to believe you've
> 'butchered' it from Java code (do you not think your teacher/lecturer
> can do the same?).
>
> Someone might well help out, but I'd be surprised if you got a "here's
> how to fix it response", as from my POV you haven't done any work.
>
> Of course, I'm occasionally wrong,
>
> Jon.
I also got the impression that it was a homework assignment. I'll
just add that trying to bring attention to your request for help with
exclamation points and words that indicate urgency is pretty bad form
in a volunteer support community.
Furthermore, this is comp.lang.python, a group right up there in pedantry
terms with cam.misc. Wandering in and demanding "immediate" help is just
begging for half a dozen replies that give you detailed and mind-boggling
versions of exactly what you asked for, especially if it's got nothing to
do with the answer you actually need.
So...
On Wed, 09 Dec 2009 23:55:01 -0000, Daniel <dkee...@yahoo.com> wrote:
> i am making a tic-tac-toe game using python. i am pretty new to it,
> but cant seem to figure this one out.
> Here is my code:
>
> X = "X"
> O = "O"
> empty = " "
> tie = "Tie"
> squares = 9
There's a convention (PEP 8, why not go to www.python.org and read it?)
that constants should be given names in CAPITAL_LETTERS_AND_UNDERSCORES so
that you can tell at a glance that they aren't supposed to be changed.
PEP 8 also has things to say about function names.
[snip rest of program]
> Here is my problem:
> If you hit 'n' at the beginning prompt, the computer does four moves
> automatically and wins- you can't input anything.
Sounds like you have a bug in your end-of-turn function. To be fair, it
took me a couple of minutes to be sure I'd spotted this correctly.
> If you hit 'y' at
> the beginning prompt, you can play but cannot win. I got three x's in
> a row and it didn't let me win. It just keeps letting
> you input numbers until the computer wins even if you have three in a
> row.
Sounds like you have a bug in your check-for-a-win function. That should
be all the hint you need.
--
Rhodri James *-* Wildebeest Herder to the Masses
You also don't define 'ask'.
Appreciate the help guys. Sorry about postin it here, mistake on my
part. Thanks again.
An exclamation point in a subject line also tends to act as an
excellent lighting rod...
--
Grant Edwards grante Yow! Gibble, Gobble, we
at ACCEPT YOU ...
visi.com