Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Immediate Help with python program!

1 view
Skip to first unread message

Daniel

unread,
Dec 9, 2009, 6:55:01 PM12/9/09
to
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

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!

Jon Clements

unread,
Dec 9, 2009, 7:18:14 PM12/9/09
to

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.

Daniel

unread,
Dec 9, 2009, 7:21:18 PM12/9/09
to

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.

Intchanter / Daniel Fackrell

unread,
Dec 9, 2009, 7:21:47 PM12/9/09
to
On Dec 9, 5:18 pm, Jon Clements <jon...@googlemail.com> wrote:

<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.

Rhodri James

unread,
Dec 9, 2009, 7:37:54 PM12/9/09
to pytho...@python.org
Ahem. This is a newsgroup/mailing list, not IM. I happen to have seen
this within half an hour of you posting it, but that's just luck.
Expecting an "immediate" response is unrealistic.

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

MRAB

unread,
Dec 9, 2009, 7:50:09 PM12/9/09
to pytho...@python.org
Daniel 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:
>
[snip]
You problem is due to your choice of variable names, because '0' looks
too much like 'O'.

You also don't define 'ask'.

Daniel

unread,
Dec 9, 2009, 8:23:18 PM12/9/09
to

Appreciate the help guys. Sorry about postin it here, mistake on my
part. Thanks again.

Grant Edwards

unread,
Dec 10, 2009, 12:28:42 PM12/10/09
to
On 2009-12-10, Rhodri James <rho...@wildebst.demon.co.uk> wrote:
> Ahem. This is a newsgroup/mailing list, not IM. I happen to have seen
> this within half an hour of you posting it, but that's just luck.
> Expecting an "immediate" response is unrealistic.
>
> 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.

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

0 new messages